Skip to content

Commit b7f0c71

Browse files
authored
Merge pull request #500 from brickzzhang/master
ccn forcenew issue when repeatly apply
2 parents 3e6afb4 + 232eb31 commit b7f0c71

File tree

6 files changed

+110
-64
lines changed

6 files changed

+110
-64
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
## 1.41.1 (Unreleased)
2+
3+
BUG FIXES:
4+
5+
* Resource: `tencentcloud_vpn_gateway` fix force new issue when apply repeatedly.
6+
* Resource: `tencentcloud_vpn_connection` fix force new issue when apply repeatedly.
7+
28
## 1.41.0 (August 17, 2020)
39

410
FEATURES:

tencentcloud/resource_tc_vpn_connection.go

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,22 @@ func resourceTencentCloudVpnConnection() *schema.Resource {
7676
Description: "Name of the VPN connection. The length of character is limited to 1-60.",
7777
},
7878
"vpc_id": {
79-
Type: schema.TypeString,
80-
Optional: true,
81-
ForceNew: true,
79+
Type: schema.TypeString,
80+
Optional: true,
81+
ForceNew: true,
82+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
83+
if v, ok := d.GetOk("is_ccn_type"); ok && v.(bool) {
84+
return true
85+
}
86+
return old == new
87+
},
8288
Description: "ID of the VPC. Required if vpn gateway is not in `CCN` type, and doesn't make sense for `CCN` vpn gateway.",
8389
},
90+
"is_ccn_type": {
91+
Type: schema.TypeBool,
92+
Computed: true,
93+
Description: "Indicate whether is ccn type. Modification of this field only impacts force new logic of `vpc_id`. If `is_ccn_type` is true, modification of `vpc_id` will be ignored.",
94+
},
8495
"customer_gateway_id": {
8596
Type: schema.TypeString,
8697
Required: true,
@@ -274,38 +285,21 @@ func resourceTencentCloudVpnConnection() *schema.Resource {
274285
func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interface{}) error {
275286
defer logElapsed("resource.tencentcloud_vpn_connection.create")()
276287

277-
logId := getLogId(contextNil)
278-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
288+
var (
289+
logId = getLogId(contextNil)
290+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
291+
service = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
292+
)
279293

280294
// pre check vpn gateway id
281-
requestVpngw := vpc.NewDescribeVpnGatewaysRequest()
282-
requestVpngw.VpnGatewayIds = []*string{helper.String(d.Get("vpn_gateway_id").(string))}
283-
var responseVpngw *vpc.DescribeVpnGatewaysResponse
284-
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
285-
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().DescribeVpnGateways(requestVpngw)
286-
if e != nil {
287-
ee, ok := e.(*errors.TencentCloudSDKError)
288-
if !ok {
289-
return retryError(e)
290-
}
291-
if ee.Code == VPCNotFound {
292-
return nil
293-
} else {
294-
return retryError(e)
295-
}
296-
}
297-
responseVpngw = result
298-
return nil
299-
})
295+
has, gateway, err := service.DescribeVpngwById(ctx, d.Get("vpn_gateway_id").(string))
300296
if err != nil {
301297
return err
302298
}
303-
if len(responseVpngw.Response.VpnGatewaySet) < 1 {
299+
if !has {
304300
return fmt.Errorf("[CRITAL] vpn_gateway_id %s doesn't exist", d.Get("vpn_gateway_id").(string))
305301
}
306302

307-
gateway := responseVpngw.Response.VpnGatewaySet[0]
308-
309303
// create vpn connection
310304
request := vpc.NewCreateVpnConnectionRequest()
311305
request.VpnConnectionName = helper.String(d.Get("name").(string))
@@ -315,6 +309,9 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
315309
}
316310
request.VpcId = helper.String(d.Get("vpc_id").(string))
317311
} else {
312+
if _, ok := d.GetOk("vpc_id"); ok {
313+
return fmt.Errorf("[CRITAL] vpc_id doesn't make sense when vpn gateway is in CCN type")
314+
}
318315
request.VpcId = helper.String("")
319316
}
320317
request.VpnGatewayId = helper.String(d.Get("vpn_gateway_id").(string))
@@ -516,8 +513,11 @@ func resourceTencentCloudVpnConnectionRead(d *schema.ResourceData, meta interfac
516513
defer logElapsed("resource.tencentcloud_vpn_connection.read")()
517514
defer inconsistentCheck(d, meta)()
518515

519-
logId := getLogId(contextNil)
520-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
516+
var (
517+
logId = getLogId(contextNil)
518+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
519+
service = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
520+
)
521521

522522
connectionId := d.Id()
523523
request := vpc.NewDescribeVpnConnectionsRequest()
@@ -545,9 +545,24 @@ func resourceTencentCloudVpnConnectionRead(d *schema.ResourceData, meta interfac
545545

546546
connection := response.Response.VpnConnectionSet[0]
547547
_ = d.Set("name", *connection.VpnConnectionName)
548-
_ = d.Set("vpc_id", *connection.VpcId)
549548
_ = d.Set("create_time", *connection.CreatedTime)
550549
_ = d.Set("vpn_gateway_id", *connection.VpnGatewayId)
550+
551+
// get vpngw type
552+
has, gateway, err := service.DescribeVpngwById(ctx, d.Get("vpn_gateway_id").(string))
553+
if err != nil {
554+
log.Printf("[CRITAL]%s read vpn_geteway failed, reason:%s\n", logId, err.Error())
555+
return err
556+
}
557+
if !has {
558+
return fmt.Errorf("[CRITAL] vpn_gateway_id %s doesn't exist", d.Get("vpn_gateway_id").(string))
559+
}
560+
561+
if *gateway.Type != "CCN" {
562+
_ = d.Set("vpc_id", *connection.VpcId)
563+
}
564+
565+
_ = d.Set("is_ccn_type", *gateway.Type == "CCN")
551566
_ = d.Set("customer_gateway_id", *connection.CustomerGatewayId)
552567
_ = d.Set("pre_share_key", *connection.PreShareKey)
553568
//set up SPD

tencentcloud/resource_tc_vpn_gateway.go

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ func resourceTencentCloudVpnGateway() *schema.Resource {
7676
Description: "Name of the VPN gateway. The length of character is limited to 1-60.",
7777
},
7878
"vpc_id": {
79-
Type: schema.TypeString,
80-
Optional: true,
81-
ForceNew: true,
82-
Description: "ID of the VPC. Required if vpn gateway is not in `CCN` type, and doesn't make sense if vpn gateway is in `CCN` type.",
79+
Type: schema.TypeString,
80+
Optional: true,
81+
ForceNew: true,
82+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
83+
if v, ok := d.GetOk("type"); ok && v.(string) == "CCN" {
84+
return true
85+
}
86+
return old == new
87+
},
88+
Description: "ID of the VPC. Required if vpn gateway is not in `CCN` type, and doesn't make sense for `CCN` vpn gateway.",
8389
},
8490
"bandwidth": {
8591
Type: schema.TypeInt,
@@ -191,6 +197,9 @@ func resourceTencentCloudVpnGatewayCreate(d *schema.ResourceData, meta interface
191197
}
192198
request.VpcId = helper.String(d.Get("vpc_id").(string))
193199
} else {
200+
if _, ok := d.GetOk("vpc_id"); ok {
201+
return fmt.Errorf("[CRITAL] vpc_id doesn't make sense when vpn gateway is in CCN type")
202+
}
194203
request.VpcId = helper.String("")
195204
}
196205
} else {
@@ -267,44 +276,23 @@ func resourceTencentCloudVpnGatewayRead(d *schema.ResourceData, meta interface{}
267276
defer logElapsed("resource.tencentcloud_vpn_gateway.read")()
268277
defer inconsistentCheck(d, meta)()
269278

270-
logId := getLogId(contextNil)
271-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
279+
var (
280+
logId = getLogId(contextNil)
281+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
282+
service = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
283+
gatewayId = d.Id()
284+
)
272285

273-
gatewayId := d.Id()
274-
request := vpc.NewDescribeVpnGatewaysRequest()
275-
request.VpnGatewayIds = []*string{&gatewayId}
276-
var response *vpc.DescribeVpnGatewaysResponse
277-
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
278-
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().DescribeVpnGateways(request)
279-
if e != nil {
280-
ee, ok := e.(*errors.TencentCloudSDKError)
281-
if !ok {
282-
return retryError(e)
283-
}
284-
if ee.Code == VPCNotFound {
285-
log.Printf("[CRITAL]%s api[%s] success, request body [%s], reason[%s]\n",
286-
logId, request.GetAction(), request.ToJsonString(), e.Error())
287-
return nil
288-
} else {
289-
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
290-
logId, request.GetAction(), request.ToJsonString(), e.Error())
291-
return retryError(e)
292-
}
293-
}
294-
response = result
295-
return nil
296-
})
286+
has, gateway, err := service.DescribeVpngwById(ctx, gatewayId)
297287
if err != nil {
298288
log.Printf("[CRITAL]%s read VPN gateway failed, reason:%s\n", logId, err.Error())
299289
return err
300290
}
301-
if len(response.Response.VpnGatewaySet) < 1 {
291+
if !has {
302292
d.SetId("")
303293
return nil
304294
}
305295

306-
gateway := response.Response.VpnGatewaySet[0]
307-
308296
_ = d.Set("name", gateway.VpnGatewayName)
309297
_ = d.Set("public_ip_address", gateway.PublicIpAddress)
310298
_ = d.Set("bandwidth", int(*gateway.InternetMaxBandwidthOut))

tencentcloud/service_tencentcloud_vpc.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,3 +3221,39 @@ func (me *VpcService) DeleteAclAttachment(ctx context.Context, attachmentAcl str
32213221
}
32223222
return
32233223
}
3224+
3225+
func (me *VpcService) DescribeVpngwById(ctx context.Context, vpngwId string) (has bool, gateway *vpc.VpnGateway, err error) {
3226+
var (
3227+
logId = getLogId(ctx)
3228+
request = vpc.NewDescribeVpnGatewaysRequest()
3229+
response *vpc.DescribeVpnGatewaysResponse
3230+
)
3231+
request.VpnGatewayIds = []*string{&vpngwId}
3232+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
3233+
response, err = me.client.UseVpcClient().DescribeVpnGateways(request)
3234+
if err != nil {
3235+
ee, ok := err.(*sdkErrors.TencentCloudSDKError)
3236+
if !ok {
3237+
return retryError(err)
3238+
}
3239+
if ee.Code == VPCNotFound {
3240+
return nil
3241+
} else {
3242+
return retryError(err)
3243+
}
3244+
}
3245+
return nil
3246+
})
3247+
if err != nil {
3248+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%v]", logId, request.GetAction(), request.ToJsonString(), err)
3249+
return
3250+
}
3251+
if len(response.Response.VpnGatewaySet) < 1 {
3252+
has = false
3253+
return
3254+
}
3255+
3256+
gateway = response.Response.VpnGatewaySet[0]
3257+
has = true
3258+
return
3259+
}

