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