Skip to content

Commit c3d2e4b

Browse files
committed
add
1 parent 4b457d4 commit c3d2e4b

File tree

3 files changed

+96
-35
lines changed

3 files changed

+96
-35
lines changed

tencentcloud/services/vpn/resource_tc_vpn_customer_gateway.go

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ 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+
Computed: true,
54+
Description: "BGP ASN. Value range: 1 - 4294967295. Using BGP requires configuring ASN.",
55+
},
5056
"create_time": {
5157
Type: schema.TypeString,
5258
Computed: true,
@@ -59,33 +65,53 @@ func ResourceTencentCloudVpnCustomerGateway() *schema.Resource {
5965
func resourceTencentCloudVpnCustomerGatewayCreate(d *schema.ResourceData, meta interface{}) error {
6066
defer tccommon.LogElapsed("resource.tencentcloud_vpn_customer_gateway.create")()
6167

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+
}
6486

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
6987
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
7088
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().CreateCustomerGateway(request)
7189
if e != nil {
7290
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
7391
logId, request.GetAction(), request.ToJsonString(), e.Error())
7492
return tccommon.RetryError(e)
7593
}
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+
7699
response = result
77100
return nil
78101
})
102+
79103
if err != nil {
80104
log.Printf("[CRITAL]%s create VPN customer gateway failed, reason:%s\n", logId, err.Error())
81105
return err
82106
}
83107

84-
if response.Response.CustomerGateway == nil {
108+
if response.Response.CustomerGateway.CustomerGatewayId == nil {
85109
return fmt.Errorf("VPN customer gateway id is nil")
86110
}
111+
87112
customerGatewayId := *response.Response.CustomerGateway.CustomerGatewayId
88113
d.SetId(customerGatewayId)
114+
89115
// must wait for finishing creating customer gateway
90116
statRequest := vpc.NewDescribeCustomerGatewaysRequest()
91117
statRequest.CustomerGatewayIds = []*string{response.Response.CustomerGateway.CustomerGatewayId}
@@ -96,14 +122,20 @@ func resourceTencentCloudVpnCustomerGatewayCreate(d *schema.ResourceData, meta i
96122
logId, request.GetAction(), request.ToJsonString(), e.Error())
97123
return tccommon.RetryError(e)
98124
} else {
125+
if result == nil || result.Response == nil || result.Response.CustomerGatewaySet == nil {
126+
return resource.NonRetryableError(fmt.Errorf("response is nil"))
127+
}
128+
99129
//if not, quit
100130
if len(result.Response.CustomerGatewaySet) != 1 {
101131
return resource.NonRetryableError(fmt.Errorf("creating error"))
102132
}
133+
103134
//else consider created, cos there is no status of gateway
104135
return nil
105136
}
106137
})
138+
107139
if err != nil {
108140
log.Printf("[CRITAL]%s create VPN customer gateway failed, reason:%s\n", logId, err.Error())
109141
return err
@@ -112,10 +144,8 @@ func resourceTencentCloudVpnCustomerGatewayCreate(d *schema.ResourceData, meta i
112144
//modify tags
113145
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
114146
tagService := svctag.NewTagService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
115-
116147
region := meta.(tccommon.ProviderMeta).GetAPIV3Conn().Region
117148
resourceName := tccommon.BuildTagResourceName("vpc", "cgw", region, customerGatewayId)
118-
119149
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
120150
return err
121151
}
@@ -128,11 +158,13 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
128158
defer tccommon.LogElapsed("resource.tencentcloud_vpn_customer_gateway.read")()
129159
defer tccommon.InconsistentCheck(d, meta)()
130160

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+
)
133167

134-
customerGatewayId := d.Id()
135-
request := vpc.NewDescribeCustomerGatewaysRequest()
136168
request.CustomerGatewayIds = []*string{&customerGatewayId}
137169
var response *vpc.DescribeCustomerGatewaysResponse
138170
var iacExtInfo connectivity.IacExtInfo
@@ -154,23 +186,41 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
154186
return tccommon.RetryError(e)
155187
}
156188
}
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+
157194
response = result
158195
return nil
159196
})
197+
160198
if err != nil {
161199
log.Printf("[CRITAL]%s read VPN customer gateway failed, reason:%s\n", logId, err.Error())
162200
return err
163201
}
164-
if response == nil || response.Response == nil || len(response.Response.CustomerGatewaySet) < 1 {
202+
203+
if len(response.Response.CustomerGatewaySet) < 1 {
165204
d.SetId("")
166205
return nil
167206
}
168207

169208
gateway := response.Response.CustomerGatewaySet[0]
209+
if gateway.CustomerGatewayName != nil {
210+
_ = d.Set("name", gateway.CustomerGatewayName)
211+
}
170212

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+
}
174224

