/
data_source_alicloud_ecp_instance_types.go
109 lines (100 loc) · 3 KB
/
data_source_alicloud_ecp_instance_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package alicloud
import (
"strconv"
"time"
"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceAlicloudEcpInstanceTypes() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudEcpInstanceTypesRead,
Schema: map[string]*schema.Schema{
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"instance_types": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_type": {
Type: schema.TypeString,
Computed: true,
},
"default_resolution": {
Type: schema.TypeString,
Computed: true,
},
"cpu_core_count": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"name_en": {
Type: schema.TypeString,
Computed: true,
},
"instance_type_family": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudEcpInstanceTypesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
action := "ListInstanceTypes"
request := make(map[string]interface{})
request["RegionId"] = client.RegionId
var objects []map[string]interface{}
var response map[string]interface{}
conn, err := client.NewCloudphoneClient()
if err != nil {
return WrapError(err)
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-12-30"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_ecp_instance_types", action, AlibabaCloudSdkGoERROR)
}
addDebug(action, response, request)
resp, err := jsonpath.Get("$.InstanceTypes.InstanceType", response)
if err != nil {
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.InstanceTypes.InstanceType", response)
}
result, _ := resp.([]interface{})
for _, v := range result {
item := v.(map[string]interface{})
objects = append(objects, item)
}
s := make([]map[string]interface{}, 0)
for _, object := range objects {
mapping := map[string]interface{}{
"instance_type": object["InstanceType"],
"default_resolution": object["DefaultResolution"],
"cpu_core_count": object["CpuCoreCount"],
"name": object["Name"],
"name_en": object["NameEn"],
"instance_type_family": object["InstanceTypeFamily"],
}
s = append(s, mapping)
}
d.SetId(strconv.FormatInt(time.Now().Unix(), 16))
if err := d.Set("instance_types", s); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}