Skip to content

Commit 186d061

Browse files
tongyimingmikatong
andauthored
Feat/cvm resource (#1521)
* append tencentcloud_cvm_launch_template * append tencentcloud_cvm_instances_modification * add doc * update doc * update * add changelog Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 280631c commit 186d061

11 files changed

+2029
-0
lines changed

.changelog/1521.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-data-source
2+
tencentcloud_cvm_instances_modification
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_cvm_launch_template
7+
```

tencentcloud/basic_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ variable "snap_id" {
145145
}
146146
`
147147

148+
// cvm-modification
149+
const defaultCommonCvmId = "ins-cr2rfq78"
150+
const defaultCvmModificationVariable = `
151+
variable "cvm_id" {
152+
default = "` + defaultCommonCvmId + `"
153+
}
154+
`
155+
148156
// AS
149157
const defaultAsVariable = `
150158
variable "availability_zone" {
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
Use this data source to query cvm instances modification.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_cvm_instances_modification" "foo" {
8+
instance_ids = ["ins-xxxxxxx"]
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"log"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
19+
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
20+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
21+
)
22+
23+
func dataSourceTencentCloudCvmInstancesModification() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceTencentCloudCvmInstancesModificationRead,
26+
27+
Schema: map[string]*schema.Schema{
28+
"instance_ids": {
29+
Optional: true,
30+
Type: schema.TypeSet,
31+
Elem: &schema.Schema{
32+
Type: schema.TypeString,
33+
},
34+
Description: "One or more instance ID to be queried. It can be obtained from the InstanceId in the returned value of API DescribeInstances. The maximum number of instances in batch for each request is 20.",
35+
},
36+
"filters": {
37+
Optional: true,
38+
Type: schema.TypeList,
39+
Description: "The upper limit of Filters for each request is 10 and the upper limit for Filter.Values is 2.",
40+
Elem: &schema.Resource{
41+
Schema: map[string]*schema.Schema{
42+
"name": {
43+
Type: schema.TypeString,
44+
Required: true,
45+
Description: "Fields to be filtered.",
46+
},
47+
"values": {
48+
Type: schema.TypeSet,
49+
Elem: &schema.Schema{
50+
Type: schema.TypeString,
51+
},
52+
Required: true,
53+
Description: "Value of the field.",
54+
},
55+
},
56+
},
57+
},
58+
59+
"result_output_file": {
60+
Type: schema.TypeString,
61+
Optional: true,
62+
Description: "Used to save results.",
63+
},
64+
65+
"instance_type_config_status_list": {
66+
Computed: true,
67+
Type: schema.TypeList,
68+
Description: "The list of model configurations that can be adjusted by the instance.",
69+
Elem: &schema.Resource{
70+
Schema: map[string]*schema.Schema{
71+
"status": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "State description.",
75+
},
76+
"message": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
Description: "Status description information.",
80+
},
81+
"instance_type_config": {
82+
Type: schema.TypeList,
83+
Computed: true,
84+
Description: "Configuration information.",
85+
Elem: &schema.Resource{
86+
Schema: map[string]*schema.Schema{
87+
"zone": {
88+
Type: schema.TypeString,
89+
Computed: true,
90+
Description: "Availability zone.",
91+
},
92+
"instance_type": {
93+
Type: schema.TypeString,
94+
Computed: true,
95+
Description: "Instance type.",
96+
},
97+
"instance_family": {
98+
Type: schema.TypeString,
99+
Computed: true,
100+
Description: "Instance family.",
101+
},
102+
"gpu": {
103+
Type: schema.TypeInt,
104+
Computed: true,
105+
Description: "The number of GPU kernels, in cores.",
106+
},
107+
"cpu": {
108+
Type: schema.TypeInt,
109+
Computed: true,
110+
Description: "The number of CPU kernels, in cores.",
111+
},
112+
"memory": {
113+
Type: schema.TypeInt,
114+
Computed: true,
115+
Description: "Memory capacity (in GB).",
116+
},
117+
"fpga": {
118+
Type: schema.TypeInt,
119+
Computed: true,
120+
Description: "The number of FPGA kernels, in cores.",
121+
},
122+
},
123+
},
124+
},
125+
},
126+
},
127+
},
128+
},
129+
}
130+
}
131+
132+
func dataSourceTencentCloudCvmInstancesModificationRead(d *schema.ResourceData, meta interface{}) error {
133+
defer logElapsed("data_source.tencentcloud_cvm_instances_modification.read")()
134+
logId := getLogId(contextNil)
135+
136+
var (
137+
request = cvm.NewDescribeInstancesModificationRequest()
138+
response = cvm.NewDescribeInstancesModificationResponse()
139+
)
140+
if v, ok := d.GetOk("instance_ids"); ok {
141+
request.InstanceIds = helper.InterfacesStringsPoint(v.(*schema.Set).List())
142+
}
143+
144+
if v, ok := d.GetOk("filters"); ok {
145+
filters := make([]*cvm.Filter, 0)
146+
for _, item := range v.(*schema.Set).List() {
147+
filter := item.(map[string]interface{})
148+
name := filter["name"].(string)
149+
filters = append(filters, &cvm.Filter{
150+
Name: &name,
151+
Values: helper.StringsStringsPoint(filter["values"].([]string)),
152+
})
153+
}
154+
request.Filters = filters
155+
}
156+
157+
instanceTypeConfigStatusList := make([]map[string]interface{}, 0)
158+
159+
var innerErr error
160+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
161+
response, innerErr = meta.(*TencentCloudClient).apiV3Conn.UseCvmClient().DescribeInstancesModification(request)
162+
if innerErr != nil {
163+
return retryError(innerErr)
164+
}
165+
return nil
166+
})
167+
if err != nil {
168+
return err
169+
}
170+
171+
ids := make([]string, 0)
172+
for _, instanceTypeConfigStatusSetItem := range response.Response.InstanceTypeConfigStatusSet {
173+
instanceTypeConfigStatus := make(map[string]interface{})
174+
instanceTypeConfigStatus["status"] = instanceTypeConfigStatusSetItem.Status
175+
instanceTypeConfigStatus["message"] = instanceTypeConfigStatusSetItem.Message
176+
177+
instanceTypeConfigMaps := make([]map[string]interface{}, 0)
178+
instanceTypeConfigMap := make(map[string]interface{})
179+
instanceTypeConfig := instanceTypeConfigStatusSetItem.InstanceTypeConfig
180+
instanceTypeConfigMap["zone"] = instanceTypeConfig.Zone
181+
ids = append(ids, *instanceTypeConfig.InstanceType)
182+
instanceTypeConfigMap["instance_type"] = instanceTypeConfig.InstanceType
183+
instanceTypeConfigMap["instance_family"] = instanceTypeConfig.InstanceFamily
184+
instanceTypeConfigMap["gpu"] = instanceTypeConfig.GPU
185+
instanceTypeConfigMap["cpu"] = instanceTypeConfig.CPU
186+
instanceTypeConfigMap["memory"] = instanceTypeConfig.Memory
187+
instanceTypeConfigMap["fpga"] = instanceTypeConfig.FPGA
188+
instanceTypeConfigMaps = append(instanceTypeConfigMaps, instanceTypeConfigMap)
189+
instanceTypeConfigStatus["instance_type_config"] = instanceTypeConfigMaps
190+
191+
instanceTypeConfigStatusList = append(instanceTypeConfigStatusList, instanceTypeConfigStatus)
192+
}
193+
194+
d.SetId(helper.DataResourceIdsHash(ids))
195+
err = d.Set("instance_type_config_status_list", instanceTypeConfigStatusList)
196+
if err != nil {
197+
log.Printf("[CRITAL]%s provider set instance list fail, reason:%s\n ", logId, err.Error())
198+
return err
199+
}
200+
201+
output, ok := d.GetOk("result_output_file")
202+
if ok && output.(string) != "" {
203+
if err := writeToFile(output.(string), instanceTypeConfigStatusList); err != nil {
204+
return err
205+
}
206+
}
207+
return nil
208+
209+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCvmInstancesModificationDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccCvmInstancesModificationDataSource,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_cvm_instances_modification.foo"),
21+
resource.TestCheckResourceAttrSet("data.tencentcloud_cvm_instances_modification.foo", "instance_type_config_status_list.#"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
const testAccCvmInstancesModificationDataSource = defaultCvmModificationVariable + `
29+
data "tencentcloud_cvm_instances_modification" "foo" {
30+
instance_ids = [var.cvm_id]
31+
}
32+
`

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Cloud Virtual Machine(CVM)
294294
tencentcloud_placement_groups
295295
tencentcloud_reserved_instance_configs
296296
tencentcloud_reserved_instances
297+
tencentcloud_cvm_instances_modification
297298
298299
Resource
299300
tencentcloud_instance
@@ -305,6 +306,7 @@ Cloud Virtual Machine(CVM)
305306
tencentcloud_reserved_instance
306307
tencentcloud_image
307308
tencentcloud_cvm_hpc_cluster
309+
tencentcloud_cvm_launch_template
308310
309311
TDSQL-C MySQL(CynosDB)
310312
Data Source
@@ -1273,6 +1275,7 @@ func Provider() terraform.ResourceProvider {
12731275
"tencentcloud_cynosdb_cluster_instance_groups": dataSourceTencentCloudCynosdbClusterInstanceGroups(),
12741276
"tencentcloud_cynosdb_cluster_params": dataSourceTencentCloudCynosdbClusterParams(),
12751277
"tencentcloud_cynosdb_param_templates": dataSourceTencentCloudCynosdbParamTemplates(),
1278+
"tencentcloud_cvm_instances_modification": dataSourceTencentCloudCvmInstancesModification(),
12761279
},
12771280

12781281
ResourcesMap: map[string]*schema.Resource{
@@ -1647,6 +1650,7 @@ func Provider() terraform.ResourceProvider {
16471650
"tencentcloud_dayu_ddos_ip_attachment_v2": resourceTencentCloudDayuDDosIpAttachmentV2(),
16481651
"tencentcloud_tsf_microservice": resourceTencentCloudTsfMicroservice(),
16491652
"tencentcloud_tsf_application_config": resourceTencentCloudTsfApplicationConfig(),
1653+
"tencentcloud_cvm_launch_template": resourceTencentCloudCvmLaunchTemplate(),
16501654
},
16511655

16521656
ConfigureFunc: providerConfigure,

0 commit comments

Comments
 (0)