-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_elb_flavors.go
172 lines (151 loc) · 3.81 KB
/
data_source_huaweicloud_elb_flavors.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
package elb
import (
"context"
"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/elb/v3/flavors"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/hashcode"
)
// @API ELB GET /v3/{project_id}/elb/flavors
func DataSourceElbFlavorsV3() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceElbFlavorsV3Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
},
"max_connections": {
Type: schema.TypeInt,
Optional: true,
},
"cps": {
Type: schema.TypeInt,
Optional: true,
},
"qps": {
Type: schema.TypeInt,
Optional: true,
},
"bandwidth": {
Type: schema.TypeInt,
Optional: true,
},
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
// Computed values.
"flavors": {
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,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"max_connections": {
Type: schema.TypeInt,
Computed: true,
},
"cps": {
Type: schema.TypeInt,
Computed: true,
},
"qps": {
Type: schema.TypeInt,
Computed: true,
},
"bandwidth": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}
func dataSourceElbFlavorsV3Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
elbClient, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ELB client: %s", err)
}
listOpts := flavors.ListOpts{}
if v, ok := d.GetOk("type"); ok {
listOpts.Type = []string{v.(string)}
}
pages, err := flavors.List(elbClient, listOpts).AllPages()
if err != nil {
return diag.FromErr(err)
}
allFlavors, err := flavors.ExtractFlavors(pages)
if err != nil {
return diag.Errorf("unable to retrieve flavors: %s", err)
}
maxConnections := d.Get("max_connections").(int)
cps := d.Get("cps").(int)
qps := d.Get("qps").(int)
bandwidth := d.Get("bandwidth").(int)
var ids []string
var flavorInfos []map[string]interface{}
for _, flavor := range allFlavors {
if flavor.SoldOut {
continue
}
if maxConnections > 0 && flavor.Info.Connection != maxConnections {
continue
}
if cps > 0 && flavor.Info.Cps != cps {
continue
}
if qps > 0 && flavor.Info.Qps != qps {
continue
}
if bandwidth > 0 && flavor.Info.Bandwidth != bandwidth*1000 {
continue
}
ids = append(ids, flavor.ID)
flavorInfo := map[string]interface{}{
"id": flavor.ID,
"name": flavor.Name,
"type": flavor.Type,
"max_connections": flavor.Info.Connection,
"cps": flavor.Info.Cps,
"qps": flavor.Info.Qps,
"bandwidth": flavor.Info.Bandwidth / 1000,
}
flavorInfos = append(flavorInfos, flavorInfo)
}
if len(ids) < 1 {
return diag.Errorf("your query returned no results. " +
"Please change your search criteria and try again.")
}
d.SetId(hashcode.Strings(ids))
mErr := multierror.Append(
d.Set("region", cfg.GetRegion(d)),
d.Set("ids", ids),
d.Set("flavors", flavorInfos),
)
return diag.FromErr(mErr.ErrorOrNil())
}