@@ -2,6 +2,7 @@ package clb
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"log"
6
7
7
8
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
@@ -58,79 +59,92 @@ func ResourceTencentCloudClbCustomizedConfig() *schema.Resource {
58
59
59
60
func resourceTencentCloudClbCustomizedConfigCreate (d * schema.ResourceData , meta interface {}) error {
60
61
defer tccommon .LogElapsed ("resource.tencentcloud_clb_customized_config.create" )()
61
- logId := tccommon .GetLogId (tccommon .ContextNil )
62
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
62
+
63
+ var (
64
+ logId = tccommon .GetLogId (tccommon .ContextNil )
65
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
66
+ request = clb .NewSetCustomizedConfigForLoadBalancerRequest ()
67
+ response = clb .NewSetCustomizedConfigForLoadBalancerResponse ()
68
+ )
63
69
64
70
configName := d .Get ("config_name" ).(string )
65
71
configContent := d .Get ("config_content" ).(string )
66
-
67
- request := clb .NewSetCustomizedConfigForLoadBalancerRequest ()
68
72
request .OperationType = helper .String ("ADD" )
69
73
request .ConfigName = helper .String (configName )
70
74
request .ConfigContent = helper .String (configContent )
71
75
72
- var response * clb.SetCustomizedConfigForLoadBalancerResponse
73
76
err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
74
77
result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ().SetCustomizedConfigForLoadBalancer (request )
75
78
if e != nil {
76
79
return tccommon .RetryError (e )
77
80
} else {
78
- log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " ,
79
- logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
81
+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
82
+ if result == nil || result .Response == nil || result .Response .RequestId == nil {
83
+ return resource .NonRetryableError (fmt .Errorf ("Create CLB Customized Config Failed, Response is nil." ))
84
+ }
85
+
80
86
requestId := * result .Response .RequestId
81
87
retryErr := waitForTaskFinish (requestId , meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ())
82
88
if retryErr != nil {
83
89
return tccommon .RetryError (errors .WithStack (retryErr ))
84
90
}
85
91
}
92
+
86
93
response = result
87
94
return nil
88
95
})
96
+
89
97
if err != nil {
90
98
log .Printf ("[CRITAL]%s Create CLB Customized Config Failed, reason:%+v" , logId , err )
91
99
return err
92
100
}
101
+
102
+ if response .Response .ConfigId == nil {
103
+ return fmt .Errorf ("ConfigId is nil." )
104
+ }
105
+
93
106
d .SetId (* response .Response .ConfigId )
94
107
95
108
if v , ok := d .GetOk ("load_balancer_ids" ); ok {
96
109
loadBalancerIds := v .(* schema.Set ).List ()
97
- clbService := ClbService {
98
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
99
- }
100
- err := clbService .BindOrUnBindCustomizedConfigWithLbId (ctx ,
101
- "BIND" , * response .Response .ConfigId , loadBalancerIds )
110
+ clbService := ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
111
+ err := clbService .BindOrUnBindCustomizedConfigWithLbId (ctx , "BIND" , * response .Response .ConfigId , loadBalancerIds )
102
112
if err != nil {
103
113
log .Printf ("[CRITAL]%s Binding LB Customized Config Failed, reason:%+v" , logId , err )
104
114
return err
105
115
}
106
116
}
117
+
107
118
return resourceTencentCloudClbCustomizedConfigRead (d , meta )
108
119
}
109
120
110
121
func resourceTencentCloudClbCustomizedConfigRead (d * schema.ResourceData , meta interface {}) error {
111
122
defer tccommon .LogElapsed ("resource.tencentcloud_clb_customized_config.read" )()
112
123
defer tccommon .InconsistentCheck (d , meta )()
113
124
114
- logId := tccommon .GetLogId (tccommon .ContextNil )
115
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
125
+ var (
126
+ logId = tccommon .GetLogId (tccommon .ContextNil )
127
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
128
+ clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
129
+ configId = d .Id ()
130
+ )
116
131
117
- configId := d .Id ()
118
- clbService := ClbService {
119
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
120
- }
121
132
var config * clb.ConfigListItem
122
133
err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
123
134
result , e := clbService .DescribeLbCustomizedConfigById (ctx , configId )
124
135
if e != nil {
125
136
return tccommon .RetryError (e )
126
137
}
138
+
127
139
config = result
128
140
return nil
129
141
})
142
+
130
143
if err != nil {
131
144
log .Printf ("[CRITAL]%s read CLB customized config failed, reason:%+v" , logId , err )
132
145
return err
133
146
}
147
+
134
148
if config == nil {
135
149
d .SetId ("" )
136
150
return nil
@@ -152,13 +166,16 @@ func resourceTencentCloudClbCustomizedConfigRead(d *schema.ResourceData, meta in
152
166
log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " ,
153
167
logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
154
168
}
169
+
155
170
response = result
156
171
return nil
157
172
})
173
+
158
174
if assErr != nil {
159
175
log .Printf ("[CRITAL]%s Describe CLB Customized Config Associate List Failed, reason:%+v" , logId , assErr )
160
176
return err
161
177
}
178
+
162
179
_ = d .Set ("load_balancer_ids" , extractBindClbList (response .Response .BindList ))
163
180
164
181
return nil
@@ -167,93 +184,95 @@ func resourceTencentCloudClbCustomizedConfigRead(d *schema.ResourceData, meta in
167
184
func resourceTencentCloudClbCustomizedConfigUpdate (d * schema.ResourceData , meta interface {}) error {
168
185
defer tccommon .LogElapsed ("resource.tencentcloud_clb_customized_config.update" )()
169
186
170
- logId := tccommon .GetLogId (tccommon .ContextNil )
187
+ var (
188
+ logId = tccommon .GetLogId (tccommon .ContextNil )
189
+ configId = d .Id ()
190
+ )
171
191
172
192
d .Partial (true )
173
193
174
- configId := d .Id ()
175
- request := clb .NewSetCustomizedConfigForLoadBalancerRequest ()
176
- request .UconfigId = & configId
177
- request .OperationType = helper .String ("UPDATE" )
178
-
179
- if d .HasChange ("config_name" ) {
194
+ if d .HasChange ("config_name" ) || d .HasChange ("config_content" ) {
195
+ request := clb .NewSetCustomizedConfigForLoadBalancerRequest ()
196
+ request .UconfigId = & configId
197
+ request .OperationType = helper .String ("UPDATE" )
180
198
configName := d .Get ("config_name" ).(string )
181
- request .ConfigName = & configName
182
- }
183
-
184
- if d .HasChange ("config_content" ) {
185
199
configContent := d .Get ("config_content" ).(string )
200
+ request .ConfigName = & configName
186
201
request .ConfigContent = & configContent
187
- }
188
202
189
- err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
190
- result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ().SetCustomizedConfigForLoadBalancer (request )
191
- if e != nil {
192
- return tccommon .RetryError (e )
193
- } else {
194
- log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " ,
195
- logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
196
- requestId := * result .Response .RequestId
197
- retryErr := waitForTaskFinish (requestId , meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ())
198
- if retryErr != nil {
199
- return tccommon .RetryError (errors .WithStack (retryErr ))
203
+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
204
+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ().SetCustomizedConfigForLoadBalancer (request )
205
+ if e != nil {
206
+ return tccommon .RetryError (e )
207
+ } else {
208
+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
209
+ if result == nil || result .Response == nil || result .Response .RequestId == nil {
210
+ return resource .NonRetryableError (fmt .Errorf ("Update CLB Customized Config Failed, Response is nil." ))
211
+ }
212
+
213
+ requestId := * result .Response .RequestId
214
+ retryErr := waitForTaskFinish (requestId , meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ())
215
+ if retryErr != nil {
216
+ return tccommon .RetryError (errors .WithStack (retryErr ))
217
+ }
200
218
}
219
+
220
+ return nil
221
+ })
222
+
223
+ if err != nil {
224
+ log .Printf ("[CRITAL]%s Update CLB Customized Config Failed, reason:%+v" , logId , err )
225
+ return err
201
226
}
202
- return nil
203
- })
204
- if err != nil {
205
- log .Printf ("[CRITAL]%s Update CLB Customized Config Failed, reason:%+v" , logId , err )
206
- return err
207
227
}
208
228
209
229
if d .HasChange ("load_balancer_ids" ) {
210
230
ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
211
- clbService := ClbService {
212
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
213
- }
231
+ clbService := ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
214
232
old , new := d .GetChange ("load_balancer_ids" )
215
233
olds := old .(* schema.Set )
216
234
news := new .(* schema.Set )
217
235
add := news .Difference (olds ).List ()
218
236
remove := olds .Difference (news ).List ()
219
237
if len (remove ) > 0 {
220
- err := clbService .BindOrUnBindCustomizedConfigWithLbId (ctx ,
221
- "UNBIND" , configId , remove )
238
+ err := clbService .BindOrUnBindCustomizedConfigWithLbId (ctx , "UNBIND" , configId , remove )
222
239
if err != nil {
223
240
log .Printf ("[CRITAL]%s UnBinding LB Customized Config Failed, reason:%+v" , logId , err )
224
241
return err
225
242
}
226
243
}
244
+
227
245
if len (add ) > 0 {
228
- err := clbService .BindOrUnBindCustomizedConfigWithLbId (ctx ,
229
- "BIND" , configId , add )
246
+ err := clbService .BindOrUnBindCustomizedConfigWithLbId (ctx , "BIND" , configId , add )
230
247
if err != nil {
231
248
log .Printf ("[CRITAL]%s Binding LB Customized Config Failed, reason:%+v" , logId , err )
232
249
return err
233
250
}
234
251
}
235
252
}
253
+
236
254
return nil
237
255
}
238
256
239
257
func resourceTencentCloudClbCustomizedConfigDelete (d * schema.ResourceData , meta interface {}) error {
240
258
defer tccommon .LogElapsed ("resource.tencentcloud_clb_customized_config.delete" )()
241
259
242
- logId := tccommon .GetLogId (tccommon .ContextNil )
243
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
244
-
245
- configId := d .Id ()
246
- clbService := ClbService {
247
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
248
- }
260
+ var (
261
+ logId = tccommon .GetLogId (tccommon .ContextNil )
262
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
263
+ clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
264
+ configId = d .Id ()
265
+ )
249
266
250
267
err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
251
268
e := clbService .DeleteLbCustomizedConfigById (ctx , configId )
252
269
if e != nil {
253
270
return tccommon .RetryError (e )
254
271
}
272
+
255
273
return nil
256
274
})
275
+
257
276
if err != nil {
258
277
log .Printf ("[CRITAL]%s delete CLB customized config failed, reason:%+v" , logId , err )
259
278
return err
0 commit comments