Skip to content

Commit 1ae64b0

Browse files
authored
Merge pull request #613 from ChrisdeR/feature/cdh
cdh feature support
2 parents d99e050 + 1a0d458 commit 1ae64b0

17 files changed

+1443
-3
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
## 1.55.3 (Unreleased)
1+
## 1.56.0 (Unreleased)
2+
3+
FEATURES:
4+
5+
* **New Resource**: `tencentcloud_cdh_instance`
6+
* **New Data Source**: `tencentcloud_cdh_instances`
7+
8+
ENHANCEMENTS:
9+
10+
* Resource: `tencentcloud_instance` add `cdh_instance_type` and `cdh_host_id` to support create instance based on cdh.
11+
212
## 1.55.2 (March 29, 2021)
313

414
ENHANCEMENTS:

examples/tencentcloud-cdh/main.tf

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
provider "tencentcloud" {
2+
region = "ap-shanghai"
3+
}
4+
5+
resource "tencentcloud_cdh_instance" "foo" {
6+
availability_zone = var.availability_zone
7+
host_type = "HM50"
8+
charge_type = "PREPAID"
9+
prepaid_period = 1
10+
host_name = "test"
11+
prepaid_renew_flag = "DISABLE_NOTIFY_AND_MANUAL_RENEW"
12+
}
13+
14+
data "tencentcloud_cdh_instances" "list" {
15+
availability_zone = var.availability_zone
16+
host_id = tencentcloud_cdh_instance.foo.id
17+
host_name = "test"
18+
host_state = "RUNNING"
19+
}
20+
21+
resource "tencentcloud_key_pair" "random_key" {
22+
key_name = "tf_example_key6"
23+
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDjd8fTnp7Dcuj4mLaQxf9Zs/ORgUL9fQxRCNKkPgP1paTy1I513maMX126i36Lxxl3+FUB52oVbo/FgwlIfX8hyCnv8MCxqnuSDozf1CD0/wRYHcTWAtgHQHBPCC2nJtod6cVC3kB18KeV4U7zsxmwFeBIxojMOOmcOBuh7+trRw=="
24+
}
25+
26+
resource "tencentcloud_placement_group" "foo" {
27+
name = "test"
28+
type = "HOST"
29+
}
30+
31+
resource "tencentcloud_instance" "foo" {
32+
availability_zone = var.availability_zone
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+
40+
instance_charge_type = "CDHPAID"
41+
cdh_instance_type = "CDH_10C10G"
42+
cdh_host_id = tencentcloud_cdh_instance.foo.id
43+
44+
vpc_id = var.vpc_id
45+
subnet_id = var.subnet_id
46+
allocate_public_ip = true
47+
internet_max_bandwidth_out = 2
48+
count = 3
49+
50+
data_disks {
51+
data_disk_type = "CLOUD_PREMIUM"
52+
data_disk_size = 50
53+
encrypt = false
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "availability_zone" {
2+
default = "ap-shanghai-4"
3+
}
4+
5+
variable "instance_name" {
6+
default = "terraform-testing"
7+
}
8+
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"
23+
}

examples/tencentcloud-cdh/version.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
required_version = ">= 0.12"
3+
}
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
/*
2+
Use this data source to query CDH instances.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_cdh_instances" "list" {
8+
availability_zone = "ap-guangzhou-3"
9+
host_id = "host-d6s7i5q4"
10+
host_name = "test"
11+
host_state = "RUNNING"
12+
project_id = 1154137
13+
}
14+
```
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"context"
20+
"log"
21+
"strconv"
22+
23+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
24+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
25+
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
26+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
27+
)
28+
29+
func dataSourceTencentCloudCdhInstances() *schema.Resource {
30+
return &schema.Resource{
31+
Read: dataSourceTencentCloudCdhInstancesRead,
32+
33+
Schema: map[string]*schema.Schema{
34+
"host_id": {
35+
Type: schema.TypeString,
36+
Optional: true,
37+
Description: "ID of the CDH instances to be queried.",
38+
},
39+
"host_name": {
40+
Type: schema.TypeString,
41+
Optional: true,
42+
Description: "Name of the CDH instances to be queried.",
43+
},
44+
"host_state": {
45+
Type: schema.TypeString,
46+
Optional: true,
47+
Description: "State of the CDH instances to be queried. Valid values: `PENDING`, `LAUNCH_FAILURE`, `RUNNING`, `EXPIRED`.",
48+
},
49+
"availability_zone": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
Description: "The available zone that the CDH instance locates at.",
53+
},
54+
"project_id": {
55+
Type: schema.TypeInt,
56+
Optional: true,
57+
Description: "The project CDH belongs to.",
58+
},
59+
"result_output_file": {
60+
Type: schema.TypeString,
61+
Optional: true,
62+
Description: "Used to save results.",
63+
},
64+
65+
// computed
66+
"cdh_instance_list": {
67+
Type: schema.TypeList,
68+
Computed: true,
69+
Description: "An information list of cdh instance. Each element contains the following attributes:",
70+
Elem: &schema.Resource{
71+
Schema: map[string]*schema.Schema{
72+
"host_id": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
Description: "ID of the CDH instance.",
76+
},
77+
"host_name": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
Description: "Name of the CDH instance.",
81+
},
82+
"host_type": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
Description: "Type of the CDH instance.",
86+
},
87+
"host_state": {
88+
Type: schema.TypeString,
89+
Computed: true,
90+
Description: "State of the CDH instance.",
91+
},
92+
"charge_type": {
93+
Type: schema.TypeString,
94+
Computed: true,
95+
Description: "The charge type of the CDH instance.",
96+
},
97+
"availability_zone": {
98+
Type: schema.TypeString,
99+
Computed: true,
100+
Description: "The available zone that the CDH instance locates at.",
101+
},
102+
"project_id": {
103+
Type: schema.TypeInt,
104+
Computed: true,
105+
Description: "The project CDH belongs to.",
106+
},
107+
"prepaid_renew_flag": {
108+
Type: schema.TypeString,
109+
Computed: true,
110+
Description: "Auto renewal flag.",
111+
},
112+
"create_time": {
113+
Type: schema.TypeString,
114+
Computed: true,
115+
Description: "Creation time of the CDH instance.",
116+
},
117+
"expired_time": {
118+
Type: schema.TypeString,
119+
Computed: true,
120+
Description: "Expired time of the CDH instance.",
121+
},
122+
"cvm_instance_ids": {
123+
Type: schema.TypeList,
124+
Computed: true,
125+
Description: "Id of CVM instances that have been created on the CDH instance.",
126+
Elem: &schema.Schema{
127+
Type: schema.TypeString,
128+
},
129+
},
130+
"cage_id": {
131+
Type: schema.TypeString,
132+
Computed: true,
133+
Description: "Cage ID of the CDH instance. This parameter is only valid for CDH instances in the cages of finance availability zones.",
134+
},
135+
"host_resource": {
136+
Type: schema.TypeList,
137+
Computed: true,
138+
Description: "An information list of host resource. Each element contains the following attributes:",
139+
Elem: &schema.Resource{
140+
Schema: map[string]*schema.Schema{
141+
"cpu_total_num": {
142+
Type: schema.TypeInt,
143+
Computed: true,
144+
Description: "The number of total CPU cores of the instance.",
145+
},
146+
"cpu_available_num": {
147+
Type: schema.TypeInt,
148+
Computed: true,
149+
Description: "The number of available CPU cores of the instance.",
150+
},
151+
"memory_total_size": {
152+
Type: schema.TypeFloat,
153+
Computed: true,
154+
Description: "Instance memory total capacity, unit in GB.",
155+
},
156+
"memory_available_size": {
157+
Type: schema.TypeFloat,
158+
Computed: true,
159+
Description: "Instance memory available capacity, unit in GB.",
160+
},
161+
"disk_total_size": {
162+
Type: schema.TypeInt,
163+
Computed: true,
164+
Description: "Instance disk total capacity, unit in GB.",
165+
},
166+
"disk_available_size": {
167+
Type: schema.TypeInt,
168+
Computed: true,
169+
Description: "Instance disk available capacity, unit in GB.",
170+
},
171+
"disk_type": {
172+
Type: schema.TypeString,
173+
Computed: true,
174+
Description: "Type of the disk.",
175+
},
176+
},
177+
},
178+
},
179+
},
180+
},
181+
},
182+
},
183+
}
184+
}
185+
186+
func dataSourceTencentCloudCdhInstancesRead(d *schema.ResourceData, meta interface{}) error {
187+
defer logElapsed("data_source.tencentcloud_cdh_instances.read")()
188+
logId := getLogId(contextNil)
189+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
190+
cdhService := CdhService{
191+
client: meta.(*TencentCloudClient).apiV3Conn,
192+
}
193+
194+
filter := make(map[string]string)
195+
if v, ok := d.GetOk("host_id"); ok {
196+
filter["host-id"] = v.(string)
197+
}
198+
if v, ok := d.GetOk("host_name"); ok {
199+
filter["host-name"] = v.(string)
200+
}
201+
if v, ok := d.GetOk("host_state"); ok {
202+
filter["host-state"] = v.(string)
203+
}
204+
if v, ok := d.GetOk("availability_zone"); ok {
205+
filter["zone"] = v.(string)
206+
}
207+
if v, ok := d.GetOk("project_id"); ok {
208+
filter["project-id"] = strconv.FormatInt(int64(v.(int)), 10)
209+
}
210+
211+
var instances []*cvm.HostItem
212+
var errRet error
213+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
214+
instances, errRet = cdhService.DescribeCdhInstanceByFilter(ctx, filter)
215+
if errRet != nil {
216+
return retryError(errRet)
217+
}
218+
return nil
219+
})
220+
if err != nil {
221+
return err
222+
}
223+
224+
instanceList := make([]map[string]interface{}, 0, len(instances))
225+
ids := make([]string, 0, len(instances))
226+
for _, instance := range instances {
227+
mapping := map[string]interface{}{
228+
"host_id": instance.HostId,
229+
"host_name": instance.HostName,
230+
"host_type": instance.HostType,
231+
"host_state": instance.HostState,
232+
"charge_type": instance.HostChargeType,
233+
"availability_zone": instance.Placement.Zone,
234+
"project_id": instance.Placement.ProjectId,
235+
"prepaid_renew_flag": instance.RenewFlag,
236+
"create_time": instance.CreatedTime,
237+
"expired_time": instance.ExpiredTime,
238+
"cvm_instance_ids": helper.StringsInterfaces(instance.InstanceIds),
239+
"cage_id": instance.CageId,
240+
}
241+
hostResource := map[string]interface{}{
242+
"cpu_total_num": instance.HostResource.CpuTotal,
243+
"cpu_available_num": instance.HostResource.CpuAvailable,
244+
"memory_total_size": instance.HostResource.MemTotal,
245+
"memory_available_size": instance.HostResource.MemAvailable,
246+
"disk_total_size": instance.HostResource.DiskTotal,
247+
"disk_available_size": instance.HostResource.DiskAvailable,
248+
"disk_type": instance.HostResource.DiskType,
249+
}
250+
mapping["host_resource"] = []map[string]interface{}{hostResource}
251+
instanceList = append(instanceList, mapping)
252+
ids = append(ids, *instance.HostId)
253+
}
254+
255+
d.SetId(helper.DataResourceIdsHash(ids))
256+
err = d.Set("cdh_instance_list", instanceList)
257+
if err != nil {
258+
log.Printf("[CRITAL]%s provider set cdh instance list fail, reason:%s\n ", logId, err.Error())
259+
return err
260+
}
261+
262+
output, ok := d.GetOk("result_output_file")
263+
if ok && output.(string) != "" {
264+
if err := writeToFile(output.(string), instanceList); err != nil {
265+
return err
266+
}
267+
}
268+
return nil
269+
}

0 commit comments

Comments
 (0)