175225
//tags
176226
tagService := svctag.NewTagService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
@@ -179,6 +229,7 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
179229
if err != nil {
180230
return err
181231
}
232+
182233
_ = d.Set("tags", tags)
183234

184235
return nil
@@ -187,24 +238,35 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
187238
func resourceTencentCloudVpnCustomerGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
188239
defer tccommon.LogElapsed("resource.tencentcloud_vpn_customer_gateway.update")()
189240

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+
)
192246

193247
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+
199259
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
200260
_, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().ModifyCustomerGatewayAttribute(request)
201261
if e != nil {
202262
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
203263
logId, request.GetAction(), request.ToJsonString(), e.Error())
204264
return tccommon.RetryError(e)
205265
}
266+
206267
return nil
207268
})
269+
208270
if err != nil {
209271
log.Printf("[CRITAL]%s modify VPN customer gateway failed, reason:%s\n", logId, err.Error())
210272
return err
@@ -223,14 +285,14 @@ func resourceTencentCloudVpnCustomerGatewayUpdate(d *schema.ResourceData, meta i
223285
return err
224286
}
225287
}
288+
226289
d.Partial(false)
227290

228291
return resourceTencentCloudVpnCustomerGatewayRead(d, meta)
229292
}
230293

231294
func resourceTencentCloudVpnCustomerGatewayDelete(d *schema.ResourceData, meta interface{}) error {
232295
defer tccommon.LogElapsed("resource.tencentcloud_vpn_customer_gateway.delete")()
233-
234296
logId := tccommon.GetLogId(tccommon.ContextNil)
235297

236298
customerGatewayId := d.Id()

tencentcloud/services/vpn/resource_tc_vpn_customer_gateway.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ Provides a resource to create a VPN customer gateway.
33
Example Usage
44

55
```hcl
6-
resource "tencentcloud_vpn_customer_gateway" "foo" {
7-
name = "test_vpn_customer_gateway"
6+
resource "tencentcloud_vpn_customer_gateway" "example" {
7+
name = "tf-example"
88
public_ip_address = "1.1.1.1"
9-
109
tags = {
11-
tag = "test"
10+
createBy = "Terraform"
1211
}
1312
}
1413
```
@@ -18,5 +17,5 @@ Import
1817
VPN customer gateway can be imported using the id, e.g.
1918

2019
```
21-
$ terraform import tencentcloud_vpn_customer_gateway.foo cgw-xfqag
20+
$ terraform import tencentcloud_vpn_customer_gateway.example cgw-xfqag
2221
```

website/docs/r/vpn_customer_gateway.html.markdown

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ Provides a resource to create a VPN customer gateway.
1414
## Example Usage
1515

1616
```hcl
17-
resource "tencentcloud_vpn_customer_gateway" "foo" {
18-
name = "test_vpn_customer_gateway"
17+
resource "tencentcloud_vpn_customer_gateway" "example" {
18+
name = "tf-example"
1919
public_ip_address = "1.1.1.1"
20-
2120
tags = {
22-
tag = "test"
21+
createBy = "Terraform"
2322
}
2423
}
2524
```
@@ -30,6 +29,7 @@ The following arguments are supported:
3029

3130
* `name` - (Required, String) Name of the customer gateway. The length of character is limited to 1-60.
3231
* `public_ip_address` - (Required, String, ForceNew) Public IP of the customer gateway.
32+
* `bgp_asn` - (Optional, Int) BGP ASN. Value range: 1 - 4294967295. Using BGP requires configuring ASN.
3333
* `tags` - (Optional, Map) A list of tags used to associate different resources.
3434

3535
## Attributes Reference
@@ -45,6 +45,6 @@ In addition to all arguments above, the following attributes are exported:
4545
VPN customer gateway can be imported using the id, e.g.
4646

4747
```
48-
$ terraform import tencentcloud_vpn_customer_gateway.foo cgw-xfqag
48+
$ terraform import tencentcloud_vpn_customer_gateway.example cgw-xfqag
4949
```
5050

0 commit comments

Comments
 (0)