@@ -47,6 +47,11 @@ func ResourceTencentCloudVpnCustomerGateway() *schema.Resource {
47
47
Optional : true ,
48
48
Description : "A list of tags used to associate different resources." ,
49
49
},
50
+ "bgp_asn" : {
51
+ Type : schema .TypeInt ,
52
+ Optional : true ,
53
+ Description : "BGP ASN. Value range: 1 - 4294967295. Using BGP requires configuring ASN. 139341, 45090, and 58835 are not available." ,
54
+ },
50
55
"create_time" : {
51
56
Type : schema .TypeString ,
52
57
Computed : true ,
@@ -59,33 +64,53 @@ func ResourceTencentCloudVpnCustomerGateway() *schema.Resource {
59
64
func resourceTencentCloudVpnCustomerGatewayCreate (d * schema.ResourceData , meta interface {}) error {
60
65
defer tccommon .LogElapsed ("resource.tencentcloud_vpn_customer_gateway.create" )()
61
66
62
- logId := tccommon .GetLogId (tccommon .ContextNil )
63
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
67
+ var (
68
+ logId = tccommon .GetLogId (tccommon .ContextNil )
69
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
70
+ request = vpc .NewCreateCustomerGatewayRequest ()
71
+ response = vpc .NewCreateCustomerGatewayResponse ()
72
+ )
73
+
74
+ if v , ok := d .GetOk ("name" ); ok {
75
+ request .CustomerGatewayName = helper .String (v .(string ))
76
+ }
77
+
78
+ if v , ok := d .GetOk ("public_ip_address" ); ok {
79
+ request .IpAddress = helper .String (v .(string ))
80
+ }
81
+
82
+ if v , ok := d .GetOkExists ("bgp_asn" ); ok {
83
+ request .BgpAsn = helper .IntInt64 (v .(int ))
84
+ }
64
85
65
- request := vpc .NewCreateCustomerGatewayRequest ()
66
- request .CustomerGatewayName = helper .String (d .Get ("name" ).(string ))
67
- request .IpAddress = helper .String (d .Get ("public_ip_address" ).(string ))
68
- var response * vpc.CreateCustomerGatewayResponse
69
86
err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
70
87
result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().CreateCustomerGateway (request )
71
88
if e != nil {
72
89
log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " ,
73
90
logId , request .GetAction (), request .ToJsonString (), e .Error ())
74
91
return tccommon .RetryError (e )
75
92
}
93
+
94
+ if result == nil || result .Response == nil || result .Response .CustomerGateway == nil {
95
+ return resource .RetryableError (fmt .Errorf ("Create VPN customer gateway failed, Response is nil." ))
96
+ }
97
+
76
98
response = result
77
99
return nil
78
100
})
101
+
79
102
if err != nil {
80
103
log .Printf ("[CRITAL]%s create VPN customer gateway failed, reason:%s\n " , logId , err .Error ())
81
104
return err
82
105
}
83
106
84
- if response .Response .CustomerGateway == nil {
107
+ if response .Response .CustomerGateway . CustomerGatewayId == nil {
85
108
return fmt .Errorf ("VPN customer gateway id is nil" )
86
109
}
110
+
87
111
customerGatewayId := * response .Response .CustomerGateway .CustomerGatewayId
88
112
d .SetId (customerGatewayId )
113
+
89
114
// must wait for finishing creating customer gateway
90
115
statRequest := vpc .NewDescribeCustomerGatewaysRequest ()
91
116
statRequest .CustomerGatewayIds = []* string {response .Response .CustomerGateway .CustomerGatewayId }
@@ -96,14 +121,20 @@ func resourceTencentCloudVpnCustomerGatewayCreate(d *schema.ResourceData, meta i
96
121
logId , request .GetAction (), request .ToJsonString (), e .Error ())
97
122
return tccommon .RetryError (e )
98
123
} else {
124
+ if result == nil || result .Response == nil || result .Response .CustomerGatewaySet == nil {
125
+ return resource .NonRetryableError (fmt .Errorf ("response is nil" ))
126
+ }
127
+
99
128
//if not, quit
100
129
if len (result .Response .CustomerGatewaySet ) != 1 {
101
130
return resource .NonRetryableError (fmt .Errorf ("creating error" ))
102
131
}
132
+
103
133
//else consider created, cos there is no status of gateway
104
134
return nil
105
135
}
106
136
})
137
+
107
138
if err != nil {
108
139
log .Printf ("[CRITAL]%s create VPN customer gateway failed, reason:%s\n " , logId , err .Error ())
109
140
return err
@@ -112,10 +143,8 @@ func resourceTencentCloudVpnCustomerGatewayCreate(d *schema.ResourceData, meta i
112
143
//modify tags
113
144
if tags := helper .GetTags (d , "tags" ); len (tags ) > 0 {
114
145
tagService := svctag .NewTagService (meta .(tccommon.ProviderMeta ).GetAPIV3Conn ())
115
-
116
146
region := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().Region
117
147
resourceName := tccommon .BuildTagResourceName ("vpc" , "cgw" , region , customerGatewayId )
118
-
119
148
if err := tagService .ModifyTags (ctx , resourceName , tags , nil ); err != nil {
120
149
return err
121
150
}
@@ -128,11 +157,13 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
128
157
defer tccommon .LogElapsed ("resource.tencentcloud_vpn_customer_gateway.read" )()
129
158
defer tccommon .InconsistentCheck (d , meta )()
130
159
131
- logId := tccommon .GetLogId (tccommon .ContextNil )
132
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
160
+ var (
161
+ logId = tccommon .GetLogId (tccommon .ContextNil )
162
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
163
+ request = vpc .NewDescribeCustomerGatewaysRequest ()
164
+ customerGatewayId = d .Id ()
165
+ )
133
166
134
- customerGatewayId := d .Id ()
135
- request := vpc .NewDescribeCustomerGatewaysRequest ()
136
167
request .CustomerGatewayIds = []* string {& customerGatewayId }
137
168
var response * vpc.DescribeCustomerGatewaysResponse
138
169
var iacExtInfo connectivity.IacExtInfo
@@ -154,23 +185,41 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
154
185
return tccommon .RetryError (e )
155
186
}
156
187
}
188
+
189
+ if result == nil || result .Response == nil || result .Response .CustomerGatewaySet == nil {
190
+ return resource .NonRetryableError (fmt .Errorf ("Read VPN customer gateway failed, Response is nil." ))
191
+ }
192
+
157
193
response = result
158
194
return nil
159
195
})
196
+
160
197
if err != nil {
161
198
log .Printf ("[CRITAL]%s read VPN customer gateway failed, reason:%s\n " , logId , err .Error ())
162
199
return err
163
200
}
164
- if response == nil || response .Response == nil || len (response .Response .CustomerGatewaySet ) < 1 {
201
+
202
+ if len (response .Response .CustomerGatewaySet ) < 1 {
165
203
d .SetId ("" )
166
204
return nil
167
205
}
168
206
169
207
gateway := response .Response .CustomerGatewaySet [0 ]
208
+ if gateway .CustomerGatewayName != nil {
209
+ _ = d .Set ("name" , gateway .CustomerGatewayName )
210
+ }
170
211
171
- _ = d .Set ("name" , * gateway .CustomerGatewayName )
172
- _ = d .Set ("public_ip_address" , * gateway .IpAddress )
173
- _ = d .Set ("create_time" , * gateway .CreatedTime )
212
+ if gateway .IpAddress != nil {
213
+ _ = d .Set ("public_ip_address" , gateway .IpAddress )
214
+ }
215
+
216
+ if gateway .BgpAsn != nil && * gateway .BgpAsn != 0 {
217
+ _ = d .Set ("bgp_asn" , gateway .BgpAsn )
218
+ }
219
+
220
+ if gateway .CreatedTime != nil {
221
+ _ = d .Set ("create_time" , gateway .CreatedTime )
222
+ }
174
223
175
224
//tags
176
225
tagService := svctag .NewTagService (meta .(tccommon.ProviderMeta ).GetAPIV3Conn ())
@@ -179,6 +228,7 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
179
228
if err != nil {
180
229
return err
181
230
}
231
+
182
232
_ = d .Set ("tags" , tags )
183
233
184
234
return nil
@@ -187,24 +237,35 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
187
237
func resourceTencentCloudVpnCustomerGatewayUpdate (d * schema.ResourceData , meta interface {}) error {
188
238
defer tccommon .LogElapsed ("resource.tencentcloud_vpn_customer_gateway.update" )()
189
239
190
- logId := tccommon .GetLogId (tccommon .ContextNil )
191
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
240
+ var (
241
+ logId = tccommon .GetLogId (tccommon .ContextNil )
242
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
243
+ customerGatewayId = d .Id ()
244
+ )
192
245
193
246
d .Partial (true )
194
- customerGatewayId := d .Id ()
195
- request := vpc .NewModifyCustomerGatewayAttributeRequest ()
196
- request .CustomerGatewayId = & customerGatewayId
197
- if d .HasChange ("name" ) {
198
- request .CustomerGatewayName = helper .String (d .Get ("name" ).(string ))
247
+ if d .HasChange ("name" ) || d .HasChange ("bgp_asn" ) {
248
+ request := vpc .NewModifyCustomerGatewayAttributeRequest ()
249
+ request .CustomerGatewayId = & customerGatewayId
250
+ if v , ok := d .GetOk ("name" ); ok {
251
+ request .CustomerGatewayName = helper .String (v .(string ))
252
+ }
253
+
254
+ if v , ok := d .GetOkExists ("bgp_asn" ); ok {
255
+ request .BgpAsn = helper .IntUint64 (v .(int ))
256
+ }
257
+
199
258
err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
200
259
_ , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().ModifyCustomerGatewayAttribute (request )
201
260
if e != nil {
202
261
log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " ,
203
262
logId , request .GetAction (), request .ToJsonString (), e .Error ())
204
263
return tccommon .RetryError (e )
205
264
}
265
+
206
266
return nil
207
267
})
268
+
208
269
if err != nil {
209
270
log .Printf ("[CRITAL]%s modify VPN customer gateway failed, reason:%s\n " , logId , err .Error ())
210
271
return err
@@ -223,14 +284,14 @@ func resourceTencentCloudVpnCustomerGatewayUpdate(d *schema.ResourceData, meta i
223
284
return err
224
285
}
225
286
}
287
+
226
288
d .Partial (false )
227
289
228
290
return resourceTencentCloudVpnCustomerGatewayRead (d , meta )
229
291
}
230
292
231
293
func resourceTencentCloudVpnCustomerGatewayDelete (d * schema.ResourceData , meta interface {}) error {
232
294
defer tccommon .LogElapsed ("resource.tencentcloud_vpn_customer_gateway.delete" )()
233
-
234
295
logId := tccommon .GetLogId (tccommon .ContextNil )
235
296
236
297
customerGatewayId := d .Id ()
@@ -259,6 +320,10 @@ func resourceTencentCloudVpnCustomerGatewayDelete(d *schema.ResourceData, meta i
259
320
logId , tRequest .GetAction (), tRequest .ToJsonString (), e .Error ())
260
321
return tccommon .RetryError (e )
261
322
} else {
323
+ if result == nil || result .Response == nil || result .Response .VpnConnectionSet == nil {
324
+ return resource .NonRetryableError (fmt .Errorf ("Read VPN connections failed, Response is nil." ))
325
+ }
326
+
262
327
if len (result .Response .VpnConnectionSet ) == 0 {
263
328
return nil
264
329
} else {
@@ -306,6 +371,10 @@ func resourceTencentCloudVpnCustomerGatewayDelete(d *schema.ResourceData, meta i
306
371
return tccommon .RetryError (e )
307
372
}
308
373
} else {
374
+ if result == nil || result .Response == nil || result .Response .CustomerGatewaySet == nil {
375
+ return resource .NonRetryableError (fmt .Errorf ("Read VPN customer gateways failed, Response is nil." ))
376
+ }
377
+
309
378
//if not, quit
310
379
if len (result .Response .CustomerGatewaySet ) == 0 {
311
380
return nil
0 commit comments