9
9
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
10
10
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11
11
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
12
+ "github.com/hashicorp/terraform-plugin-sdk/helper/validation"
12
13
)
13
14
14
15
func resourceAliCloudVpcGatewayEndpoint () * schema.Resource {
@@ -21,9 +22,9 @@ func resourceAliCloudVpcGatewayEndpoint() *schema.Resource {
21
22
State : schema .ImportStatePassthrough ,
22
23
},
23
24
Timeouts : & schema.ResourceTimeout {
24
- Create : schema .DefaultTimeout (5 * time .Minute ),
25
+ Create : schema .DefaultTimeout (8 * time .Minute ),
25
26
Update : schema .DefaultTimeout (5 * time .Minute ),
26
- Delete : schema .DefaultTimeout (5 * time .Minute ),
27
+ Delete : schema .DefaultTimeout (10 * time .Minute ),
27
28
},
28
29
Schema : map [string ]* schema.Schema {
29
30
"create_time" : {
@@ -39,8 +40,9 @@ func resourceAliCloudVpcGatewayEndpoint() *schema.Resource {
39
40
Optional : true ,
40
41
},
41
42
"policy_document" : {
42
- Type : schema .TypeString ,
43
- Optional : true ,
43
+ Type : schema .TypeString ,
44
+ Optional : true ,
45
+ ValidateFunc : validation .ValidateJsonString ,
44
46
DiffSuppressFunc : func (k , old , new string , d * schema.ResourceData ) bool {
45
47
equal , _ := compareJsonTemplateAreEquivalent (old , new )
46
48
return equal
@@ -51,6 +53,11 @@ func resourceAliCloudVpcGatewayEndpoint() *schema.Resource {
51
53
Optional : true ,
52
54
Computed : true ,
53
55
},
56
+ "route_tables" : {
57
+ Type : schema .TypeSet ,
58
+ Optional : true ,
59
+ Elem : & schema.Schema {Type : schema .TypeString },
60
+ },
54
61
"service_name" : {
55
62
Type : schema .TypeString ,
56
63
Required : true ,
@@ -71,11 +78,13 @@ func resourceAliCloudVpcGatewayEndpoint() *schema.Resource {
71
78
}
72
79
73
80
func resourceAliCloudVpcGatewayEndpointCreate (d * schema.ResourceData , meta interface {}) error {
81
+
74
82
client := meta .(* connectivity.AliyunClient )
75
83
76
84
action := "CreateVpcGatewayEndpoint"
77
85
var request map [string ]interface {}
78
86
var response map [string ]interface {}
87
+ query := make (map [string ]interface {})
79
88
var err error
80
89
request = make (map [string ]interface {})
81
90
request ["RegionId" ] = client .RegionId
@@ -95,21 +104,24 @@ func resourceAliCloudVpcGatewayEndpointCreate(d *schema.ResourceData, meta inter
95
104
if v , ok := d .GetOk ("resource_group_id" ); ok {
96
105
request ["ResourceGroupId" ] = v
97
106
}
107
+ if v , ok := d .GetOk ("tags" ); ok {
108
+ tagsMap := ConvertTags (v .(map [string ]interface {}))
109
+ request = expandTagsToMap (request , tagsMap )
110
+ }
111
+
98
112
wait := incrementalWait (3 * time .Second , 5 * time .Second )
99
113
err = resource .Retry (d .Timeout (schema .TimeoutCreate ), func () * resource.RetryError {
100
- response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , nil , request , true )
101
- request ["ClientToken" ] = buildClientToken (action )
102
-
114
+ response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , query , request , true )
103
115
if err != nil {
104
116
if IsExpectedErrors (err , []string {"OperationFailed.ConcurrentOperation" , "IdempotentParameterMismatch" , "IncorrectStatus.Vpc" , "OperationConflict" , "IncorrectStatus" , "ServiceUnavailable" , "LastTokenProcessing" , "SystemBusy" }) || NeedRetry (err ) {
105
117
wait ()
106
118
return resource .RetryableError (err )
107
119
}
108
120
return resource .NonRetryableError (err )
109
121
}
110
- addDebug (action , response , request )
111
122
return nil
112
123
})
124
+ addDebug (action , response , request )
113
125
114
126
if err != nil {
115
127
return WrapErrorf (err , DefaultErrorMsg , "alicloud_vpc_gateway_endpoint" , action , AlibabaCloudSdkGoERROR )
@@ -149,6 +161,12 @@ func resourceAliCloudVpcGatewayEndpointRead(d *schema.ResourceData, meta interfa
149
161
d .Set ("status" , objectRaw ["EndpointStatus" ])
150
162
d .Set ("vpc_id" , objectRaw ["VpcId" ])
151
163
164
+ routeTablesRaw := make ([]interface {}, 0 )
165
+ if objectRaw ["RouteTables" ] != nil {
166
+ routeTablesRaw = objectRaw ["RouteTables" ].([]interface {})
167
+ }
168
+
169
+ d .Set ("route_tables" , routeTablesRaw )
152
170
tagsMaps := objectRaw ["Tags" ]
153
171
d .Set ("tags" , tagsToMap (tagsMaps ))
154
172
@@ -159,11 +177,14 @@ func resourceAliCloudVpcGatewayEndpointUpdate(d *schema.ResourceData, meta inter
159
177
client := meta .(* connectivity.AliyunClient )
160
178
var request map [string ]interface {}
161
179
var response map [string ]interface {}
180
+ var query map [string ]interface {}
162
181
update := false
163
182
d .Partial (true )
164
- action := "UpdateVpcGatewayEndpointAttribute"
183
+
165
184
var err error
185
+ action := "UpdateVpcGatewayEndpointAttribute"
166
186
request = make (map [string ]interface {})
187
+ query = make (map [string ]interface {})
167
188
request ["EndpointId" ] = d .Id ()
168
189
request ["RegionId" ] = client .RegionId
169
190
request ["ClientToken" ] = buildClientToken (action )
@@ -185,19 +206,17 @@ func resourceAliCloudVpcGatewayEndpointUpdate(d *schema.ResourceData, meta inter
185
206
if update {
186
207
wait := incrementalWait (3 * time .Second , 5 * time .Second )
187
208
err = resource .Retry (d .Timeout (schema .TimeoutUpdate ), func () * resource.RetryError {
188
- response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , nil , request , true )
189
- request ["ClientToken" ] = buildClientToken (action )
190
-
209
+ response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , query , request , true )
191
210
if err != nil {
192
211
if IsExpectedErrors (err , []string {"OperationConflict" , "IncorrectStatus" , "ServiceUnavailable" , "LastTokenProcessing" , "SystemBusy" , "OperationFailed.ConcurrentOperation" , "IdempotentParameterMismatch" , "IncorrectStatus.Vpc" }) || NeedRetry (err ) {
193
212
wait ()
194
213
return resource .RetryableError (err )
195
214
}
196
215
return resource .NonRetryableError (err )
197
216
}
198
- addDebug (action , response , request )
199
217
return nil
200
218
})
219
+ addDebug (action , response , request )
201
220
if err != nil {
202
221
return WrapErrorf (err , DefaultErrorMsg , d .Id (), action , AlibabaCloudSdkGoERROR )
203
222
}
@@ -206,50 +225,120 @@ func resourceAliCloudVpcGatewayEndpointUpdate(d *schema.ResourceData, meta inter
206
225
if _ , err := stateConf .WaitForState (); err != nil {
207
226
return WrapErrorf (err , IdMsg , d .Id ())
208
227
}
209
- d .SetPartial ("gateway_endpoint_name" )
210
- d .SetPartial ("gateway_endpoint_descrption" )
211
- d .SetPartial ("policy_document" )
212
228
}
213
229
update = false
214
230
action = "MoveResourceGroup"
215
231
request = make (map [string ]interface {})
232
+ query = make (map [string ]interface {})
216
233
request ["ResourceId" ] = d .Id ()
217
234
request ["RegionId" ] = client .RegionId
218
- if ! d .IsNewResource () && d .HasChange ("resource_group_id" ) {
235
+ if _ , ok := d . GetOk ( "resource_group_id" ); ok && ! d .IsNewResource () && d .HasChange ("resource_group_id" ) {
219
236
update = true
220
- request ["NewResourceGroupId" ] = d .Get ("resource_group_id" )
221
237
}
222
-
238
+ request [ "NewResourceGroupId" ] = d . Get ( "resource_group_id" )
223
239
request ["ResourceType" ] = "GatewayEndpoint"
224
240
if update {
225
241
wait := incrementalWait (3 * time .Second , 5 * time .Second )
226
242
err = resource .Retry (d .Timeout (schema .TimeoutUpdate ), func () * resource.RetryError {
227
- response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , nil , request , false )
228
-
243
+ response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , query , request , true )
229
244
if err != nil {
230
245
if NeedRetry (err ) {
231
246
wait ()
232
247
return resource .RetryableError (err )
233
248
}
234
249
return resource .NonRetryableError (err )
235
250
}
236
- addDebug (action , response , request )
237
251
return nil
238
252
})
253
+ addDebug (action , response , request )
239
254
if err != nil {
240
255
return WrapErrorf (err , DefaultErrorMsg , d .Id (), action , AlibabaCloudSdkGoERROR )
241
256
}
242
- d .SetPartial ("resource_group_id" )
243
257
}
244
258
245
- update = false
259
+ if d .HasChange ("route_tables" ) {
260
+ oldEntry , newEntry := d .GetChange ("route_tables" )
261
+ oldEntrySet := oldEntry .(* schema.Set )
262
+ newEntrySet := newEntry .(* schema.Set )
263
+ removed := oldEntrySet .Difference (newEntrySet )
264
+ added := newEntrySet .Difference (oldEntrySet )
265
+
266
+ if removed .Len () > 0 {
267
+ action := "DissociateRouteTablesFromVpcGatewayEndpoint"
268
+ request = make (map [string ]interface {})
269
+ query = make (map [string ]interface {})
270
+ request ["EndpointId" ] = d .Id ()
271
+ request ["RegionId" ] = client .RegionId
272
+ request ["ClientToken" ] = buildClientToken (action )
273
+ localData := removed .List ()
274
+ routeTableIdsMapsArray := localData
275
+ request ["RouteTableIds" ] = routeTableIdsMapsArray
276
+
277
+ wait := incrementalWait (3 * time .Second , 5 * time .Second )
278
+ err = resource .Retry (d .Timeout (schema .TimeoutUpdate ), func () * resource.RetryError {
279
+ response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , query , request , true )
280
+ if err != nil {
281
+ if NeedRetry (err ) {
282
+ wait ()
283
+ return resource .RetryableError (err )
284
+ }
285
+ return resource .NonRetryableError (err )
286
+ }
287
+ return nil
288
+ })
289
+ addDebug (action , response , request )
290
+ if err != nil {
291
+ return WrapErrorf (err , DefaultErrorMsg , d .Id (), action , AlibabaCloudSdkGoERROR )
292
+ }
293
+ vpcServiceV2 := VpcServiceV2 {client }
294
+ stateConf := BuildStateConf ([]string {}, []string {"Created" }, d .Timeout (schema .TimeoutUpdate ), 5 * time .Second , vpcServiceV2 .VpcGatewayEndpointStateRefreshFunc (d .Id (), "EndpointStatus" , []string {}))
295
+ if _ , err := stateConf .WaitForState (); err != nil {
296
+ return WrapErrorf (err , IdMsg , d .Id ())
297
+ }
298
+
299
+ }
300
+
301
+ if added .Len () > 0 {
302
+ action := "AssociateRouteTablesWithVpcGatewayEndpoint"
303
+ request = make (map [string ]interface {})
304
+ query = make (map [string ]interface {})
305
+ request ["EndpointId" ] = d .Id ()
306
+ request ["RegionId" ] = client .RegionId
307
+ request ["ClientToken" ] = buildClientToken (action )
308
+ localData := added .List ()
309
+ routeTableIdsMapsArray := localData
310
+ request ["RouteTableIds" ] = routeTableIdsMapsArray
311
+
312
+ wait := incrementalWait (3 * time .Second , 5 * time .Second )
313
+ err = resource .Retry (d .Timeout (schema .TimeoutUpdate ), func () * resource.RetryError {
314
+ response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , query , request , true )
315
+ if err != nil {
316
+ if NeedRetry (err ) {
317
+ wait ()
318
+ return resource .RetryableError (err )
319
+ }
320
+ return resource .NonRetryableError (err )
321
+ }
322
+ return nil
323
+ })
324
+ addDebug (action , response , request )
325
+ if err != nil {
326
+ return WrapErrorf (err , DefaultErrorMsg , d .Id (), action , AlibabaCloudSdkGoERROR )
327
+ }
328
+ vpcServiceV2 := VpcServiceV2 {client }
329
+ stateConf := BuildStateConf ([]string {}, []string {"Created" }, d .Timeout (schema .TimeoutUpdate ), 5 * time .Second , vpcServiceV2 .VpcGatewayEndpointStateRefreshFunc (d .Id (), "EndpointStatus" , []string {}))
330
+ if _ , err := stateConf .WaitForState (); err != nil {
331
+ return WrapErrorf (err , IdMsg , d .Id ())
332
+ }
333
+
334
+ }
335
+
336
+ }
246
337
if d .HasChange ("tags" ) {
247
- update = true
248
338
vpcServiceV2 := VpcServiceV2 {client }
249
339
if err := vpcServiceV2 .SetResourceTags (d , "GatewayEndpoint" ); err != nil {
250
340
return WrapError (err )
251
341
}
252
- d .SetPartial ("tags" )
253
342
}
254
343
d .Partial (false )
255
344
return resourceAliCloudVpcGatewayEndpointRead (d , meta )
@@ -261,16 +350,16 @@ func resourceAliCloudVpcGatewayEndpointDelete(d *schema.ResourceData, meta inter
261
350
action := "DeleteVpcGatewayEndpoint"
262
351
var request map [string ]interface {}
263
352
var response map [string ]interface {}
353
+ query := make (map [string ]interface {})
264
354
var err error
265
355
request = make (map [string ]interface {})
266
356
request ["EndpointId" ] = d .Id ()
267
357
request ["RegionId" ] = client .RegionId
268
-
269
358
request ["ClientToken" ] = buildClientToken (action )
270
359
271
360
wait := incrementalWait (3 * time .Second , 5 * time .Second )
272
361
err = resource .Retry (d .Timeout (schema .TimeoutDelete ), func () * resource.RetryError {
273
- response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , nil , request , true )
362
+ response , err = client .RpcPost ("Vpc" , "2016-04-28" , action , query , request , true )
274
363
request ["ClientToken" ] = buildClientToken (action )
275
364
276
365
if err != nil {
@@ -280,12 +369,12 @@ func resourceAliCloudVpcGatewayEndpointDelete(d *schema.ResourceData, meta inter
280
369
}
281
370
return resource .NonRetryableError (err )
282
371
}
283
- addDebug (action , response , request )
284
372
return nil
285
373
})
374
+ addDebug (action , response , request )
286
375
287
376
if err != nil {
288
- if IsExpectedErrors (err , []string {"ResourceNotFound.GatewayEndpoint" }) {
377
+ if IsExpectedErrors (err , []string {"ResourceNotFound.GatewayEndpoint" }) || NotFoundError ( err ) {
289
378
return nil
290
379
}
291
380
return WrapErrorf (err , DefaultErrorMsg , d .Id (), action , AlibabaCloudSdkGoERROR )
@@ -296,5 +385,6 @@ func resourceAliCloudVpcGatewayEndpointDelete(d *schema.ResourceData, meta inter
296
385
if _ , err := stateConf .WaitForState (); err != nil {
297
386
return WrapErrorf (err , IdMsg , d .Id ())
298
387
}
388
+
299
389
return nil
300
390
}
0 commit comments