-
Notifications
You must be signed in to change notification settings - Fork 27
/
cvo_gcp.go
344 lines (291 loc) · 13 KB
/
cvo_gcp.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
package cloudmanager
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/fatih/structs"
"github.com/hashicorp/terraform/helper/schema"
)
// GCPLicenseTypes is the GCP License types
var GCPLicenseTypes = []string{"gcp-cot-standard-paygo", "gcp-cot-explore-paygo", "gcp-cot-premium-paygo",
"gcp-cot-premium-byol", "gcp-ha-cot-standard-paygo", "gcp-ha-cot-premium-paygo", "gcp-ha-cot-explore-paygo",
"gcp-ha-cot-premium-byol", "capacity-paygo", "ha-capacity-paygo"}
// createCVOGCPDetails the users input for creating a CVO
type createCVOGCPDetails struct {
Name string `structs:"name"`
DataEncryptionType string `structs:"dataEncryptionType"`
WorkspaceID string `structs:"tenantId,omitempty"`
Region string `structs:"region"`
GCPServiceAccount string `structs:"gcpServiceAccount,omitempty"`
VpcID string `structs:"vpcId"`
SvmPassword string `structs:"svmPassword"`
SvmName string `structs:"svmName,omitempty"`
VsaMetadata vsaMetadata `structs:"vsaMetadata"`
GCPVolumeSize diskSize `structs:"gcpVolumeSize"`
GCPVolumeType string `structs:"gcpVolumeType"`
SubnetID string `structs:"subnetId"`
SubnetPath string `structs:"subnetPath"`
Project string `structs:"project"`
CapacityTier string `structs:"capacityTier,omitempty"`
TierLevel string `structs:"tierLevel,omitempty"`
SaasSubscriptionID string `structs:"saasSubscriptionId,omitempty"`
NssAccount string `structs:"nssAccount,omitempty"`
WormRequest wormRequest `structs:"wormRequest,omitempty"`
WritingSpeedState string `structs:"writingSpeedState,omitempty"`
SerialNumber string `structs:"serialNumber,omitempty"`
GCPLabels []gcpLabels `structs:"gcpLabels,omitempty"`
GcpEncryptionParameters gcpEncryptionParameters `structs:"gcpEncryptionParameters,omitempty"`
FirewallRule string `structs:"firewallRule,omitempty"`
FirewallTagNameRule string `structs:"firewallTagNameRule,omitempty"`
FirewallIPRanges bool `structs:"firewallIpRanges"`
BackupVolumesToCbs bool `structs:"backupVolumesToCbs"`
EnableCompliance bool `structs:"enableCompliance"`
IsHA bool
HAParams haParamsGCP `structs:"haParams,omitempty"`
FlashCache bool `structs:"flashCache"`
}
// gcpLabels the input for requesting a CVO
type gcpLabels struct {
LabelKey string `structs:"labelKey"`
LabelValue string `structs:"labelValue,omitempty"`
}
// gcpSVMs the input for adding SVMs to a CVO HA
type gcpSVM struct {
SvmName string `structs:"svmName"`
// more parameters might be added in the future
}
// gcpEncryptionParameters the input for requesting a CVO
type gcpEncryptionParameters struct {
Key string `structs:"key,omitempty"`
}
// haParamsGCP the input for requesting a CVO
type haParamsGCP struct {
PlatformSerialNumberNode1 string `structs:"platformSerialNumberNode1,omitempty"`
PlatformSerialNumberNode2 string `structs:"platformSerialNumberNode2,omitempty"`
Node1Zone string `structs:"node1Zone,omitempty"`
Node2Zone string `structs:"node2Zone,omitempty"`
MediatorZone string `structs:"mediatorZone,omitempty"`
VPC0NodeAndDataConnectivity string `structs:"vpc0NodeAndDataConnectivity,omitempty"`
VPC1ClusterConnectivity string `structs:"vpc1ClusterConnectivity,omitempty"`
VPC2HAConnectivity string `structs:"vpc2HAConnectivity,omitempty"`
VPC3DataReplication string `structs:"vpc3DataReplication,omitempty"`
Subnet0NodeAndDataConnectivity string `structs:"subnet0NodeAndDataConnectivity,omitempty"`
Subnet1ClusterConnectivity string `structs:"subnet1ClusterConnectivity,omitempty"`
Subnet2HAConnectivity string `structs:"subnet2HAConnectivity,omitempty"`
Subnet3DataReplication string `structs:"subnet3DataReplication,omitempty"`
VPC0FirewallRuleName string `structs:"vpc0FirewallRuleName,omitempty"`
VPC1FirewallRuleName string `structs:"vpc1FirewallRuleName,omitempty"`
VPC2FirewallRuleName string `structs:"vpc2FirewallRuleName,omitempty"`
VPC3FirewallRuleName string `structs:"vpc3FirewallRuleName,omitempty"`
VPC0FirewallRuleTagName string `structs:"vpc0FirewallRuleTagName,omitempty"`
VPC1FirewallRuleTagName string `structs:"vpc1FirewallRuleTagName,omitempty"`
VPC2FirewallRuleTagName string `structs:"vpc2FirewallRuleTagName,omitempty"`
VPC3FirewallRuleTagName string `structs:"vpc3FirewallRuleTagName,omitempty"`
}
func (c *Client) createCVOGCP(cvoDetails createCVOGCPDetails, clientID string) (cvoResult, error) {
log.Printf("\n\ncreateCVO %s client_id %s", cvoDetails.Name, clientID)
accessTokenResult, err := c.getAccessToken()
if err != nil {
log.Print("in createCVO request, failed to get AccessToken")
return cvoResult{}, err
}
c.Token = accessTokenResult.Token
if cvoDetails.WorkspaceID == "" {
tenantID, err := c.getTenant(clientID)
if err != nil {
log.Print("getTenant request failed ", err)
return cvoResult{}, err
}
log.Print("tenant result ", tenantID)
cvoDetails.WorkspaceID = tenantID
}
if cvoDetails.NssAccount == "" && (cvoDetails.VsaMetadata.LicenseType == "gcp-cot-premium-byol" || cvoDetails.VsaMetadata.LicenseType == "gcp-ha-cot-premium-byol") && !strings.HasPrefix(cvoDetails.SerialNumber, "Eval-") {
nssAccount, err := c.getNSS(clientID)
if err != nil {
log.Print("getNSS request failed ", err)
return cvoResult{}, err
}
log.Printf("%s %s getNSS result %s", cvoDetails.Name, clientID, nssAccount)
cvoDetails.NssAccount = nssAccount
}
baseURL := getAPIRootForWorkingEnvironment(cvoDetails.IsHA, "")
hostType := "CloudManagerHost"
params := structs.Map(cvoDetails)
log.Printf("Create GCP CVO: %#v", params)
statusCode, response, onCloudRequestID, err := c.CallAPIMethod("POST", baseURL, params, c.Token, hostType, clientID)
if err != nil {
log.Print("createCVO request failed ", statusCode)
return cvoResult{}, err
}
responseError := apiResponseChecker(statusCode, response, "createCVO")
if responseError != nil {
return cvoResult{}, responseError
}
log.Print("retries ", c.Retries)
var CreationRetries int
if !cvoDetails.IsHA {
CreationRetries = c.Retries
} else {
CreationRetries = c.Retries + 30
}
err = c.waitOnCompletion(onCloudRequestID, "CVO", "create", CreationRetries, 60, clientID)
if err != nil {
return cvoResult{}, err
}
var result cvoResult
if err := json.Unmarshal(response, &result); err != nil {
log.Printf("Response: %#v", response)
log.Print("Failed to unmarshall response from createCVO ", err)
return cvoResult{}, err
}
return result, nil
}
func (c *Client) deleteCVOGCP(id string, isHA bool, clientID string) error {
log.Printf("deleteCVO: id %s client %s", id, clientID)
accessTokenResult, err := c.getAccessToken()
if err != nil {
log.Print("in deleteCVO request, failed to get AccessToken")
return err
}
c.Token = accessTokenResult.Token
baseURL := getAPIRootForWorkingEnvironment(isHA, id)
hostType := "CloudManagerHost"
statusCode, response, onCloudRequestID, err := c.CallAPIMethod("DELETE", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Printf("deleteCVO %s request failed %#v", id, statusCode)
return err
}
responseError := apiResponseChecker(statusCode, response, "deleteCVO")
if responseError != nil {
return responseError
}
err = c.waitOnCompletion(onCloudRequestID, "CVO", "delete", 40, 60, clientID)
if err != nil {
return err
}
return nil
}
// This is used on GCP CVO HA only
func (c *Client) addSVMtoCVO(id string, clientID string, svmName string) error {
log.Printf("addSVMtoCVO: id %s client %s svm %s", id, clientID, svmName)
accessTokenResult, err := c.getAccessToken()
if err != nil {
log.Print("In addSVMtoCVO request, failed to get AccessToken")
return err
}
c.Token = accessTokenResult.Token
// GCP CVO SVM add and deletion only support HA
baseURL := getAPIRootForWorkingEnvironment(true, id) + "/svm"
hostType := "CloudManagerHost"
var svm gcpSVM
svm.SvmName = svmName
params := structs.Map(svm)
log.Printf("\taddSVMtoCVO params: %#v", params)
statusCode, response, onCloudRequestID, err := c.CallAPIMethod("POST", baseURL, params, c.Token, hostType, clientID)
if err != nil {
log.Print("addSVMtoCVO request failed ", statusCode)
return err
}
responseError := apiResponseChecker(statusCode, response, "addSVMtoCVO")
if responseError != nil {
return responseError
}
err = c.waitOnCompletion(onCloudRequestID, "CVO_SVM", "add", 60, 60, clientID)
return err
}
func (c *Client) deleteSVMfromCVO(id string, clientID string, svmName string) error {
log.Printf("deleteSVMfromCVO: id %s client %s svm %s", id, clientID, svmName)
accessTokenResult, err := c.getAccessToken()
if err != nil {
log.Print("In deleteSVMfromCVO request, failed to get AccessToken")
return err
}
c.Token = accessTokenResult.Token
// GCP CVO SVM add and deletion only support HA
baseURL := getAPIRootForWorkingEnvironment(true, id)
baseURL = fmt.Sprintf("%s/svm/%s", baseURL, svmName)
log.Print("\tDelete svm url: ", baseURL)
hostType := "CloudManagerHost"
statusCode, response, onCloudRequestID, err := c.CallAPIMethod("DELETE", baseURL, nil, c.Token, hostType, clientID)
if err != nil {
log.Printf("deleteSVMfromCVO %s request failed %#v", id, statusCode)
return err
}
responseError := apiResponseChecker(statusCode, response, "deleteSVMfromCVO")
if responseError != nil {
return responseError
}
err = c.waitOnCompletion(onCloudRequestID, "CVO_SVM", "delete", 40, 60, clientID)
return err
}
// expandGCPLabels converts set to gcpLabels struct
func expandGCPLabels(set *schema.Set) []gcpLabels {
labels := []gcpLabels{}
for _, v := range set.List() {
label := v.(map[string]interface{})
gcpLabel := gcpLabels{}
gcpLabel.LabelKey = label["label_key"].(string)
gcpLabel.LabelValue = label["label_value"].(string)
labels = append(labels, gcpLabel)
}
return labels
}
// expandGCPLabels converts set to gcpLabels struct
func expandGCPSVMs(set *schema.Set) []gcpSVM {
svms := []gcpSVM{}
for _, v := range set.List() {
svm := v.(map[string]interface{})
gcpSVM := gcpSVM{}
gcpSVM.SvmName = svm["svm_name"].(string)
svms = append(svms, gcpSVM)
}
return svms
}
// expandGCPLabelsToUseTags
func expandGCPLabelsToUserTags(set *schema.Set) []userTags {
tags := []userTags{}
for _, v := range set.List() {
label := v.(map[string]interface{})
userTag := userTags{}
userTag.TagKey = label["label_key"].(string)
userTag.TagValue = label["label_value"].(string)
tags = append(tags, userTag)
}
return tags
}
// validateCVOGCPParams validates params
func validateCVOGCPParams(cvoDetails createCVOGCPDetails) error {
if cvoDetails.VsaMetadata.UseLatestVersion && cvoDetails.VsaMetadata.OntapVersion != "latest" {
return fmt.Errorf("ontap_version parameter not required when having use_latest_version as true")
}
// by Node byol license
if cvoDetails.IsHA && cvoDetails.VsaMetadata.LicenseType == "gcp-ha-cot-premium-byol" {
if cvoDetails.HAParams.PlatformSerialNumberNode1 == "" || cvoDetails.HAParams.PlatformSerialNumberNode2 == "" {
return fmt.Errorf("both platform_serial_number_node1 and platform_serial_number_node2 parameters are required when having ha type as true and license_type as gcp-ha-cot-premium-byol")
}
}
// by Node byol license
if !cvoDetails.IsHA && (cvoDetails.HAParams.PlatformSerialNumberNode1 != "" || cvoDetails.HAParams.PlatformSerialNumberNode2 != "") {
return fmt.Errorf("both platform_serial_number_node1 and platform_serial_number_node2 parameters are only required when having ha type as true and license_type as gcp-ha-cot-premium-byol")
}
// by Node byol license
if cvoDetails.VsaMetadata.LicenseType == "gcp-cot-premium-byol" {
if cvoDetails.SerialNumber == "" {
return fmt.Errorf("serial_number parameter is required when having license_type as gcp-cot-premium-byol")
}
}
// by Capacity license
if cvoDetails.VsaMetadata.CapacityPackageName != "" {
if cvoDetails.IsHA && cvoDetails.VsaMetadata.LicenseType != "ha-capacity-paygo" {
return fmt.Errorf("license_type must be ha-capacity-paygo")
}
if !cvoDetails.IsHA && cvoDetails.VsaMetadata.LicenseType != "capacity-paygo" {
return fmt.Errorf("license_type must be capacity-paygo")
}
}
if strings.HasSuffix(cvoDetails.VsaMetadata.LicenseType, "capacity-paygo") && cvoDetails.VsaMetadata.CapacityPackageName == "" {
return fmt.Errorf("capacity_package_name is required with capacity based license type")
}
return nil
}