@@ -2,10 +2,10 @@ package postgresql
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
"log"
7
8
"strings"
8
- "time"
9
9
10
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -21,7 +21,7 @@ func ResourceTencentCloudPostgresqlInstanceNetworkAccess() *schema.Resource {
21
21
Read : resourceTencentCloudPostgresqlInstanceNetworkAccessRead ,
22
22
Delete : resourceTencentCloudPostgresqlInstanceNetworkAccessDelete ,
23
23
Importer : & schema.ResourceImporter {
24
- StateContext : networkAccessCustomResourceImporter ,
24
+ State : schema . ImportStatePassthrough ,
25
25
},
26
26
Schema : map [string ]* schema.Schema {
27
27
"db_instance_id" : {
@@ -45,13 +45,6 @@ func ResourceTencentCloudPostgresqlInstanceNetworkAccess() *schema.Resource {
45
45
Description : "Subnet ID." ,
46
46
},
47
47
48
- "is_assign_vip" : {
49
- Type : schema .TypeBool ,
50
- Required : true ,
51
- ForceNew : true ,
52
- Description : "Whether to manually assign the VIP. Valid values: `true` (manually assign), `false` (automatically assign)." ,
53
- },
54
-
55
48
"vip" : {
56
49
Type : schema .TypeString ,
57
50
Optional : true ,
@@ -93,12 +86,11 @@ func resourceTencentCloudPostgresqlInstanceNetworkAccessCreate(d *schema.Resourc
93
86
subnetId = v .(string )
94
87
}
95
88
96
- if v , ok := d .GetOkExists ("is_assign_vip" ); ok {
97
- request .IsAssignVip = helper .Bool (v .(bool ))
98
- }
89
+ request .IsAssignVip = helper .Bool (false )
99
90
100
91
if v , ok := d .GetOk ("vip" ); ok {
101
92
request .Vip = helper .String (v .(string ))
93
+ request .IsAssignVip = helper .Bool (true )
102
94
vip = v .(string )
103
95
}
104
96
@@ -128,22 +120,41 @@ func resourceTencentCloudPostgresqlInstanceNetworkAccessCreate(d *schema.Resourc
128
120
}
129
121
130
122
// wait & get vip
123
+ flowId := * response .Response .FlowId
131
124
flowRequest := postgresqlv20170312 .NewDescribeTasksRequest ()
132
- flowRequest .TaskId = response . Response . FlowId
133
- err : = resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
134
- result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlV20170312Client ().DescribeTasksWithContext (ctx , request )
125
+ flowRequest .TaskId = helper . Int64Uint64 ( flowId )
126
+ err = resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
127
+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlV20170312Client ().DescribeTasksWithContext (ctx , flowRequest )
135
128
if e != nil {
136
129
return tccommon .RetryError (e )
137
130
} else {
138
131
log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
139
132
}
140
133
141
- if result == nil || result .Response == nil {
142
- return resource .NonRetryableError (fmt .Errorf ("Create postgresql instance network access failed, Response is nil." ))
134
+ if result == nil || result .Response == nil || result . Response . TaskSet == nil {
135
+ return resource .NonRetryableError (fmt .Errorf ("Describe tasks failed, Response is nil." ))
143
136
}
144
137
145
- response = result
146
- return nil
138
+ if len (result .Response .TaskSet ) == 0 {
139
+ return resource .RetryableError (fmt .Errorf ("wait TaskSet init." ))
140
+ }
141
+
142
+ if result .Response .TaskSet [0 ].Status != nil && * result .Response .TaskSet [0 ].Status == "Success" {
143
+ if result .Response .TaskSet [0 ].TaskDetail != nil && result .Response .TaskSet [0 ].TaskDetail .Output != nil {
144
+ outPutObj := make (map [string ]interface {})
145
+ outputStr := * result .Response .TaskSet [0 ].TaskDetail .Output
146
+ e := json .Unmarshal ([]byte (outputStr ), & outPutObj )
147
+ if e != nil {
148
+ return resource .NonRetryableError (fmt .Errorf ("Json unmarshall output error: %s." , e .Error ()))
149
+ }
150
+
151
+ dBInstanceNetInfo := outPutObj ["DBInstanceNetInfo" ].(map [string ]interface {})
152
+ vip = dBInstanceNetInfo ["Ip" ].(string )
153
+ return nil
154
+ }
155
+ }
156
+
157
+ return resource .RetryableError (fmt .Errorf ("postgresql instance network access is running, status is %s." , * result .Response .TaskSet [0 ].Status ))
147
158
})
148
159
149
160
if err != nil {
@@ -160,16 +171,17 @@ func resourceTencentCloudPostgresqlInstanceNetworkAccessRead(d *schema.ResourceD
160
171
defer tccommon .LogElapsed ("resource.tencentcloud_postgresql_instance_network_access.read" )()
161
172
defer tccommon .InconsistentCheck (d , meta )()
162
173
163
- logId := tccommon . GetLogId ( tccommon . ContextNil )
164
-
165
- ctx : = tccommon .NewResourceLifeCycleHandleFuncContext (context .Background (), logId , d , meta )
166
-
167
- service := PostgresqlService { client : meta .(tccommon. ProviderMeta ). GetAPIV3Conn ()}
174
+ var (
175
+ logId = tccommon . GetLogId ( tccommon . ContextNil )
176
+ ctx = tccommon .NewResourceLifeCycleHandleFuncContext (context .Background (), logId , d , meta )
177
+ service = PostgresqlService { client : meta .(tccommon. ProviderMeta ). GetAPIV3Conn ()}
178
+ )
168
179
169
180
idSplit := strings .Split (d .Id (), tccommon .FILED_SP )
170
181
if len (idSplit ) != 4 {
171
182
return fmt .Errorf ("id is broken,%s" , d .Id ())
172
183
}
184
+
173
185
dbInsntaceId := idSplit [0 ]
174
186
vpcId := idSplit [1 ]
175
187
subnetId := idSplit [2 ]
@@ -191,8 +203,9 @@ func resourceTencentCloudPostgresqlInstanceNetworkAccessRead(d *schema.ResourceD
191
203
log .Printf ("[WARN]%s resource `postgresql_instance_network_access` [%s] not found, please check if it has been deleted.\n " , logId , d .Id ())
192
204
return nil
193
205
}
194
- if err := resourceTencentCloudPostgresqlInstanceNetworkAccessReadPreHandleResponse0 (ctx , respData ); err != nil {
195
- return err
206
+
207
+ if respData .DBInstanceId != nil {
208
+ _ = d .Set ("db_instance_id" , respData .DBInstanceId )
196
209
}
197
210
198
211
if respData .VpcId != nil {
@@ -203,128 +216,95 @@ func resourceTencentCloudPostgresqlInstanceNetworkAccessRead(d *schema.ResourceD
203
216
_ = d .Set ("subnet_id" , respData .SubnetId )
204
217
}
205
218
206
- if respData .DBInstanceId != nil {
207
- _ = d .Set ("db_instance_id" , respData .DBInstanceId )
219
+ if respData .DBInstanceNetInfo != nil && len (respData .DBInstanceNetInfo ) > 0 {
220
+ for _ , item := range respData .DBInstanceNetInfo {
221
+ if * item .Ip == vip {
222
+ _ = d .Set ("vip" , item .Ip )
223
+ break
224
+ }
225
+ }
208
226
}
209
227
210
- _ = vpcId
211
- _ = subnetId
212
- _ = vip
213
228
return nil
214
229
}
215
230
216
231
func resourceTencentCloudPostgresqlInstanceNetworkAccessDelete (d * schema.ResourceData , meta interface {}) error {
217
232
defer tccommon .LogElapsed ("resource.tencentcloud_postgresql_instance_network_access.delete" )()
218
233
defer tccommon .InconsistentCheck (d , meta )()
219
234
220
- logId := tccommon .GetLogId (tccommon .ContextNil )
221
- ctx := tccommon .NewResourceLifeCycleHandleFuncContext (context .Background (), logId , d , meta )
235
+ var (
236
+ logId = tccommon .GetLogId (tccommon .ContextNil )
237
+ ctx = tccommon .NewResourceLifeCycleHandleFuncContext (context .Background (), logId , d , meta )
238
+ request = postgresqlv20170312 .NewDeleteDBInstanceNetworkAccessRequest ()
239
+ response = postgresqlv20170312 .NewDeleteDBInstanceNetworkAccessResponse ()
240
+ )
222
241
223
242
idSplit := strings .Split (d .Id (), tccommon .FILED_SP )
224
243
if len (idSplit ) != 4 {
225
244
return fmt .Errorf ("id is broken,%s" , d .Id ())
226
245
}
246
+
227
247
dbInsntaceId := idSplit [0 ]
228
248
vpcId := idSplit [1 ]
229
249
subnetId := idSplit [2 ]
230
250
vip := idSplit [3 ]
231
251
232
- var (
233
- request = postgresqlv20170312 .NewDeleteDBInstanceNetworkAccessRequest ()
234
- response = postgresqlv20170312 .NewDeleteDBInstanceNetworkAccessResponse ()
235
- )
236
-
237
252
request .DBInstanceId = helper .String (dbInsntaceId )
238
-
239
253
request .VpcId = helper .String (vpcId )
240
-
241
254
request .SubnetId = helper .String (subnetId )
242
-
243
255
request .Vip = helper .String (vip )
244
-
245
256
err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
246
257
result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlV20170312Client ().DeleteDBInstanceNetworkAccessWithContext (ctx , request )
247
258
if e != nil {
248
259
return tccommon .RetryError (e )
249
260
} else {
250
261
log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
251
262
}
263
+
264
+ if result == nil || result .Response == nil {
265
+ return resource .NonRetryableError (fmt .Errorf ("Delete postgresql instance network access failed, Response is nil." ))
266
+ }
267
+
252
268
response = result
253
269
return nil
254
270
})
271
+
255
272
if err != nil {
256
273
log .Printf ("[CRITAL]%s delete postgresql instance network access failed, reason:%+v" , logId , err )
257
274
return err
258
275
}
259
276
260
- _ = response
261
- if _ , err := (& resource.StateChangeConf {
262
- Delay : 10 * time .Second ,
263
- MinTimeout : 3 * time .Second ,
264
- Pending : []string {},
265
- Refresh : resourcePostgresqlInstanceNetworkAccessDeleteStateRefreshFunc_0_0 (ctx , dbInsntaceId , vpcId , subnetId , vip ),
266
- Target : []string {"Running" },
267
- Timeout : 180 * time .Second ,
268
- }).WaitForStateContext (ctx ); err != nil {
269
- return err
270
- }
271
- return nil
272
- }
273
-
274
- func resourcePostgresqlInstanceNetworkAccessCreateStateRefreshFunc_0_0 (ctx context.Context , dbInsntaceId string , vpcId string , subnetId string , vip string ) resource.StateRefreshFunc {
275
- var req * postgresqlv20170312.DescribeDBInstanceAttributeRequest
276
- return func () (interface {}, string , error ) {
277
- meta := tccommon .ProviderMetaFromContext (ctx )
278
- if meta == nil {
279
- return nil , "" , fmt .Errorf ("resource data can not be nil" )
277
+ // wait
278
+ flowId := * response .Response .FlowId
279
+ flowRequest := postgresqlv20170312 .NewDescribeTasksRequest ()
280
+ flowRequest .TaskId = helper .Int64Uint64 (flowId )
281
+ err = resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
282
+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlV20170312Client ().DescribeTasksWithContext (ctx , flowRequest )
283
+ if e != nil {
284
+ return tccommon .RetryError (e )
285
+ } else {
286
+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
280
287
}
281
- if req == nil {
282
- d := tccommon .ResourceDataFromContext (ctx )
283
- if d == nil {
284
- return nil , "" , fmt .Errorf ("resource data can not be nil" )
285
- }
286
- _ = d
287
- req = postgresqlv20170312 .NewDescribeDBInstanceAttributeRequest ()
288
- req .DBInstanceId = helper .String (dbInsntaceId )
289
288
289
+ if result == nil || result .Response == nil || result .Response .TaskSet == nil {
290
+ return resource .NonRetryableError (fmt .Errorf ("Describe tasks failed, Response is nil." ))
290
291
}
291
- resp , err := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlV20170312Client ().DescribeDBInstanceAttributeWithContext (ctx , req )
292
- if err != nil {
293
- return nil , "" , err
294
- }
295
- if resp == nil || resp .Response == nil {
296
- return nil , "" , nil
297
- }
298
- state := fmt .Sprintf ("%v" , * resp .Response .DBInstance .DBInstanceStatus )
299
- return resp .Response , state , nil
300
- }
301
- }
302
292
303
- func resourcePostgresqlInstanceNetworkAccessDeleteStateRefreshFunc_0_0 (ctx context.Context , dbInsntaceId string , vpcId string , subnetId string , vip string ) resource.StateRefreshFunc {
304
- var req * postgresqlv20170312.DescribeDBInstanceAttributeRequest
305
- return func () (interface {}, string , error ) {
306
- meta := tccommon .ProviderMetaFromContext (ctx )
307
- if meta == nil {
308
- return nil , "" , fmt .Errorf ("resource data can not be nil" )
293
+ if len (result .Response .TaskSet ) == 0 {
294
+ return resource .RetryableError (fmt .Errorf ("wait TaskSet init." ))
309
295
}
310
- if req == nil {
311
- d := tccommon .ResourceDataFromContext (ctx )
312
- if d == nil {
313
- return nil , "" , fmt .Errorf ("resource data can not be nil" )
314
- }
315
- _ = d
316
- req = postgresqlv20170312 .NewDescribeDBInstanceAttributeRequest ()
317
- req .DBInstanceId = helper .String (dbInsntaceId )
318
296
297
+ if result .Response .TaskSet [0 ].Status != nil && * result .Response .TaskSet [0 ].Status == "Success" {
298
+ return nil
319
299
}
320
- resp , err := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlV20170312Client ().DescribeDBInstanceAttributeWithContext (ctx , req )
321
- if err != nil {
322
- return nil , "" , err
323
- }
324
- if resp == nil || resp .Response == nil {
325
- return nil , "" , nil
326
- }
327
- state := fmt .Sprintf ("%v" , * resp .Response .DBInstance .DBInstanceStatus )
328
- return resp .Response , state , nil
300
+
301
+ return resource .RetryableError (fmt .Errorf ("postgresql instance network access is running, status is %s." , * result .Response .TaskSet [0 ].Status ))
302
+ })
303
+
304
+ if err != nil {
305
+ log .Printf ("[CRITAL]%s delete postgresql instance network access failed, reason:%+v" , logId , err )
306
+ return err
329
307
}
308
+
309
+ return nil
330
310
}
0 commit comments