4
4
"context"
5
5
"fmt"
6
6
"log"
7
+ "regexp"
7
8
"strings"
8
9
9
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -25,34 +26,44 @@ func ResourceTencentCloudClbTargetGroupAttachments() *schema.Resource {
25
26
Importer : & schema.ResourceImporter {
26
27
State : schema .ImportStatePassthrough ,
27
28
},
29
+
28
30
Schema : map [string ]* schema.Schema {
29
31
"load_balancer_id" : {
30
32
Type : schema .TypeString ,
31
- Required : true ,
32
- Description : "CLB instance ID" ,
33
+ Optional : true ,
34
+ Description : "CLB instance ID, (load_balancer_id and target_group_id require at least one)." ,
35
+ },
36
+ "target_group_id" : {
37
+ Type : schema .TypeString ,
38
+ Optional : true ,
39
+ Description : "Target group ID, (load_balancer_id and target_group_id require at least one)." ,
33
40
},
34
41
"associations" : {
35
42
Required : true ,
36
43
Type : schema .TypeSet ,
37
44
MaxItems : 20 ,
38
- Description : "Association array, the combination cannot exceed 20" ,
45
+ Description : "Association array, the combination cannot exceed 20. " ,
39
46
Elem : & schema.Resource {
40
47
Schema : map [string ]* schema.Schema {
41
-
42
- "listener_id" : {
48
+ "load_balancer_id" : {
43
49
Type : schema .TypeString ,
44
50
Optional : true ,
45
- Description : "Listener ID " ,
51
+ Description : "CLB instance ID, when the binding target is target group, load_balancer_id in associations is required. " ,
46
52
},
47
53
"target_group_id" : {
48
54
Type : schema .TypeString ,
49
- Required : true ,
50
- Description : "Target group ID" ,
55
+ Optional : true ,
56
+ Description : "Target group ID, when the binding target is clb, the target_group_id in associations is required." ,
57
+ },
58
+ "listener_id" : {
59
+ Type : schema .TypeString ,
60
+ Optional : true ,
61
+ Description : "Listener ID." ,
51
62
},
52
63
"location_id" : {
53
64
Type : schema .TypeString ,
54
65
Optional : true ,
55
- Description : "Forwarding rule ID" ,
66
+ Description : "Forwarding rule ID. " ,
56
67
},
57
68
},
58
69
},
@@ -61,36 +72,71 @@ func ResourceTencentCloudClbTargetGroupAttachments() *schema.Resource {
61
72
}
62
73
}
63
74
64
- func resourceTencentCloudClbTargetGroupAttachmentsCreate (d * schema.ResourceData , meta interface {}) error {
65
- defer tccommon .LogElapsed ("resource.tencentcloud_clb_target_group_attachments.create" )()
66
- defer tccommon .InconsistentCheck (d , meta )()
75
+ const ResourcePrefixFromClb = "lb"
67
76
68
- logId := tccommon .GetLogId (tccommon .ContextNil )
77
+ func checkParam (d * schema.ResourceData ) error {
78
+ _ , clbIdExists := d .GetOk ("load_balancer_id" )
79
+ _ , groupIdExists := d .GetOk ("target_group_id" )
69
80
70
- var (
71
- request = clb .NewAssociateTargetGroupsRequest ()
72
- loadBalancerId string
73
- )
74
- if v , ok := d .GetOk ("load_balancer_id" ); ok {
75
- loadBalancerId = v .(string )
81
+ if ! clbIdExists && ! groupIdExists {
82
+ return fmt .Errorf ("one of load_balancer_id and target_group_id must be specified as the binding target" )
76
83
}
84
+
85
+ if clbIdExists && groupIdExists {
86
+ return fmt .Errorf ("load_balancer_id and target_group_id cannot be used as binding targets at the same time" )
87
+ }
88
+
77
89
if v , ok := d .GetOk ("associations" ); ok {
78
90
for _ , item := range v .(* schema.Set ).List () {
79
91
dMap := item .(map [string ]interface {})
80
- targetGroupAssociation := clb.TargetGroupAssociation {}
81
- targetGroupAssociation .LoadBalancerId = helper .String (loadBalancerId )
82
- if v , ok := dMap ["listener_id" ]; ok {
83
- targetGroupAssociation .ListenerId = helper .String (v .(string ))
92
+ associationsClbExists := false
93
+ associationsGroupExists := false
94
+
95
+ if v , ok := dMap ["load_balancer_id" ]; ok && v .(string ) != "" {
96
+ associationsClbExists = true
97
+ }
98
+ if v , ok := dMap ["target_group_id" ]; ok && v .(string ) != "" {
99
+ associationsGroupExists = true
84
100
}
85
- if v , ok := dMap ["target_group_id" ]; ok {
86
- targetGroupAssociation .TargetGroupId = helper .String (v .(string ))
101
+
102
+ if ! associationsClbExists && ! associationsGroupExists {
103
+ return fmt .Errorf ("then in associations, load_balancer_id and target_group_id must be filled in one" )
104
+ }
105
+
106
+ if clbIdExists && associationsClbExists {
107
+ return fmt .Errorf ("if the binding target is clb, then in associations, it is expected that " +
108
+ "target_group_id exists and load_balancer_id does not exist" )
87
109
}
88
- if v , ok := dMap ["location_id" ]; ok {
89
- targetGroupAssociation .LocationId = helper .String (v .(string ))
110
+
111
+ if groupIdExists && associationsGroupExists {
112
+ return fmt .Errorf ("if the binding target is targetGroup, then in associations, it is expected that " +
113
+ "load_balancer_id exists and target_group_id does not exist" )
90
114
}
91
- request .Associations = append (request .Associations , & targetGroupAssociation )
92
115
}
93
116
}
117
+ return nil
118
+ }
119
+ func resourceTencentCloudClbTargetGroupAttachmentsCreate (d * schema.ResourceData , meta interface {}) error {
120
+ defer tccommon .LogElapsed ("resource.tencentcloud_clb_target_group_attachments.create" )()
121
+ defer tccommon .InconsistentCheck (d , meta )()
122
+
123
+ logId := tccommon .GetLogId (tccommon .ContextNil )
124
+ if err := checkParam (d ); err != nil {
125
+ return err
126
+ }
127
+ var (
128
+ request = clb .NewAssociateTargetGroupsRequest ()
129
+ resourceId string
130
+ )
131
+ if v , ok := d .GetOk ("load_balancer_id" ); ok && v .(string ) != "" {
132
+ resourceId = v .(string )
133
+ request .Associations = parseParamToRequest (d , "load_balancer_id" , resourceId )
134
+ }
135
+
136
+ if v , ok := d .GetOk ("target_group_id" ); ok && v .(string ) != "" {
137
+ resourceId = v .(string )
138
+ request .Associations = parseParamToRequest (d , "target_group_id" , resourceId )
139
+ }
94
140
95
141
err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
96
142
result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ().AssociateTargetGroups (request )
@@ -112,7 +158,7 @@ func resourceTencentCloudClbTargetGroupAttachmentsCreate(d *schema.ResourceData,
112
158
return err
113
159
}
114
160
115
- d .SetId (loadBalancerId )
161
+ d .SetId (resourceId )
116
162
117
163
return resourceTencentCloudClbTargetGroupAttachmentsRead (d , meta )
118
164
}
@@ -127,38 +173,7 @@ func resourceTencentCloudClbTargetGroupAttachmentsRead(d *schema.ResourceData, m
127
173
128
174
service := ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
129
175
130
- loadBalancerId := d .Id ()
131
- associationsSet := make (map [string ]struct {}, 0 )
132
- targetGroupList := make ([]string , 0 )
133
- if v , ok := d .GetOk ("associations" ); ok {
134
- for _ , item := range v .(* schema.Set ).List () {
135
- dMap := item .(map [string ]interface {})
136
- ids := make ([]string , 0 )
137
-
138
- ids = append (ids , loadBalancerId )
139
- if v , ok := dMap ["listener_id" ]; ok {
140
- ids = append (ids , v .(string ))
141
- } else {
142
- ids = append (ids , "null" )
143
- }
144
-
145
- if v , ok := dMap ["target_group_id" ]; ok {
146
- ids = append (ids , v .(string ))
147
- targetGroupList = append (targetGroupList , v .(string ))
148
- } else {
149
- ids = append (ids , "null" )
150
- }
151
-
152
- if v , ok := dMap ["location_id" ]; ok {
153
- ids = append (ids , v .(string ))
154
- } else {
155
- ids = append (ids , "null" )
156
- }
157
-
158
- associationsSet [strings .Join (ids , tccommon .FILED_SP )] = struct {}{}
159
- }
160
- }
161
-
176
+ targetGroupList , associationsSet := margeReadRequest (d )
162
177
targetGroupAttachments , err := service .DescribeClbTargetGroupAttachmentsById (ctx , targetGroupList , associationsSet )
163
178
if err != nil {
164
179
return err
@@ -177,12 +192,16 @@ func resourceTencentCloudClbTargetGroupAttachmentsRead(d *schema.ResourceData, m
177
192
return fmt .Errorf ("id is broken,%s" , info )
178
193
}
179
194
associationsMap := map [string ]interface {}{}
180
- _ = d . Set ( "load_balancer_id" , info [ 0 ])
181
-
182
- associationsMap ["listener_id " ] = info [1 ]
195
+ if isBindFromClb ( d . Id ()) {
196
+ _ = d . Set ( "load_balancer_id" , info [ 0 ])
197
+ associationsMap ["target_group_id " ] = info [1 ]
183
198
184
- associationsMap ["target_group_id" ] = info [2 ]
199
+ } else {
200
+ _ = d .Set ("target_group_id" , info [1 ])
201
+ associationsMap ["load_balancer_id" ] = info [0 ]
202
+ }
185
203
204
+ associationsMap ["listener_id" ] = info [2 ]
186
205
associationsMap ["location_id" ] = info [3 ]
187
206
188
207
associationsList = append (associationsList , associationsMap )
@@ -206,24 +225,11 @@ func resourceTencentCloudClbTargetGroupAttachmentsDelete(d *schema.ResourceData,
206
225
207
226
logId := tccommon .GetLogId (tccommon .ContextNil )
208
227
request := clb .NewDisassociateTargetGroupsRequest ()
209
-
210
- loadBalancerId := d .Id ()
211
- if v , ok := d .GetOk ("associations" ); ok {
212
- for _ , item := range v .(* schema.Set ).List () {
213
- dMap := item .(map [string ]interface {})
214
- targetGroupAssociation := clb.TargetGroupAssociation {}
215
- targetGroupAssociation .LoadBalancerId = helper .String (loadBalancerId )
216
- if v , ok := dMap ["listener_id" ]; ok {
217
- targetGroupAssociation .ListenerId = helper .String (v .(string ))
218
- }
219
- if v , ok := dMap ["target_group_id" ]; ok {
220
- targetGroupAssociation .TargetGroupId = helper .String (v .(string ))
221
- }
222
- if v , ok := dMap ["location_id" ]; ok {
223
- targetGroupAssociation .LocationId = helper .String (v .(string ))
224
- }
225
- request .Associations = append (request .Associations , & targetGroupAssociation )
226
- }
228
+ id := d .Id ()
229
+ if isBindFromClb (id ) {
230
+ request .Associations = parseParamToRequest (d , "load_balancer_id" , id )
231
+ } else {
232
+ request .Associations = parseParamToRequest (d , "target_group_id" , id )
227
233
}
228
234
err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
229
235
ratelimit .Check (request .GetAction ())
@@ -248,3 +254,82 @@ func resourceTencentCloudClbTargetGroupAttachmentsDelete(d *schema.ResourceData,
248
254
249
255
return nil
250
256
}
257
+ func parseParamToRequest (d * schema.ResourceData , param string , id string ) (associations []* clb.TargetGroupAssociation ) {
258
+
259
+ if v , ok := d .GetOk ("associations" ); ok {
260
+ for _ , item := range v .(* schema.Set ).List () {
261
+ dMap := item .(map [string ]interface {})
262
+ targetGroupAssociation := clb.TargetGroupAssociation {}
263
+ dMap [param ] = id
264
+ for name := range dMap {
265
+ setString (name , dMap [name ].(string ), & targetGroupAssociation )
266
+ }
267
+ associations = append (associations , & targetGroupAssociation )
268
+ }
269
+ }
270
+ return associations
271
+ }
272
+ func setString (fieldName string , value string , request * clb.TargetGroupAssociation ) {
273
+ switch fieldName {
274
+ case "load_balancer_id" :
275
+ request .LoadBalancerId = helper .String (value )
276
+ case "target_group_id" :
277
+ request .TargetGroupId = helper .String (value )
278
+ case "listener_id" :
279
+ request .ListenerId = helper .String (value )
280
+ case "location_id" :
281
+ request .LocationId = helper .String (value )
282
+ default :
283
+ log .Printf ("Invalid field name: %s\n " , fieldName )
284
+ }
285
+ }
286
+ func isBindFromClb (id string ) bool {
287
+ re := regexp .MustCompile (`^(.*?)-` )
288
+ match := re .FindStringSubmatch (id )
289
+
290
+ if len (match ) > 1 {
291
+ return match [1 ] == ResourcePrefixFromClb
292
+ }
293
+ return false
294
+ }
295
+ func margeReadRequest (d * schema.ResourceData ) ([]string , map [string ]struct {}) {
296
+ resourceId := d .Id ()
297
+
298
+ associationsSet := make (map [string ]struct {})
299
+ targetGroupList := make ([]string , 0 )
300
+ if v , ok := d .GetOk ("associations" ); ok {
301
+ for _ , item := range v .(* schema.Set ).List () {
302
+ dMap := item .(map [string ]interface {})
303
+ ids := make ([]string , 0 )
304
+
305
+ processIds (resourceId , dMap , "load_balancer_id" , isBindFromClb (resourceId ), & ids )
306
+ processIds (resourceId , dMap , "target_group_id" , isBindFromClb (resourceId ), & ids )
307
+ processIds (resourceId , dMap , "listener_id" , isBindFromClb (resourceId ), & ids )
308
+ processIds (resourceId , dMap , "location_id" , isBindFromClb (resourceId ), & ids )
309
+
310
+ if groupId , ok := dMap ["target_group_id" ]; ok && groupId .(string ) != "" {
311
+ targetGroupList = append (targetGroupList , groupId .(string ))
312
+ }
313
+
314
+ associationsSet [strings .Join (ids , tccommon .FILED_SP )] = struct {}{}
315
+ }
316
+ }
317
+ if len (targetGroupList ) < 1 && ! isBindFromClb (resourceId ) {
318
+ targetGroupList = append (targetGroupList , resourceId )
319
+ }
320
+ return targetGroupList , associationsSet
321
+ }
322
+ func processIds (id string , dMap map [string ]interface {}, key string , clbFlag bool , ids * []string ) {
323
+ if clbFlag && key == "load_balancer_id" {
324
+ * ids = append (* ids , id )
325
+ } else if ! clbFlag && key == "target_group_id" {
326
+ * ids = append (* ids , id )
327
+ } else {
328
+ if v , ok := dMap [key ]; ok {
329
+ * ids = append (* ids , v .(string ))
330
+ } else {
331
+ * ids = append (* ids , "null" )
332
+ }
333
+
334
+ }
335
+ }
0 commit comments