Skip to content

Commit d932653

Browse files
tongyimingmikatong
andauthored
add datasource (#1759)
* add datasource * add changelog * update * update * update --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 7e0bd2d commit d932653

32 files changed

+2975
-1
lines changed

.changelog/1759.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
```release-note:new-data-source
2+
tencentcloud_lighthouse_zone
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_lighthouse_scene
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_lighthouse_reset_instance_blueprint
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_lighthouse_region
15+
```
16+
17+
```release-note:new-data-source
18+
tencentcloud_lighthouse_instance_vnc_url
19+
```
20+
21+
```release-note:new-data-source
22+
tencentcloud_lighthouse_instance_traffic_package
23+
```
24+
25+
```release-note:new-data-source
26+
tencentcloud_lighthouse_instance_disk_num
27+
```
28+
29+
```release-note:new-data-source
30+
tencentcloud_lighthouse_instance_blueprint
31+
```
32+
33+
```release-note:new-data-source
34+
tencentcloud_lighthouse_disk_config
35+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/hashicorp/errwrap v1.1.0 // indirect
1515
github.com/hashicorp/go-multierror v1.1.1
1616
github.com/hashicorp/hcl/v2 v2.13.0
17+
github.com/hashicorp/terraform-plugin-sdk v1.7.0
1718
github.com/hashicorp/terraform-plugin-sdk/v2 v2.20.0
1819
github.com/katbyte/terrafmt v0.2.0
1920
github.com/mitchellh/go-homedir v1.1.0
@@ -190,7 +191,6 @@ require (
190191
github.com/hashicorp/terraform-json v0.14.0 // indirect
191192
github.com/hashicorp/terraform-plugin-go v0.12.0 // indirect
192193
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
193-
github.com/hashicorp/terraform-plugin-sdk v1.7.0 // indirect
194194
github.com/hashicorp/terraform-plugin-test v1.2.0 // indirect
195195
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
196196
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
Use this data source to query detailed information of lighthouse disk_config
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_lighthouse_disk_config" "disk_config" {
8+
filters {
9+
name = "zone"
10+
values = ["ap-guangzhou-3"]
11+
}
12+
}
13+
```
14+
*/
15+
package tencentcloud
16+
17+
import (
18+
"context"
19+
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
22+
lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324"
23+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
24+
)
25+
26+
func dataSourceTencentCloudLighthouseDiskConfig() *schema.Resource {
27+
return &schema.Resource{
28+
Read: dataSourceTencentCloudLighthouseDiskConfigRead,
29+
Schema: map[string]*schema.Schema{
30+
"filters": {
31+
Optional: true,
32+
Type: schema.TypeList,
33+
Description: "Filter list.zoneFilter by availability zone.Type: StringRequired: no.",
34+
Elem: &schema.Resource{
35+
Schema: map[string]*schema.Schema{
36+
"name": {
37+
Type: schema.TypeString,
38+
Required: true,
39+
Description: "Field to be filtered.",
40+
},
41+
"values": {
42+
Type: schema.TypeSet,
43+
Elem: &schema.Schema{
44+
Type: schema.TypeString,
45+
},
46+
Required: true,
47+
Description: "Filter value of field.",
48+
},
49+
},
50+
},
51+
},
52+
53+
"disk_config_set": {
54+
Computed: true,
55+
Type: schema.TypeList,
56+
Description: "List of cloud disk configurations.",
57+
Elem: &schema.Resource{
58+
Schema: map[string]*schema.Schema{
59+
"zone": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
Description: "Availability zone.",
63+
},
64+
"disk_type": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
Description: "Cloud disk type.",
68+
},
69+
"disk_sales_state": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Description: "Cloud disk sale status.",
73+
},
74+
"max_disk_size": {
75+
Type: schema.TypeInt,
76+
Computed: true,
77+
Description: "Maximum cloud disk size.",
78+
},
79+
"min_disk_size": {
80+
Type: schema.TypeInt,
81+
Computed: true,
82+
Description: "Minimum cloud disk size.",
83+
},
84+
"disk_step_size": {
85+
Type: schema.TypeInt,
86+
Computed: true,
87+
Description: "Cloud disk increment.",
88+
},
89+
},
90+
},
91+
},
92+
93+
"result_output_file": {
94+
Type: schema.TypeString,
95+
Optional: true,
96+
Description: "Used to save results.",
97+
},
98+
},
99+
}
100+
}
101+
102+
func dataSourceTencentCloudLighthouseDiskConfigRead(d *schema.ResourceData, meta interface{}) error {
103+
defer logElapsed("data_source.tencentcloud_lighthouse_disk_config.read")()
104+
defer inconsistentCheck(d, meta)()
105+
106+
logId := getLogId(contextNil)
107+
108+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
109+
110+
paramMap := make(map[string]interface{})
111+
if v, ok := d.GetOk("filters"); ok {
112+
filtersSet := v.([]interface{})
113+
tmpSet := make([]*lighthouse.Filter, 0, len(filtersSet))
114+
115+
for _, item := range filtersSet {
116+
filter := lighthouse.Filter{}
117+
filterMap := item.(map[string]interface{})
118+
119+
if v, ok := filterMap["name"]; ok {
120+
filter.Name = helper.String(v.(string))
121+
}
122+
if v, ok := filterMap["values"]; ok {
123+
valuesSet := v.(*schema.Set).List()
124+
filter.Values = helper.InterfacesStringsPoint(valuesSet)
125+
}
126+
tmpSet = append(tmpSet, &filter)
127+
}
128+
paramMap["filters"] = tmpSet
129+
}
130+
131+
service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn}
132+
133+
var diskConfigSet []*lighthouse.DiskConfig
134+
135+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
136+
result, e := service.DescribeLighthouseDiskConfigByFilter(ctx, paramMap)
137+
if e != nil {
138+
return retryError(e)
139+
}
140+
diskConfigSet = result
141+
return nil
142+
})
143+
if err != nil {
144+
return err
145+
}
146+
147+
tmpList := make([]map[string]interface{}, 0, len(diskConfigSet))
148+
149+
if diskConfigSet != nil {
150+
for _, diskConfig := range diskConfigSet {
151+
diskConfigMap := map[string]interface{}{}
152+
153+
if diskConfig.Zone != nil {
154+
diskConfigMap["zone"] = diskConfig.Zone
155+
}
156+
157+
if diskConfig.DiskType != nil {
158+
diskConfigMap["disk_type"] = diskConfig.DiskType
159+
}
160+
161+
if diskConfig.DiskSalesState != nil {
162+
diskConfigMap["disk_sales_state"] = diskConfig.DiskSalesState
163+
}
164+
165+
if diskConfig.MaxDiskSize != nil {
166+
diskConfigMap["max_disk_size"] = diskConfig.MaxDiskSize
167+
}
168+
169+
if diskConfig.MinDiskSize != nil {
170+
diskConfigMap["min_disk_size"] = diskConfig.MinDiskSize
171+
}
172+
173+
if diskConfig.DiskStepSize != nil {
174+
diskConfigMap["disk_step_size"] = diskConfig.DiskStepSize
175+
}
176+
177+
tmpList = append(tmpList, diskConfigMap)
178+
}
179+
180+
_ = d.Set("disk_config_set", tmpList)
181+
}
182+
d.SetId(helper.BuildToken())
183+
output, ok := d.GetOk("result_output_file")
184+
if ok && output.(string) != "" {
185+
if e := writeToFile(output.(string), tmpList); e != nil {
186+
return e
187+
}
188+
}
189+
return nil
190+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudLighthouseDiskConfigDataSource_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: testAccLighthouseDiskConfigDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_lighthouse_disk_config.disk_config")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccLighthouseDiskConfigDataSource = `
26+
27+
data "tencentcloud_lighthouse_disk_config" "disk_config" {
28+
filters {
29+
name = "zone"
30+
values = ["ap-guangzhou-3"]
31+
}
32+
}
33+
`

0 commit comments

Comments
 (0)