website/docs/r/vpn_connection.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ In addition to all arguments above, the following attributes are exported:
8585
* `id` - ID of the resource.
8686
* `create_time` - Create time of the VPN connection.
8787
* `encrypt_proto` - Encrypt proto of the VPN connection.
88+
* `is_ccn_type` - Indicate whether is ccn type. Modification of this field only impacts force new logic of `vpc_id`. If `is_ccn_type` is true, modification of `vpc_id` will be ignored.
8889
* `net_status` - Net status of the VPN connection, values are `AVAILABLE`.
8990
* `route_type` - Route type of the VPN connection.
9091
* `state` - State of the connection, values are `PENDING`, `AVAILABLE`, `DELETING`.

website/docs/r/vpn_gateway.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The following arguments are supported:
5858
* `prepaid_renew_flag` - (Optional) Flag indicates whether to renew or not, valid values are `NOTIFY_AND_RENEW`, `NOTIFY_AND_AUTO_RENEW`, `NOT_NOTIFY_AND_NOT_RENEW`. This para can only be set to take effect in create operation.
5959
* `tags` - (Optional) A list of tags used to associate different resources.
6060
* `type` - (Optional) Type of gateway instance, valid values are `IPSEC`, `SSL` and `CCN`. Note: CCN type is only for whitelist customer now.
61-
* `vpc_id` - (Optional, ForceNew) ID of the VPC. Required if vpn gateway is not in `CCN` type, and doesn't make sense if vpn gateway is in `CCN` type.
61+
* `vpc_id` - (Optional, ForceNew) ID of the VPC. Required if vpn gateway is not in `CCN` type, and doesn't make sense for `CCN` vpn gateway.
6262

6363
## Attributes Reference
6464

0 commit comments

Comments
 (0)