-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_iec_bandwidths.go
138 lines (117 loc) · 3.11 KB
/
data_source_huaweicloud_iec_bandwidths.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
package iec
import (
"context"
"log"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/openstack/iec/v1/bandwidths"
ieccommon "github.com/chnsz/golangsdk/openstack/iec/v1/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/hashcode"
)
// @API IEC GET /v1/bandwidths
func DataSourceBandWidths() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBandWidthsRead,
Schema: map[string]*schema.Schema{
"site_id": {
Type: schema.TypeString,
Required: true,
},
"site_info": {
Type: schema.TypeString,
Computed: true,
},
"bandwidths": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
"share_type": {
Type: schema.TypeString,
Computed: true,
},
"charge_mode": {
Type: schema.TypeString,
Computed: true,
},
"line": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceBandWidthsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
iecClient, err := cfg.IECV1Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating IEC client: %s", err)
}
listOpts := bandwidths.ListOpts{
SiteID: d.Get("site_id").(string),
}
allBWs, err := bandwidths.List(iecClient, listOpts).Extract()
if err != nil {
return diag.Errorf("unable to extract IEC bandwidths: %s", err)
}
total := len(allBWs.BandWidth)
log.Printf("[INFO] Retrieved [%d] IEC bandwidths using given filter", total)
ids := make([]string, 0, total)
iecBWs := make([]map[string]interface{}, total)
for i, item := range allBWs.BandWidth {
ids = append(ids, item.ID)
iecBWs[i] = map[string]interface{}{
"id": item.ID,
"name": item.Name,
"size": item.Size,
"share_type": item.ShareType,
"charge_mode": item.ChargeMode,
"status": item.Status,
"line": getLineName(item.Operator),
}
}
d.SetId(hashcode.Strings(ids))
mErr := multierror.Append(nil,
d.Set("bandwidths", iecBWs),
)
if total > 0 {
mErr = multierror.Append(mErr, d.Set("site_info", allBWs.BandWidth[0].SiteInfo))
}
if err := mErr.ErrorOrNil(); err != nil {
return diag.Errorf("error saving IEC bandwidths: %s", err)
}
return nil
}
func getLineName(operator ieccommon.Operator) string {
if operator.Name != "" {
return operator.Name
}
if operator.I18nName != "" {
return operator.I18nName
}
if operator.Sa != "" {
return operator.Sa
}
return ""
}