/
resource_huaweicloud_cc_global_connection_bandwidth.go
405 lines (367 loc) · 12.5 KB
/
resource_huaweicloud_cc_global_connection_bandwidth.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package cc
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"
"github.com/chnsz/golangsdk"
"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 CC POST /v3/{domain_id}/gcb/gcbandwidths
// @API CC GET /v3/{domain_id}/gcb/gcbandwidths/{id}
// @API CC PUT /v3/{domain_id}/gcb/gcbandwidths/{id}
// @API CC DELETE /v3/{domain_id}/gcb/gcbandwidths/{id}
// @API CC POST /v3/gcb/{resource_id}/tags/create
// @API CC POST /v3/gcb/{resource_id}/tags/delete
func ResourceGlobalConnectionBandwidth() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGlobalConnectionBandwidthCreate,
UpdateContext: resourceGlobalConnectionBandwidthUpdate,
ReadContext: resourceGlobalConnectionBandwidthRead,
DeleteContext: resourceGlobalConnectionBandwidthDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"bordercross": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
},
"size": {
Type: schema.TypeInt,
Required: true,
},
"charge_mode": {
Type: schema.TypeString,
Required: true,
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"sla_level": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"local_area": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"remote_area": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"spec_code_id": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"tags": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"binding_service": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"enable_share": {
Type: schema.TypeBool,
Computed: true,
},
"instances": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"frozen": {
Type: schema.TypeBool,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceGlobalConnectionBandwidthCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("cc", region)
if err != nil {
return diag.Errorf("error creating CC client: %s", err)
}
createGCBHttpUrl := "v3/{domain_id}/gcb/gcbandwidths"
createGCBPath := client.Endpoint + createGCBHttpUrl
createGCBPath = strings.ReplaceAll(createGCBPath, "{domain_id}", cfg.DomainID)
createGCBOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
JSONBody: map[string]interface{}{
"globalconnection_bandwidth": utils.RemoveNil(buildCreateGCBParams(d, cfg.GetEnterpriseProjectID(d))),
},
}
createGCBResp, err := client.Request("POST", createGCBPath, &createGCBOpt)
if err != nil {
return diag.Errorf("error creating global connection bandwidth: %s", err)
}
createGCBRespBody, err := utils.FlattenResponse(createGCBResp)
if err != nil {
return diag.FromErr(err)
}
id, err := jmespath.Search("globalconnection_bandwidth.id", createGCBRespBody)
if err != nil {
return diag.Errorf("error creating global connection bandwidth: %s is not found in API response", "id")
}
d.SetId(id.(string))
if v, ok := d.GetOk("binding_service"); ok && v.(string) != "ALL" {
err = updateGCB(client, d, cfg.DomainID)
if err != nil {
return diag.FromErr(err)
}
}
return resourceGlobalConnectionBandwidthRead(ctx, d, meta)
}
func buildCreateGCBParams(d *schema.ResourceData, epsID string) map[string]interface{} {
params := map[string]interface{}{
"name": d.Get("name"),
"type": d.Get("type"),
"size": d.Get("size"),
"charge_mode": d.Get("charge_mode"),
"bordercross": d.Get("bordercross"),
"enterprise_project_id": utils.ValueIngoreEmpty(epsID),
"sla_level": utils.ValueIngoreEmpty(d.Get("sla_level")),
"local_area": utils.ValueIngoreEmpty(d.Get("local_area")),
"remote_area": utils.ValueIngoreEmpty(d.Get("remote_area")),
"spec_code_id": utils.ValueIngoreEmpty(d.Get("spec_code_id")),
"description": utils.ValueIngoreEmpty(d.Get("description")),
"tags": utils.ValueIngoreEmpty(utils.ExpandResourceTags(d.Get("tags").(map[string]interface{}))),
}
return params
}
func resourceGlobalConnectionBandwidthRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("cc", region)
if err != nil {
return diag.Errorf("error creating CC client: %s", err)
}
getGCBHttpUrl := "v3/{domain_id}/gcb/gcbandwidths/{id}"
getGCBPath := client.Endpoint + getGCBHttpUrl
getGCBPath = strings.ReplaceAll(getGCBPath, "{domain_id}", cfg.DomainID)
getGCBPath = strings.ReplaceAll(getGCBPath, "{id}", d.Id())
getGCBOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
getGCBResp, err := client.Request("GET", getGCBPath, &getGCBOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving global connection bandwidth")
}
getGCBRespBody, err := utils.FlattenResponse(getGCBResp)
if err != nil {
return diag.FromErr(err)
}
getGCBRespBody, err = jmespath.Search("globalconnection_bandwidth", getGCBRespBody)
if err != nil {
return diag.Errorf("error getting global connection bandwidth: %s is not found in API response",
"globalconnection_bandwidth")
}
mErr := multierror.Append(nil,
d.Set("name", utils.PathSearch("name", getGCBRespBody, nil)),
d.Set("type", utils.PathSearch("type", getGCBRespBody, nil)),
d.Set("size", int(utils.PathSearch("size", getGCBRespBody, float64(0)).(float64))),
d.Set("charge_mode", utils.PathSearch("charge_mode", getGCBRespBody, nil)),
d.Set("bordercross", utils.PathSearch("bordercross", getGCBRespBody, false)),
d.Set("enterprise_project_id", utils.PathSearch("enterprise_project_id", getGCBRespBody, nil)),
d.Set("sla_level", utils.PathSearch("sla_level", getGCBRespBody, nil)),
d.Set("local_area", utils.PathSearch("local_site_code", getGCBRespBody, nil)),
d.Set("remote_area", utils.PathSearch("remote_site_code", getGCBRespBody, nil)),
d.Set("spec_code_id", utils.PathSearch("spec_code_id", getGCBRespBody, nil)),
d.Set("description", utils.PathSearch("description", getGCBRespBody, nil)),
d.Set("tags", utils.FlattenTagsToMap(utils.PathSearch("tags", getGCBRespBody, nil))),
d.Set("enable_share", utils.PathSearch("enable_share", getGCBRespBody, false)),
d.Set("binding_service", utils.PathSearch("binding_service", getGCBRespBody, nil)),
d.Set("instances", flattenInstances(utils.PathSearch("instances", getGCBRespBody, make([]interface{}, 0)))),
d.Set("frozen", utils.PathSearch("frozen", getGCBRespBody, false)),
d.Set("created_at", utils.PathSearch("created_at", getGCBRespBody, nil)),
d.Set("updated_at", utils.PathSearch("updated_at", getGCBRespBody, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenInstances(rawParams interface{}) []map[string]interface{} {
rawArray, _ := rawParams.([]interface{})
if len(rawArray) == 0 {
return nil
}
rst := make([]map[string]interface{}, 0)
for _, val := range rawArray {
params := map[string]interface{}{
"id": utils.PathSearch("id", val, nil),
"type": utils.PathSearch("type", val, nil),
"region": utils.PathSearch("region_id", val, nil),
}
rst = append(rst, params)
}
return rst
}
func resourceGlobalConnectionBandwidthUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("cc", region)
if err != nil {
return diag.Errorf("error creating CC client: %s", err)
}
updateChanges := []string{
"name",
"size",
"charge_mode",
"sla_level",
"binding_service",
"spec_code_id",
"description",
}
if d.HasChanges(updateChanges...) {
err = updateGCB(client, d, cfg.DomainID)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChanges("tags") {
tagErr := updateTags(client, d)
if tagErr != nil {
return diag.Errorf("error updating tags of global connection bandwidth (%s): %s", d.Id(), tagErr)
}
}
return resourceGlobalConnectionBandwidthRead(ctx, d, meta)
}
func updateTags(client *golangsdk.ServiceClient, d *schema.ResourceData) error {
oRaw, nRaw := d.GetChange("tags")
oMap := oRaw.(map[string]interface{})
nMap := nRaw.(map[string]interface{})
manageTagsHttpUrl := "v3/gcb/{resource_id}/tags/{action}"
manageTagsPath := client.Endpoint + manageTagsHttpUrl
manageTagsPath = strings.ReplaceAll(manageTagsPath, "{resource_id}", d.Id())
manageTagsOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
204,
},
}
// remove old tags
if len(oMap) > 0 {
manageTagsOpt.JSONBody = map[string]interface{}{
"tags": utils.ExpandResourceTags(oMap),
}
deleteTagsPath := strings.ReplaceAll(manageTagsPath, "{action}", "delete")
_, err := client.Request("POST", deleteTagsPath, &manageTagsOpt)
if err != nil {
return err
}
}
// set new tags
if len(nMap) > 0 {
manageTagsOpt.JSONBody = map[string]interface{}{
"tags": utils.ExpandResourceTags(nMap),
}
createTagsPath := strings.ReplaceAll(manageTagsPath, "{action}", "create")
_, err := client.Request("POST", createTagsPath, &manageTagsOpt)
if err != nil {
return err
}
}
return nil
}
func updateGCB(client *golangsdk.ServiceClient, d *schema.ResourceData, domainID string) error {
updateGCBHttpUrl := "v3/{domain_id}/gcb/gcbandwidths/{id}"
updateGCBPath := client.Endpoint + updateGCBHttpUrl
updateGCBPath = strings.ReplaceAll(updateGCBPath, "{domain_id}", domainID)
updateGCBPath = strings.ReplaceAll(updateGCBPath, "{id}", d.Id())
updateGCBOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
JSONBody: map[string]interface{}{
"globalconnection_bandwidth": utils.RemoveNil(buildUpdateGCBParams(d)),
},
}
_, err := client.Request("PUT", updateGCBPath, &updateGCBOpt)
if err != nil {
return fmt.Errorf("error updating global connection bandwidth: %s", err)
}
return nil
}
func buildUpdateGCBParams(d *schema.ResourceData) map[string]interface{} {
params := map[string]interface{}{
"name": d.Get("name"),
"size": d.Get("size"),
"charge_mode": d.Get("charge_mode"),
"sla_level": utils.ValueIngoreEmpty(d.Get("sla_level")),
"binding_service": utils.ValueIngoreEmpty(d.Get("binding_service")),
"spec_code_id": utils.ValueIngoreEmpty(d.Get("spec_code_id")),
"description": d.Get("description"),
}
return params
}
func resourceGlobalConnectionBandwidthDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("cc", region)
if err != nil {
return diag.Errorf("error creating CC client: %s", err)
}
deleteGCBHttpUrl := "v3/{domain_id}/gcb/gcbandwidths/{id}"
deleteGCBPath := client.Endpoint + deleteGCBHttpUrl
deleteGCBPath = strings.ReplaceAll(deleteGCBPath, "{domain_id}", cfg.DomainID)
deleteGCBPath = strings.ReplaceAll(deleteGCBPath, "{id}", d.Id())
deleteGCBOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
_, err = client.Request("DELETE", deleteGCBPath, &deleteGCBOpt)
if err != nil {
return diag.Errorf("error deleting global connection bandwidth: %s", err)
}
return nil
}