forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_huaweicloud_cts_tracker_v1.go
228 lines (195 loc) · 6.37 KB
/
resource_huaweicloud_cts_tracker_v1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package huaweicloud
import (
"time"
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/cts/v1/tracker"
)
func resourceCTSTrackerV1() *schema.Resource {
return &schema.Resource{
Create: resourceCTSTrackerCreate,
Read: resourceCTSTrackerRead,
Update: resourceCTSTrackerUpdate,
Delete: resourceCTSTrackerDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tracker_name": {
Type: schema.TypeString,
Computed: true,
},
"bucket_name": {
Type: schema.TypeString,
Required: true,
},
"file_prefix_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateName,
},
"is_support_smn": {
Type: schema.TypeBool,
Required: true,
},
"topic_id": {
Type: schema.TypeString,
Required: true,
},
"operations": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"is_send_all_key_operation": {
Type: schema.TypeBool,
Required: true,
},
"need_notify_user_list": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func resourceCTSTrackerCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctsClient, err := config.ctsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating cts Client: %s", err)
}
createOpts := tracker.CreateOptsWithSMN{
BucketName: d.Get("bucket_name").(string),
FilePrefixName: d.Get("file_prefix_name").(string),
SimpleMessageNotification: tracker.SimpleMessageNotification{
IsSupportSMN: d.Get("is_support_smn").(bool),
TopicID: d.Get("topic_id").(string),
Operations: resourceCTSOperations(d),
IsSendAllKeyOperation: d.Get("is_send_all_key_operation").(bool),
NeedNotifyUserList: resourceCTSNeedNotifyUserList(d),
},
}
trackers, err := tracker.Create(ctsClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating CTS tracker : %s", err)
}
d.SetId(trackers.TrackerName)
time.Sleep(20 * time.Second)
return resourceCTSTrackerRead(d, meta)
}
func resourceCTSTrackerRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctsClient, err := config.ctsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating cts Client: %s", err)
}
listOpts := tracker.ListOpts{
TrackerName: d.Get("tracker_name").(string),
BucketName: d.Get("bucket_name").(string),
FilePrefixName: d.Get("file_prefix_name").(string),
Status: d.Get("status").(string),
}
trackers, err := tracker.List(ctsClient, listOpts)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
log.Printf("[WARN] Removing cts tracker %s as it's already gone", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving cts tracker: %s", err)
}
ctsTracker := trackers[0]
d.Set("tracker_name", ctsTracker.TrackerName)
d.Set("bucket_name", ctsTracker.BucketName)
d.Set("status", ctsTracker.Status)
d.Set("file_prefix_name", ctsTracker.FilePrefixName)
d.Set("is_support_smn", ctsTracker.SimpleMessageNotification.IsSupportSMN)
d.Set("topic_id", ctsTracker.SimpleMessageNotification.TopicID)
d.Set("is_send_all_key_operation", ctsTracker.SimpleMessageNotification.IsSendAllKeyOperation)
d.Set("operations", ctsTracker.SimpleMessageNotification.Operations)
d.Set("need_notify_user_list", ctsTracker.SimpleMessageNotification.NeedNotifyUserList)
d.Set("region", GetRegion(d, config))
time.Sleep(20 * time.Second)
return nil
}
func resourceCTSTrackerUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctsClient, err := config.ctsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating cts Client: %s", err)
}
var updateOpts tracker.UpdateOptsWithSMN
//as bucket_name is mandatory while updating tracker
updateOpts.BucketName = d.Get("bucket_name").(string)
updateOpts.SimpleMessageNotification.TopicID = d.Get("topic_id").(string)
updateOpts.SimpleMessageNotification.Operations = resourceCTSOperations(d)
updateOpts.SimpleMessageNotification.NeedNotifyUserList = resourceCTSNeedNotifyUserList(d)
updateOpts.SimpleMessageNotification.IsSupportSMN = d.Get("is_support_smn").(bool)
if d.HasChange("file_prefix_name") {
updateOpts.FilePrefixName = d.Get("file_prefix_name").(string)
}
if d.HasChange("status") {
updateOpts.Status = d.Get("status").(string)
}
if d.HasChange("is_send_all_key_operation") {
updateOpts.SimpleMessageNotification.IsSendAllKeyOperation = d.Get("is_send_all_key_operation").(bool)
}
_, err = tracker.Update(ctsClient, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating cts tracker: %s", err)
}
time.Sleep(20 * time.Second)
return resourceCTSTrackerRead(d, meta)
}
func resourceCTSTrackerDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctsClient, err := config.ctsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating cts Client: %s", err)
}
result := tracker.Delete(ctsClient)
if result.Err != nil {
return err
}
time.Sleep(20 * time.Second)
log.Printf("[DEBUG] Successfully deleted cts tracker %s", d.Id())
return nil
}
func resourceCTSOperations(d *schema.ResourceData) []string {
rawOperations := d.Get("operations").(*schema.Set)
operation := make([]string, (rawOperations).Len())
for i, raw := range rawOperations.List() {
operation[i] = raw.(string)
}
return operation
}
func resourceCTSNeedNotifyUserList(d *schema.ResourceData) []string {
rawNotify := d.Get("need_notify_user_list").(*schema.Set)
notify := make([]string, (rawNotify).Len())
for i, raw := range rawNotify.List() {
notify[i] = raw.(string)
}
return notify
}