/
resource_quota_specification.go
246 lines (218 loc) · 6.64 KB
/
resource_quota_specification.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
package nomad
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceQuotaSpecification() *schema.Resource {
return &schema.Resource{
Create: resourceQuotaSpecificationWrite,
Update: resourceQuotaSpecificationWrite,
Delete: resourceQuotaSpecificationDelete,
Read: resourceQuotaSpecificationRead,
Exists: resourceQuotaSpecificationExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Description: "Unique name for this quota specification.",
Required: true,
Type: schema.TypeString,
ForceNew: true,
},
"description": {
Description: "Description for this quota specification.",
Optional: true,
Type: schema.TypeString,
},
"limits": {
Description: "Limits encapsulated by this quota specification.",
Required: true,
Type: schema.TypeSet,
Elem: resourceQuotaSpecificationLimits(),
},
},
}
}
func resourceQuotaSpecificationLimits() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"region": {
Description: "Region in which this limit has affect.",
Type: schema.TypeString,
Required: true,
},
"region_limit": {
Required: true,
Description: "The limit applied to this region.",
Type: schema.TypeSet,
MaxItems: 1,
Elem: resourceQuotaSpecificationRegionLimits(),
},
},
}
}
func resourceQuotaSpecificationRegionLimits() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"cpu": {
Type: schema.TypeInt,
Optional: true,
},
"memory_mb": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}
func resourceQuotaSpecificationWrite(d *schema.ResourceData, meta interface{}) error {
client := meta.(ProviderConfig).client
spec := api.QuotaSpec{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
limits, err := expandQuotaLimits(d)
if err != nil {
return err
}
spec.Limits = limits
log.Printf("[DEBUG] Upserting quota specification %q", spec.Name)
_, err = client.Quotas().Register(&spec, nil)
if err != nil {
return fmt.Errorf("error upserting quota specification %q: %s", spec.Name, err.Error())
}
log.Printf("[DEBUG] Upserted quota specification %q", spec.Name)
d.SetId(spec.Name)
return resourceQuotaSpecificationRead(d, meta)
}
func resourceQuotaSpecificationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(ProviderConfig).client
name := d.Id()
// delete the quota spec
log.Printf("[DEBUG] Deleting quota specification %q", name)
_, err := client.Quotas().Delete(name, nil)
if err != nil {
return fmt.Errorf("error deleting quota specification %q: %s", name, err.Error())
}
log.Printf("[DEBUG] Deleted quota specification %q", name)
return nil
}
func resourceQuotaSpecificationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(ProviderConfig).client
name := d.Id()
// retrieve the policy
log.Printf("[DEBUG] Reading quota specification %q", name)
spec, _, err := client.Quotas().Info(name, nil)
if err != nil {
// we have Exists, so no need to handle 404
return fmt.Errorf("error reading quota specification %q: %s", name, err.Error())
}
log.Printf("[DEBUG] Read quota specification %q", name)
d.Set("name", spec.Name)
d.Set("description", spec.Description)
err = d.Set("limits", flattenQuotaLimits(spec.Limits))
if err != nil {
return fmt.Errorf("error setting quota specification limits for %q: %s", name, err.Error())
}
return nil
}
func resourceQuotaSpecificationExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(ProviderConfig).client
name := d.Id()
log.Printf("[DEBUG] Checking if quota specification %q exists", name)
resp, _, err := client.Quotas().Info(name, nil)
if err != nil {
// As of Nomad 0.4.1, the API client returns an error for 404
// rather than a nil result, so we must check this way.
if strings.Contains(err.Error(), "404") {
return false, nil
}
return true, fmt.Errorf("error checking for quota specification %q: %#v", name, err)
}
// just to be safe
if resp == nil {
log.Printf("[DEBUG] Response was nil, so assuming quota specification %q doesn't exist", name)
return false, nil
}
return true, nil
}
func flattenQuotaLimits(limits []*api.QuotaLimit) *schema.Set {
var results []interface{}
for _, limit := range limits {
res := map[string]interface{}{
"region": limit.Region,
"region_limit": flattenQuotaRegionLimit(limit.RegionLimit),
}
results = append(results, res)
}
return schema.NewSet(schema.HashResource(resourceQuotaSpecificationLimits()), results)
}
func flattenQuotaRegionLimit(limit *api.Resources) *schema.Set {
if limit == nil {
return nil
}
result := map[string]interface{}{}
if limit.CPU != nil {
result["cpu"] = *limit.CPU
}
if limit.MemoryMB != nil {
result["memory_mb"] = *limit.MemoryMB
}
return schema.NewSet(schema.HashResource(resourceQuotaSpecificationRegionLimits()),
[]interface{}{result})
}
func expandQuotaLimits(d *schema.ResourceData) ([]*api.QuotaLimit, error) {
configs := d.Get("limits")
limits := configs.(*schema.Set).List()
results := make([]*api.QuotaLimit, 0, len(limits))
for _, lim := range limits {
limit, ok := lim.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected limit to be a map[string]interface{}, got %T instead", lim)
}
region, ok := limit["region"].(string)
if !ok {
return nil, fmt.Errorf("expected region to be string, got %T instead", limit["region"])
}
res := &api.QuotaLimit{
Region: region,
}
regLimit, err := expandRegionLimit(limit["region_limit"])
if err != nil {
return nil, fmt.Errorf("error parsing region limit for region %q: %s", limit["region"], err.Error())
}
res.RegionLimit = regLimit
results = append(results, res)
}
return results, nil
}
func expandRegionLimit(limit interface{}) (*api.Resources, error) {
regLimits := limit.(*schema.Set).List()
if len(regLimits) < 1 {
return nil, nil
}
regLimit, ok := regLimits[0].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected map[string]interface{} for region limit, got %T", regLimits[0])
}
var res api.Resources
if cpu, ok := regLimit["cpu"]; ok {
c, ok := cpu.(int)
if !ok {
return nil, fmt.Errorf("expected CPU to be int, got %T", cpu)
}
res.CPU = &c
}
if mem, ok := regLimit["memory_mb"]; ok {
m, ok := mem.(int)
if !ok {
return nil, fmt.Errorf("expected memory to be int, got %T", mem)
}
res.MemoryMB = &m
}
return &res, nil
}