forked from NetApp/terraform-provider-netapp-cloudmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume.go
508 lines (465 loc) · 17.8 KB
/
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package cloudmanager
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"github.com/fatih/structs"
"github.com/hashicorp/terraform/helper/schema"
)
type volumeRequest struct {
Name string `structs:"name"`
WorkingEnvironmentID string `structs:"workingEnvironmentId"`
SvmName string `structs:"svmName"`
AggregateName string `structs:"aggregateName"`
Size size `structs:"size"`
SnapshotPolicyName string `structs:"snapshotPolicyName,omitempty"`
EnableThinProvisioning bool `structs:"enableThinProvisioning"`
EnableCompression bool `structs:"enableCompression"`
EnableDeduplication bool `structs:"enableDeduplication"`
ExportPolicyInfo exportPolicyInfo `structs:"exportPolicyInfo,omitempty"`
ID string `structs:"uuid"`
NewAggregate bool `structs:"newAggregate"`
CapacityTier string `structs:"capacityTier,omitempty"`
ProviderVolumeType string `structs:"providerVolumeType"`
TieringPolicy string `structs:"tieringPolicy,omitempty"`
NumOfDisks float64 `structs:"maxNumOfDisksApprovedToAdd,omitempty"`
AutoVsaCapacityManagement bool `structs:"autoVsaCapacityManagement"`
DiskSize size `structs:"diskSize,omitempty"`
Iops int `structs:"iops,omitempty"`
Throughput int `structs:"throughput,omitempty"`
WorkingEnvironmentType string `structs:"workingEnvironmentType,omitempty"`
ShareInfo shareInfoRequest `structs:"shareInfo,omitempty"`
ShareInfoUpdate shareInfoUpdateRequest `structs:"shareInfo,omitempty"`
IscsiInfo iscsiInfo `structs:"iscsiInfo,omitempty"`
FileSystemID string `structs:"fileSystemId,omitempty"`
TenantID string `structs:"tenantId,omitempty"`
EnableStorageEfficiency bool `structs:"enableStorageEfficiency,omitempty"`
}
type volumeResponse struct {
Name string `json:"name"`
SvmName string `json:"svmName"`
AggregateName string `json:"aggregateName"`
Size size `json:"size"`
SnapshotPolicyName string `json:"snapshotPolicy"`
EnableThinProvisioning bool `json:"thinProvisioning"`
EnableCompression bool `json:"compression"`
EnableDeduplication bool `json:"deduplication"`
ExportPolicyInfo exportPolicyInfo `json:"exportPolicyInfo"`
ID string `json:"uuid"`
CapacityTier string `json:"capacityTier,omitempty"`
TieringPolicy string `json:"tieringPolicy,omitempty"`
ProviderVolumeType string `json:"providerVolumeType"`
ShareInfo []shareInfoResponse `json:"shareInfo"`
MountPoint string `json:"mountPoint"`
IscsiEnabled bool `json:"iscsiEnabled"`
}
type exportPolicyInfo struct {
Name string `structs:"name,omitempty"`
PolicyType string `structs:"policyType,omitempty"`
Ips []string `structs:"ips,omitempty"`
NfsVersion []string `structs:"nfsVersion,omitempty"`
}
type size struct {
Size float64 `structs:"size"`
Unit string `structs:"unit"`
}
type quoteRequest struct {
Size size `structs:"size"`
WorkingEnvironmentID string `structs:"workingEnvironmentId"`
SvmName string `structs:"svmName"`
AggregateName string `structs:"aggregateName,omitempty"`
EnableThinProvisioning bool `structs:"enableThinProvisioning"`
EnableCompression bool `structs:"enableCompression"`
EnableDeduplication bool `structs:"enableDeduplication"`
ReplicationFlow bool `structs:"replicationFlow"`
ExportPolicyInfo exportPolicyInfo `structs:"exportPolicyInfo,omitempty"`
SnapshotPolicyName string `structs:"snapshotPolicyName"`
Name string `structs:"name"`
CapacityTier string `structs:"capacityTier,omitempty"`
ProviderVolumeType string `structs:"providerVolumeType"`
TieringPolicy string `structs:"tieringPolicy,omitempty"`
VerifyNameUniqueness bool `structs:"verifyNameUniqueness"`
Iops int `structs:"iops,omitempty"`
Throughput int `structs:"throughput,omitempty"`
WorkingEnvironmentType string `structs:"workingEnvironmentType"`
}
type shareInfoRequest struct {
ShareName string `structs:"shareName,omitempty"`
AccessControl accessControl `structs:"accessControl,omitempty"`
}
type accessControl struct {
Permission string `structs:"permission,omitempty"`
Users []string `structs:"users,omitempty"`
}
// cifs volume response has different strcuture comparing to cifs create.
type shareInfoResponse struct {
ShareName string `json:"shareName"`
AccessControlList []accessControlListResponse `json:"accessControlList"`
}
type accessControlListResponse struct {
Permission string `json:"permission"`
Users []string `json:"users"`
}
type shareInfoUpdateRequest struct {
ShareName string `structs:"shareName,omitempty"`
AccessControlList []accessControlList `structs:"accessControlList,omitempty"`
}
type accessControlList struct {
Permission string `structs:"permission,omitempty"`
Users []string `structs:"users,omitempty"`
}
type iscsiInfo struct {
OsName string `structs:"osName,omitempty"`
IgroupCreationRequest struct {
Initiators []string `structs:"initiators,omitempty"`
IgroupName string `structs:"igroupName,omitempty"`
} `structs:"igroupCreationRequest,omitempty"`
Igroups []string `structs:"igroups,omitempty"`
}
type initiator struct {
AliasName string `structs:"aliasName,omitempty"`
Iqn string `structs:"iqn,omitempty"`
WorkingEnvironmentID string `structs:"workingEnvironmentId,omitempty"`
SvmName string `structs:"svmName,omitempty"`
WorkingEnvironmentType string `structs:"workingEnvironmentType,omitempty"`
}
func (c *Client) createVolume(vol volumeRequest, createAggregateIfNotFound bool, clientID string) error {
var id string
if vol.FileSystemID != "" {
id = vol.FileSystemID
} else {
id = vol.WorkingEnvironmentID
}
baseURL, _, err := c.getAPIRoot(id, clientID)
if err != nil {
return err
}
if vol.FileSystemID != "" {
baseURL = fmt.Sprintf("%s/volumes", baseURL)
} else {
baseURL = fmt.Sprintf("%s/volumes?createAggregateIfNotFound=%s", baseURL, strconv.FormatBool(createAggregateIfNotFound))
}
hostType := "CloudManagerHost"
param := structs.Map(vol)
statusCode, response, onCloudRequestID, err := c.CallAPIMethod("POST", baseURL, param, c.Token, hostType, clientID)
if err != nil {
log.Print("createVolume request failed ", statusCode)
return err
}
responseError := apiResponseChecker(statusCode, response, "createVolume")
if responseError != nil {
return responseError
}
err = c.waitOnCompletion(onCloudRequestID, "volume", "create", 40, 10, clientID)
if err != nil {
return err
}
return nil
}
func (c *Client) deleteVolume(vol volumeRequest, clientID string) error {
var id string
if vol.FileSystemID != "" {
id = vol.FileSystemID
} else {
id = vol.WorkingEnvironmentID
}
baseURL, _, err := c.getAPIRoot(id, clientID)
if err != nil {
return err
}
baseURL = fmt.Sprintf("%s/volumes/%s/%s/%s", baseURL, id, vol.SvmName, vol.Name)
hostType := "CloudManagerHost"
statusCode, response, onCloudRequestID, err := c.CallAPIMethod("DELETE", baseURL, nil, c.Token, hostType, clientID)
responseError := apiResponseChecker(statusCode, response, "deleteVolume")
if responseError != nil {
return responseError
}
log.Print("Wait for volume deletion.")
err = c.waitOnCompletion(onCloudRequestID, "volume", "delete", 10, 60, clientID)
if err != nil {
log.Print("deleteVolume request failed ", statusCode)
return err
}
return nil
}
func (c *Client) getVolume(vol volumeRequest, clientID string) ([]volumeResponse, error) {
var result []volumeResponse
hostType := "CloudManagerHost"
var id string
if vol.FileSystemID != "" {
id = vol.FileSystemID
} else {
id = vol.WorkingEnvironmentID
}
baseURL, _, err := c.getAPIRoot(id, clientID)
if err != nil {
return result, err
}
if vol.FileSystemID != "" {
baseURL = fmt.Sprintf("%s/volumes?fileSystemId=%s", baseURL, id)
} else {
baseURL = fmt.Sprintf("%s/volumes?workingEnvironmentId=%s", baseURL, id)
}
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("getVolume request failed ", statusCode)
return result, err
}
responseError := apiResponseChecker(statusCode, response, "getVolume")
if responseError != nil {
return result, responseError
}
if err := json.Unmarshal(response, &result); err != nil {
log.Print("Failed to unmarshall response from getVolume ", err)
return result, err
}
return result, nil
}
func (c *Client) getVolumeForOnPrem(vol volumeRequest, clientID string) ([]volumeResponse, error) {
var result []volumeResponse
hostType := "CloudManagerHost"
baseURL := fmt.Sprintf("/occm/api/onprem/volumes?workingEnvironmentId=%s", vol.WorkingEnvironmentID)
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("getVolumeForOnPrem request failed ", statusCode)
return result, err
}
responseError := apiResponseChecker(statusCode, response, "getVolumeForOnPrem")
if responseError != nil {
return result, responseError
}
if err := json.Unmarshal(response, &result); err != nil {
log.Print("Failed to unmarshall response from getVolumeForOnPrem ", err)
return result, err
}
return result, nil
}
func (c *Client) getVolumeByID(request volumeRequest, clientID string) (volumeResponse, error) {
res, err := c.getVolume(request, clientID)
if err != nil {
return volumeResponse{}, err
}
for _, vol := range res {
if vol.ID == request.ID {
return vol, nil
}
}
return volumeResponse{}, fmt.Errorf("Error fetching volume: volume doesn't exist")
}
func (c *Client) updateVolume(request volumeRequest, clientID string) error {
hostType := "CloudManagerHost"
var id string
if request.FileSystemID != "" {
id = request.FileSystemID
} else {
id = request.WorkingEnvironmentID
}
baseURL, _, err := c.getAPIRoot(id, clientID)
if err != nil {
return err
}
baseURL = fmt.Sprintf("%s/volumes/%s/%s/%s", baseURL, id, request.SvmName, request.Name)
params := structs.Map(request)
statusCode, response, _, err := c.CallAPIMethod("PUT", baseURL, params, c.Token, hostType, clientID)
responseError := apiResponseChecker(statusCode, response, "updateVolume")
if responseError != nil {
return responseError
}
if err != nil {
log.Print("updateVolume request failed ", statusCode)
return err
}
return nil
}
func (c *Client) quoteVolume(request quoteRequest, clientID string) (map[string]interface{}, error) {
hostType := "CloudManagerHost"
baseURL, _, err := c.getAPIRoot(request.WorkingEnvironmentID, clientID)
if err != nil {
return nil, err
}
baseURL = fmt.Sprintf("%s/volumes/quote", baseURL)
params := structs.Map(request)
statusCode, response, _, err := c.CallAPIMethod("POST", baseURL, params, c.Token, hostType, clientID)
if err != nil {
log.Print("quoteVolume request failed ", statusCode)
return nil, err
}
responseError := apiResponseChecker(statusCode, response, "quoteVolume")
if responseError != nil {
return nil, responseError
}
var result map[string]interface{}
json.Unmarshal(response, &result)
return result, nil
}
func (c *Client) createInitiator(request initiator, clientID string) error {
hostType := "CloudManagerHost"
baseURL, _, err := c.getAPIRoot(request.WorkingEnvironmentID, clientID)
if err != nil {
return err
}
baseURL = fmt.Sprintf("%s/volumes/initiator", baseURL)
params := structs.Map(request)
statusCode, response, _, err := c.CallAPIMethod("POST", baseURL, params, c.Token, hostType, clientID)
if err != nil {
log.Print("createInitiator request failed ", statusCode)
return err
}
responseError := apiResponseChecker(statusCode, response, "createInitiator")
if responseError != nil {
return responseError
}
return nil
}
func (c *Client) getInitiator(request initiator, clientID string) ([]initiator, error) {
hostType := "CloudManagerHost"
baseURL, _, err := c.getAPIRoot(request.WorkingEnvironmentID, clientID)
var result []initiator
if err != nil {
return result, err
}
baseURL = fmt.Sprintf("%s/volumes/initiator", baseURL)
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("createInitiator request failed ", statusCode)
return result, err
}
responseError := apiResponseChecker(statusCode, response, "createInitiator")
if responseError != nil {
return result, responseError
}
if err := json.Unmarshal(response, &result); err != nil {
log.Print("Failed to unmarshall response from getInitiator ", err)
return result, err
}
return result, nil
}
type igroup struct {
IgroupName string `json:"igroupName"`
OsType string `json:"osType"`
PortsetName string `json:"portsetName"`
IgroupType string `json:"igroupType"`
Initiators []string `json:"initiators"`
WorkingEnvironmentID string `structs:"workingEnvironmentId"`
SvmName string `structs:"svmName"`
WorkingEnvironmentType string `structs:"workingEnvironmentType,omitempty"`
}
func (c *Client) getIgroups(request igroup, clientID string) ([]igroup, error) {
hostType := "CloudManagerHost"
baseURL, _, err := c.getAPIRoot(request.WorkingEnvironmentID, clientID)
var result []igroup
if err != nil {
return result, err
}
baseURL = fmt.Sprintf("%s/volumes/igroups/%s/%s", baseURL, request.WorkingEnvironmentID, request.SvmName)
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("getIgroups request failed ", statusCode)
return result, err
}
responseError := apiResponseChecker(statusCode, response, "getIgroups")
if responseError != nil {
return result, responseError
}
if err := json.Unmarshal(response, &result); err != nil {
log.Print("Failed to unmarshall response from getIgroups ", err)
return result, err
}
return result, nil
}
func (c *Client) checkCifsExists(id string, svm string, clientID string) (bool, error) {
hostType := "CloudManagerHost"
baseURL, _, err := c.getAPIRoot(id, clientID)
var result []map[string]interface{}
if err != nil {
return false, err
}
baseURL = fmt.Sprintf("%s/working-environments/%s/cifs?svm=%s", baseURL, id, svm)
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("chkeckCifsExists request failed ", statusCode)
return false, err
}
responseError := apiResponseChecker(statusCode, response, "getIgroups")
if responseError != nil {
return false, responseError
}
if err := json.Unmarshal(response, &result); err != nil {
log.Print("Failed to unmarshall response from chkeckCifsExists ", err)
return false, err
}
if len(result) > 0 {
return true, nil
}
return false, nil
}
func convertSizeUnit(size float64, from string, to string) float64 {
if strings.ToUpper(from) == "GB" && strings.ToUpper(to) == "GB" {
return size
}
if strings.ToUpper(from) == "GB" && strings.ToUpper(to) == "TB" {
size = size / 1024
}
if strings.ToUpper(from) == "GB" && strings.ToUpper(to) == "B" {
size = size * 1073741824
}
if strings.ToUpper(from) == "B" && strings.ToUpper(to) == "GB" {
size = size / 1073741824
}
return size
}
func (c *Client) setCommonAttributes(d *schema.ResourceData, volume *volumeRequest, clientID string) error {
volume.Name = d.Get("name").(string)
volume.Size.Size = d.Get("size").(float64)
volume.Size.Unit = d.Get("unit").(string)
volume.SnapshotPolicyName = d.Get("snapshot_policy_name").(string)
var weid string
if fsid, ok := d.GetOk("file_system_id"); ok {
weid = fsid.(string)
} else {
weid = volume.WorkingEnvironmentID
}
if v, ok := d.GetOk("export_policy_type"); ok {
volume.ExportPolicyInfo.PolicyType = v.(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
}
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
}
volumeProtocol := d.Get("volume_protocol").(string)
if volumeProtocol == "cifs" {
exist, err := c.checkCifsExists(weid, volume.SvmName, clientID)
if err != nil {
return err
}
if !exist {
return fmt.Errorf("cifs has not been set up yet")
}
if v, ok := d.GetOk("share_name"); ok {
volume.ShareInfo.ShareName = v.(string)
}
if v, ok := d.GetOk("permission"); ok {
volume.ShareInfo.AccessControl.Permission = v.(string)
}
if v, ok := d.GetOk("users"); ok {
users := make([]string, 0, v.(*schema.Set).Len())
for _, x := range v.(*schema.Set).List() {
users = append(users, x.(string))
}
volume.ShareInfo.AccessControl.Users = users
}
}
return nil
}