Skip to content

Commit 17a5c44

Browse files
Merge branch 'master' into ckafka_topic
2 parents 49cbc6b + 3086be8 commit 17a5c44

10 files changed

+169
-77
lines changed

tencentcloud/resource_tc_instance.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ func resourceTencentCloudInstance() *schema.Resource {
200200
Type: schema.TypeInt,
201201
Optional: true,
202202
Computed: true,
203-
ForceNew: true,
204203
Description: "Maximum outgoing bandwidth to the public network, measured in Mbps (Mega bit per second). This value does not need to be set when `allocate_public_ip` is false.",
205204
},
206205
"allocate_public_ip": {
@@ -943,6 +942,34 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
943942
d.SetPartial("tags")
944943
}
945944

945+
if d.HasChange("internet_max_bandwidth_out") {
946+
chargeType := d.Get("internet_charge_type").(string)
947+
if chargeType != "TRAFFIC_POSTPAID_BY_HOUR" && chargeType != "BANDWIDTH_POSTPAID_BY_HOUR" && chargeType != "BANDWIDTH_PACKAGE" {
948+
return fmt.Errorf("charge type should be one of `TRAFFIC_POSTPAID_BY_HOUR BANDWIDTH_POSTPAID_BY_HOUR BANDWIDTH_PACKAGE` when adjusting internet_max_bandwidth_out")
949+
}
950+
951+
err := cvmService.ModifyInternetMaxBandwidthOut(ctx, instanceId, chargeType, int64(d.Get("internet_max_bandwidth_out").(int)))
952+
if err != nil {
953+
return err
954+
}
955+
d.SetPartial("internet_max_bandwidth_out")
956+
time.Sleep(1 * time.Second)
957+
err = resource.Retry(2*readRetryTimeout, func() *resource.RetryError {
958+
instance, errRet := cvmService.DescribeInstanceById(ctx, instanceId)
959+
if errRet != nil {
960+
return retryError(errRet, InternalError)
961+
}
962+
if instance != nil && *instance.LatestOperationState == CVM_LATEST_OPERATION_STATE_OPERATING {
963+
return resource.RetryableError(fmt.Errorf("cvm instance latest operetion status is %s, retry...", *instance.LatestOperationState))
964+
}
965+
return nil
966+
})
967+
if err != nil {
968+
return err
969+
}
970+
971+
}
972+
946973
d.Partial(false)
947974

948975
return resourceTencentCloudInstanceRead(d, meta)

