-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathresource_netapp_cloudmanager_aws_fsx_volume.go
365 lines (347 loc) · 10.3 KB
/
resource_netapp_cloudmanager_aws_fsx_volume.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package cloudmanager
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceFsxVolume() *schema.Resource {
return &schema.Resource{
Create: resourceFSXVolumeCreate,
Read: resourceFSXVolumeRead,
Delete: resourceFSXVolumeDelete,
Update: resourceFSXVolumeUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
CustomizeDiff: resourceFSXVolumeCustomizeDiff,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"svm_name": {
Type: schema.TypeString,
Optional: true,
},
"size": {
Type: schema.TypeFloat,
Required: true,
},
"unit": {
Type: schema.TypeString,
Required: true,
},
"snapshot_policy_name": {
Type: schema.TypeString,
Optional: true,
Default: "default",
},
"client_id": {
Type: schema.TypeString,
Required: true,
},
"export_policy_type": {
Type: schema.TypeString,
Optional: true,
},
"export_policy_ip": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"export_policy_nfs_version": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"tiering_policy": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"none", "snapshot_only", "auto", "all"}, false),
},
"volume_protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"nfs", "cifs"}, false),
},
"share_name": {
Type: schema.TypeString,
Optional: true,
},
"permission": {
Type: schema.TypeString,
Optional: true,
},
"users": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"file_system_id": {
Type: schema.TypeString,
Required: true,
},
"enable_storage_efficiency": {
Type: schema.TypeBool,
Optional: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"tenant_id": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceFSXVolumeCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("Creating volume: %#v", d)
client := meta.(*Client)
clientID := d.Get("client_id").(string)
var svm string
volume := volumeRequest{}
weInfo, err := client.getWorkingEnvironmentDetail(d, clientID)
if err != nil {
log.Printf("Cannot find working environment: %#v", err)
return fmt.Errorf("cannot find working environment: %#v", err)
}
if svm == "" {
svm = weInfo.SvmName
}
volume.SvmName = svm
volume.FileSystemID = d.Get("file_system_id").(string)
volume.TenantID = d.Get("tenant_id").(string)
volume.EnableCompression = false
volume.EnableThinProvisioning = true
volume.EnableStorageEfficiency = d.Get("enable_storage_efficiency").(bool)
if o, ok := d.GetOk("tags"); ok {
tags := make([]volumeTag, 0)
for k, v := range o.(map[string]interface{}) {
tag := volumeTag{}
tag.TagKey = k
tag.TagValue = v.(string)
tags = append(tags, tag)
}
volume.VolumeFSXTags = tags
}
err = client.setCommonAttributes(weInfo.WorkingEnvironmentType, d, &volume, clientID)
if err != nil {
return err
}
err = client.createVolume(volume, false, clientID)
if err != nil {
log.Printf("Error creating volume: %#v", err)
return err
}
res, err := client.getVolume(volume, clientID)
if err != nil {
log.Printf("Error reading volume after creation: %#v", err)
return err
}
for _, volume := range res {
if volume.SvmName == svm && volume.Name == d.Get("name") {
d.SetId(volume.ID)
break
}
}
return resourceFSXVolumeRead(d, meta)
}
func resourceFSXVolumeRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("Fetching volume: %#v", d)
client := meta.(*Client)
clientID := d.Get("client_id").(string)
volume := volumeRequest{}
var svm string
if v, ok := d.GetOk("svm_name"); ok {
svm = v.(string)
} else {
weInfo, err := client.getWorkingEnvironmentDetail(d, clientID)
if err != nil {
log.Printf("Cannot find working environment: %#v", err)
return fmt.Errorf("Cannot find working environment: %#v", err)
}
if svm == "" {
svm = weInfo.SvmName
}
}
volume.SvmName = svm
volume.Name = d.Get("name").(string)
volume.FileSystemID = d.Get("file_system_id").(string)
res, err := client.getVolume(volume, clientID)
if err != nil {
log.Printf("Error reading volume: %#v", err)
return err
}
for _, volume := range res {
if volume.ID == d.Id() {
if _, ok := d.GetOk("enable_storage_efficiency"); ok {
// EnableDeduplication reflects enable_storage_efficiency for fsx volume
d.Set("enable_storage_efficiency", volume.EnableDeduplication)
}
if v, ok := d.GetOk("tiering_policy"); ok {
if v.(string) != "none" {
d.Set("tiering_policy", volume.TieringPolicy)
}
}
if _, ok := d.GetOk("snapshot_policy_name"); ok {
d.Set("snapshot_policy_name", volume.SnapshotPolicyName)
}
if _, ok := d.GetOk("export_policy_ip"); ok {
d.Set("export_policy_ip", volume.ExportPolicyInfo.Ips)
}
if _, ok := d.GetOk("export_policy_nfs_version"); ok {
d.Set("export_policy_nfs_version", volume.ExportPolicyInfo.NfsVersion)
}
if _, ok := d.GetOk("export_policy_type"); ok {
d.Set("export_policy_type", volume.ExportPolicyInfo.PolicyType)
}
if d.Get("unit") != "GB" {
d.Set("size", convertSizeUnit(volume.Size.Size, volume.Size.Unit, d.Get("unit").(string)))
d.Set("unit", d.Get("unit").(string))
} else {
d.Set("size", volume.Size.Size)
d.Set("unit", volume.Size.Unit)
}
if d.Get("volume_protocol") == "cifs" {
if _, ok := d.GetOk("share_name"); ok {
if len(volume.ShareInfo) > 0 {
d.Set("share_name", volume.ShareInfo[0].ShareName)
}
}
if _, ok := d.GetOk("permission"); ok {
if len(volume.ShareInfo) > 0 {
if len(volume.ShareInfo[0].AccessControlList) > 0 {
d.Set("permission", volume.ShareInfo[0].AccessControlList[0].Permission)
}
}
}
if _, ok := d.GetOk("users"); ok {
if len(volume.ShareInfo) > 0 {
if len(volume.ShareInfo[0].AccessControlList) > 0 {
d.Set("users", volume.ShareInfo[0].AccessControlList[0].Users)
}
}
}
}
return nil
}
}
return fmt.Errorf("Error reading volume: volume doesn't exist")
}
func resourceFSXVolumeDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("Deleting volume: %#v", d)
client := meta.(*Client)
clientID := d.Get("client_id").(string)
volume := volumeRequest{}
var svm string
if v, ok := d.GetOk("svm_name"); ok {
svm = v.(string)
} else {
weInfo, err := client.getWorkingEnvironmentDetail(d, clientID)
if err != nil {
log.Printf("Cannot find working environment: %#v", err)
return fmt.Errorf("Cannot find working environment: %#v", err)
}
svm = weInfo.SvmName
}
volume.FileSystemID = d.Get("file_system_id").(string)
volume.SvmName = svm
volume.Name = d.Get("name").(string)
err := client.deleteVolume(volume, clientID)
if err != nil {
log.Printf("Error deleting volume: %#v", err)
return err
}
return nil
}
func resourceFSXVolumeUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("Updating volume: %#v", d)
client := meta.(*Client)
clientID := d.Get("client_id").(string)
volume := volumeRequest{}
var svm string
volume.Name = d.Get("name").(string)
volume.ExportPolicyInfo.PolicyType = d.Get("export_policy_type").(string)
if v, ok := d.GetOk("export_policy_ip"); ok {
ips := make([]string, 0, v.(*schema.Set).Len())
for _, x := range v.(*schema.Set).List() {
ips = append(ips, x.(string))
}
volume.ExportPolicyInfo.Ips = ips
}
weInfo, err := client.getWorkingEnvironmentDetail(d, clientID)
if err != nil {
log.Printf("Cannot find working environment: %#v", err)
return fmt.Errorf("Cannot find working environment: %#v", err)
}
if svm == "" {
svm = weInfo.SvmName
}
volume.SvmName = svm
if v, ok := d.GetOk("export_policy_nfs_version"); ok {
nfs := make([]string, 0, v.(*schema.Set).Len())
for _, x := range v.(*schema.Set).List() {
nfs = append(nfs, x.(string))
}
volume.ExportPolicyInfo.NfsVersion = nfs
}
if d.HasChange("permission") || d.HasChange("users") {
volume.ShareInfoUpdate.ShareName = d.Get("share_name").(string)
volume.ShareInfoUpdate.AccessControlList = make([]accessControlList, 1)
volume.ShareInfoUpdate.AccessControlList[0].Permission = d.Get("permission").(string)
users := make([]string, 0, d.Get("users").(*schema.Set).Len())
for _, x := range d.Get("users").(*schema.Set).List() {
users = append(users, x.(string))
}
volume.ShareInfoUpdate.AccessControlList[0].Users = users
}
err = client.updateVolume(volume, clientID)
if err != nil {
log.Printf("Error updating volume: %#v", err)
return err
}
return resourceFSXVolumeRead(d, meta)
}
func resourceFSXVolumeCustomizeDiff(diff *schema.ResourceDiff, v interface{}) error {
if diff.HasChange("volume_protocol") {
currentVolumeType, expectedVolumeType := diff.GetChange("volume_protocol")
if currentVolumeType.(string) == "" {
if expectedVolumeType.(string) == "nfs" {
if _, ok := diff.GetOk("export_policy_type"); !ok {
return fmt.Errorf("export_policy_type is required when volume type is nfs")
}
if _, ok := diff.GetOk("export_policy_ip"); !ok {
return fmt.Errorf("export_policy_ip is required when volume type is nfs")
}
if _, ok := diff.GetOk("export_policy_nfs_version"); !ok {
return fmt.Errorf("export_policy_nfs_version is required when volume type is nfs")
}
} else if expectedVolumeType.(string) == "cifs" {
if _, ok := diff.GetOk("share_name"); !ok {
return fmt.Errorf("share_name is required when volume type is cifs")
}
if _, ok := diff.GetOk("permission"); !ok {
return fmt.Errorf("permission is required when volume type is cifs")
}
if _, ok := diff.GetOk("users"); !ok {
return fmt.Errorf("users is required when volume type is cifs")
}
}
} else {
return fmt.Errorf("volume type %s can not be changed to %s", currentVolumeType.(string), expectedVolumeType.(string))
}
}
return nil
}