Skip to content

Commit b0070b3

Browse files
committed
[compatibility]
1. compatibility
1 parent 9e3c958 commit b0070b3

File tree

5 files changed

+100
-65
lines changed

5 files changed

+100
-65
lines changed

tencentcloud/resource_tc_vpn_connection.go

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,21 @@ 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,
82-
Description: "ID of the VPC. Required if vpn gateway is not in `CCN` type, and doesn't make sense for `CCN` vpn gateway.",
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+
},
88+
Description: "ID of the VPC. Required if vpn gateway is not in `CCN` type, and not allowed to be used for `CCN` vpn gateway.",
89+
},
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 login of `vpc_id`. If `is_ccn_type` is true, modification of `vpc_id` will be ignored.",
8394
},
8495
"customer_gateway_id": {
8596
Type: schema.TypeString,
@@ -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))
@@ -519,8 +513,11 @@ func resourceTencentCloudVpnConnectionRead(d *schema.ResourceData, meta interfac
519513
defer logElapsed("resource.tencentcloud_vpn_connection.read")()
520514
defer inconsistentCheck(d, meta)()
521515

522-
logId := getLogId(contextNil)
523-
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+
)
524521

525522
connectionId := d.Id()
526523
request := vpc.NewDescribeVpnConnectionsRequest()
@@ -550,6 +547,22 @@ func resourceTencentCloudVpnConnectionRead(d *schema.ResourceData, meta interfac
550547
_ = d.Set("name", *connection.VpnConnectionName)
551548
_ = d.Set("create_time", *connection.CreatedTime)
552549
_ = 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")
553566
_ = d.Set("customer_gateway_id", *connection.CustomerGatewayId)
554567
_ = d.Set("pre_share_key", *connection.PreShareKey)
555568
//set up SPD

tencentcloud/resource_tc_vpn_gateway.go

Lines changed: 18 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 not allowed to be used if vpn gateway is in `CCN` type.",
8389
},
8490
"bandwidth": {
8591
Type: schema.TypeInt,
@@ -270,44 +276,23 @@ func resourceTencentCloudVpnGatewayRead(d *schema.ResourceData, meta interface{}
270276
defer logElapsed("resource.tencentcloud_vpn_gateway.read")()
271277
defer inconsistentCheck(d, meta)()
272278

273-
logId := getLogId(contextNil)
274-
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+
)
275285

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

309-
gateway := response.Response.VpnGatewaySet[0]
310-
311296
_ = d.Set("name", gateway.VpnGatewayName)
312297
_ = d.Set("public_ip_address", gateway.PublicIpAddress)
313298
_ = 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ The following arguments are supported:
7171
* `ipsec_sa_lifetime_seconds` - (Optional) SA lifetime of the IPSEC operation specification, unit is `second`. The value ranges from 180 to 604800. Default value is 3600 seconds.
7272
* `ipsec_sa_lifetime_traffic` - (Optional) SA lifetime of the IPSEC operation specification, unit is `KB`. The value should not be less then 2560. Default value is 1843200.
7373
* `tags` - (Optional) A list of tags used to associate different resources.
74-
* `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.
74+
* `vpc_id` - (Optional, ForceNew) ID of the VPC. Required if vpn gateway is not in `CCN` type, and not allowed to be used for `CCN` vpn gateway.
7575

7676
The `security_group_policy` object supports the following:
7777

@@ -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 login 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 not allowed to be used if vpn gateway is in `CCN` type.
6262

6363
## Attributes Reference
6464

0 commit comments

Comments
 (0)