Skip to content

Commit 1a0d458

Browse files
committed
update resource tencentcloud_instance to support modifying cdh_instance_type
1 parent dbf1cf6 commit 1a0d458

File tree

4 files changed

+191
-14
lines changed

4 files changed

+191
-14
lines changed

examples/tencentcloud-cdh/main.tf

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
data "tencentcloud_images" "my_favorate_image" {
2-
image_type = ["PUBLIC_IMAGE"]
3-
os_name = var.os_name
1+
provider "tencentcloud" {
2+
region = "ap-shanghai"
43
}
54

65
resource "tencentcloud_cdh_instance" "foo" {
76
availability_zone = var.availability_zone
8-
host_type = "HC20"
7+
host_type = "HM50"
98
charge_type = "PREPAID"
109
prepaid_period = 1
1110
host_name = "test"
12-
prepaid_renew_flag = "NOTIFY_AND_MANUAL_RENEW"
11+
prepaid_renew_flag = "DISABLE_NOTIFY_AND_MANUAL_RENEW"
1312
}
1413

1514
data "tencentcloud_cdh_instances" "list" {
@@ -24,17 +23,33 @@ resource "tencentcloud_key_pair" "random_key" {
2423
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDjd8fTnp7Dcuj4mLaQxf9Zs/ORgUL9fQxRCNKkPgP1paTy1I513maMX126i36Lxxl3+FUB52oVbo/FgwlIfX8hyCnv8MCxqnuSDozf1CD0/wRYHcTWAtgHQHBPCC2nJtod6cVC3kB18KeV4U7zsxmwFeBIxojMOOmcOBuh7+trRw=="
2524
}
2625

26+
resource "tencentcloud_placement_group" "foo" {
27+
name = "test"
28+
type = "HOST"
29+
}
30+
2731
resource "tencentcloud_instance" "foo" {
28-
instance_name = var.instance_name
2932
availability_zone = var.availability_zone
30-
image_id = data.tencentcloud_images.my_favorate_image.images.0.image_id
33+
instance_name = var.instance_name
34+
image_id = var.image_id
35+
key_name = tencentcloud_key_pair.random_key.id
36+
placement_group_id = tencentcloud_placement_group.foo.id
37+
security_groups = [var.security_group_id]
38+
system_disk_type = "CLOUD_PREMIUM"
39+
3140
instance_charge_type = "CDHPAID"
3241
cdh_instance_type = "CDH_10C10G"
3342
cdh_host_id = tencentcloud_cdh_instance.foo.id
34-
key_name = tencentcloud_key_pair.random_key.id
35-
system_disk_type = "CLOUD_PREMIUM"
3643

44+
vpc_id = var.vpc_id
45+
subnet_id = var.subnet_id
3746
allocate_public_ip = true
3847
internet_max_bandwidth_out = 2
39-
count = 1
48+
count = 3
49+
50+
data_disks {
51+
data_disk_type = "CLOUD_PREMIUM"
52+
data_disk_size = 50
53+
encrypt = false
54+
}
4055
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
variable "availability_zone" {
2-
default = "ap-guangzhou-3"
2+
default = "ap-shanghai-4"
33
}
44

55
variable "instance_name" {
66
default = "terraform-testing"
77
}
88

9-
variable "os_name" {
10-
default = "centos"
9+
variable "image_id" {
10+
default = "img-ix05e4px"
11+
}
12+
13+
variable "security_group_id" {
14+
default = "sg-9c3f33xk"
15+
}
16+
17+
variable "vpc_id" {
18+
default = "vpc-31zmeluu"
19+
}
20+
21+
variable "subnet_id" {
22+
default = "subnet-aujc02np"
1123
}

tencentcloud/resource_tc_instance.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,65 @@ resource "tencentcloud_instance" "my_awesome_app" {
6767
}
6868
```
6969
70+
Create CVM instance based on CDH
71+
```hcl
72+
variable "availability_zone" {
73+
default = "ap-shanghai-4"
74+
}
75+
76+
resource "tencentcloud_cdh_instance" "foo" {
77+
availability_zone = var.availability_zone
78+
host_type = "HM50"
79+
charge_type = "PREPAID"
80+
prepaid_period = 1
81+
host_name = "test"
82+
prepaid_renew_flag = "DISABLE_NOTIFY_AND_MANUAL_RENEW"
83+
}
84+
85+
data "tencentcloud_cdh_instances" "list" {
86+
availability_zone = var.availability_zone
87+
host_id = tencentcloud_cdh_instance.foo.id
88+
host_name = "test"
89+
host_state = "RUNNING"
90+
}
91+
92+
resource "tencentcloud_key_pair" "random_key" {
93+
key_name = "tf_example_key6"
94+
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDjd8fTnp7Dcuj4mLaQxf9Zs/ORgUL9fQxRCNKkPgP1paTy1I513maMX126i36Lxxl3+FUB52oVbo/FgwlIfX8hyCnv8MCxqnuSDozf1CD0/wRYHcTWAtgHQHBPCC2nJtod6cVC3kB18KeV4U7zsxmwFeBIxojMOOmcOBuh7+trRw=="
95+
}
96+
97+
resource "tencentcloud_placement_group" "foo" {
98+
name = "test"
99+
type = "HOST"
100+
}
101+
102+
resource "tencentcloud_instance" "foo" {
103+
availability_zone = var.availability_zone
104+
instance_name = "terraform-testing"
105+
image_id = "img-ix05e4px"
106+
key_name = tencentcloud_key_pair.random_key.id
107+
placement_group_id = tencentcloud_placement_group.foo.id
108+
security_groups = ["sg-9c3f33xk"]
109+
system_disk_type = "CLOUD_PREMIUM"
110+
111+
instance_charge_type = "CDHPAID"
112+
cdh_instance_type = "CDH_10C10G"
113+
cdh_host_id = tencentcloud_cdh_instance.foo.id
114+
115+
vpc_id = "vpc-31zmeluu"
116+
subnet_id = "subnet-aujc02np"
117+
allocate_public_ip = true
118+
internet_max_bandwidth_out = 2
119+
count = 3
120+
121+
data_disks {
122+
data_disk_type = "CLOUD_PREMIUM"
123+
data_disk_size = 50
124+
encrypt = false
125+
}
126+
}
127+
```
128+
70129
Import
71130
72131
CVM instance can be imported using the id, e.g.
@@ -199,6 +258,7 @@ func resourceTencentCloudInstance() *schema.Resource {
199258
"cdh_host_id": {
200259
Type: schema.TypeString,
201260
Optional: true,
261+
ForceNew: true,
202262
Description: "Id of cdh instance. Note: it only works when instance_charge_type is set to `CDHPAID`.",
203263
},
204264
// network
@@ -816,6 +876,10 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
816876
_ = d.Set("expired_time", instance.ExpiredTime)
817877
_ = d.Set("cam_role_name", instance.CamRoleName)
818878

879+
if *instance.InstanceChargeType == CVM_CHARGE_TYPE_CDHPAID {
880+
_ = d.Set("cdh_instance_type", instance.InstanceType)
881+
}
882+
819883
if _, ok := d.GetOkExists("allocate_public_ip"); !ok {
820884
_ = d.Set("allocate_public_ip", len(instance.PublicIpAddresses) > 0)
821885
}
@@ -1063,6 +1127,32 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
10631127
}
10641128
}
10651129

1130+
if d.HasChange("cdh_instance_type") {
1131+
err := cvmService.ModifyInstanceType(ctx, instanceId, d.Get("cdh_instance_type").(string))
1132+
if err != nil {
1133+
return err
1134+
}
1135+
d.SetPartial("cdh_instance_type")
1136+
1137+
// wait for status
1138+
err = resource.Retry(2*readRetryTimeout, func() *resource.RetryError {
1139+
instance, errRet := cvmService.DescribeInstanceById(ctx, instanceId)
1140+
if errRet != nil {
1141+
return retryError(errRet, InternalError)
1142+
}
1143+
// Modifying instance type need restart the instance
1144+
// so status of CVM must be running when running flag is true
1145+
if instance != nil && (*instance.LatestOperationState == CVM_LATEST_OPERATION_STATE_OPERATING ||
1146+
(flag && *instance.InstanceState != CVM_STATUS_RUNNING)) {
1147+
return resource.RetryableError(fmt.Errorf("cvm instance latest operetion status is %s, retry...", *instance.LatestOperationState))
1148+
}
1149+
return nil
1150+
})
1151+
if err != nil {
1152+
return err
1153+
}
1154+
}
1155+
10661156
if d.HasChange("password") {
10671157
err := cvmService.ModifyPassword(ctx, instanceId, d.Get("password").(string))
10681158
if err != nil {

website/docs/r/instance.html.markdown

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,66 @@ resource "tencentcloud_instance" "my_awesome_app" {
7777
}
7878
```
7979

80+
Create CVM instance based on CDH
81+
82+
```hcl
83+
variable "availability_zone" {
84+
default = "ap-shanghai-4"
85+
}
86+
87+
resource "tencentcloud_cdh_instance" "foo" {
88+
availability_zone = var.availability_zone
89+
host_type = "HM50"
90+
charge_type = "PREPAID"
91+
prepaid_period = 1
92+
host_name = "test"
93+
prepaid_renew_flag = "DISABLE_NOTIFY_AND_MANUAL_RENEW"
94+
}
95+
96+
data "tencentcloud_cdh_instances" "list" {
97+
availability_zone = var.availability_zone
98+
host_id = tencentcloud_cdh_instance.foo.id
99+
host_name = "test"
100+
host_state = "RUNNING"
101+
}
102+
103+
resource "tencentcloud_key_pair" "random_key" {
104+
key_name = "tf_example_key6"
105+
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDjd8fTnp7Dcuj4mLaQxf9Zs/ORgUL9fQxRCNKkPgP1paTy1I513maMX126i36Lxxl3+FUB52oVbo/FgwlIfX8hyCnv8MCxqnuSDozf1CD0/wRYHcTWAtgHQHBPCC2nJtod6cVC3kB18KeV4U7zsxmwFeBIxojMOOmcOBuh7+trRw=="
106+
}
107+
108+
resource "tencentcloud_placement_group" "foo" {
109+
name = "test"
110+
type = "HOST"
111+
}
112+
113+
resource "tencentcloud_instance" "foo" {
114+
availability_zone = var.availability_zone
115+
instance_name = "terraform-testing"
116+
image_id = "img-ix05e4px"
117+
key_name = tencentcloud_key_pair.random_key.id
118+
placement_group_id = tencentcloud_placement_group.foo.id
119+
security_groups = ["sg-9c3f33xk"]
120+
system_disk_type = "CLOUD_PREMIUM"
121+
122+
instance_charge_type = "CDHPAID"
123+
cdh_instance_type = "CDH_10C10G"
124+
cdh_host_id = tencentcloud_cdh_instance.foo.id
125+
126+
vpc_id = "vpc-31zmeluu"
127+
subnet_id = "subnet-aujc02np"
128+
allocate_public_ip = true
129+
internet_max_bandwidth_out = 2
130+
count = 3
131+
132+
data_disks {
133+
data_disk_type = "CLOUD_PREMIUM"
134+
data_disk_size = 50
135+
encrypt = false
136+
}
137+
}
138+
```
139+
80140
## Argument Reference
81141

82142
The following arguments are supported:
@@ -85,7 +145,7 @@ The following arguments are supported:
85145
* `image_id` - (Required, ForceNew) The image to use for the instance. Changing `image_id` will cause the instance to be destroyed and re-created.
86146
* `allocate_public_ip` - (Optional, ForceNew) Associate a public IP address with an instance in a VPC or Classic. Boolean value, Default is false.
87147
* `cam_role_name` - (Optional, ForceNew) CAM role name authorized to access.
88-
* `cdh_host_id` - (Optional) Id of cdh instance. Note: it only works when instance_charge_type is set to `CDHPAID`.
148+
* `cdh_host_id` - (Optional, ForceNew) Id of cdh instance. Note: it only works when instance_charge_type is set to `CDHPAID`.
89149
* `cdh_instance_type` - (Optional) 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`.
90150
* `data_disks` - (Optional, ForceNew) Settings for data disks.
91151
* `disable_monitor_service` - (Optional) Disable enhance service for monitor, it is enabled by default. When this options is set, monitor agent won't be installed.

0 commit comments

Comments
 (0)