Skip to content

Commit 05a758b

Browse files
tongyimingmikatong
and
mikatong
authored
fix(cvm): [121832450]support updating cam_role_name (#3114)
* support updating cam_role_name * add changelog --------- Co-authored-by: mikatong <[email protected]>
1 parent 22f4509 commit 05a758b

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-2
lines changed

.changelog/3114.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: `cam_role_name` support update
3+
```

tencentcloud/services/cvm/resource_tc_instance.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ func ResourceTencentCloudInstance() *schema.Resource {
417417
// role
418418
"cam_role_name": {
419419
Type: schema.TypeString,
420-
ForceNew: true,
421420
Optional: true,
422421
Description: "CAM role name authorized to access.",
423422
},
@@ -1554,6 +1553,13 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
15541553
}
15551554
}
15561555

1556+
if d.HasChange("cam_role_name") {
1557+
err := cvmService.ModifyCamRoleName(ctx, instanceId, d.Get("cam_role_name").(string))
1558+
if err != nil {
1559+
return err
1560+
}
1561+
}
1562+
15571563
if d.HasChange("security_groups") {
15581564
securityGroups := d.Get("security_groups").(*schema.Set).List()
15591565
securityGroupIds := make([]*string, 0, len(securityGroups))

tencentcloud/services/cvm/resource_tc_instance_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ func TestAccTencentCloudInstanceResourceBasic(t *testing.T) {
100100
Config: testAccCvmInstanceResource_BasicChange1,
101101
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")),
102102
},
103+
{
104+
Config: testAccCvmInstanceResource_BasicChangeCamRoleName,
105+
Check: resource.ComposeTestCheckFunc(
106+
testAccCheckCvmInstanceExists("tencentcloud_instance.cvm_basic"),
107+
resource.TestCheckResourceAttr("tencentcloud_instance.cvm_basic", "cam_role_name", "CVM_QcsRole"),
108+
resource.TestCheckResourceAttr("tencentcloud_instance.cvm_basic", "instance_status", "RUNNING"),
109+
),
110+
},
103111
{
104112
ResourceName: "tencentcloud_instance.cvm_basic",
105113
ImportState: true,
@@ -266,6 +274,63 @@ resource "tencentcloud_instance" "cvm_basic" {
266274
267275
`
268276

277+
const testAccCvmInstanceResource_BasicChangeCamRoleName = `
278+
279+
data "tencentcloud_availability_zones" "default" {
280+
}
281+
data "tencentcloud_images" "default" {
282+
image_type = ["PUBLIC_IMAGE"]
283+
image_name_regex = "Final"
284+
}
285+
data "tencentcloud_images" "testing" {
286+
image_type = ["PUBLIC_IMAGE"]
287+
}
288+
data "tencentcloud_instance_types" "default" {
289+
memory_size = 2
290+
exclude_sold_out = true
291+
292+
filter {
293+
name = "instance-family"
294+
values = ["S1","S2","S3","S4","S5"]
295+
}
296+
filter {
297+
values = ["ap-guangzhou-7"]
298+
name = "zone"
299+
}
300+
cpu_core_count = 2
301+
}
302+
resource "tencentcloud_vpc" "vpc" {
303+
name = "cvm-basic-vpc"
304+
cidr_block = "10.0.0.0/16"
305+
}
306+
resource "tencentcloud_subnet" "subnet" {
307+
availability_zone = "ap-guangzhou-7"
308+
vpc_id = tencentcloud_vpc.vpc.id
309+
name = "cvm-basic-subnet"
310+
cidr_block = "10.0.0.0/16"
311+
}
312+
resource "tencentcloud_instance" "cvm_basic" {
313+
instance_name = "tf-ci-test"
314+
availability_zone = "ap-guangzhou-7"
315+
image_id = data.tencentcloud_images.default.images.0.image_id
316+
vpc_id = tencentcloud_vpc.vpc.id
317+
318+
tags = {
319+
hostname = "tci"
320+
}
321+
322+
lifecycle {
323+
ignore_changes = [instance_type]
324+
}
325+
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
326+
subnet_id = tencentcloud_subnet.subnet.id
327+
system_disk_type = "CLOUD_PREMIUM"
328+
project_id = 0
329+
cam_role_name = "CVM_QcsRole"
330+
}
331+
332+
`
333+
269334
func TestAccTencentCloudInstance_SystemDiskResizeOnline(t *testing.T) {
270335
t.Parallel()
271336
resource.Test(t, resource.TestCase{

tencentcloud/services/cvm/service_tencentcloud_cvm.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,25 @@ func (me *CvmService) ModifyDisableApiTermination(ctx context.Context, instanceI
274274
return nil
275275
}
276276

277+
func (me *CvmService) ModifyCamRoleName(ctx context.Context, instanceId, camRoleName string) error {
278+
logId := tccommon.GetLogId(ctx)
279+
request := cvm.NewModifyInstancesAttributeRequest()
280+
request.InstanceIds = []*string{&instanceId}
281+
request.CamRoleName = &camRoleName
282+
283+
ratelimit.Check(request.GetAction())
284+
response, err := me.client.UseCvmClient().ModifyInstancesAttribute(request)
285+
if err != nil {
286+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
287+
logId, request.GetAction(), request.ToJsonString(), err.Error())
288+
return err
289+
}
290+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
291+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
292+
293+
return nil
294+
}
295+
277296
func (me *CvmService) ModifyProjectId(ctx context.Context, instanceId string, projectId int64) error {
278297
logId := tccommon.GetLogId(ctx)
279298
request := cvm.NewModifyInstancesProjectRequest()

website/docs/r/instance.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ The following arguments are supported:
219219
* `image_id` - (Required, String) The image to use for the instance. Changing `image_id` will cause the instance reset.
220220
* `allocate_public_ip` - (Optional, Bool, ForceNew) Associate a public IP address with an instance in a VPC or Classic. Boolean value, Default is false.
221221
* `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.
222-
* `cam_role_name` - (Optional, String, ForceNew) CAM role name authorized to access.
222+
* `cam_role_name` - (Optional, String) CAM role name authorized to access.
223223
* `cdh_host_id` - (Optional, String, ForceNew) Id of cdh instance. Note: it only works when instance_charge_type is set to `CDHPAID`.
224224
* `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`.
225225
* `data_disks` - (Optional, List, ForceNew) Settings for data disks.

0 commit comments

Comments
 (0)