/
resource_openstack_blockstorage_quotaset_v3.go
267 lines (225 loc) · 7.52 KB
/
resource_openstack_blockstorage_quotaset_v3.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package nhncloud
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/quotasets"
)
func resourceBlockStorageQuotasetV3() *schema.Resource {
return &schema.Resource{
CreateContext: resourceBlockStorageQuotasetV3Create,
ReadContext: resourceBlockStorageQuotasetV3Read,
UpdateContext: resourceBlockStorageQuotasetV3Update,
Delete: schema.RemoveFromState,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"volumes": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"snapshots": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"gigabytes": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"per_volume_gigabytes": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"backups": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"backup_gigabytes": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"groups": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"volume_type_quota": {
Type: schema.TypeMap,
Optional: true,
},
},
}
}
func resourceBlockStorageQuotasetV3Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
region := GetRegion(d, config)
blockStorageClient, err := config.BlockStorageV3Client(region)
if err != nil {
return diag.Errorf("Error creating OpenStack block storage client: %s", err)
}
projectID := d.Get("project_id").(string)
volumes := d.Get("volumes").(int)
snapshots := d.Get("snapshots").(int)
gigabytes := d.Get("gigabytes").(int)
perVolumeGigabytes := d.Get("per_volume_gigabytes").(int)
backups := d.Get("backups").(int)
backupGigabytes := d.Get("backup_gigabytes").(int)
groups := d.Get("groups").(int)
volumeTypeQuotaRaw := d.Get("volume_type_quota").(map[string]interface{})
volumeTypeQuota, err := blockStorageQuotasetVolTypeQuotaToInt(volumeTypeQuotaRaw)
if err != nil {
return diag.Errorf("Error parsing volume_type_quota in openstack_blockstorage_quotaset_v3: %s", err)
}
updateOpts := quotasets.UpdateOpts{
Volumes: &volumes,
Snapshots: &snapshots,
Gigabytes: &gigabytes,
PerVolumeGigabytes: &perVolumeGigabytes,
Backups: &backups,
BackupGigabytes: &backupGigabytes,
Groups: &groups,
Extra: volumeTypeQuota,
}
q, err := quotasets.Update(blockStorageClient, projectID, updateOpts).Extract()
if err != nil {
return diag.Errorf("Error creating openstack_blockstorage_quotaset_v3: %s", err)
}
id := fmt.Sprintf("%s/%s", projectID, region)
d.SetId(id)
log.Printf("[DEBUG] Created openstack_blockstorage_quotaset_v3 %#v", q)
return resourceBlockStorageQuotasetV3Read(ctx, d, meta)
}
func resourceBlockStorageQuotasetV3Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
region := GetRegion(d, config)
blockStorageClient, err := config.BlockStorageV3Client(region)
if err != nil {
return diag.Errorf("Error creating OpenStack block storage client: %s", err)
}
// Depending on the provider version the resource was created, the resource id
// can be either <project_id> or <project_id>/<region>. This parses the project_id
// in both cases
projectID := strings.Split(d.Id(), "/")[0]
q, err := quotasets.Get(blockStorageClient, projectID).Extract()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error retrieving openstack_blockstorage_quotaset_v3"))
}
log.Printf("[DEBUG] Retrieved openstack_blockstorage_quotaset_v3 %s: %#v", d.Id(), q)
d.Set("project_id", projectID)
d.Set("region", region)
d.Set("volumes", q.Volumes)
d.Set("snapshots", q.Snapshots)
d.Set("gigabytes", q.Gigabytes)
d.Set("per_volume_gigabytes", q.PerVolumeGigabytes)
d.Set("backups", q.Backups)
d.Set("backup_gigabytes", q.BackupGigabytes)
d.Set("groups", q.Groups)
// We only set volume_type_quota when user is defining them
volumeTypeQuotaProvided := d.Get("volume_type_quota").(map[string]interface{})
if len(volumeTypeQuotaProvided) > 0 {
volumeTypeQuota, err := blockStorageQuotasetVolTypeQuotaToStr(q.Extra)
if err != nil {
log.Printf("[WARN] Unable to read openstack_blockstorage_quotaset_v3 %s volume_type_quotas: %s", d.Id(), err)
}
if err := d.Set("volume_type_quota", volumeTypeQuota); err != nil {
log.Printf("[WARN] Unable to set openstack_blockstorage_quotaset_v3 %s volume_type_quotas: %s", d.Id(), err)
}
}
return nil
}
func resourceBlockStorageQuotasetV3Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
blockStorageClient, err := config.BlockStorageV3Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack block storage client: %s", err)
}
var (
hasChange bool
updateOpts quotasets.UpdateOpts
)
if d.HasChange("volumes") {
hasChange = true
volumes := d.Get("volumes").(int)
updateOpts.Volumes = &volumes
}
if d.HasChange("snapshots") {
hasChange = true
snapshots := d.Get("snapshots").(int)
updateOpts.Snapshots = &snapshots
}
if d.HasChange("gigabytes") {
hasChange = true
gigabytes := d.Get("gigabytes").(int)
updateOpts.Gigabytes = &gigabytes
}
if d.HasChange("per_volume_gigabytes") {
hasChange = true
perVolumeGigabytes := d.Get("per_volume_gigabytes").(int)
updateOpts.PerVolumeGigabytes = &perVolumeGigabytes
}
if d.HasChange("backups") {
hasChange = true
backups := d.Get("backups").(int)
updateOpts.Backups = &backups
}
if d.HasChange("backup_gigabytes") {
hasChange = true
backupGigabytes := d.Get("backup_gigabytes").(int)
updateOpts.BackupGigabytes = &backupGigabytes
}
if d.HasChange("groups") {
hasChange = true
groups := d.Get("groups").(int)
updateOpts.Groups = &groups
}
if d.HasChange("volume_type_quota") {
volumeTypeQuotaRaw := d.Get("volume_type_quota").(map[string]interface{})
// if len(volumeTypeQuotaRaw) == 0 it can lead to error when trying to do an update with
// zero attributes. Not updating when a user removes all attributes is acceptable
// as this attributes are not removed anyways.
if len(volumeTypeQuotaRaw) > 0 {
volumeTypeQuota, err := blockStorageQuotasetVolTypeQuotaToInt(volumeTypeQuotaRaw)
if err != nil {
return diag.Errorf("Error parsing volume_type_quota in openstack_blockstorage_quotaset_v3: %s", err)
}
updateOpts.Extra = volumeTypeQuota
hasChange = true
}
}
if hasChange {
log.Printf("[DEBUG] openstack_blockstorage_quotaset_v3 %s update options: %#v", d.Id(), updateOpts)
projectID := d.Get("project_id").(string)
_, err = quotasets.Update(blockStorageClient, projectID, updateOpts).Extract()
if err != nil {
return diag.Errorf("Error updating openstack_blockstorage_quotaset_v3: %s", err)
}
}
return resourceBlockStorageQuotasetV3Read(ctx, d, meta)
}