Skip to content

Commit e628508

Browse files
authored
Merge pull request #1682 from tencentcloudstack/feat/cvm_support_diable_api
cvm support set disable_api_termination
2 parents 2e40b81 + 4526e4b commit e628508

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

.changelog/1682.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_instance: support set `disable_api_termination`
3+
```

tencentcloud/resource_tc_instance.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@ func resourceTencentCloudInstance() *schema.Resource {
494494
Default: false,
495495
Description: "Indicate whether to force delete the instance. Default is `false`. If set true, the instance will be permanently deleted instead of being moved into the recycle bin. Note: only works for `PREPAID` instance.",
496496
},
497+
"disable_api_termination": {
498+
Type: schema.TypeBool,
499+
Optional: true,
500+
Default: false,
501+
Description: "Whether the termination protection is enabled. Default is `false`. If set true, which means that this instance can not be deleted by an API action.",
502+
},
497503
// role
498504
"cam_role_name": {
499505
Type: schema.TypeString,
@@ -739,6 +745,10 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{}
739745
request.UserData = &userData
740746
}
741747

748+
if v, ok := d.GetOkExists("disable_api_termination"); ok {
749+
request.DisableApiTermination = helper.Bool(v.(bool))
750+
}
751+
742752
if v := helper.GetTags(d, "tags"); len(v) > 0 {
743753
tags := make([]*cvm.Tag, 0)
744754
for tagKey, tagValue := range v {
@@ -958,6 +968,7 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
958968
_ = d.Set("create_time", instance.CreatedTime)
959969
_ = d.Set("expired_time", instance.ExpiredTime)
960970
_ = d.Set("cam_role_name", instance.CamRoleName)
971+
_ = d.Set("disable_api_termination", instance.DisableApiTermination)
961972

962973
if *instance.InstanceChargeType == CVM_CHARGE_TYPE_CDHPAID {
963974
_ = d.Set("cdh_instance_type", instance.InstanceType)
@@ -1162,6 +1173,13 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
11621173
d.SetPartial("instance_name")
11631174
}
11641175

1176+
if d.HasChange("disable_api_termination") {
1177+
err := cvmService.ModifyDisableApiTermination(ctx, instanceId, d.Get("disable_api_termination").(bool))
1178+
if err != nil {
1179+
return err
1180+
}
1181+
}
1182+
11651183
if d.HasChange("security_groups") {
11661184
securityGroups := d.Get("security_groups").(*schema.Set).List()
11671185
securityGroupIds := make([]*string, 0, len(securityGroups))

tencentcloud/resource_tc_instance_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ func TestAccTencentCloudInstanceResource_WithImageLogin(t *testing.T) {
297297
testAccCheckTencentCloudInstanceExists(id),
298298
resource.TestCheckResourceAttr(id, "instance_status", "RUNNING"),
299299
resource.TestCheckResourceAttr(id, "keep_image_login", "true"),
300+
resource.TestCheckResourceAttr(id, "disable_api_termination", "false"),
300301
),
301302
},
302303
},
@@ -996,6 +997,7 @@ resource "tencentcloud_instance" "foo" {
996997
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
997998
keep_image_login = true
998999
system_disk_type = "CLOUD_PREMIUM"
1000+
disable_api_termination = false
9991001
}
10001002
`
10011003

tencentcloud/service_tencentcloud_cvm.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,25 @@ func (me *CvmService) ModifySecurityGroups(ctx context.Context, instanceId strin
246246
return nil
247247
}
248248

249+
func (me *CvmService) ModifyDisableApiTermination(ctx context.Context, instanceId string, disableApiTermination bool) error {
250+
logId := getLogId(ctx)
251+
request := cvm.NewModifyInstancesAttributeRequest()
252+
request.InstanceIds = []*string{&instanceId}
253+
request.DisableApiTermination = &disableApiTermination
254+
255+
ratelimit.Check(request.GetAction())
256+
response, err := me.client.UseCvmClient().ModifyInstancesAttribute(request)
257+
if err != nil {
258+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
259+
logId, request.GetAction(), request.ToJsonString(), err.Error())
260+
return err
261+
}
262+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
263+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
264+
265+
return nil
266+
}
267+
249268
func (me *CvmService) ModifyProjectId(ctx context.Context, instanceId string, projectId int64) error {
250269
logId := getLogId(ctx)
251270
request := cvm.NewModifyInstancesProjectRequest()

website/docs/r/instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ The following arguments are supported:
147147
* `cdh_host_id` - (Optional, String, ForceNew) Id of cdh instance. Note: it only works when instance_charge_type is set to `CDHPAID`.
148148
* `cdh_instance_type` - (Optional, String) Type of instance created on cdh, the value of this parameter is in the format of CDH_XCXG based on the number of CPU cores and memory capacity. Note: it only works when instance_charge_type is set to `CDHPAID`.
149149
* `data_disks` - (Optional, List, ForceNew) Settings for data disks.
150+
* `disable_api_termination` - (Optional, Bool) Whether the termination protection is enabled. Default is `false`. If set true, which means that this instance can not be deleted by an API action.
150151
* `disable_monitor_service` - (Optional, Bool) Disable enhance service for monitor, it is enabled by default. When this options is set, monitor agent won't be installed. Modifying will cause the instance reset.
151152
* `disable_security_service` - (Optional, Bool) Disable enhance service for security, it is enabled by default. When this options is set, security agent won't be installed. Modifying will cause the instance reset.
152153
* `force_delete` - (Optional, Bool) Indicate whether to force delete the instance. Default is `false`. If set true, the instance will be permanently deleted instead of being moved into the recycle bin. Note: only works for `PREPAID` instance.

0 commit comments

Comments
 (0)