-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_openstack_compute_flavor_v2.go
186 lines (153 loc) · 3.95 KB
/
data_source_openstack_compute_flavor_v2.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
package openstack
import (
"fmt"
"log"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceComputeFlavorV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceComputeFlavorV2Read,
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"min_ram": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"ram": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"vcpus": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"min_disk": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"disk": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"swap": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"rx_tx_factor": {
Type: schema.TypeFloat,
Optional: true,
ForceNew: true,
},
// Computed values
"is_public": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
// dataSourceComputeFlavorV2Read performs the image lookup.
func dataSourceComputeFlavorV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := config.computeV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
listOpts := flavors.ListOpts{
MinDisk: d.Get("min_disk").(int),
MinRAM: d.Get("min_ram").(int),
AccessType: flavors.PublicAccess,
}
log.Printf("[DEBUG] List Options: %#v", listOpts)
var flavor flavors.Flavor
allPages, err := flavors.ListDetail(computeClient, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to query flavors: %s", err)
}
allFlavors, err := flavors.ExtractFlavors(allPages)
if err != nil {
return fmt.Errorf("Unable to retrieve flavors: %s", err)
}
// Loop through all flavors to find a more specific one.
if len(allFlavors) > 1 {
var filteredFlavors []flavors.Flavor
for _, flavor := range allFlavors {
if v := d.Get("name").(string); v != "" {
if flavor.Name != v {
continue
}
}
// d.GetOk is used because 0 might be a valid choice.
if v, ok := d.GetOk("ram"); ok {
if flavor.RAM != v.(int) {
continue
}
}
if v, ok := d.GetOk("vcpus"); ok {
if flavor.VCPUs != v.(int) {
continue
}
}
if v, ok := d.GetOk("disk"); ok {
if flavor.Disk != v.(int) {
continue
}
}
if v, ok := d.GetOk("swap"); ok {
if flavor.Swap != v.(int) {
continue
}
}
if v, ok := d.GetOk("rx_tx_factor"); ok {
if flavor.RxTxFactor != v.(float64) {
continue
}
}
filteredFlavors = append(filteredFlavors, flavor)
}
allFlavors = filteredFlavors
}
if len(allFlavors) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(allFlavors) > 1 {
log.Printf("[DEBUG] Multiple results found: %#v", allFlavors)
return fmt.Errorf("Your query returned more than one result. " +
"Please try a more specific search criteria")
} else {
flavor = allFlavors[0]
}
log.Printf("[DEBUG] Single Flavor found: %s", flavor.ID)
return dataSourceComputeFlavorV2Attributes(d, &flavor)
}
// dataSourceComputeFlavorV2Attributes populates the fields of an Image resource.
func dataSourceComputeFlavorV2Attributes(d *schema.ResourceData, flavor *flavors.Flavor) error {
log.Printf("[DEBUG] openstack_compute_flavor_v2 details: %#v", flavor)
d.SetId(flavor.ID)
d.Set("name", flavor.Name)
d.Set("disk", flavor.Disk)
d.Set("ram", flavor.RAM)
d.Set("rx_tx_factor", flavor.RxTxFactor)
d.Set("swap", flavor.Swap)
d.Set("vcpus", flavor.VCPUs)
d.Set("is_public", flavor.IsPublic)
return nil
}