diff --git a/.changelog/3114.txt b/.changelog/3114.txt new file mode 100644 index 0000000000..40d8d030e1 --- /dev/null +++ b/.changelog/3114.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_instance: `cam_role_name` support update +``` diff --git a/tencentcloud/services/cvm/resource_tc_instance.go b/tencentcloud/services/cvm/resource_tc_instance.go index 5317295e99..333179d9fd 100644 --- a/tencentcloud/services/cvm/resource_tc_instance.go +++ b/tencentcloud/services/cvm/resource_tc_instance.go @@ -417,7 +417,6 @@ func ResourceTencentCloudInstance() *schema.Resource { // role "cam_role_name": { Type: schema.TypeString, - ForceNew: true, Optional: true, Description: "CAM role name authorized to access.", }, @@ -1554,6 +1553,13 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{} } } + if d.HasChange("cam_role_name") { + err := cvmService.ModifyCamRoleName(ctx, instanceId, d.Get("cam_role_name").(string)) + if err != nil { + return err + } + } + if d.HasChange("security_groups") { securityGroups := d.Get("security_groups").(*schema.Set).List() securityGroupIds := make([]*string, 0, len(securityGroups)) diff --git a/tencentcloud/services/cvm/resource_tc_instance_test.go b/tencentcloud/services/cvm/resource_tc_instance_test.go index 3c2e3e08aa..5039065f3c 100644 --- a/tencentcloud/services/cvm/resource_tc_instance_test.go +++ b/tencentcloud/services/cvm/resource_tc_instance_test.go @@ -100,6 +100,14 @@ func TestAccTencentCloudInstanceResourceBasic(t *testing.T) { Config: testAccCvmInstanceResource_BasicChange1, Check: resource.ComposeTestCheckFunc(testAccCheckCvmInstanceExists("tencentcloud_instance.cvm_basic"), resource.TestCheckResourceAttrSet("tencentcloud_instance.cvm_basic", "instance_type"), resource.TestCheckResourceAttr("tencentcloud_instance.cvm_basic", "instance_status", "RUNNING")), }, + { + Config: testAccCvmInstanceResource_BasicChangeCamRoleName, + Check: resource.ComposeTestCheckFunc( + testAccCheckCvmInstanceExists("tencentcloud_instance.cvm_basic"), + resource.TestCheckResourceAttr("tencentcloud_instance.cvm_basic", "cam_role_name", "CVM_QcsRole"), + resource.TestCheckResourceAttr("tencentcloud_instance.cvm_basic", "instance_status", "RUNNING"), + ), + }, { ResourceName: "tencentcloud_instance.cvm_basic", ImportState: true, @@ -266,6 +274,63 @@ resource "tencentcloud_instance" "cvm_basic" { ` +const testAccCvmInstanceResource_BasicChangeCamRoleName = ` + +data "tencentcloud_availability_zones" "default" { +} +data "tencentcloud_images" "default" { + image_type = ["PUBLIC_IMAGE"] + image_name_regex = "Final" +} +data "tencentcloud_images" "testing" { + image_type = ["PUBLIC_IMAGE"] +} +data "tencentcloud_instance_types" "default" { + memory_size = 2 + exclude_sold_out = true + + filter { + name = "instance-family" + values = ["S1","S2","S3","S4","S5"] + } + filter { + values = ["ap-guangzhou-7"] + name = "zone" + } + cpu_core_count = 2 +} +resource "tencentcloud_vpc" "vpc" { + name = "cvm-basic-vpc" + cidr_block = "10.0.0.0/16" +} +resource "tencentcloud_subnet" "subnet" { + availability_zone = "ap-guangzhou-7" + vpc_id = tencentcloud_vpc.vpc.id + name = "cvm-basic-subnet" + cidr_block = "10.0.0.0/16" +} +resource "tencentcloud_instance" "cvm_basic" { + instance_name = "tf-ci-test" + availability_zone = "ap-guangzhou-7" + image_id = data.tencentcloud_images.default.images.0.image_id + vpc_id = tencentcloud_vpc.vpc.id + + tags = { + hostname = "tci" + } + + lifecycle { + ignore_changes = [instance_type] + } + instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type + subnet_id = tencentcloud_subnet.subnet.id + system_disk_type = "CLOUD_PREMIUM" + project_id = 0 + cam_role_name = "CVM_QcsRole" +} + +` + func TestAccTencentCloudInstance_SystemDiskResizeOnline(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ diff --git a/tencentcloud/services/cvm/service_tencentcloud_cvm.go b/tencentcloud/services/cvm/service_tencentcloud_cvm.go index 4957e0e5c1..dbcf76e508 100644 --- a/tencentcloud/services/cvm/service_tencentcloud_cvm.go +++ b/tencentcloud/services/cvm/service_tencentcloud_cvm.go @@ -274,6 +274,25 @@ func (me *CvmService) ModifyDisableApiTermination(ctx context.Context, instanceI return nil } +func (me *CvmService) ModifyCamRoleName(ctx context.Context, instanceId, camRoleName string) error { + logId := tccommon.GetLogId(ctx) + request := cvm.NewModifyInstancesAttributeRequest() + request.InstanceIds = []*string{&instanceId} + request.CamRoleName = &camRoleName + + ratelimit.Check(request.GetAction()) + response, err := me.client.UseCvmClient().ModifyInstancesAttribute(request) + if err != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", + logId, request.GetAction(), request.ToJsonString(), err.Error()) + return err + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", + logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + return nil +} + func (me *CvmService) ModifyProjectId(ctx context.Context, instanceId string, projectId int64) error { logId := tccommon.GetLogId(ctx) request := cvm.NewModifyInstancesProjectRequest() diff --git a/website/docs/r/instance.html.markdown b/website/docs/r/instance.html.markdown index e6efcff126..e3697046a0 100644 --- a/website/docs/r/instance.html.markdown +++ b/website/docs/r/instance.html.markdown @@ -219,7 +219,7 @@ The following arguments are supported: * `image_id` - (Required, String) The image to use for the instance. Changing `image_id` will cause the instance reset. * `allocate_public_ip` - (Optional, Bool, ForceNew) Associate a public IP address with an instance in a VPC or Classic. Boolean value, Default is false. * `bandwidth_package_id` - (Optional, String) bandwidth package id. if user is standard user, then the bandwidth_package_id is needed, or default has bandwidth_package_id. -* `cam_role_name` - (Optional, String, ForceNew) CAM role name authorized to access. +* `cam_role_name` - (Optional, String) CAM role name authorized to access. * `cdh_host_id` - (Optional, String, ForceNew) Id of cdh instance. Note: it only works when instance_charge_type is set to `CDHPAID`. * `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`. * `data_disks` - (Optional, List, ForceNew) Settings for data disks.