Skip to content

Commit f742e02

Browse files
committed
feat:support as instances data source
1 parent c147373 commit f742e02

File tree

4 files changed

+394
-0
lines changed

4 files changed

+394
-0
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
/*
2+
Use this data source to query detailed information of as instances
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_as_instances" "instances" {
8+
instance_ids =
9+
filters {
10+
name = ""
11+
values =
12+
13+
}
14+
}
15+
```
16+
*/
17+
package tencentcloud
18+
19+
import (
20+
"context"
21+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
22+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
23+
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
24+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
25+
)
26+
27+
func dataSourceTencentCloudAsInstances() *schema.Resource {
28+
return &schema.Resource{
29+
Read: dataSourceTencentCloudAsInstancesRead,
30+
Schema: map[string]*schema.Schema{
31+
"instance_ids": {
32+
Optional: true,
33+
Type: schema.TypeSet,
34+
Elem: &schema.Schema{
35+
Type: schema.TypeString,
36+
},
37+
Description: "Instance ID of the cloud server (CVM) to be queried. The limit is 100 per request.",
38+
},
39+
40+
"filters": {
41+
Optional: true,
42+
Type: schema.TypeList,
43+
Description: "Filter conditions. If there are multiple Filters, the relationship between Filters is a logical AND (AND) relationship. If there are multiple Values in the same Filter, the relationship between Values under the same Filter is a logical OR (OR) relationship.",
44+
Elem: &schema.Resource{
45+
Schema: map[string]*schema.Schema{
46+
"name": {
47+
Type: schema.TypeString,
48+
Required: true,
49+
Description: "Fields to be filtered. Valid names: `instance-id`: Filters by instance ID, `auto-scaling-group-id`: Filter by scaling group ID.",
50+
},
51+
"values": {
52+
Type: schema.TypeSet,
53+
Elem: &schema.Schema{
54+
Type: schema.TypeString,
55+
},
56+
Required: true,
57+
Description: "Value of the field.",
58+
},
59+
},
60+
},
61+
},
62+
63+
"instance_list": {
64+
Computed: true,
65+
Type: schema.TypeList,
66+
Description: "List of instance details.",
67+
Elem: &schema.Resource{
68+
Schema: map[string]*schema.Schema{
69+
"instance_id": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Description: "Instance ID.",
73+
},
74+
"auto_scaling_group_id": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
Description: "Auto scaling group ID.",
78+
},
79+
"auto_scaling_group_name": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
Description: "Auto scaling group name.",
83+
},
84+
"launch_configuration_id": {
85+
Type: schema.TypeString,
86+
Computed: true,
87+
Description: "Launch configuration ID.",
88+
},
89+
"launch_configuration_name": {
90+
Type: schema.TypeString,
91+
Computed: true,
92+
Description: "Launch configuration name.",
93+
},
94+
"life_cycle_state": {
95+
Type: schema.TypeString,
96+
Computed: true,
97+
Description: "Life cycle state. Please refer to the link for field value details: https://cloud.tencent.com/document/api/377/20453#Instance.",
98+
},
99+
"health_status": {
100+
Type: schema.TypeString,
101+
Computed: true,
102+
Description: "Health status, the valid values are HEALTHY and UNHEALTHY.",
103+
},
104+
"protected_from_scale_in": {
105+
Type: schema.TypeBool,
106+
Computed: true,
107+
Description: "Enable scale in protection.",
108+
},
109+
"zone": {
110+
Type: schema.TypeString,
111+
Computed: true,
112+
Description: "Available zone.",
113+
},
114+
"creation_type": {
115+
Type: schema.TypeString,
116+
Computed: true,
117+
Description: "Valid values: `AUTO_CREATION`, `MANUAL_ATTACHING`.",
118+
},
119+
"add_time": {
120+
Type: schema.TypeString,
121+
Computed: true,
122+
Description: "The time when the instance joined the group.",
123+
},
124+
"instance_type": {
125+
Type: schema.TypeString,
126+
Computed: true,
127+
Description: "Instance type.",
128+
},
129+
"version_number": {
130+
Type: schema.TypeInt,
131+
Computed: true,
132+
Description: "Version ID.",
133+
},
134+
},
135+
},
136+
},
137+
138+
"result_output_file": {
139+
Type: schema.TypeString,
140+
Optional: true,
141+
Description: "Used to save results.",
142+
},
143+
},
144+
}
145+
}
146+
147+
func dataSourceTencentCloudAsInstancesRead(d *schema.ResourceData, meta interface{}) error {
148+
defer logElapsed("data_source.tencentcloud_as_instances.read")()
149+
defer inconsistentCheck(d, meta)()
150+
151+
logId := getLogId(contextNil)
152+
153+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
154+
155+
paramMap := make(map[string]interface{})
156+
if v, ok := d.GetOk("instance_ids"); ok {
157+
instanceIdsSet := v.(*schema.Set).List()
158+
paramMap["InstanceIds"] = helper.InterfacesStringsPoint(instanceIdsSet)
159+
}
160+
161+
if v, ok := d.GetOk("filters"); ok {
162+
filtersSet := v.([]interface{})
163+
tmpSet := make([]*as.Filter, 0, len(filtersSet))
164+
165+
for _, item := range filtersSet {
166+
filter := as.Filter{}
167+
filterMap := item.(map[string]interface{})
168+
169+
if v, ok := filterMap["name"]; ok {
170+
filter.Name = helper.String(v.(string))
171+
}
172+
if v, ok := filterMap["values"]; ok {
173+
valuesSet := v.(*schema.Set).List()
174+
filter.Values = helper.InterfacesStringsPoint(valuesSet)
175+
}
176+
tmpSet = append(tmpSet, &filter)
177+
}
178+
paramMap["filters"] = tmpSet
179+
}
180+
181+
service := AsService{client: meta.(*TencentCloudClient).apiV3Conn}
182+
183+
var instanceList []*as.Instance
184+
185+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
186+
result, e := service.DescribeAsInstancesByFilter(ctx, paramMap)
187+
if e != nil {
188+
return retryError(e)
189+
}
190+
instanceList = result
191+
return nil
192+
})
193+
if err != nil {
194+
return err
195+
}
196+
197+
ids := make([]string, 0, len(instanceList))
198+
tmpList := make([]map[string]interface{}, 0, len(instanceList))
199+
200+
if instanceList != nil {
201+
for _, instance := range instanceList {
202+
instanceMap := map[string]interface{}{}
203+
204+
if instance.InstanceId != nil {
205+
instanceMap["instance_id"] = instance.InstanceId
206+
}
207+
208+
if instance.AutoScalingGroupId != nil {
209+
instanceMap["auto_scaling_group_id"] = instance.AutoScalingGroupId
210+
}
211+
212+
if instance.AutoScalingGroupName != nil {
213+
instanceMap["auto_scaling_group_name"] = instance.AutoScalingGroupName
214+
}
215+
216+
if instance.LaunchConfigurationId != nil {
217+
instanceMap["launch_configuration_id"] = instance.LaunchConfigurationId
218+
}
219+
220+
if instance.LaunchConfigurationName != nil {
221+
instanceMap["launch_configuration_name"] = instance.LaunchConfigurationName
222+
}
223+
224+
if instance.LifeCycleState != nil {
225+
instanceMap["life_cycle_state"] = instance.LifeCycleState
226+
}
227+
228+
if instance.HealthStatus != nil {
229+
instanceMap["health_status"] = instance.HealthStatus
230+
}
231+
232+
if instance.ProtectedFromScaleIn != nil {
233+
instanceMap["protected_from_scale_in"] = instance.ProtectedFromScaleIn
234+
}
235+
236+
if instance.Zone != nil {
237+
instanceMap["zone"] = instance.Zone
238+
}
239+
240+
if instance.CreationType != nil {
241+
instanceMap["creation_type"] = instance.CreationType
242+
}
243+
244+
if instance.AddTime != nil {
245+
instanceMap["add_time"] = instance.AddTime
246+
}
247+
248+
if instance.InstanceType != nil {
249+
instanceMap["instance_type"] = instance.InstanceType
250+
}
251+
252+
if instance.VersionNumber != nil {
253+
instanceMap["version_number"] = instance.VersionNumber
254+
}
255+
256+
ids = append(ids, *instance.InstanceId)
257+
tmpList = append(tmpList, instanceMap)
258+
}
259+
260+
_ = d.Set("instance_list", tmpList)
261+
}
262+
263+
d.SetId(helper.DataResourceIdsHash(ids))
264+
output, ok := d.GetOk("result_output_file")
265+
if ok && output.(string) != "" {
266+
if e := writeToFile(output.(string), tmpList); e != nil {
267+
return e
268+
}
269+
}
270+
return nil
271+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudAsInstancesDataSource_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: testAccAsInstancesDataSource_basic(),
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_as_instances.instances"),
21+
resource.TestCheckResourceAttrSet("data.tencentcloud_as_instances.instances", "instance_list.#"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
func testAccAsInstancesDataSource_basic() string {
29+
return defaultAsVariable + `
30+
resource "tencentcloud_vpc" "vpc" {
31+
name = "tf-as-vpc"
32+
cidr_block = "10.2.0.0/16"
33+
}
34+
35+
resource "tencentcloud_subnet" "subnet" {
36+
vpc_id = tencentcloud_vpc.vpc.id
37+
name = "tf-as-subnet"
38+
cidr_block = "10.2.11.0/24"
39+
availability_zone = var.availability_zone
40+
}
41+
42+
resource "tencentcloud_as_scaling_config" "launch_configuration" {
43+
configuration_name = "tf-as-configuration-ds-ins-basic"
44+
image_id = "img-2lr9q49h"
45+
instance_types = [data.tencentcloud_instance_types.default.instance_types.0.instance_type]
46+
}
47+
48+
resource "tencentcloud_as_scaling_group" "scaling_group" {
49+
scaling_group_name = "tf-as-group-ds-ins-basic"
50+
configuration_id = tencentcloud_as_scaling_config.launch_configuration.id
51+
max_size = 1
52+
min_size = 0
53+
vpc_id = tencentcloud_vpc.vpc.id
54+
subnet_ids = [tencentcloud_subnet.subnet.id]
55+
56+
tags = {
57+
"test" = "test"
58+
}
59+
}
60+
61+
data "tencentcloud_as_instances" "instances" {
62+
filters {
63+
name = "auto-scaling-group-id"
64+
values = [tencentcloud_as_scaling_group.scaling_group.id]
65+
66+
}
67+
}
68+
69+
`
70+
}

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Auto Scaling(AS)
116116
tencentcloud_as_scaling_configs
117117
tencentcloud_as_scaling_groups
118118
tencentcloud_as_scaling_policies
119+
tencentcloud_as_instances
119120
120121
Resource
121122
tencentcloud_as_scaling_config
@@ -1195,6 +1196,7 @@ func Provider() terraform.ResourceProvider {
11951196
"tencentcloud_tdmq_rocketmq_topic": dataSourceTencentCloudTdmqRocketmqTopic(),
11961197
"tencentcloud_tdmq_rocketmq_role": dataSourceTencentCloudTdmqRocketmqRole(),
11971198
"tencentcloud_tdmq_rocketmq_group": dataSourceTencentCloudTdmqRocketmqGroup(),
1199+
"tencentcloud_as_instances": dataSourceTencentCloudAsInstances(),
11981200
},
11991201

12001202
ResourcesMap: map[string]*schema.Resource{

0 commit comments

Comments
 (0)