-
Notifications
You must be signed in to change notification settings - Fork 134
/
resource_tc_clb_target_group_attachments.go
344 lines (304 loc) · 11.3 KB
/
resource_tc_clb_target_group_attachments.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
package clb
import (
"context"
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
)
func ResourceTencentCloudClbTargetGroupAttachments() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudClbTargetGroupAttachmentsCreate,
Read: resourceTencentCloudClbTargetGroupAttachmentsRead,
Delete: resourceTencentCloudClbTargetGroupAttachmentsDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"load_balancer_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "CLB instance ID, (load_balancer_id and target_group_id require at least one).",
},
"target_group_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Target group ID, (load_balancer_id and target_group_id require at least one).",
},
"associations": {
Required: true,
ForceNew: true,
Type: schema.TypeSet,
MaxItems: 20,
Description: "Association array, the combination cannot exceed 20.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"load_balancer_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "CLB instance ID, when the binding target is target group, load_balancer_id in associations is required.",
},
"target_group_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Target group ID, when the binding target is clb, the target_group_id in associations is required.",
},
"listener_id": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Description: "Listener ID.",
},
"location_id": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Description: "Forwarding rule ID.",
},
},
},
},
},
}
}
const ResourcePrefixFromClb = "lb"
func checkParam(d *schema.ResourceData) error {
_, clbIdExists := d.GetOk("load_balancer_id")
_, groupIdExists := d.GetOk("target_group_id")
if !clbIdExists && !groupIdExists {
return fmt.Errorf("one of load_balancer_id and target_group_id must be specified as the binding target")
}
if clbIdExists && groupIdExists {
return fmt.Errorf("load_balancer_id and target_group_id cannot be used as binding targets at the same time")
}
if v, ok := d.GetOk("associations"); ok {
for _, item := range v.(*schema.Set).List() {
dMap := item.(map[string]interface{})
associationsClbExists := false
associationsGroupExists := false
if v, ok := dMap["load_balancer_id"]; ok && v.(string) != "" {
associationsClbExists = true
}
if v, ok := dMap["target_group_id"]; ok && v.(string) != "" {
associationsGroupExists = true
}
if !associationsClbExists && !associationsGroupExists {
return fmt.Errorf("then in associations, load_balancer_id and target_group_id must be filled in one")
}
if clbIdExists && associationsClbExists {
return fmt.Errorf("if the binding target is clb, then in associations, it is expected that " +
"target_group_id exists and load_balancer_id does not exist")
}
if groupIdExists && associationsGroupExists {
return fmt.Errorf("if the binding target is targetGroup, then in associations, it is expected that " +
"load_balancer_id exists and target_group_id does not exist")
}
}
}
return nil
}
func resourceTencentCloudClbTargetGroupAttachmentsCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_target_group_attachments.create")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
if err := checkParam(d); err != nil {
return err
}
var (
request = clb.NewAssociateTargetGroupsRequest()
resourceId string
)
if v, ok := d.GetOk("load_balancer_id"); ok && v.(string) != "" {
resourceId = v.(string)
request.Associations = parseParamToRequest(d, "load_balancer_id", resourceId)
}
if v, ok := d.GetOk("target_group_id"); ok && v.(string) != "" {
resourceId = v.(string)
request.Associations = parseParamToRequest(d, "target_group_id", resourceId)
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().AssociateTargetGroups(request)
if err != nil {
if e := processRetryErrMsg(err); e != nil {
return e
}
return tccommon.RetryError(err, tccommon.InternalError)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), result.ToJsonString(), result.ToJsonString())
requestId := *result.Response.RequestId
retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient())
if retryErr != nil {
return resource.NonRetryableError(errors.WithStack(retryErr))
}
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create clb targetGroupAttachments failed, reason:%+v", logId, err)
return err
}
d.SetId(resourceId)
return resourceTencentCloudClbTargetGroupAttachmentsRead(d, meta)
}
func resourceTencentCloudClbTargetGroupAttachmentsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_target_group_attachments.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := ClbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
targetGroupList, associationsSet := margeReadRequest(d)
targetGroupAttachments, err := service.DescribeClbTargetGroupAttachmentsById(ctx, targetGroupList, associationsSet)
if err != nil {
return err
}
if len(targetGroupAttachments) < 1 {
d.SetId("")
log.Printf("[WARN]%s resource `ClbTargetGroupAttachments` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
return nil
}
var associationsList []interface{}
for _, attachment := range targetGroupAttachments {
info := strings.Split(attachment, tccommon.FILED_SP)
if len(info) != 4 {
return fmt.Errorf("id is broken,%s", info)
}
associationsMap := map[string]interface{}{}
if isBindFromClb(d.Id()) {
_ = d.Set("load_balancer_id", info[0])
associationsMap["target_group_id"] = info[1]
} else {
_ = d.Set("target_group_id", info[1])
associationsMap["load_balancer_id"] = info[0]
}
if info[2] != "" && info[2] != "null" {
associationsMap["listener_id"] = info[2]
}
if info[3] != "" && info[3] != "null" {
associationsMap["location_id"] = info[3]
}
associationsList = append(associationsList, associationsMap)
}
_ = d.Set("associations", associationsList)
return nil
}
func resourceTencentCloudClbTargetGroupAttachmentsDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_target_group_attachments.delete")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
request := clb.NewDisassociateTargetGroupsRequest()
id := d.Id()
if isBindFromClb(id) {
request.Associations = parseParamToRequest(d, "load_balancer_id", id)
} else {
request.Associations = parseParamToRequest(d, "target_group_id", id)
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
ratelimit.Check(request.GetAction())
result, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().DisassociateTargetGroups(request)
if err != nil {
if e := processRetryErrMsg(err); e != nil {
return e
}
return tccommon.RetryError(err, tccommon.InternalError)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), result.ToJsonString(), result.ToJsonString())
requestId := *result.Response.RequestId
retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient())
if retryErr != nil {
return resource.NonRetryableError(errors.WithStack(retryErr))
}
}
return nil
})
if err != nil {
return err
}
return nil
}
func parseParamToRequest(d *schema.ResourceData, param string, id string) (associations []*clb.TargetGroupAssociation) {
if v, ok := d.GetOk("associations"); ok {
for _, item := range v.(*schema.Set).List() {
dMap := item.(map[string]interface{})
targetGroupAssociation := clb.TargetGroupAssociation{}
dMap[param] = id
for name := range dMap {
if dMap[name] != nil && dMap[name].(string) != "" {
setString(name, dMap[name].(string), &targetGroupAssociation)
}
}
associations = append(associations, &targetGroupAssociation)
}
}
return associations
}
func setString(fieldName string, value string, request *clb.TargetGroupAssociation) {
switch fieldName {
case "load_balancer_id":
request.LoadBalancerId = helper.String(value)
case "target_group_id":
request.TargetGroupId = helper.String(value)
case "listener_id":
request.ListenerId = helper.String(value)
case "location_id":
request.LocationId = helper.String(value)
default:
log.Printf("Invalid field name: %s\n", fieldName)
}
}
func isBindFromClb(id string) bool {
re := regexp.MustCompile(`^(.*?)-`)
match := re.FindStringSubmatch(id)
if len(match) > 1 {
return match[1] == ResourcePrefixFromClb
}
return false
}
func margeReadRequest(d *schema.ResourceData) ([]string, map[string]struct{}) {
resourceId := d.Id()
associationsSet := make(map[string]struct{})
targetGroupList := make([]string, 0)
if v, ok := d.GetOk("associations"); ok {
for _, item := range v.(*schema.Set).List() {
dMap := item.(map[string]interface{})
ids := make([]string, 0)
processIds(resourceId, dMap, "load_balancer_id", isBindFromClb(resourceId), &ids)
processIds(resourceId, dMap, "target_group_id", isBindFromClb(resourceId), &ids)
processIds(resourceId, dMap, "listener_id", isBindFromClb(resourceId), &ids)
processIds(resourceId, dMap, "location_id", isBindFromClb(resourceId), &ids)
if groupId, ok := dMap["target_group_id"]; ok && groupId.(string) != "" {
targetGroupList = append(targetGroupList, groupId.(string))
}
associationsSet[strings.Join(ids, tccommon.FILED_SP)] = struct{}{}
}
}
if len(targetGroupList) < 1 && !isBindFromClb(resourceId) {
targetGroupList = append(targetGroupList, resourceId)
}
return targetGroupList, associationsSet
}
func processIds(id string, dMap map[string]interface{}, key string, clbFlag bool, ids *[]string) {
if clbFlag && key == "load_balancer_id" {
*ids = append(*ids, id)
} else if !clbFlag && key == "target_group_id" {
*ids = append(*ids, id)
} else {
if v, ok := dMap[key]; ok && v.(string) != "" {
*ids = append(*ids, v.(string))
} else {
*ids = append(*ids, "null")
}
}
}