Skip to content

Commit 65ba36b

Browse files
committed
add resource tencentcloud_cdh_instance and data source tencentcloud_cdh_instances
1 parent 0fcdf26 commit 65ba36b

10 files changed

+1169
-0
lines changed
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 GiB.",
155+
},
156+
"memory_available_size": {
157+
Type: schema.TypeFloat,
158+
Computed: true,
159+
Description: "Instance memory available capacity, unit in GiB.",
160+
},
161+
"disk_total_size": {
162+
Type: schema.TypeInt,
163+
Computed: true,
164+
Description: "Instance disk total capacity, unit in GiB.",
165+
},
166+
"disk_available_size": {
167+
Type: schema.TypeInt,
168+
Computed: true,
169+
Description: "Instance disk available capacity, unit in GiB.",
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCdhInstancesDataSource(t *testing.T) {
10+
dataSourceName := "data.tencentcloud_cdh_instances.list"
11+
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: TestAccTencentCloudCdhInstancesDataSourceConfig,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID(dataSourceName),
22+
resource.TestCheckResourceAttrSet(dataSourceName, "cdh_instance_list.0.host_id"),
23+
resource.TestCheckResourceAttrSet(dataSourceName, "cdh_instance_list.0.create_time"),
24+
resource.TestCheckResourceAttrSet(dataSourceName, "cdh_instance_list.0.expired_time"),
25+
resource.TestCheckResourceAttr(dataSourceName, "cdh_instance_list.0.host_name", "unit-test"),
26+
resource.TestCheckResourceAttr(dataSourceName, "cdh_instance_list.0.host_type", "HC20"),
27+
resource.TestCheckResourceAttr(dataSourceName, "cdh_instance_list.0.host_state", "RUNNING"),
28+
resource.TestCheckResourceAttr(dataSourceName, "cdh_instance_list.0.charge_type", "PREPAID"),
29+
resource.TestCheckResourceAttr(dataSourceName, "cdh_instance_list.0.availability_zone", "ap-guangzhou-3"),
30+
resource.TestCheckResourceAttr(dataSourceName, "cdh_instance_list.0.project_id", "0"),
31+
resource.TestCheckResourceAttr(dataSourceName, "cdh_instance_list.0.prepaid_renew_flag", "DISABLE_NOTIFY_AND_MANUAL_RENEW"),
32+
resource.TestCheckResourceAttr(dataSourceName, "cdh_instance_list.0.host_resource.#", "1"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
const TestAccTencentCloudCdhInstancesDataSourceConfig = `
40+
variable "availability_zone" {
41+
default = "ap-guangzhou-3"
42+
}
43+
44+
resource "tencentcloud_cdh_instance" "foo" {
45+
availability_zone = var.availability_zone
46+
host_type = "HC20"
47+
charge_type = "PREPAID"
48+
host_name = "unit-test"
49+
prepaid_renew_flag = "DISABLE_NOTIFY_AND_MANUAL_RENEW"
50+
}
51+
52+
data "tencentcloud_cdh_instances" "list" {
53+
availability_zone = var.availability_zone
54+
host_id = tencentcloud_cdh_instance.foo.id
55+
host_name = tencentcloud_cdh_instance.foo.host_name
56+
host_state = "RUNNING"
57+
}
58+
59+
`

tencentcloud/extension_cdh.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package tencentcloud
2+
3+
const (
4+
CDH_CHARGE_TYPE_PREPAID = "PREPAID"
5+
6+
CDH_PREPAID_RENEW_FLAG_NOTIFY_AND_AUTO_RENEW = "NOTIFY_AND_AUTO_RENEW"
7+
CDH_PREPAID_RENEW_FLAG_NOTIFY_AND_MANUAL_RENEW = "NOTIFY_AND_MANUAL_RENEW"
8+
CDH_PREPAID_RENEW_FLAG_DISABLE_NOTIFY_AND_MANUAL_RENEW = "DISABLE_NOTIFY_AND_MANUAL_RENEW"
9+
10+
CDH_HOST_STATE_PENDING = "PENDING"
11+
CDH_HOST_STATE_LAUNCH_FAILURE = "LAUNCH_FAILURE"
12+
CDH_HOST_RUNNING = "RUNNING"
13+
CDH_HOST_EXPIRED = "EXPIRED"
14+
)
15+
16+
var CDH_PREPAID_PERIOD = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 24, 36}
17+
18+
var CDH_PREPAID_RENEW_FLAG = []string{
19+
CDH_PREPAID_RENEW_FLAG_NOTIFY_AND_AUTO_RENEW,
20+
CDH_PREPAID_RENEW_FLAG_NOTIFY_AND_MANUAL_RENEW,
21+
CDH_PREPAID_RENEW_FLAG_DISABLE_NOTIFY_AND_MANUAL_RENEW,
22+
}

tencentcloud/provider.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ Cloud Connect Network(CCN)
167167
tencentcloud_ccn_attachment
168168
tencentcloud_ccn_bandwidth_limit
169169
170+
CVM Dedicated Host(CDH)
171+
Data Source
172+
tencentcloud_cdh_instances
173+
174+
Resource
175+
tencentcloud_cdh_instance
176+
170177
Cloud File Storage(CFS)
171178
Data Source
172179
tencentcloud_cfs_access_groups
@@ -783,6 +790,7 @@ func Provider() terraform.ResourceProvider {
783790
"tencentcloud_kms_keys": dataSourceTencentCloudKmsKeys(),
784791
"tencentcloud_ssm_secrets": dataSourceTencentCloudSsmSecrets(),
785792
"tencentcloud_ssm_secret_versions": dataSourceTencentCloudSsmSecretVersions(),
793+
"tencentcloud_cdh_instances": dataSourceTencentCloudCdhInstances(),
786794
},
787795

788796
ResourcesMap: map[string]*schema.Resource{
@@ -945,6 +953,7 @@ func Provider() terraform.ResourceProvider {
945953
"tencentcloud_kms_external_key": resourceTencentCloudKmsExternalKey(),
946954
"tencentcloud_ssm_secret": resourceTencentCloudSsmSecret(),
947955
"tencentcloud_ssm_secret_version": resourceTencentCloudSsmSecretVersion(),
956+
"tencentcloud_cdh_instance": resourceTencentCloudCdhInstance(),
948957
},
949958

950959
ConfigureFunc: providerConfigure,

0 commit comments

Comments
 (0)