Skip to content

Commit d19e16f

Browse files
authored
Merge pull request #499 from piggona/master
feature: update internet_max_bandwidth_out with no forceNew operation
2 parents b7f0c71 + 751fce2 commit d19e16f

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ BUG FIXES:
44

55
* Resource: `tencentcloud_vpn_gateway` fix force new issue when apply repeatedly.
66
* Resource: `tencentcloud_vpn_connection` fix force new issue when apply repeatedly.
7+
* Resource: `tencentcloud_instance` support for adjusting `internet_max_bandwidth_out` without forceNew when attribute `internet_charge_type` within `TRAFFIC_POSTPAID_BY_HOUR`,`BANDWIDTH_POSTPAID_BY_HOUR`,`BANDWIDTH_PACKAGE` ([#498](https://github.com/tencentcloudstack/terraform-provider-tencentcloud/issues/498)).
78

89
## 1.41.0 (August 17, 2020)
910

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/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()

website/docs/r/instance.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ The following arguments are supported:
9292
* `instance_name` - (Optional) The name of the CVM. The max length of instance_name is 60, and default value is `Terraform-CVM-Instance`.
9393
* `instance_type` - (Optional) The type of instance to start.
9494
* `internet_charge_type` - (Optional, ForceNew) Internet charge type of the instance, Valid values are `BANDWIDTH_PREPAID`, `TRAFFIC_POSTPAID_BY_HOUR`, `BANDWIDTH_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`. This value does not need to be set when `allocate_public_ip` is false.
95-
* `internet_max_bandwidth_out` - (Optional, ForceNew) 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.
95+
* `internet_max_bandwidth_out` - (Optional) 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.
9696
* `key_name` - (Optional) The key pair to use for the instance, it looks like skey-16jig7tx.
9797
* `password` - (Optional) Password to an instance. In order to take effect new password, the instance will be restarted after modifying the password.
9898
* `placement_group_id` - (Optional, ForceNew) The id of a placement group.

0 commit comments

Comments
 (0)