-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_dms_kafka_smart_connect_task.go
378 lines (333 loc) · 14.2 KB
/
resource_huaweicloud_dms_kafka_smart_connect_task.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package dms
import (
"context"
"fmt"
"strings"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API Kafka POST /v2/{project_id}/connectors/{connector_id}/sink-tasks
// @API Kafka GET /v2/{project_id}/connectors/{connector_id}/sink-tasks
// @API Kafka DELETE /v2/{project_id}/connectors/{connector_id}/sink-tasks/{task_id}
func ResourceDmsKafkaSmartConnectTask() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDmsKafkaSmartConnectTaskCreate,
ReadContext: resourceDmsKafkaSmartConnectTaskRead,
DeleteContext: resourceDmsKafkaSmartConnectTaskDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceDmsKafkaSmartConnectTaskImportState,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(50 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"connector_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the connector id of the kafka instance.`,
},
"source_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the source type of the smart connect task.`,
},
"task_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the name of the smart connect task.`,
},
"destination_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the destination type of the smart connect task.`,
},
"access_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the access key used to access the OBS bucket.`,
},
"secret_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
Description: `Specifies the secret access key used to access the OBS bucket.`,
},
"consumer_strategy": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the consumer strategy of the smart connect task.`,
},
"deliver_time_interval": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: `Specifies the deliver time interval of the smart connect task.`,
},
"obs_bucket_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the obs bucket name of the smart connect task.`,
},
"partition_format": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the partiton format of the smart connect task.`,
},
"obs_path": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Specifies the obs path of the smart connect task.`,
},
"destination_file_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Specifies the destination file type of the smart connect task.`,
},
"record_delimiter": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Specifies the record delimiter of the smart connect task.`,
},
"topics": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Specifies the topics of the task.`,
ExactlyOneOf: []string{"topics", "topics_regex"},
},
"topics_regex": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Specifies the topics regular expression of the smart connect task.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the status of the smart connect task.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the creation time of the smart connect task.`,
},
},
}
}
func resourceDmsKafkaSmartConnectTaskCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// createKafkaSmartConnectTask: create DMS kafka smart connect task
var (
createKafkaSmartConnectTaskHttpUrl = "v2/{project_id}/connectors/{connector_id}/sink-tasks"
createKafkaSmartConnectTaskProduct = "dms"
)
createKafkaSmartConnectTaskClient, err := cfg.NewServiceClient(createKafkaSmartConnectTaskProduct, region)
if err != nil {
return diag.Errorf("error creating DMS Client: %s", err)
}
connectorID := d.Get("connector_id").(string)
createKafkaSmartConnectTaskPath := createKafkaSmartConnectTaskClient.Endpoint + createKafkaSmartConnectTaskHttpUrl
createKafkaSmartConnectTaskPath = strings.ReplaceAll(createKafkaSmartConnectTaskPath, "{project_id}",
createKafkaSmartConnectTaskClient.ProjectID)
createKafkaSmartConnectTaskPath = strings.ReplaceAll(createKafkaSmartConnectTaskPath, "{connector_id}", connectorID)
createKafkaSmartConnectTaskOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
createKafkaSmartConnectTaskOpt.JSONBody = utils.RemoveNil(buildCreateKafkaSmartConnectTaskBodyParams(d))
createKafkaSmartConnectTaskResp, err := createKafkaSmartConnectTaskClient.Request("POST",
createKafkaSmartConnectTaskPath, &createKafkaSmartConnectTaskOpt)
if err != nil {
return diag.Errorf("error creating DMS kafka smart connect task: %v", err)
}
kafkaSmartConnectTaskRespBody, err := utils.FlattenResponse(createKafkaSmartConnectTaskResp)
if err != nil {
return diag.FromErr(err)
}
taskID := utils.PathSearch("task_id", kafkaSmartConnectTaskRespBody, nil)
if taskID == nil {
return diag.Errorf("error retrieving DMS kafka smart connect task id: the task id is nil.")
}
d.SetId(taskID.(string))
stateConf := &resource.StateChangeConf{
Pending: []string{"CREATING"},
Target: []string{"RUNNING"},
Refresh: smartConnectTaskStateRefreshFunc(createKafkaSmartConnectTaskClient, connectorID, d.Id()),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 1 * time.Second,
PollInterval: 5 * time.Second,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("error waiting for the smart connect task (%s) to be done: %s", d.Id(), err)
}
return resourceDmsKafkaSmartConnectTaskRead(ctx, d, meta)
}
func buildCreateKafkaSmartConnectTaskBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"source_type": d.Get("source_type"),
"task_name": d.Get("task_name"),
"destination_type": d.Get("destination_type"),
"obs_destination_descriptor": buildObsDestinationDescriptorStruct(d),
}
return bodyParams
}
func buildObsDestinationDescriptorStruct(d *schema.ResourceData) map[string]interface{} {
rst := map[string]interface{}{
"consumer_strategy": d.Get("consumer_strategy"),
"destination_file_type": d.Get("destination_file_type"),
"access_key": d.Get("access_key"),
"secret_key": d.Get("secret_key"),
"obs_bucket_name": d.Get("obs_bucket_name"),
"obs_path": d.Get("obs_path"),
"partition_format": d.Get("partition_format"),
"record_delimiter": d.Get("record_delimiter"),
"deliver_time_interval": d.Get("deliver_time_interval"),
"topics": d.Get("topics"),
"topics_regex": d.Get("topics_regex"),
}
return rst
}
func resourceDmsKafkaSmartConnectTaskRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getKafkaSmartConnectTask: query DMS kafka smart connect task
var (
getKafkaSmartConnectTaskHttpUrl = "v2/{project_id}/connectors/{connector_id}/sink-tasks/{task_id}"
getKafkaSmartConnectTaskProduct = "dms"
)
getKafkaSmartConnectTaskClient, err := cfg.NewServiceClient(getKafkaSmartConnectTaskProduct, region)
if err != nil {
return diag.Errorf("error creating DMS Client: %s", err)
}
connectorID := d.Get("connector_id").(string)
getKafkaSmartConnectTaskPath := getKafkaSmartConnectTaskClient.Endpoint + getKafkaSmartConnectTaskHttpUrl
getKafkaSmartConnectTaskPath = strings.ReplaceAll(getKafkaSmartConnectTaskPath, "{project_id}",
getKafkaSmartConnectTaskClient.ProjectID)
getKafkaSmartConnectTaskPath = strings.ReplaceAll(getKafkaSmartConnectTaskPath, "{connector_id}", connectorID)
getKafkaSmartConnectTaskPath = strings.ReplaceAll(getKafkaSmartConnectTaskPath, "{task_id}", d.Id())
getKafkaSmartConnectTaskOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
getKafkaSmartConnectTaskResp, err := getKafkaSmartConnectTaskClient.Request("GET", getKafkaSmartConnectTaskPath,
&getKafkaSmartConnectTaskOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DMS kafka smart connect task")
}
getKafkaSmartConnectTaskRespBody, err := utils.FlattenResponse(getKafkaSmartConnectTaskResp)
if err != nil {
return diag.FromErr(err)
}
obsDestinationDescriptorStruct := utils.PathSearch("obs_destination_descriptor", getKafkaSmartConnectTaskRespBody, nil)
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("connector_id", connectorID),
d.Set("task_name", utils.PathSearch("task_name", getKafkaSmartConnectTaskRespBody, nil)),
d.Set("destination_type", utils.PathSearch("destination_type", getKafkaSmartConnectTaskRespBody, nil)),
d.Set("topics", utils.PathSearch("topics", obsDestinationDescriptorStruct, nil)),
d.Set("topics_regex", utils.PathSearch("topics_regex", obsDestinationDescriptorStruct, nil)),
d.Set("consumer_strategy", utils.PathSearch("consumer_strategy", obsDestinationDescriptorStruct, nil)),
d.Set("destination_file_type", utils.PathSearch("destination_file_type", obsDestinationDescriptorStruct, nil)),
d.Set("obs_bucket_name", utils.PathSearch("obs_bucket_name", obsDestinationDescriptorStruct, nil)),
d.Set("obs_path", utils.PathSearch("obs_path", obsDestinationDescriptorStruct, nil)),
d.Set("partition_format", utils.PathSearch("partition_format", obsDestinationDescriptorStruct, nil)),
d.Set("record_delimiter", utils.PathSearch("record_delimiter", obsDestinationDescriptorStruct, nil)),
d.Set("deliver_time_interval", utils.PathSearch("deliver_time_interval", obsDestinationDescriptorStruct, nil)),
d.Set("created_at", utils.FormatTimeStampRFC3339(
int64(utils.PathSearch("create_time", getKafkaSmartConnectTaskRespBody, float64(0)).(float64)/1000), false)),
d.Set("status", utils.PathSearch("status", getKafkaSmartConnectTaskRespBody, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func resourceDmsKafkaSmartConnectTaskDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// deleteKafkaSmartConnectTask: delete DMS kafka smart connect task
var (
deleteKafkaSmartConnectTaskHttpUrl = "v2/{project_id}/connectors/{connector_id}/sink-tasks/{task_id}"
deleteKafkaSmartConnectTaskProduct = "dms"
)
deleteKafkaSmartConnectTaskClient, err := cfg.NewServiceClient(deleteKafkaSmartConnectTaskProduct, region)
if err != nil {
return diag.Errorf("error creating DMS Client: %s", err)
}
connectorID := d.Get("connector_id").(string)
deleteKafkaSmartConnectTaskPath := deleteKafkaSmartConnectTaskClient.Endpoint + deleteKafkaSmartConnectTaskHttpUrl
deleteKafkaSmartConnectTaskPath = strings.ReplaceAll(deleteKafkaSmartConnectTaskPath, "{project_id}",
deleteKafkaSmartConnectTaskClient.ProjectID)
deleteKafkaSmartConnectTaskPath = strings.ReplaceAll(deleteKafkaSmartConnectTaskPath, "{connector_id}", connectorID)
deleteKafkaSmartConnectTaskPath = strings.ReplaceAll(deleteKafkaSmartConnectTaskPath, "{task_id}", d.Id())
deleteKafkaSmartConnectTaskOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
204,
},
}
_, err = deleteKafkaSmartConnectTaskClient.Request("DELETE", deleteKafkaSmartConnectTaskPath, &deleteKafkaSmartConnectTaskOpt)
if err != nil {
return diag.Errorf("error deleting DMS kafka smart connect task: %v", err)
}
return nil
}
// resourceDmsKafkaSmartConnectTaskImportState is used to import an id with format <connector_id>/<id>
func resourceDmsKafkaSmartConnectTaskImportState(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid format specified for import id, must be <connector_id>/<id>")
}
d.Set("connector_id", parts[0])
d.SetId(parts[1])
return []*schema.ResourceData{d}, nil
}
func smartConnectTaskStateRefreshFunc(client *golangsdk.ServiceClient, connectorID, taskID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
// getSmartConnectTask: query smart connect task
var (
getSmartConnectTaskHttpUrl = "v2/{project_id}/connectors/{connector_id}/sink-tasks/{task_id}"
)
getSmartConnectTaskPath := client.Endpoint + getSmartConnectTaskHttpUrl
getSmartConnectTaskPath = strings.ReplaceAll(getSmartConnectTaskPath, "{project_id}", client.ProjectID)
getSmartConnectTaskPath = strings.ReplaceAll(getSmartConnectTaskPath, "{connector_id}", connectorID)
getSmartConnectTaskPath = strings.ReplaceAll(getSmartConnectTaskPath, "{task_id}", taskID)
getSmartConnectTaskOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
getSmartConnectTaskResp, err := client.Request("GET", getSmartConnectTaskPath, &getSmartConnectTaskOpt)
if err != nil {
return nil, "QUERY ERROR", err
}
smartConnectTaskRespBody, err := utils.FlattenResponse(getSmartConnectTaskResp)
if err != nil {
return nil, "PARSE ERROR", err
}
status := utils.PathSearch("status", smartConnectTaskRespBody, "").(string)
return smartConnectTaskRespBody, status, nil
}
}