Skip to content

Commit e6208a5

Browse files
committed
feature: update internet_max_bandwidth_out with no forceNew operation
1 parent 3e6afb4 commit e6208a5

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

tencentcloud/resource_tc_instance.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func resourceTencentCloudInstance() *schema.Resource {
200200
Type: schema.TypeInt,
201201
Optional: true,
202202
Computed: true,
203-
ForceNew: true,
203+
ForceNew: false,
204204
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.",
205205
},
206206
"allocate_public_ip": {
@@ -943,6 +943,33 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
943943
d.SetPartial("tags")
944944
}
945945

946+
if d.HasChange("internet_max_bandwidth_out") {
947+
if chargeType := d.Get("internet_charge_type").(string); chargeType != "TRAFFIC_POSTPAID_BY_HOUR" && chargeType != "BANDWIDTH_POSTPAID_BY_HOUR" && chargeType != "BANDWIDTH_PACKAGE" {
948+
return fmt.Errorf("bad internet_charge_type.type should be in: TRAFFIC_POSTPAID_BY_HOUR BANDWIDTH_POSTPAID_BY_HOUR BANDWIDTH_PACKAGE")
949+
}
950+
951+
err := cvmService.ModifyInternetMaxBandwidthOut(ctx, instanceId, d.Get("internet_charge_type").(string), d.Get("internet_max_bandwidth_out").(int64))
952+
if err != nil {
953+
return err
954+
}
955+
d.SetPartial("internet_max_bandwidth_out")
956+
time.Sleep(10 * 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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,28 @@ 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+
response, 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+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
199+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
200+
201+
return nil
202+
}
203+
182204
func (me *CvmService) ModifyVpc(ctx context.Context, instanceId, vpcId, subnetId, privateIp string) error {
183205
logId := getLogId(ctx)
184206
request := cvm.NewModifyInstancesVpcAttributeRequest()

0 commit comments

Comments
 (0)