@@ -47,6 +47,11 @@ func ResourceTencentCloudVpnCustomerGateway() *schema.Resource {
4747 Optional : true ,
4848 Description : "A list of tags used to associate different resources." ,
4949 },
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+ },
5055 "create_time" : {
5156 Type : schema .TypeString ,
5257 Computed : true ,
@@ -59,33 +64,53 @@ func ResourceTencentCloudVpnCustomerGateway() *schema.Resource {
5964func resourceTencentCloudVpnCustomerGatewayCreate (d * schema.ResourceData , meta interface {}) error {
6065 defer tccommon .LogElapsed ("resource.tencentcloud_vpn_customer_gateway.create" )()
6166
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+ }
6485
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
6986 err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
7087 result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().CreateCustomerGateway (request )
7188 if e != nil {
7289 log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " ,
7390 logId , request .GetAction (), request .ToJsonString (), e .Error ())
7491 return tccommon .RetryError (e )
7592 }
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+
7698 response = result
7799 return nil
78100 })
101+
79102 if err != nil {
80103 log .Printf ("[CRITAL]%s create VPN customer gateway failed, reason:%s\n " , logId , err .Error ())
81104 return err
82105 }
83106
84- if response .Response .CustomerGateway == nil {
107+ if response .Response .CustomerGateway . CustomerGatewayId == nil {
85108 return fmt .Errorf ("VPN customer gateway id is nil" )
86109 }
110+
87111 customerGatewayId := * response .Response .CustomerGateway .CustomerGatewayId
88112 d .SetId (customerGatewayId )
113+
89114 // must wait for finishing creating customer gateway
90115 statRequest := vpc .NewDescribeCustomerGatewaysRequest ()
91116 statRequest .CustomerGatewayIds = []* string {response .Response .CustomerGateway .CustomerGatewayId }
@@ -96,14 +121,20 @@ func resourceTencentCloudVpnCustomerGatewayCreate(d *schema.ResourceData, meta i
96121 logId , request .GetAction (), request .ToJsonString (), e .Error ())
97122 return tccommon .RetryError (e )
98123 } else {
124+ if result == nil || result .Response == nil || result .Response .CustomerGatewaySet == nil {
125+ return resource .NonRetryableError (fmt .Errorf ("response is nil" ))
126+ }
127+
99128 //if not, quit
100129 if len (result .Response .CustomerGatewaySet ) != 1 {
101130 return resource .NonRetryableError (fmt .Errorf ("creating error" ))
102131 }
132+
103133 //else consider created, cos there is no status of gateway
104134 return nil
105135 }
106136 })
137+
107138 if err != nil {
108139 log .Printf ("[CRITAL]%s create VPN customer gateway failed, reason:%s\n " , logId , err .Error ())
109140 return err
@@ -112,10 +143,8 @@ func resourceTencentCloudVpnCustomerGatewayCreate(d *schema.ResourceData, meta i
112143 //modify tags
113144 if tags := helper .GetTags (d , "tags" ); len (tags ) > 0 {
114145 tagService := svctag .NewTagService (meta .(tccommon.ProviderMeta ).GetAPIV3Conn ())
115-
116146 region := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().Region
117147 resourceName := tccommon .BuildTagResourceName ("vpc" , "cgw" , region , customerGatewayId )
118-
119148 if err := tagService .ModifyTags (ctx , resourceName , tags , nil ); err != nil {
120149 return err
121150 }
@@ -128,11 +157,13 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
128157 defer tccommon .LogElapsed ("resource.tencentcloud_vpn_customer_gateway.read" )()
129158 defer tccommon .InconsistentCheck (d , meta )()
130159
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+ )
133166
134- customerGatewayId := d .Id ()
135- request := vpc .NewDescribeCustomerGatewaysRequest ()
136167 request .CustomerGatewayIds = []* string {& customerGatewayId }
137168 var response * vpc.DescribeCustomerGatewaysResponse
138169 var iacExtInfo connectivity.IacExtInfo
@@ -154,23 +185,41 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
154185 return tccommon .RetryError (e )
155186 }
156187 }
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+
157193 response = result
158194 return nil
159195 })
196+
160197 if err != nil {
161198 log .Printf ("[CRITAL]%s read VPN customer gateway failed, reason:%s\n " , logId , err .Error ())
162199 return err
163200 }
164- if response == nil || response .Response == nil || len (response .Response .CustomerGatewaySet ) < 1 {
201+
202+ if len (response .Response .CustomerGatewaySet ) < 1 {
165203 d .SetId ("" )
166204 return nil
167205 }
168206
169207 gateway := response .Response .CustomerGatewaySet [0 ]
208+ if gateway .CustomerGatewayName != nil {
209+ _ = d .Set ("name" , gateway .CustomerGatewayName )
210+ }
170211
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+ }
174223
175224 //tags
176225 tagService := svctag .NewTagService (meta .(tccommon.ProviderMeta ).GetAPIV3Conn ())
@@ -179,6 +228,7 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
179228 if err != nil {
180229 return err
181230 }
231+
182232 _ = d .Set ("tags" , tags )
183233
184234 return nil
@@ -187,24 +237,35 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
187237func resourceTencentCloudVpnCustomerGatewayUpdate (d * schema.ResourceData , meta interface {}) error {
188238 defer tccommon .LogElapsed ("resource.tencentcloud_vpn_customer_gateway.update" )()
189239
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+ )
192245
193246 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+
199258 err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
200259 _ , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseVpcClient ().ModifyCustomerGatewayAttribute (request )
201260 if e != nil {
202261 log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " ,
203262 logId , request .GetAction (), request .ToJsonString (), e .Error ())
204263 return tccommon .RetryError (e )
205264 }
265+
206266 return nil
207267 })
268+
208269 if err != nil {
209270 log .Printf ("[CRITAL]%s modify VPN customer gateway failed, reason:%s\n " , logId , err .Error ())
210271 return err
@@ -223,14 +284,14 @@ func resourceTencentCloudVpnCustomerGatewayUpdate(d *schema.ResourceData, meta i
223284 return err
224285 }
225286 }
287+
226288 d .Partial (false )
227289
228290 return resourceTencentCloudVpnCustomerGatewayRead (d , meta )
229291}
230292
231293func resourceTencentCloudVpnCustomerGatewayDelete (d * schema.ResourceData , meta interface {}) error {
232294 defer tccommon .LogElapsed ("resource.tencentcloud_vpn_customer_gateway.delete" )()
233-
234295 logId := tccommon .GetLogId (tccommon .ContextNil )
235296
236297 customerGatewayId := d .Id ()
@@ -259,6 +320,10 @@ func resourceTencentCloudVpnCustomerGatewayDelete(d *schema.ResourceData, meta i
259320 logId , tRequest .GetAction (), tRequest .ToJsonString (), e .Error ())
260321 return tccommon .RetryError (e )
261322 } 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+
262327 if len (result .Response .VpnConnectionSet ) == 0 {
263328 return nil
264329 } else {
@@ -306,6 +371,10 @@ func resourceTencentCloudVpnCustomerGatewayDelete(d *schema.ResourceData, meta i
306371 return tccommon .RetryError (e )
307372 }
308373 } 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+
309378 //if not, quit
310379 if len (result .Response .CustomerGatewaySet ) == 0 {
311380 return nil
0 commit comments