-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_iec_flavors.go
127 lines (115 loc) · 2.95 KB
/
data_source_huaweicloud_iec_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
package iec
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/openstack/iec/v1/flavors"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
// @API IEC GET /v1/cloudservers/flavors
func DataSourceFlavors() *schema.Resource {
return &schema.Resource{
Read: dataSourceFlavorsRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"site_ids": {
Type: schema.TypeString,
Optional: true,
},
"area": {
Type: schema.TypeString,
Optional: true,
},
"province": {
Type: schema.TypeString,
Optional: true,
},
"city": {
Type: schema.TypeString,
Optional: true,
},
"operator": {
Type: schema.TypeString,
Optional: true,
},
"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,
},
"vcpus": {
Type: schema.TypeInt,
Computed: true,
},
"memory": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}
func dataSourceFlavorsRead(d *schema.ResourceData, meta interface{}) error {
cfg := meta.(*config.Config)
iecClient, err := cfg.IECV1Client(cfg.GetRegion(d))
if err != nil {
return fmt.Errorf("error creating IEC client: %s", err)
}
listOpts := flavors.ListOpts{
Name: d.Get("name").(string),
SiteIDS: d.Get("site_ids").(string),
Area: d.Get("area").(string),
Province: d.Get("province").(string),
City: d.Get("city").(string),
Operator: d.Get("operator").(string),
}
log.Printf("[DEBUG] fetching IEC flavors by filter: %#v", listOpts)
allFlavors, err := flavors.List(iecClient, listOpts).Extract()
if err != nil {
return fmt.Errorf("unable to extract IEC flavors: %s", err)
}
total := len(allFlavors.Flavors)
if total < 1 {
return fmt.Errorf("your query returned no results of iec_flavors, " +
"please change your search criteria and try again")
}
log.Printf("[INFO] Retrieved [%d] IEC flavors using given filter", total)
iecFlavors := make([]map[string]interface{}, 0, total)
for _, item := range allFlavors.Flavors {
val := map[string]interface{}{
"id": item.ID,
"name": item.Name,
"memory": item.Ram,
}
if vcpus, err := strconv.Atoi(item.Vcpus); err == nil {
val["vcpus"] = vcpus
}
iecFlavors = append(iecFlavors, val)
}
mErr := multierror.Append(d.Set("flavors", iecFlavors))
if err := mErr.ErrorOrNil(); err != nil {
return fmt.Errorf("error saving IEC flavors: %s", err)
}
d.SetId(allFlavors.Flavors[0].ID)
return nil
}