This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
resource_openstack_blockstorage_quotaset_v3.go
220 lines (182 loc) · 5.51 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
package openstack
import (
"fmt"
"log"
"time"
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/quotasets"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceBlockStorageQuotasetV3() *schema.Resource {
return &schema.Resource{
Create: resourceBlockStorageQuotasetV3Create,
Read: resourceBlockStorageQuotasetV3Read,
Update: resourceBlockStorageQuotasetV3Update,
Delete: resourceBlockStorageQuotasetV3Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
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,
},
},
}
}
func resourceBlockStorageQuotasetV3Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
blockStorageClient, err := config.blockStorageV3Client(GetRegion(d, config))
if err != nil {
return fmt.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)
updateOpts := quotasets.UpdateOpts{
Volumes: &volumes,
Snapshots: &snapshots,
Gigabytes: &gigabytes,
PerVolumeGigabytes: &perVolumeGigabytes,
Backups: &backups,
BackupGigabytes: &backupGigabytes,
Groups: &groups,
}
q, err := quotasets.Update(blockStorageClient, projectID, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating openstack_blockstorage_quotaset_v3: %s", err)
}
d.SetId(projectID)
log.Printf("[DEBUG] Created openstack_blockstorage_quotaset_v3 %#v", q)
return resourceBlockStorageQuotasetV3Read(d, meta)
}
func resourceBlockStorageQuotasetV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
blockStorageClient, err := config.blockStorageV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
}
q, err := quotasets.Get(blockStorageClient, d.Id()).Extract()
if err != nil {
return 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", d.Id())
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)
return nil
}
func resourceBlockStorageQuotasetV3Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
blockStorageClient, err := config.blockStorageV3Client(GetRegion(d, config))
if err != nil {
return fmt.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 hasChange {
log.Printf("[DEBUG] openstack_blockstorage_quotaset_v3 %s update options: %#v", d.Id(), updateOpts)
_, err := quotasets.Update(blockStorageClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating openstack_blockstorage_quotaset_v3: %s", err)
}
}
return resourceBlockStorageQuotasetV3Read(d, meta)
}
func resourceBlockStorageQuotasetV3Delete(_ *schema.ResourceData, _ interface{}) error {
log.Printf("[DEBUG] openstack_blockstorage_quotaset_v3 deletion is a no-op operation")
return nil
}