/
data_source_huaweicloud_ga_accelerators.go
251 lines (227 loc) · 7.11 KB
/
data_source_huaweicloud_ga_accelerators.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package ga
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/pagination"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API GA GET /v1/accelerators
func DataSourceAccelerators() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAcceleratorsRead,
Schema: map[string]*schema.Schema{
"accelerator_id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the accelerator.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the accelerator.",
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: "The current status of the accelerator.",
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the enterprise project to which the accelerator belongs.",
},
"accelerators": {
Type: schema.TypeList,
Elem: acceleratorsSchema(),
Computed: true,
Description: "The list of the accelerators.",
},
},
}
}
func acceleratorsSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the accelerator.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the accelerator.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the accelerator.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The current status of the accelerator.",
},
"ip_sets": {
Type: schema.TypeList,
Computed: true,
Description: "The IP information of the accelerator.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_type": {
Type: schema.TypeString,
Computed: true,
Description: "The IP type of the accelerator.",
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
Description: "The IP address of the accelerator.",
},
"area": {
Type: schema.TypeString,
Computed: true,
Description: "The acceleration zone of the accelerator.",
},
},
},
},
"flavor_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the flavor to which the accelerator belongs.",
},
"enterprise_project_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the enterprise project to which the accelerator belongs.",
},
"tags": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The key/value pairs to associate with the accelerator.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The creation time of the accelerator.",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "The latest update time of the accelerator.",
},
},
}
return &sc
}
func dataSourceAcceleratorsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// listAccelerators: Query the list of accelerators
var (
listAcceleratorsHttpUrl = "v1/accelerators"
listAcceleratorsProduct = "ga"
)
listAcceleratorsClient, err := cfg.NewServiceClient(listAcceleratorsProduct, region)
if err != nil {
return diag.Errorf("error creating GA client: %s", err)
}
listAcceleratorsPath := listAcceleratorsClient.Endpoint + listAcceleratorsHttpUrl
listAcceleratorsqueryParams := buildListAcceleratorsQueryParams(d, cfg)
listAcceleratorsPath += listAcceleratorsqueryParams
listAcceleratorsResp, err := pagination.ListAllItems(
listAcceleratorsClient,
"marker",
listAcceleratorsPath,
&pagination.QueryOpts{MarkerField: ""})
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving accelerators")
}
listAcceleratorsRespJson, err := json.Marshal(listAcceleratorsResp)
if err != nil {
return diag.FromErr(err)
}
var listAcceleratorsRespBody interface{}
err = json.Unmarshal(listAcceleratorsRespJson, &listAcceleratorsRespBody)
if err != nil {
return diag.FromErr(err)
}
uuid, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(uuid)
var mErr *multierror.Error
mErr = multierror.Append(
mErr,
d.Set("accelerators", flattenListAcceleratorsResponseBody(listAcceleratorsRespBody)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenListAcceleratorsResponseBody(resp interface{}) []interface{} {
if resp == nil {
return nil
}
curJson := utils.PathSearch("accelerators", resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
rst := make([]interface{}, 0, len(curArray))
for _, v := range curArray {
rst = append(rst, map[string]interface{}{
"id": utils.PathSearch("id", v, nil),
"name": utils.PathSearch("name", v, nil),
"status": utils.PathSearch("status", v, nil),
"description": utils.PathSearch("description", v, nil),
"ip_sets": flattenIpSets(utils.PathSearch("ip_sets", v, make([]interface{}, 0))),
"enterprise_project_id": utils.PathSearch("enterprise_project_id", v, nil),
"tags": utils.FlattenTagsToMap(utils.PathSearch("tags", v, nil)),
"flavor_id": utils.PathSearch("flavor_id", v, nil),
"created_at": utils.PathSearch("created_at", v, nil),
"updated_at": utils.PathSearch("updated_at", v, nil),
})
}
return rst
}
func flattenIpSets(raw interface{}) []map[string]interface{} {
curArray := raw.([]interface{})
if len(curArray) == 0 {
return nil
}
result := make([]map[string]interface{}, len(curArray))
for i, ipSets := range curArray {
result[i] = map[string]interface{}{
"ip_type": utils.PathSearch("ip_type", ipSets, nil),
"ip_address": utils.PathSearch("ip_address", ipSets, nil),
"area": utils.PathSearch("area", ipSets, nil),
}
}
return result
}
func buildListAcceleratorsQueryParams(d *schema.ResourceData, cfg *config.Config) string {
res := ""
epsID := cfg.GetEnterpriseProjectID(d)
if v, ok := d.GetOk("accelerator_id"); ok {
res = fmt.Sprintf("%s&id=%v", res, v)
}
if v, ok := d.GetOk("name"); ok {
res = fmt.Sprintf("%s&name=%v", res, v)
}
if v, ok := d.GetOk("status"); ok {
res = fmt.Sprintf("%s&status=%v", res, v)
}
if epsID != "" {
res = fmt.Sprintf("%s&enterprise_project_id=%s", res, epsID)
}
if res != "" {
res = "?" + res[1:]
}
return res
}