tencentcloud/resource_tc_instance_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestAccTencentCloudInstanceWithNetwork(t *testing.T) {
131131
CheckDestroy: testAccCheckInstanceDestroy,
132132
Steps: []resource.TestStep{
133133
{
134-
Config: testAccTencentCloudInstanceWithNetwork("false"),
134+
Config: testAccTencentCloudInstanceWithNetwork("false", 1),
135135
Check: resource.ComposeTestCheckFunc(
136136
testAccCheckTencentCloudDataSourceID(id),
137137
testAccCheckTencentCloudInstanceExists(id),
@@ -140,10 +140,11 @@ func TestAccTencentCloudInstanceWithNetwork(t *testing.T) {
140140
),
141141
},
142142
{
143-
Config: testAccTencentCloudInstanceWithNetwork("true"),
143+
Config: testAccTencentCloudInstanceWithNetwork("true", 5),
144144
Check: resource.ComposeTestCheckFunc(
145145
testAccCheckTencentCloudDataSourceID(id),
146146
testAccCheckTencentCloudInstanceExists(id),
147+
resource.TestCheckResourceAttr(id, "internet_max_bandwidth_out", "5"),
147148
resource.TestCheckResourceAttr(id, "instance_status", "RUNNING"),
148149
resource.TestCheckResourceAttrSet(id, "public_ip"),
149150
),
@@ -579,7 +580,7 @@ resource "tencentcloud_instance" "foo" {
579580
}
580581
`
581582

582-
func testAccTencentCloudInstanceWithNetwork(hasPublicIp string) string {
583+
func testAccTencentCloudInstanceWithNetwork(hasPublicIp string, maxBandWidthOut int64) string {
583584
return fmt.Sprintf(
584585
defaultInstanceVariable+`
585586
resource "tencentcloud_instance" "foo" {
@@ -588,12 +589,12 @@ resource "tencentcloud_instance" "foo" {
588589
image_id = data.tencentcloud_images.default.images.0.image_id
589590
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
590591
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
591-
internet_max_bandwidth_out = 1
592+
internet_max_bandwidth_out = %d
592593
allocate_public_ip = %s
593594
system_disk_type = "CLOUD_PREMIUM"
594595
}
595596
`,
596-
hasPublicIp,
597+
maxBandWidthOut, hasPublicIp,
597598
)
598599
}
599600

tencentcloud/resource_tc_vpn_connection.go

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import (
5454
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
5555
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5656
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
57+
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
5758
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
5859
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
5960
)
@@ -76,11 +77,22 @@ func resourceTencentCloudVpnConnection() *schema.Resource {
7677
Description: "Name of the VPN connection. The length of character is limited to 1-60.",
7778
},
7879
"vpc_id": {
79-
Type: schema.TypeString,
80-
Optional: true,
81-
ForceNew: true,
80+
Type: schema.TypeString,
81+
Optional: true,
82+
ForceNew: true,
83+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
84+
if v, ok := d.GetOk("is_ccn_type"); ok && v.(bool) {
85+
return true
86+
}
87+
return old == new
88+
},
8289
Description: "ID of the VPC. Required if vpn gateway is not in `CCN` type, and doesn't make sense for `CCN` vpn gateway.",
8390
},
91+
"is_ccn_type": {
92+
Type: schema.TypeBool,
93+
Computed: true,
94+
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.",
95+
},
8496
"customer_gateway_id": {
8597
Type: schema.TypeString,
8698
Required: true,
@@ -99,7 +111,7 @@ func resourceTencentCloudVpnConnection() *schema.Resource {
99111
Description: "Pre-shared key of the VPN connection.",
100112
},
101113
"security_group_policy": {
102-
Type: schema.TypeList,
114+
Type: schema.TypeSet,
103115
Required: true,
104116
Description: "Security group policy of the VPN connection.",
105117
Elem: &schema.Resource{
@@ -274,38 +286,21 @@ func resourceTencentCloudVpnConnection() *schema.Resource {
274286
func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interface{}) error {
275287
defer logElapsed("resource.tencentcloud_vpn_connection.create")()
276288

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

280295
// 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-
})
296+
has, gateway, err := service.DescribeVpngwById(ctx, d.Get("vpn_gateway_id").(string))
300297
if err != nil {
301298
return err
302299
}
303-
if len(responseVpngw.Response.VpnGatewaySet) < 1 {
300+
if !has {
304301
return fmt.Errorf("[CRITAL] vpn_gateway_id %s doesn't exist", d.Get("vpn_gateway_id").(string))
305302
}
306303

307-
gateway := responseVpngw.Response.VpnGatewaySet[0]
308-
309304
// create vpn connection
310305
request := vpc.NewCreateVpnConnectionRequest()
311306
request.VpnConnectionName = helper.String(d.Get("name").(string))
@@ -315,6 +310,9 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
315310
}
316311
request.VpcId = helper.String(d.Get("vpc_id").(string))
317312
} else {
313+
if _, ok := d.GetOk("vpc_id"); ok {
314+
return fmt.Errorf("[CRITAL] vpc_id doesn't make sense when vpn gateway is in CCN type")
315+
}
318316
request.VpcId = helper.String("")
319317
}
320318
request.VpnGatewayId = helper.String(d.Get("vpn_gateway_id").(string))
@@ -323,7 +321,7 @@ func resourceTencentCloudVpnConnectionCreate(d *schema.ResourceData, meta interf
323321

324322
//set up SecurityPolicyDatabases
325323

326-
sgps := d.Get("security_group_policy").([]interface{})
324+
sgps := d.Get("security_group_policy").(*schema.Set).List()
327325
if len(sgps) < 1 {
328326
return fmt.Errorf("Para `security_group_policy` should be set at least one.")
329327
}
@@ -516,8 +514,11 @@ func resourceTencentCloudVpnConnectionRead(d *schema.ResourceData, meta interfac
516514
defer logElapsed("resource.tencentcloud_vpn_connection.read")()
517515
defer inconsistentCheck(d, meta)()
518516

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

522523
connectionId := d.Id()
523524
request := vpc.NewDescribeVpnConnectionsRequest()
@@ -528,6 +529,10 @@ func resourceTencentCloudVpnConnectionRead(d *schema.ResourceData, meta interfac
528529
if e != nil {
529530
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
530531
logId, request.GetAction(), request.ToJsonString(), e.Error())
532+
ee, ok := e.(*sdkErrors.TencentCloudSDKError)
533+
if ok && ee.Code == VPCNotFound {
534+
return nil
535+
}
531536
return retryError(e)
532537
}
533538

@@ -538,16 +543,31 @@ func resourceTencentCloudVpnConnectionRead(d *schema.ResourceData, meta interfac
538543
log.Printf("[CRITAL]%s read VPN connection failed, reason:%s\n", logId, err.Error())
539544
return err
540545
}
541-
if len(response.Response.VpnConnectionSet) < 1 {
546+
if response == nil || response.Response == nil || len(response.Response.VpnConnectionSet) < 1 {
542547
d.SetId("")
543548
return nil
544549
}
545550

546551
connection := response.Response.VpnConnectionSet[0]
547552
_ = d.Set("name", *connection.VpnConnectionName)
548-
_ = d.Set("vpc_id", *connection.VpcId)
549553
_ = d.Set("create_time", *connection.CreatedTime)
550554
_ = d.Set("vpn_gateway_id", *connection.VpnGatewayId)
555+
556+
// get vpngw type
557+
has, gateway, err := service.DescribeVpngwById(ctx, d.Get("vpn_gateway_id").(string))
558+
if err != nil {
559+
log.Printf("[CRITAL]%s read vpn_geteway failed, reason:%s\n", logId, err.Error())
560+
return err
561+
}
562+
if !has {
563+
return fmt.Errorf("[CRITAL] vpn_gateway_id %s doesn't exist", d.Get("vpn_gateway_id").(string))
564+
}
565+
566+
if *gateway.Type != "CCN" {
567+
_ = d.Set("vpc_id", *connection.VpcId)
568+
}
569+
570+
_ = d.Set("is_ccn_type", *gateway.Type == "CCN")
551571
_ = d.Set("customer_gateway_id", *connection.CustomerGatewayId)
552572
_ = d.Set("pre_share_key", *connection.PreShareKey)
553573
//set up SPD
@@ -624,13 +644,13 @@ func resourceTencentCloudVpnConnectionUpdate(d *schema.ResourceData, meta interf
624644

625645
//set up SecurityPolicyDatabases
626646
if d.HasChange("security_group_policy") {
627-
sgps := d.Get("security_group_policy").([]interface{})
647+
sgps := d.Get("security_group_policy").(*schema.Set).List()
628648
if len(sgps) < 1 {
629649
return fmt.Errorf("Para `security_group_policy` should be set at least one.")
630650
}
651+
request.SecurityPolicyDatabases = make([]*vpc.SecurityPolicyDatabase, 0, len(sgps))
631652
for _, v := range sgps {
632653
m := v.(map[string]interface{})
633-
request.SecurityPolicyDatabases = make([]*vpc.SecurityPolicyDatabase, 0, len(sgps))
634654
var sgp vpc.SecurityPolicyDatabase
635655
local := m["local_cidr_block"].(string)
636656
sgp.LocalCidrBlock = &local

tencentcloud/resource_tc_vpn_customer_gateway.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func resourceTencentCloudVpnCustomerGatewayRead(d *schema.ResourceData, meta int
177177
log.Printf("[CRITAL]%s read VPN customer gateway failed, reason:%s\n", logId, err.Error())
178178
return err
179179
}
180-
if len(response.Response.CustomerGatewaySet) < 1 {
180+
if response == nil || response.Response == nil || len(response.Response.CustomerGatewaySet) < 1 {
181181
d.SetId("")
182182
return nil
183183
}

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_cvm.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,25 @@ func (me *CvmService) ModifyPassword(ctx context.Context, instanceId, password s
179179
return nil
180180
}
181181

182+
func (me *CvmService) ModifyInternetMaxBandwidthOut(ctx context.Context, instanceId, internetChargeType string, internetMaxBandWidthOut int64) error {
183+
logId := getLogId(ctx)
184+
request := cvm.NewResetInstancesInternetMaxBandwidthRequest()
185+
request.InstanceIds = []*string{&instanceId}
186+
request.InternetAccessible = &cvm.InternetAccessible{
187+
InternetChargeType: &internetChargeType,
188+
InternetMaxBandwidthOut: &internetMaxBandWidthOut,
189+
}
190+
191+
ratelimit.Check(request.GetAction())
192+
_, err := me.client.UseCvmClient().ResetInstancesInternetMaxBandwidth(request)
193+
if err != nil {
194+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
195+
logId, request.GetAction(), request.ToJsonString(), err.Error())
196+
return err
197+
}
198+
return nil
199+
}
200+
182201
func (me *CvmService) ModifyVpc(ctx context.Context, instanceId, vpcId, subnetId, privateIp string) error {
183202
logId := getLogId(ctx)
184203
request := cvm.NewModifyInstancesVpcAttributeRequest()

0 commit comments

Comments
 (0)