Skip to content

Commit a5582e5

Browse files
authored
Merge pull request #1482 from tencentcloudstack/feat/support-as-instance-datasource
Feat/support as instance datasource
2 parents 7b70856 + 5f2af30 commit a5582e5

File tree

7 files changed

+481
-0
lines changed

7 files changed

+481
-0
lines changed

.changelog/1482.txt

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

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
@@ -1211,6 +1212,7 @@ func Provider() terraform.ResourceProvider {
12111212
"tencentcloud_tcmq_queue": dataSourceTencentCloudTcmqQueue(),
12121213
"tencentcloud_tcmq_topic": dataSourceTencentCloudTcmqTopic(),
12131214
"tencentcloud_tcmq_subscribe": dataSourceTencentCloudTcmqSubscribe(),
1215+
"tencentcloud_as_instances": dataSourceTencentCloudAsInstances(),
12141216
},
12151217

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

0 commit comments

Comments
 (0)