-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathanf.go
336 lines (305 loc) · 11.8 KB
/
anf.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
package cloudmanager
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/fatih/structs"
)
type anfVolumeRequest struct {
Size float64 `structs:"quotaInBytes"`
Name string `structs:"name"`
VolumePath string `structs:"volumePath"`
ProtocolTypes []string `structs:"protocolTypes"`
ServiceLevel string `structs:"serviceLevel"`
SubnetName string `structs:"subnetName"`
VirtualNetworkName string `structs:"virtualNetworkName"`
Location string `structs:"location"`
Rules []rule `structs:"rules"`
WorkingEnvironmentName string `structs:"workingEnvironmentName"`
}
// VolumePath is returned as creationToken
type anfVolumeResponse struct {
Size float64 `json:"quotaInBytes"`
Name string `json:"name"`
VolumePath string `json:"creationToken"`
ProtocolTypes []string `json:"protocolTypes"`
ServiceLevel string `json:"serviceLevel"`
SubnetName string `json:"subnet"`
Location string `json:"location"`
Rules map[string][]ruleResponse `json:"exportPolicy"`
WorkingEnvironmentName string `json:"workingEnvironmentName"`
}
type ruleResponse struct {
AllowedClients string `json:"allowedClients"`
Cifs bool `json:"cifs"`
Nfsv3 bool `json:"nfsv3"`
Nfsv41 bool `json:"nfsv41"`
RuleIndex int `json:"ruleIndex"`
UnixReadOnly bool `json:"unixReadOnly"`
UnixReadWrite bool `json:"unixReadWrite"`
}
type rule struct {
AllowedClients string `structs:"allowedClients"`
Cifs bool `structs:"cifs"`
Nfsv3 bool `structs:"nfsv3"`
Nfsv41 bool `structs:"nfsv41"`
RuleIndex int `structs:"ruleIndex"`
UnixReadOnly bool `structs:"unixReadOnly"`
UnixReadWrite bool `structs:"unixReadWrite"`
}
// AWS,ANF and GCP share this struct.
type cvsInfo struct {
AccountName string `structs:"accountName"`
AccountID string `structs:"accountID"`
CredentialsID string `structs:"credentialsID"`
SubscriptionName string `structs:"subscriptionName"`
NetAppAccountName string `structs:"netAppAccountName"`
ResourceGroupsName string `structs:"resourceGroupsName"`
CapacityPools string `structs:"capacityPools"`
VirtualNetworkName string `structs:"virtualNetworkName"`
SubnetName string `structs:"subnetName"`
}
func (c *Client) getAccountByName(name string, clientID string) (string, error) {
log.Print("getAccount")
baseURL := "/tenancy/account"
hostType := "CloudManagerHost"
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("getAccount request failed ", statusCode)
return "", err
}
responseError := apiResponseChecker(statusCode, response, "getAccount")
if responseError != nil {
return "", responseError
}
var results []accountIDResult
if err := json.Unmarshal(response, &results); err != nil {
log.Print("Failed to unmarshall response from getAccount ", err)
return "", err
}
if len(results) == 0 {
return "", fmt.Errorf("no account exists")
}
if name != "" {
for _, result := range results {
if name == result.AccountName {
return result.AccountID, nil
}
}
return "", fmt.Errorf("account: %s not found", name)
}
// return the first account if name is not provided.
return results[0].AccountID, nil
}
func (c *Client) createANFVolume(vol anfVolumeRequest, info cvsInfo, clientID string) error {
baseURL, err := c.getCVSAPIRoot(info.AccountName, vol.WorkingEnvironmentName, clientID)
if err != nil {
return err
}
subscription, err := c.getSubscription(baseURL, info.SubscriptionName, clientID)
if err != nil {
return err
}
subnet, err := c.getSubnetID(fmt.Sprintf("%s/subscriptions/%s", baseURL, subscription), info.VirtualNetworkName, info.SubnetName, vol.Location, clientID)
if err != nil {
return err
}
baseURL = fmt.Sprintf("%s/subscriptions/%s/resourceGroups/%s/netAppAccounts/%s/capacityPools/%s/volumes", baseURL, subscription, info.ResourceGroupsName, info.NetAppAccountName, info.CapacityPools)
hostType := "CVSHost"
param := structs.Map(vol)
param["subnetId"] = subnet
statusCode, response, _, err := c.CallAPIMethod("POST", baseURL, param, c.Token, hostType, clientID)
if err != nil {
log.Print("createANFVolume request failed ", statusCode)
return err
}
responseError := apiResponseChecker(statusCode, response, "createANFVolume")
if responseError != nil {
return responseError
}
return nil
}
func (c *Client) getANFVolume(vol anfVolumeRequest, info cvsInfo, clientID string) (anfVolumeResponse, error) {
baseURL, err := c.getCVSAPIRoot(info.AccountName, vol.WorkingEnvironmentName, clientID)
if err != nil {
return anfVolumeResponse{}, err
}
subscription, err := c.getSubscription(baseURL, info.SubscriptionName, clientID)
if err != nil {
return anfVolumeResponse{}, err
}
subnet, err := c.getSubnetID(fmt.Sprintf("%s/subscriptions/%s", baseURL, subscription), info.VirtualNetworkName, info.SubnetName, vol.Location, clientID)
if err != nil {
return anfVolumeResponse{}, err
}
baseURL = fmt.Sprintf("%s/subscriptions/%s/resourceGroups/%s/netAppAccounts/%s/capacityPools/%s/volumes/%s", baseURL, subscription, info.ResourceGroupsName, info.NetAppAccountName, info.CapacityPools, vol.Name)
hostType := "CVSHost"
param := structs.Map(vol)
param["subnetId"] = subnet
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("getANFVolume request failed ", statusCode)
return anfVolumeResponse{}, err
}
responseError := apiResponseChecker(statusCode, response, "getANFVolume")
if responseError != nil {
return anfVolumeResponse{}, responseError
}
var result anfVolumeResponse
if err := json.Unmarshal(response, &result); err != nil {
log.Print("Failed to unmarshall response from getANFVolume ", err)
return anfVolumeResponse{}, err
}
return result, nil
}
func (c *Client) getCVSWorkingEnvironment(accountID string, WorkingEnvironment string, clientID string) (string, string, error) {
if c.Token == "" {
accesTokenResult, err := c.getAccessToken()
if err != nil {
log.Print("Not able to get the access token.")
return "", "", err
}
c.Token = accesTokenResult.Token
}
baseURL := fmt.Sprintf("/cvs/accounts/%s/working-environments", accountID)
hostType := "CVSHost"
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("getCVSWorkingEnvironment request failed ", statusCode)
return "", "", err
}
responseError := apiResponseChecker(statusCode, response, "getCVSWorkingEnvironment")
if responseError != nil {
return "", "", responseError
}
var results []map[string]interface{}
if err := json.Unmarshal(response, &results); err != nil {
log.Print("Failed to unmarshall response from getCVSWorkingEnvironment ", err)
return "", "", err
}
for _, result := range results {
if strings.ToLower(result["name"].(string)) == strings.ToLower(WorkingEnvironment) {
return result["credentialsId"].(string), result["provider"].(string), nil
}
}
return "", "", fmt.Errorf(" working environment: %s doesn't exist", WorkingEnvironment)
}
func (c *Client) getCVSAPIRoot(accountName string, workingEnvironment string, clientID string) (string, error) {
if c.Token == "" {
accesTokenResult, err := c.getAccessToken()
if err != nil {
log.Print("Not able to get the access token.")
return "", err
}
c.Token = accesTokenResult.Token
}
accountID, err := c.getAccountByName(accountName, clientID)
if err != nil {
return "", err
}
credentialsID, provider, err := c.getCVSWorkingEnvironment(accountID, workingEnvironment, clientID)
if err != nil {
return "", err
}
if provider == "azure" {
return fmt.Sprintf("/cvs/azure/accounts/%s/credentials/%s", accountID, credentialsID), nil
} else if provider == "gcp" {
return fmt.Sprintf("/cvs/gcp/accounts/%s/credentials/%s", accountID, credentialsID), nil
} else if provider == "aws" {
return fmt.Sprintf("/cvs/aws/accounts/%s/credentials/%s", accountID, credentialsID), nil
} else {
return "", fmt.Errorf("working environment's provider is not supported or not found")
}
}
func (c *Client) getSubscription(baseURL string, subscription string, clientID string) (string, error) {
if c.Token == "" {
accesTokenResult, err := c.getAccessToken()
if err != nil {
log.Print("Not able to get the access token.")
return "", err
}
c.Token = accesTokenResult.Token
}
baseURL = fmt.Sprintf("%s/subscriptions", baseURL)
hostType := "CVSHost"
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("getSubscriptions request failed ", statusCode)
return "", err
}
responseError := apiResponseChecker(statusCode, response, "getSubscriptions")
if responseError != nil {
return "", responseError
}
var results []map[string]interface{}
if err := json.Unmarshal(response, &results); err != nil {
log.Print("Failed to unmarshall response from getSubscriptions ", err)
return "", err
}
for _, result := range results {
if strings.ToLower(subscription) == strings.ToLower(result["displayName"].(string)) {
return result["subscriptionId"].(string), nil
}
}
return "", fmt.Errorf("subscription: %s doesn't exist", subscription)
}
func (c *Client) getSubnetID(baseURL string, virtualNetwork string, subnet string, location string, clientID string) (string, error) {
if c.Token == "" {
accesTokenResult, err := c.getAccessToken()
if err != nil {
log.Print("Not able to get the access token.")
return "", err
}
c.Token = accesTokenResult.Token
}
baseURL = fmt.Sprintf("%s/virtualNetworks?location=%s", baseURL, location)
hostType := "CVSHost"
statusCode, response, _, err := c.CallAPIMethod("GET", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("getSubnetID request failed ", statusCode)
return "", err
}
responseError := apiResponseChecker(statusCode, response, "getSubnetID")
if responseError != nil {
return "", responseError
}
var results []interface{}
if err := json.Unmarshal(response, &results); err != nil {
log.Print("Failed to unmarshall response from getSubnetID ", err)
return "", err
}
for _, result := range results {
if strings.ToLower(virtualNetwork) == strings.ToLower(result.(map[string]interface{})["name"].(string)) {
subnetResults := result.(map[string]interface{})["subnets"].([]interface{})
for _, subnetResult := range subnetResults {
if strings.ToLower(subnet) == strings.ToLower(subnetResult.(map[string]interface{})["name"].(string)) {
return subnetResult.(map[string]interface{})["subnetId"].(string), nil
}
}
}
}
return "", fmt.Errorf("subnet: %s doesn't exist", subnet)
}
func (c *Client) deleteANFVolume(vol anfVolumeRequest, info cvsInfo, clientID string) error {
baseURL, err := c.getCVSAPIRoot(info.AccountName, vol.WorkingEnvironmentName, clientID)
if err != nil {
return err
}
subscription, err := c.getSubscription(baseURL, info.SubscriptionName, clientID)
if err != nil {
return err
}
baseURL = fmt.Sprintf("%s/subscriptions/%s/resourceGroups/%s/netAppAccounts/%s/capacityPools/%s/volumes/%s", baseURL, subscription, info.ResourceGroupsName, info.NetAppAccountName, info.CapacityPools, vol.Name)
hostType := "CVSHost"
statusCode, response, _, err := c.CallAPIMethod("DELETE", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Print("deleteANFVolume request failed ", statusCode)
return err
}
responseError := apiResponseChecker(statusCode, response, "deleteANFVolume")
if responseError != nil {
return responseError
}
return nil
}