-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_dms_kafka_smart_connect_tasks.go
206 lines (185 loc) · 6.36 KB
/
data_source_huaweicloud_dms_kafka_smart_connect_tasks.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
package dms
import (
"context"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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 GET /v2/{project_id}/connectors/{connector_id}/sink-tasks
func DataSourceDmsKafkaSmartConnectTasks() *schema.Resource {
return &schema.Resource{
ReadContext: resourceDmsKafkaSmartConnectTasksRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"connector_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the connector ID of the kafka instance.`,
},
"task_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of the smart connect task.`,
},
"task_name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the name of the smart connect task.`,
},
"destination_type": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the destination type of the smart connect task.`,
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the status of the smart connect task.`,
},
"tasks": {
Type: schema.TypeList,
Elem: tasksSchema(),
Computed: true,
Description: `Indicates the list of the smart connect tasks.`,
},
},
}
}
func tasksSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the ID of the smart connect task.`,
},
"task_name": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the name of the smart connect task.`,
},
"destination_type": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the destination type of the smart connect task.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the creation time of the smart connect task.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the status of the smart connect task.`,
},
"topics": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the topic names separated by commas or the topic regular expression of the smart connect task.`,
},
},
}
return &sc
}
func resourceDmsKafkaSmartConnectTasksRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getKafkaSmartConnectTasks: query DMS kafka smart connect tasks
var (
getKafkaSmartConnectTasksHttpUrl = "v2/{project_id}/connectors/{connector_id}/sink-tasks"
getKafkaSmartConnectTasksProduct = "dms"
)
getKafkaSmartConnectTasksClient, err := cfg.NewServiceClient(getKafkaSmartConnectTasksProduct, region)
if err != nil {
return diag.Errorf("error creating DMS Client: %s", err)
}
connectorID := d.Get("connector_id").(string)
getKafkaSmartConnectTasksPath := getKafkaSmartConnectTasksClient.Endpoint + getKafkaSmartConnectTasksHttpUrl
getKafkaSmartConnectTasksPath = strings.ReplaceAll(getKafkaSmartConnectTasksPath, "{project_id}",
getKafkaSmartConnectTasksClient.ProjectID)
getKafkaSmartConnectTasksPath = strings.ReplaceAll(getKafkaSmartConnectTasksPath, "{connector_id}", connectorID)
getKafkaSmartConnectTasksOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
getKafkaSmartConnectTasksResp, err := getKafkaSmartConnectTasksClient.Request("GET", getKafkaSmartConnectTasksPath,
&getKafkaSmartConnectTasksOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DMS kafka smart connect tasks")
}
getKafkaSmartConnectTasksRespBody, respBodyerr := utils.FlattenResponse(getKafkaSmartConnectTasksResp)
if respBodyerr != nil {
return diag.FromErr(respBodyerr)
}
dataSourceId, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(dataSourceId)
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("tasks", flattenTasks(filterTasks(d, getKafkaSmartConnectTasksRespBody))),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenTasks(taskRespBody []interface{}) []interface{} {
if taskRespBody == nil {
return nil
}
rst := make([]interface{}, 0, len(taskRespBody))
for _, v := range taskRespBody {
rst = append(rst, map[string]interface{}{
"id": utils.PathSearch("task_id", v, nil),
"task_name": utils.PathSearch("task_name", v, nil),
"destination_type": utils.PathSearch("destination_type", v, nil),
"created_at": utils.FormatTimeStampRFC3339(int64(utils.PathSearch("create_time", v, float64(0)).(float64)/1000), false),
"status": utils.PathSearch("status", v, nil),
"topics": utils.PathSearch("topics", v, nil),
})
}
return rst
}
func filterTasks(d *schema.ResourceData, resp interface{}) []interface{} {
taskJson := utils.PathSearch("tasks", resp, make([]interface{}, 0))
taskArray := taskJson.([]interface{})
if len(taskArray) < 1 {
return nil
}
result := make([]interface{}, 0, len(taskArray))
rawTaskId, rawTaskIdOK := d.GetOk("task_id")
rawTaskName, rawTaskNameOK := d.GetOk("task_name")
rawDestinationType, rawDestinationTypeOK := d.GetOk("destination_type")
rawStatus, rawStatusOK := d.GetOk("status")
for _, task := range taskArray {
taskID := utils.PathSearch("task_id", task, nil)
taskName := utils.PathSearch("task_name", task, nil)
destinationType := utils.PathSearch("destination_type", task, nil)
status := utils.PathSearch("status", task, nil)
if rawTaskIdOK && rawTaskId != taskID {
continue
}
if rawTaskNameOK && rawTaskName != taskName {
continue
}
if rawDestinationTypeOK && rawDestinationType != destinationType {
continue
}
if rawStatusOK && rawStatus != status {
continue
}
result = append(result, task)
}
return result
}