Skip to content

fix(cvm): [121832450]support updating cam_role_name #3114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3114.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_instance: `cam_role_name` support update
```
8 changes: 7 additions & 1 deletion tencentcloud/services/cvm/resource_tc_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
Expand Down Expand Up @@ -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))
Expand Down
65 changes: 65 additions & 0 deletions tencentcloud/services/cvm/resource_tc_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
19 changes: 19 additions & 0 deletions tencentcloud/services/cvm/service_tencentcloud_cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading