| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# Copyright (c) 2022, Laurent Nicolas <[email protected]> |
| 8 |
# All rights reserved. |
| 9 |
# |
| 10 |
# Redistribution and use in source and binary forms, with or without modification, |
| 11 |
# are permitted provided that the following conditions are met: |
| 12 |
# |
| 13 |
# * Redistributions of source code must retain the above copyright |
| 14 |
# notice, this list of conditions and the following disclaimer. |
| 15 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
# this list of conditions and the following disclaimer in the documentation |
| 17 |
# and/or other materials provided with the distribution. |
| 18 |
# |
| 19 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 |
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 27 |
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
''' Support class for NetApp ansible modules ''' |
| 30 |
|
| 31 |
from __future__ import (absolute_import, division, print_function) |
| 32 |
__metaclass__ = type |
| 33 |
|
| 34 |
from copy import deepcopy |
| 35 |
import json |
| 36 |
import re |
| 37 |
import base64 |
| 38 |
import time |
| 39 |
|
| 40 |
|
| 41 |
def cmp(a, b): |
| 42 |
''' |
| 43 |
Python 3 does not have a cmp function, this will do the cmp. |
| 44 |
:param a: first object to check |
| 45 |
:param b: second object to check |
| 46 |
:return: |
| 47 |
''' |
| 48 |
# convert to lower case for string comparison. |
| 49 |
if a is None: |
| 50 |
return -1 |
| 51 |
if isinstance(a, str) and isinstance(b, str): |
| 52 |
a = a.lower() |
| 53 |
b = b.lower() |
| 54 |
# if list has string element, convert string to lower case. |
| 55 |
if isinstance(a, list) and isinstance(b, list): |
| 56 |
a = [x.lower() if isinstance(x, str) else x for x in a] |
| 57 |
b = [x.lower() if isinstance(x, str) else x for x in b] |
| 58 |
a.sort() |
| 59 |
b.sort() |
| 60 |
return (a > b) - (a < b) |
| 61 |
|
| 62 |
|
| 63 |
class NetAppModule(object): |
| 64 |
''' |
| 65 |
Common class for NetApp modules |
| 66 |
set of support functions to derive actions based |
| 67 |
on the current state of the system, and a desired state |
| 68 |
''' |
| 69 |
|
| 70 |
def __init__(self): |
| 71 |
self.log = [] |
| 72 |
self.changed = False |
| 73 |
self.parameters = {'name': 'not intialized'} |
| 74 |
|
| 75 |
def set_parameters(self, ansible_params): |
| 76 |
self.parameters = {} |
| 77 |
for param in ansible_params: |
| 78 |
if ansible_params[param] is not None: |
| 79 |
self.parameters[param] = ansible_params[param] |
| 80 |
return self.parameters |
| 81 |
|
| 82 |
def get_cd_action(self, current, desired): |
| 83 |
''' takes a desired state and a current state, and return an action: |
| 84 |
create, delete, None |
| 85 |
eg: |
| 86 |
is_present = 'absent' |
| 87 |
some_object = self.get_object(source) |
| 88 |
if some_object is not None: |
| 89 |
is_present = 'present' |
| 90 |
action = cd_action(current=is_present, desired = self.desired.state()) |
| 91 |
''' |
| 92 |
desired_state = desired['state'] if 'state' in desired else 'present' |
| 93 |
if current is None and desired_state == 'absent': |
| 94 |
return None |
| 95 |
if current is not None and desired_state == 'present': |
| 96 |
return None |
| 97 |
# change in state |
| 98 |
self.changed = True |
| 99 |
if current is not None: |
| 100 |
return 'delete' |
| 101 |
return 'create' |
| 102 |
|
| 103 |
def compare_and_update_values(self, current, desired, keys_to_compare): |
| 104 |
updated_values = {} |
| 105 |
is_changed = False |
| 106 |
for key in keys_to_compare: |
| 107 |
if key in current: |
| 108 |
if key in desired and desired[key] is not None: |
| 109 |
if current[key] != desired[key]: |
| 110 |
updated_values[key] = desired[key] |
| 111 |
is_changed = True |
| 112 |
else: |
| 113 |
updated_values[key] = current[key] |
| 114 |
else: |
| 115 |
updated_values[key] = current[key] |
| 116 |
|
| 117 |
return updated_values, is_changed |
| 118 |
|
| 119 |
def get_working_environments_info(self, rest_api, headers): |
| 120 |
''' |
| 121 |
Get all working environments info |
| 122 |
''' |
| 123 |
api = "/occm/api/working-environments" |
| 124 |
response, error, dummy = rest_api.get(api, None, header=headers) |
| 125 |
if error is not None: |
| 126 |
return response, error |
| 127 |
else: |
| 128 |
return response, None |
| 129 |
|
| 130 |
def look_up_working_environment_by_name_in_list(self, we_list, name): |
| 131 |
''' |
| 132 |
Look up working environment by the name in working environment list |
| 133 |
''' |
| 134 |
for we in we_list: |
| 135 |
if we['name'] == name: |
| 136 |
return we, None |
| 137 |
return None, "look_up_working_environment_by_name_in_list: Working environment not found" |
| 138 |
|
| 139 |
def get_working_environment_details_by_name(self, rest_api, headers, name, provider=None): |
| 140 |
''' |
| 141 |
Use working environment name to get working environment details including: |
| 142 |
name: working environment name, |
| 143 |
publicID: working environment ID |
| 144 |
cloudProviderName, |
| 145 |
isHA, |
| 146 |
svmName |
| 147 |
''' |
| 148 |
# check the working environment exist or not |
| 149 |
api = "/occm/api/working-environments/exists/" + name |
| 150 |
response, error, dummy = rest_api.get(api, None, header=headers) |
| 151 |
if error is not None: |
| 152 |
return None, error |
| 153 |
|
| 154 |
# get working environment lists |
| 155 |
api = "/occm/api/working-environments" |
| 156 |
response, error, dummy = rest_api.get(api, None, header=headers) |
| 157 |
if error is not None: |
| 158 |
return None, error |
| 159 |
# look up the working environment in the working environment lists |
| 160 |
if provider is None or provider == 'onPrem': |
| 161 |
working_environment_details, error = self.look_up_working_environment_by_name_in_list(response['onPremWorkingEnvironments'], name) |
| 162 |
if error is None: |
| 163 |
return working_environment_details, None |
| 164 |
if provider is None or provider == 'gcp': |
| 165 |
working_environment_details, error = self.look_up_working_environment_by_name_in_list(response['gcpVsaWorkingEnvironments'], name) |
| 166 |
if error is None: |
| 167 |
return working_environment_details, None |
| 168 |
if provider is None or provider == 'azure': |
| 169 |
working_environment_details, error = self.look_up_working_environment_by_name_in_list(response['azureVsaWorkingEnvironments'], name) |
| 170 |
if error is None: |
| 171 |
return working_environment_details, None |
| 172 |
if provider is None or provider == 'aws': |
| 173 |
working_environment_details, error = self.look_up_working_environment_by_name_in_list(response['vsaWorkingEnvironments'], name) |
| 174 |
if error is None: |
| 175 |
return working_environment_details, None |
| 176 |
return None, "get_working_environment_details_by_name: Working environment not found" |
| 177 |
|
| 178 |
def get_working_environment_details(self, rest_api, headers): |
| 179 |
''' |
| 180 |
Use working environment id to get working environment details including: |
| 181 |
name: working environment name, |
| 182 |
publicID: working environment ID |
| 183 |
cloudProviderName, |
| 184 |
ontapClusterProperties, |
| 185 |
isHA, |
| 186 |
status, |
| 187 |
userTags, |
| 188 |
workingEnvironmentType, |
| 189 |
''' |
| 190 |
api = "/occm/api/working-environments/" |
| 191 |
api += self.parameters['working_environment_id'] |
| 192 |
response, error, dummy = rest_api.get(api, None, header=headers) |
| 193 |
if error: |
| 194 |
return None, "Error: get_working_environment_details %s" % error |
| 195 |
return response, None |
| 196 |
|
| 197 |
def get_aws_fsx_details(self, rest_api, header=None, name=None): |
| 198 |
''' |
| 199 |
Use working environment id and tenantID to get working environment details including: |
| 200 |
name: working environment name, |
| 201 |
publicID: working environment ID |
| 202 |
''' |
| 203 |
api = "/fsx-ontap/working-environments/" |
| 204 |
api += self.parameters['tenant_id'] |
| 205 |
count = 0 |
| 206 |
fsx_details = None |
| 207 |
if name is None: |
| 208 |
name = self.parameters['name'] |
| 209 |
response, error, dummy = rest_api.get(api, None, header=header) |
| 210 |
if error: |
| 211 |
return response, "Error: get_aws_fsx_details %s" % error |
| 212 |
for each in response: |
| 213 |
if each['name'] == name: |
| 214 |
count += 1 |
| 215 |
fsx_details = each |
| 216 |
if self.parameters.get('working_environment_id'): |
| 217 |
if each['id'] == self.parameters['working_environment_id']: |
| 218 |
return each, None |
| 219 |
if count == 1: |
| 220 |
return fsx_details, None |
| 221 |
elif count > 1: |
| 222 |
return response, "More than one AWS FSx found for %s, use working_environment_id for delete" \ |
| 223 |
"or use different name for create" % name |
| 224 |
return None, None |
| 225 |
|
| 226 |
def get_aws_fsx_details_by_id(self, rest_api, header=None): |
| 227 |
''' |
| 228 |
Use working environment id and tenantID to get working environment details including: |
| 229 |
publicID: working environment ID |
| 230 |
''' |
| 231 |
api = "/fsx-ontap/working-environments/%s" % self.parameters['tenant_id'] |
| 232 |
response, error, dummy = rest_api.get(api, None, header=header) |
| 233 |
if error: |
| 234 |
return response, "Error: get_aws_fsx_details %s" % error |
| 235 |
for each in response: |
| 236 |
if self.parameters.get('destination_working_environment_id') and each['id'] == self.parameters['destination_working_environment_id']: |
| 237 |
return each, None |
| 238 |
return None, None |
| 239 |
|
| 240 |
def get_aws_fsx_details_by_name(self, rest_api, header=None): |
| 241 |
''' |
| 242 |
Use working environment name and tenantID to get working environment details including: |
| 243 |
name: working environment name, |
| 244 |
''' |
| 245 |
api = "/fsx-ontap/working-environments/%s" % self.parameters['tenant_id'] |
| 246 |
count = 0 |
| 247 |
fsx_details = None |
| 248 |
response, error, dummy = rest_api.get(api, None, header=header) |
| 249 |
if error: |
| 250 |
return response, "Error: get_aws_fsx_details_by_name %s" % error |
| 251 |
for each in response: |
| 252 |
if each['name'] == self.parameters['destination_working_environment_name']: |
| 253 |
count += 1 |
| 254 |
fsx_details = each |
| 255 |
if count == 1: |
| 256 |
return fsx_details['id'], None |
| 257 |
if count > 1: |
| 258 |
return response, "More than one AWS FSx found for %s" % self.parameters['name'] |
| 259 |
return None, None |
| 260 |
|
| 261 |
def get_aws_fsx_svm(self, rest_api, id, header=None): |
| 262 |
''' |
| 263 |
Use working environment id and tenantID to get FSx svm details including: |
| 264 |
publicID: working environment ID |
| 265 |
''' |
| 266 |
api = "/occm/api/fsx/working-environments/%s/svms" % id |
| 267 |
response, error, dummy = rest_api.get(api, None, header=header) |
| 268 |
if error: |
| 269 |
return response, "Error: get_aws_fsx_svm %s" % error |
| 270 |
if len(response) == 0: |
| 271 |
return None, "Error: no SVM found for %s" % id |
| 272 |
return response[0]['name'], None |
| 273 |
|
| 274 |
def get_working_environment_detail_for_snapmirror(self, rest_api, headers): |
| 275 |
|
| 276 |
source_working_env_detail, dest_working_env_detail = {}, {} |
| 277 |
if self.parameters.get('source_working_environment_id'): |
| 278 |
api = '/occm/api/working-environments' |
| 279 |
working_env_details, error, dummy = rest_api.get(api, None, header=headers) |
| 280 |
if error: |
| 281 |
return None, None, "Error getting WE info: %s: %s" % (error, working_env_details) |
| 282 |
for dummy, values in working_env_details.items(): |
| 283 |
for each in values: |
| 284 |
if each['publicId'] == self.parameters['source_working_environment_id']: |
| 285 |
source_working_env_detail = each |
| 286 |
break |
| 287 |
elif self.parameters.get('source_working_environment_name'): |
| 288 |
source_working_env_detail, error = self.get_working_environment_details_by_name(rest_api, headers, |
| 289 |
self.parameters['source_working_environment_name']) |
| 290 |
if error: |
| 291 |
return None, None, error |
| 292 |
else: |
| 293 |
return None, None, "Cannot find working environment by source_working_environment_id or source_working_environment_name" |
| 294 |
|
| 295 |
if self.parameters.get('destination_working_environment_id'): |
| 296 |
if self.parameters['destination_working_environment_id'].startswith('fs-'): |
| 297 |
if self.parameters.get('tenant_id'): |
| 298 |
working_env_details, error = self.get_aws_fsx_details_by_id(rest_api, header=headers) |
| 299 |
if error: |
| 300 |
return None, None, "Error getting WE info for FSx: %s: %s" % (error, working_env_details) |
| 301 |
dest_working_env_detail['publicId'] = self.parameters['destination_working_environment_id'] |
| 302 |
svm_name, error = self.get_aws_fsx_svm(rest_api, self.parameters['destination_working_environment_id'], header=headers) |
| 303 |
if error: |
| 304 |
return None, None, "Error getting svm name for FSx: %s" % error |
| 305 |
dest_working_env_detail['svmName'] = svm_name |
| 306 |
else: |
| 307 |
return None, None, "Cannot find FSx WE by destination WE %s, missing tenant_id" % self.parameters['destination_working_environment_id'] |
| 308 |
else: |
| 309 |
api = '/occm/api/working-environments' |
| 310 |
working_env_details, error, dummy = rest_api.get(api, None, header=headers) |
| 311 |
if error: |
| 312 |
return None, None, "Error getting WE info: %s: %s" % (error, working_env_details) |
| 313 |
for dummy, values in working_env_details.items(): |
| 314 |
for each in values: |
| 315 |
if each['publicId'] == self.parameters['destination_working_environment_id']: |
| 316 |
dest_working_env_detail = each |
| 317 |
break |
| 318 |
elif self.parameters.get('destination_working_environment_name'): |
| 319 |
if self.parameters.get('tenant_id'): |
| 320 |
fsx_id, error = self.get_aws_fsx_details_by_name(rest_api, header=headers) |
| 321 |
if error: |
| 322 |
return None, None, "Error getting WE info for FSx: %s" % error |
| 323 |
dest_working_env_detail['publicId'] = fsx_id |
| 324 |
svm_name, error = self.get_aws_fsx_svm(rest_api, fsx_id, header=headers) |
| 325 |
if error: |
| 326 |
return None, None, "Error getting svm name for FSx: %s" % error |
| 327 |
dest_working_env_detail['svmName'] = svm_name |
| 328 |
else: |
| 329 |
dest_working_env_detail, error = self.get_working_environment_details_by_name(rest_api, headers, |
| 330 |
self.parameters['destination_working_environment_name']) |
| 331 |
if error: |
| 332 |
return None, None, error |
| 333 |
else: |
| 334 |
return None, None, "Cannot find working environment by destination_working_environment_id or destination_working_environment_name" |
| 335 |
|
| 336 |
return source_working_env_detail, dest_working_env_detail, None |
| 337 |
|
| 338 |
def create_account(self, rest_api): |
| 339 |
""" |
| 340 |
Create Account |
| 341 |
:return: Account ID |
| 342 |
""" |
| 343 |
# TODO? do we need to create an account? And the code below is broken |
| 344 |
return None, 'Error: creating an account is not supported.' |
| 345 |
# headers = { |
| 346 |
# "X-User-Token": rest_api.token_type + " " + rest_api.token, |
| 347 |
# } |
| 348 |
|
| 349 |
# api = '/tenancy/account/MyAccount' |
| 350 |
# account_res, error, dummy = rest_api.post(api, header=headers) |
| 351 |
# account_id = None if error is not None else account_res['accountPublicId'] |
| 352 |
# return account_id, error |
| 353 |
|
| 354 |
def get_or_create_account(self, rest_api): |
| 355 |
""" |
| 356 |
Get Account |
| 357 |
:return: Account ID |
| 358 |
""" |
| 359 |
accounts, error = self.get_account_info(rest_api) |
| 360 |
if error is not None: |
| 361 |
return None, error |
| 362 |
if len(accounts) == 0: |
| 363 |
return None, 'Error: account cannot be located - check credentials or provide account_id.' |
| 364 |
# TODO? creating an account is not supported |
| 365 |
# return self.create_account(rest_api) |
| 366 |
|
| 367 |
return accounts[0]['accountPublicId'], None |
| 368 |
|
| 369 |
def get_account_info(self, rest_api, headers=None): |
| 370 |
""" |
| 371 |
Get Account |
| 372 |
:return: Account ID |
| 373 |
""" |
| 374 |
headers = { |
| 375 |
"X-User-Token": rest_api.token_type + " " + rest_api.token, |
| 376 |
} |
| 377 |
|
| 378 |
api = '/tenancy/account' |
| 379 |
account_res, error, dummy = rest_api.get(api, header=headers) |
| 380 |
if error is not None: |
| 381 |
return None, error |
| 382 |
return account_res, None |
| 383 |
|
| 384 |
def get_account_id(self, rest_api): |
| 385 |
accounts, error = self.get_account_info(rest_api) |
| 386 |
if error: |
| 387 |
return None, error |
| 388 |
if not accounts: |
| 389 |
return None, 'Error: no account found - check credentials or provide account_id.' |
| 390 |
return accounts[0]['accountPublicId'], None |
| 391 |
|
| 392 |
def get_accounts_info(self, rest_api, headers): |
| 393 |
''' |
| 394 |
Get all accounts info |
| 395 |
''' |
| 396 |
api = "/occm/api/accounts" |
| 397 |
response, error, dummy = rest_api.get(api, None, header=headers) |
| 398 |
if error is not None: |
| 399 |
return None, error |
| 400 |
else: |
| 401 |
return response, None |
| 402 |
|
| 403 |
def set_api_root_path(self, working_environment_details, rest_api): |
| 404 |
''' |
| 405 |
set API url root path based on the working environment provider |
| 406 |
''' |
| 407 |
provider = working_environment_details['cloudProviderName'] if working_environment_details.get('cloudProviderName') else None |
| 408 |
api_root_path = None |
| 409 |
if self.parameters['working_environment_id'].startswith('fs-'): |
| 410 |
api_root_path = "/occm/api/fsx" |
| 411 |
elif provider == "Amazon": |
| 412 |
api_root_path = "/occm/api/aws/ha" if working_environment_details['isHA'] else "/occm/api/vsa" |
| 413 |
elif working_environment_details['isHA']: |
| 414 |
api_root_path = "/occm/api/" + provider.lower() + "/ha" |
| 415 |
else: |
| 416 |
api_root_path = "/occm/api/" + provider.lower() + "/vsa" |
| 417 |
rest_api.api_root_path = api_root_path |
| 418 |
|
| 419 |
def have_required_parameters(self, action): |
| 420 |
''' |
| 421 |
Check if all the required parameters in self.params are available or not besides the mandatory parameters |
| 422 |
''' |
| 423 |
actions = {'create_aggregate': ['number_of_disks', 'disk_size_size', 'disk_size_unit', 'working_environment_id'], |
| 424 |
'update_aggregate': ['number_of_disks', 'disk_size_size', 'disk_size_unit', 'working_environment_id'], |
| 425 |
'delete_aggregate': ['working_environment_id'], |
| 426 |
} |
| 427 |
missed_params = [ |
| 428 |
parameter |
| 429 |
for parameter in actions[action] |
| 430 |
if parameter not in self.parameters |
| 431 |
] |
| 432 |
|
| 433 |
if not missed_params: |
| 434 |
return True, None |
| 435 |
else: |
| 436 |
return False, missed_params |
| 437 |
|
| 438 |
def get_modified_attributes(self, current, desired, get_list_diff=False): |
| 439 |
''' takes two dicts of attributes and return a dict of attributes that are |
| 440 |
not in the current state |
| 441 |
It is expected that all attributes of interest are listed in current and |
| 442 |
desired. |
| 443 |
:param: current: current attributes in ONTAP |
| 444 |
:param: desired: attributes from playbook |
| 445 |
:param: get_list_diff: specifies whether to have a diff of desired list w.r.t current list for an attribute |
| 446 |
:return: dict of attributes to be modified |
| 447 |
:rtype: dict |
| 448 |
|
| 449 |
NOTE: depending on the attribute, the caller may need to do a modify or a |
| 450 |
different operation (eg move volume if the modified attribute is an |
| 451 |
aggregate name) |
| 452 |
''' |
| 453 |
# if the object does not exist, we can't modify it |
| 454 |
modified = {} |
| 455 |
if current is None: |
| 456 |
return modified |
| 457 |
|
| 458 |
# error out if keys do not match |
| 459 |
# self.check_keys(current, desired) |
| 460 |
|
| 461 |
# collect changed attributes |
| 462 |
for key, value in current.items(): |
| 463 |
if key in desired and desired[key] is not None: |
| 464 |
if isinstance(value, list): |
| 465 |
modified_list = self.compare_lists(value, desired[key], get_list_diff) # get modified list from current and desired |
| 466 |
if modified_list is not None: |
| 467 |
modified[key] = modified_list |
| 468 |
elif isinstance(value, dict): |
| 469 |
modified_dict = self.get_modified_attributes(value, desired[key]) |
| 470 |
if modified_dict: |
| 471 |
modified[key] = modified_dict |
| 472 |
else: |
| 473 |
try: |
| 474 |
result = cmp(value, desired[key]) |
| 475 |
except TypeError as exc: |
| 476 |
raise TypeError("%s, key: %s, value: %s, desired: %s" % (repr(exc), key, repr(value), repr(desired[key]))) |
| 477 |
else: |
| 478 |
if result != 0: |
| 479 |
modified[key] = desired[key] |
| 480 |
if modified: |
| 481 |
self.changed = True |
| 482 |
return modified |
| 483 |
|
| 484 |
@staticmethod |
| 485 |
def compare_lists(current, desired, get_list_diff): |
| 486 |
''' compares two lists and return a list of elements that are either the desired elements or elements that are |
| 487 |
modified from the current state depending on the get_list_diff flag |
| 488 |
:param: current: current item attribute in ONTAP |
| 489 |
:param: desired: attributes from playbook |
| 490 |
:param: get_list_diff: specifies whether to have a diff of desired list w.r.t current list for an attribute |
| 491 |
:return: list of attributes to be modified |
| 492 |
:rtype: list |
| 493 |
''' |
| 494 |
current_copy = deepcopy(current) |
| 495 |
desired_copy = deepcopy(desired) |
| 496 |
|
| 497 |
# get what in desired and not in current |
| 498 |
desired_diff_list = list() |
| 499 |
for item in desired: |
| 500 |
if item in current_copy: |
| 501 |
current_copy.remove(item) |
| 502 |
else: |
| 503 |
desired_diff_list.append(item) |
| 504 |
|
| 505 |
# get what in current but not in desired |
| 506 |
current_diff_list = [] |
| 507 |
for item in current: |
| 508 |
if item in desired_copy: |
| 509 |
desired_copy.remove(item) |
| 510 |
else: |
| 511 |
current_diff_list.append(item) |
| 512 |
|
| 513 |
if desired_diff_list or current_diff_list: |
| 514 |
# there are changes |
| 515 |
if get_list_diff: |
| 516 |
return desired_diff_list |
| 517 |
else: |
| 518 |
return desired |
| 519 |
else: |
| 520 |
return None |
| 521 |
|
| 522 |
@staticmethod |
| 523 |
def convert_module_args_to_api(parameters, exclusion=None): |
| 524 |
''' |
| 525 |
Convert a list of string module args to API option format. |
| 526 |
For example, convert test_option to testOption. |
| 527 |
:param parameters: dict of parameters to be converted. |
| 528 |
:param exclusion: list of parameters to be ignored. |
| 529 |
:return: dict of key value pairs. |
| 530 |
''' |
| 531 |
exclude_list = ['api_url', 'token_type', 'refresh_token', 'sa_secret_key', 'sa_client_id'] |
| 532 |
if exclusion is not None: |
| 533 |
exclude_list += exclusion |
| 534 |
api_keys = {} |
| 535 |
for k, v in parameters.items(): |
| 536 |
if k not in exclude_list: |
| 537 |
words = k.split("_") |
| 538 |
api_key = "" |
| 539 |
for word in words: |
| 540 |
if len(api_key) > 0: |
| 541 |
word = word.title() |
| 542 |
api_key += word |
| 543 |
api_keys[api_key] = v |
| 544 |
return api_keys |
| 545 |
|
| 546 |
@staticmethod |
| 547 |
def convert_data_to_tabbed_jsonstring(data): |
| 548 |
''' |
| 549 |
Convert a dictionary data to json format string |
| 550 |
''' |
| 551 |
dump = json.dumps(data, indent=2, separators=(',', ': ')) |
| 552 |
return re.sub( |
| 553 |
'\n +', |
| 554 |
lambda match: '\n' + '\t' * int(len(match.group().strip('\n')) / 2), |
| 555 |
dump, |
| 556 |
) |
| 557 |
|
| 558 |
@staticmethod |
| 559 |
def encode_certificates(certificate_file): |
| 560 |
''' |
| 561 |
Read certificate file and encode it |
| 562 |
''' |
| 563 |
try: |
| 564 |
with open(certificate_file, mode='rb') as fh: |
| 565 |
cert = fh.read() |
| 566 |
except (OSError, IOError) as exc: |
| 567 |
return None, str(exc) |
| 568 |
if not cert: |
| 569 |
return None, "Error: file is empty" |
| 570 |
return base64.b64encode(cert).decode('utf-8'), None |
| 571 |
|
| 572 |
@staticmethod |
| 573 |
def get_occm_agents_by_account(rest_api, account_id): |
| 574 |
""" |
| 575 |
Collect a list of agents matching account_id. |
| 576 |
:return: list of agents, error |
| 577 |
""" |
| 578 |
params = {'account_id': account_id} |
| 579 |
api = "/agents-mgmt/agent" |
| 580 |
headers = { |
| 581 |
"X-User-Token": rest_api.token_type + " " + rest_api.token, |
| 582 |
} |
| 583 |
agents, error, dummy = rest_api.get(api, header=headers, params=params) |
| 584 |
return agents, error |
| 585 |
|
| 586 |
def get_occm_agents_by_name(self, rest_api, account_id, name, provider): |
| 587 |
""" |
| 588 |
Collect a list of agents matching account_id, name, and provider. |
| 589 |
:return: list of agents, error |
| 590 |
""" |
| 591 |
# I tried to query by name and provider in addition to account_id, but it returned everything |
| 592 |
agents, error = self.get_occm_agents_by_account(rest_api, account_id) |
| 593 |
if isinstance(agents, dict) and 'agents' in agents: |
| 594 |
agents = [agent for agent in agents['agents'] if agent['name'] == name and agent['provider'] == provider] |
| 595 |
return agents, error |
| 596 |
|
| 597 |
def get_agents_info(self, rest_api, headers): |
| 598 |
""" |
| 599 |
Collect a list of agents matching account_id. |
| 600 |
:return: list of agents, error |
| 601 |
""" |
| 602 |
account_id, error = self.get_account_id(rest_api) |
| 603 |
if error: |
| 604 |
return None, error |
| 605 |
agents, error = self.get_occm_agents_by_account(rest_api, account_id) |
| 606 |
return agents, error |
| 607 |
|
| 608 |
def get_active_agents_info(self, rest_api, headers): |
| 609 |
""" |
| 610 |
Collect a list of agents matching account_id. |
| 611 |
:return: list of agents, error |
| 612 |
""" |
| 613 |
clients = [] |
| 614 |
account_id, error = self.get_account_id(rest_api) |
| 615 |
if error: |
| 616 |
return None, error |
| 617 |
agents, error = self.get_occm_agents_by_account(rest_api, account_id) |
| 618 |
if isinstance(agents, dict) and 'agents' in agents: |
| 619 |
agents = [agent for agent in agents['agents'] if agent['status'] == 'active'] |
| 620 |
clients = [{'name': agent['name'], 'client_id': agent['agentId'], 'provider': agent['provider']} for agent in agents] |
| 621 |
return clients, error |
| 622 |
|
| 623 |
@staticmethod |
| 624 |
def get_occm_agent_by_id(rest_api, client_id): |
| 625 |
""" |
| 626 |
Fetch OCCM agent given its client id |
| 627 |
:return: agent details, error |
| 628 |
""" |
| 629 |
api = "/agents-mgmt/agent/" + rest_api.format_client_id(client_id) |
| 630 |
headers = { |
| 631 |
"X-User-Token": rest_api.token_type + " " + rest_api.token, |
| 632 |
} |
| 633 |
response, error, dummy = rest_api.get(api, header=headers) |
| 634 |
if isinstance(response, dict) and 'agent' in response: |
| 635 |
agent = response['agent'] |
| 636 |
return agent, error |
| 637 |
return response, error |
| 638 |
|
| 639 |
@staticmethod |
| 640 |
def check_occm_status(rest_api, client_id): |
| 641 |
""" |
| 642 |
Check OCCM status |
| 643 |
:return: status |
| 644 |
DEPRECATED - use get_occm_agent_by_id but the retrun value format is different! |
| 645 |
""" |
| 646 |
|
| 647 |
api = "/agents-mgmt/agent/" + rest_api.format_client_id(client_id) |
| 648 |
headers = { |
| 649 |
"X-User-Token": rest_api.token_type + " " + rest_api.token, |
| 650 |
} |
| 651 |
occm_status, error, dummy = rest_api.get(api, header=headers) |
| 652 |
return occm_status, error |
| 653 |
|
| 654 |
def register_agent_to_service(self, rest_api, provider, vpc): |
| 655 |
''' |
| 656 |
register agent to service |
| 657 |
''' |
| 658 |
api = '/agents-mgmt/connector-setup' |
| 659 |
|
| 660 |
headers = { |
| 661 |
"X-User-Token": rest_api.token_type + " " + rest_api.token, |
| 662 |
} |
| 663 |
body = { |
| 664 |
"accountId": self.parameters['account_id'], |
| 665 |
"name": self.parameters['name'], |
| 666 |
"company": self.parameters['company'], |
| 667 |
"placement": { |
| 668 |
"provider": provider, |
| 669 |
"region": self.parameters['region'], |
| 670 |
"network": vpc, |
| 671 |
"subnet": self.parameters['subnet_id'], |
| 672 |
}, |
| 673 |
"extra": { |
| 674 |
"proxy": { |
| 675 |
"proxyUrl": self.parameters.get('proxy_url'), |
| 676 |
"proxyUserName": self.parameters.get('proxy_user_name'), |
| 677 |
"proxyPassword": self.parameters.get('proxy_password'), |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
if provider == "AWS": |
| 683 |
body['placement']['network'] = vpc |
| 684 |
|
| 685 |
response, error, dummy = rest_api.post(api, body, header=headers) |
| 686 |
return response, error |
| 687 |
|
| 688 |
def delete_occm(self, rest_api, client_id): |
| 689 |
''' |
| 690 |
delete occm |
| 691 |
''' |
| 692 |
api = '/agents-mgmt/agent/' + rest_api.format_client_id(client_id) |
| 693 |
headers = { |
| 694 |
"X-User-Token": rest_api.token_type + " " + rest_api.token, |
| 695 |
"X-Tenancy-Account-Id": self.parameters['account_id'], |
| 696 |
} |
| 697 |
|
| 698 |
occm_status, error, dummy = rest_api.delete(api, None, header=headers) |
| 699 |
return occm_status, error |
| 700 |
|
| 701 |
def delete_occm_agents(self, rest_api, agents): |
| 702 |
''' |
| 703 |
delete a list of occm |
| 704 |
''' |
| 705 |
results = [] |
| 706 |
for agent in agents: |
| 707 |
if 'agentId' in agent: |
| 708 |
occm_status, error = self.delete_occm(rest_api, agent['agentId']) |
| 709 |
else: |
| 710 |
occm_status, error = None, 'unexpected agent contents: %s' % repr(agent) |
| 711 |
if error: |
| 712 |
results.append((occm_status, error)) |
| 713 |
return results |
| 714 |
|
| 715 |
@staticmethod |
| 716 |
def call_parameters(): |
| 717 |
return """ |
| 718 |
{ |
| 719 |
"location": { |
| 720 |
"value": "string" |
| 721 |
}, |
| 722 |
"virtualMachineName": { |
| 723 |
"value": "string" |
| 724 |
}, |
| 725 |
"virtualMachineSize": { |
| 726 |
"value": "string" |
| 727 |
}, |
| 728 |
"networkSecurityGroupName": { |
| 729 |
"value": "string" |
| 730 |
}, |
| 731 |
"adminUsername": { |
| 732 |
"value": "string" |
| 733 |
}, |
| 734 |
"virtualNetworkId": { |
| 735 |
"value": "string" |
| 736 |
}, |
| 737 |
"adminPassword": { |
| 738 |
"value": "string" |
| 739 |
}, |
| 740 |
"subnetId": { |
| 741 |
"value": "string" |
| 742 |
}, |
| 743 |
"customData": { |
| 744 |
"value": "string" |
| 745 |
}, |
| 746 |
"environment": { |
| 747 |
"value": "prod" |
| 748 |
}, |
| 749 |
"storageAccount": { |
| 750 |
"value": "string" |
| 751 |
} |
| 752 |
} |
| 753 |
""" |
| 754 |
|
| 755 |
@staticmethod |
| 756 |
def call_template(): |
| 757 |
return """ |
| 758 |
{ |
| 759 |
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", |
| 760 |
"contentVersion": "1.0.0.0", |
| 761 |
"parameters": { |
| 762 |
"location": { |
| 763 |
"type": "string", |
| 764 |
"defaultValue": "eastus" |
| 765 |
}, |
| 766 |
"virtualMachineName": { |
| 767 |
"type": "string" |
| 768 |
}, |
| 769 |
"virtualMachineSize":{ |
| 770 |
"type": "string" |
| 771 |
}, |
| 772 |
"adminUsername": { |
| 773 |
"type": "string" |
| 774 |
}, |
| 775 |
"virtualNetworkId": { |
| 776 |
"type": "string" |
| 777 |
}, |
| 778 |
"networkSecurityGroupName": { |
| 779 |
"type": "string" |
| 780 |
}, |
| 781 |
"adminPassword": { |
| 782 |
"type": "securestring" |
| 783 |
}, |
| 784 |
"subnetId": { |
| 785 |
"type": "string" |
| 786 |
}, |
| 787 |
"customData": { |
| 788 |
"type": "string" |
| 789 |
}, |
| 790 |
"environment": { |
| 791 |
"type": "string", |
| 792 |
"defaultValue": "prod" |
| 793 |
}, |
| 794 |
"storageAccount": { |
| 795 |
"type": "string" |
| 796 |
} |
| 797 |
}, |
| 798 |
"variables": { |
| 799 |
"vnetId": "[parameters('virtualNetworkId')]", |
| 800 |
"subnetRef": "[parameters('subnetId')]", |
| 801 |
"networkInterfaceName": "[concat(parameters('virtualMachineName'),'-nic')]", |
| 802 |
"diagnosticsStorageAccountName": "[parameters('storageAccount')]", |
| 803 |
"diagnosticsStorageAccountId": "[concat('Microsoft.Storage/storageAccounts/', variables('diagnosticsStorageAccountName'))]", |
| 804 |
"diagnosticsStorageAccountType": "Standard_LRS", |
| 805 |
"publicIpAddressName": "[concat(parameters('virtualMachineName'),'-ip')]", |
| 806 |
"publicIpAddressType": "Dynamic", |
| 807 |
"publicIpAddressSku": "Basic", |
| 808 |
"msiExtensionName": "ManagedIdentityExtensionForLinux", |
| 809 |
"occmOffer": "[if(equals(parameters('environment'), 'stage'), 'netapp-oncommand-cloud-manager-staging-preview', 'netapp-oncommand-cloud-manager')]" |
| 810 |
}, |
| 811 |
"resources": [ |
| 812 |
{ |
| 813 |
"name": "[parameters('virtualMachineName')]", |
| 814 |
"type": "Microsoft.Compute/virtualMachines", |
| 815 |
"apiVersion": "2018-04-01", |
| 816 |
"location": "[parameters('location')]", |
| 817 |
"dependsOn": [ |
| 818 |
"[concat('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]", |
| 819 |
"[concat('Microsoft.Storage/storageAccounts/', variables('diagnosticsStorageAccountName'))]" |
| 820 |
], |
| 821 |
"properties": { |
| 822 |
"osProfile": { |
| 823 |
"computerName": "[parameters('virtualMachineName')]", |
| 824 |
"adminUsername": "[parameters('adminUsername')]", |
| 825 |
"adminPassword": "[parameters('adminPassword')]", |
| 826 |
"customData": "[base64(parameters('customData'))]" |
| 827 |
}, |
| 828 |
"hardwareProfile": { |
| 829 |
"vmSize": "[parameters('virtualMachineSize')]" |
| 830 |
}, |
| 831 |
"storageProfile": { |
| 832 |
"imageReference": { |
| 833 |
"publisher": "netapp", |
| 834 |
"offer": "[variables('occmOffer')]", |
| 835 |
"sku": "occm-byol", |
| 836 |
"version": "latest" |
| 837 |
}, |
| 838 |
"osDisk": { |
| 839 |
"createOption": "fromImage", |
| 840 |
"managedDisk": { |
| 841 |
"storageAccountType": "Premium_LRS" |
| 842 |
} |
| 843 |
}, |
| 844 |
"dataDisks": [] |
| 845 |
}, |
| 846 |
"networkProfile": { |
| 847 |
"networkInterfaces": [ |
| 848 |
{ |
| 849 |
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]" |
| 850 |
} |
| 851 |
] |
| 852 |
}, |
| 853 |
"diagnosticsProfile": { |
| 854 |
"bootDiagnostics": { |
| 855 |
"enabled": true, |
| 856 |
"storageUri": |
| 857 |
"[concat('https://', variables('diagnosticsStorageAccountName'), '.blob.core.windows.net/')]" |
| 858 |
} |
| 859 |
} |
| 860 |
}, |
| 861 |
"plan": { |
| 862 |
"name": "occm-byol", |
| 863 |
"publisher": "netapp", |
| 864 |
"product": "[variables('occmOffer')]" |
| 865 |
}, |
| 866 |
"identity": { |
| 867 |
"type": "systemAssigned" |
| 868 |
} |
| 869 |
}, |
| 870 |
{ |
| 871 |
"apiVersion": "2017-12-01", |
| 872 |
"type": "Microsoft.Compute/virtualMachines/extensions", |
| 873 |
"name": "[concat(parameters('virtualMachineName'),'/', variables('msiExtensionName'))]", |
| 874 |
"location": "[parameters('location')]", |
| 875 |
"dependsOn": [ |
| 876 |
"[concat('Microsoft.Compute/virtualMachines/', parameters('virtualMachineName'))]" |
| 877 |
], |
| 878 |
"properties": { |
| 879 |
"publisher": "Microsoft.ManagedIdentity", |
| 880 |
"type": "[variables('msiExtensionName')]", |
| 881 |
"typeHandlerVersion": "1.0", |
| 882 |
"autoUpgradeMinorVersion": true, |
| 883 |
"settings": { |
| 884 |
"port": 50342 |
| 885 |
} |
| 886 |
} |
| 887 |
}, |
| 888 |
{ |
| 889 |
"name": "[variables('diagnosticsStorageAccountName')]", |
| 890 |
"type": "Microsoft.Storage/storageAccounts", |
| 891 |
"apiVersion": "2015-06-15", |
| 892 |
"location": "[parameters('location')]", |
| 893 |
"properties": { |
| 894 |
"accountType": "[variables('diagnosticsStorageAccountType')]" |
| 895 |
} |
| 896 |
}, |
| 897 |
{ |
| 898 |
"name": "[variables('networkInterfaceName')]", |
| 899 |
"type": "Microsoft.Network/networkInterfaces", |
| 900 |
"apiVersion": "2018-04-01", |
| 901 |
"location": "[parameters('location')]", |
| 902 |
"dependsOn": [ |
| 903 |
"[concat('Microsoft.Network/publicIpAddresses/', variables('publicIpAddressName'))]" |
| 904 |
], |
| 905 |
"properties": { |
| 906 |
"ipConfigurations": [ |
| 907 |
{ |
| 908 |
"name": "ipconfig1", |
| 909 |
"properties": { |
| 910 |
"subnet": { |
| 911 |
"id": "[variables('subnetRef')]" |
| 912 |
}, |
| 913 |
"privateIPAllocationMethod": "Dynamic", |
| 914 |
"publicIpAddress": { |
| 915 |
"id": "[resourceId(resourceGroup().name,'Microsoft.Network/publicIpAddresses', variables('publicIpAddressName'))]" |
| 916 |
} |
| 917 |
} |
| 918 |
} |
| 919 |
], |
| 920 |
"networkSecurityGroup": { |
| 921 |
"id": "[parameters('networkSecurityGroupName')]" |
| 922 |
} |
| 923 |
} |
| 924 |
}, |
| 925 |
{ |
| 926 |
"name": "[variables('publicIpAddressName')]", |
| 927 |
"type": "Microsoft.Network/publicIpAddresses", |
| 928 |
"apiVersion": "2017-08-01", |
| 929 |
"location": "[parameters('location')]", |
| 930 |
"properties": { |
| 931 |
"publicIpAllocationMethod": "[variables('publicIpAddressType')]" |
| 932 |
}, |
| 933 |
"sku": { |
| 934 |
"name": "[variables('publicIpAddressSku')]" |
| 935 |
} |
| 936 |
} |
| 937 |
], |
| 938 |
"outputs": { |
| 939 |
"publicIpAddressName": { |
| 940 |
"type": "string", |
| 941 |
"value": "[variables('publicIpAddressName')]" |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
""" |
| 946 |
|
| 947 |
def get_tenant(self, rest_api, headers): |
| 948 |
""" |
| 949 |
Get workspace ID (tenant) |
| 950 |
""" |
| 951 |
api = '/occm/api/tenants' |
| 952 |
response, error, dummy = rest_api.get(api, header=headers) |
| 953 |
if error is not None: |
| 954 |
return None, 'Error: unexpected response on getting tenant for cvo: %s, %s' % (str(error), str(response)) |
| 955 |
|
| 956 |
return response[0]['publicId'], None |
| 957 |
|
| 958 |
def get_nss(self, rest_api, headers): |
| 959 |
""" |
| 960 |
Get nss account |
| 961 |
""" |
| 962 |
api = '/occm/api/accounts' |
| 963 |
response, error, dummy = rest_api.get(api, header=headers) |
| 964 |
if error is not None: |
| 965 |
return None, 'Error: unexpected response on getting nss for cvo: %s, %s' % (str(error), str(response)) |
| 966 |
|
| 967 |
if len(response['nssAccounts']) == 0: |
| 968 |
return None, "Error: could not find any NSS account" |
| 969 |
|
| 970 |
return response['nssAccounts'][0]['publicId'], None |
| 971 |
|
| 972 |
def get_working_environment_property(self, rest_api, headers, fields): |
| 973 |
# GET /vsa/working-environments/{workingEnvironmentId}?fields=status,awsProperties,ontapClusterProperties |
| 974 |
api = '%s/working-environments/%s' % (rest_api.api_root_path, self.parameters['working_environment_id']) |
| 975 |
params = {'fields': ','.join(fields)} |
| 976 |
response, error, dummy = rest_api.get(api, params=params, header=headers) |
| 977 |
if error: |
| 978 |
return None, "Error: get_working_environment_property %s" % error |
| 979 |
return response, None |
| 980 |
|
| 981 |
def user_tag_key_unique(self, tag_list, key_name): |
| 982 |
checked_keys = [] |
| 983 |
for t in tag_list: |
| 984 |
if t[key_name] in checked_keys: |
| 985 |
return False, 'Error: %s %s must be unique' % (key_name, t[key_name]) |
| 986 |
else: |
| 987 |
checked_keys.append(t[key_name]) |
| 988 |
return True, None |
| 989 |
|
| 990 |
def current_label_exist(self, current, desired, is_ha=False): |
| 991 |
current_key_set = set(current.keys()) |
| 992 |
# Ignore auto generated gcp label in CVO GCP HA |
| 993 |
current_key_set.discard('gcp_resource_id') |
| 994 |
current_key_set.discard('count-down') |
| 995 |
if is_ha: |
| 996 |
current_key_set.discard('partner-platform-serial-number') |
| 997 |
# python 2.6 doe snot support set comprehension |
| 998 |
desired_keys = set([a_dict['label_key'] for a_dict in desired]) |
| 999 |
if current_key_set.issubset(desired_keys): |
| 1000 |
return True, None |
| 1001 |
else: |
| 1002 |
return False, 'Error: label_key %s in gcp_label cannot be removed' % str(current_key_set) |
| 1003 |
|
| 1004 |
def is_label_value_changed(self, current_tags, desired_tags): |
| 1005 |
tag_keys = list(current_tags.keys()) |
| 1006 |
user_tag_keys = [key for key in tag_keys if |
| 1007 |
key not in ('count-down', 'gcp_resource_id', 'partner-platform-serial-number')] |
| 1008 |
desired_keys = [a_dict['label_key'] for a_dict in desired_tags] |
| 1009 |
if user_tag_keys == desired_keys: |
| 1010 |
for tag in desired_tags: |
| 1011 |
if current_tags[tag['label_key']] != tag['label_value']: |
| 1012 |
return True |
| 1013 |
return False |
| 1014 |
else: |
| 1015 |
return True |
| 1016 |
|
| 1017 |
def compare_gcp_labels(self, current_tags, user_tags, is_ha): |
| 1018 |
''' |
| 1019 |
Update user-tag API behaves differently in GCP CVO. |
| 1020 |
It only supports adding gcp_labels and modifying the values of gcp_labels. Removing gcp_label is not allowed. |
| 1021 |
''' |
| 1022 |
# check if any current gcp_labels are going to be removed or not |
| 1023 |
# gcp HA has one extra gcp_label created automatically |
| 1024 |
resp, error = self.user_tag_key_unique(user_tags, 'label_key') |
| 1025 |
if error is not None: |
| 1026 |
return None, error |
| 1027 |
# check if any current key labels are in the desired key labels |
| 1028 |
resp, error = self.current_label_exist(current_tags, user_tags, is_ha) |
| 1029 |
if error is not None: |
| 1030 |
return None, error |
| 1031 |
if self.is_label_value_changed(current_tags, user_tags): |
| 1032 |
return True, None |
| 1033 |
else: |
| 1034 |
# no change |
| 1035 |
return None, None |
| 1036 |
|
| 1037 |
def compare_cvo_tags_labels(self, current_tags, user_tags): |
| 1038 |
''' |
| 1039 |
Compare exiting tags/labels and user input tags/labels to see if there is a change |
| 1040 |
gcp_labels: label_key, label_value |
| 1041 |
aws_tag/azure_tag: tag_key, tag_label |
| 1042 |
''' |
| 1043 |
# azure has one extra azure_tag DeployedByOccm created automatically and it cannot be modified. |
| 1044 |
tag_keys = list(current_tags.keys()) |
| 1045 |
user_tag_keys = [key for key in tag_keys if key != 'DeployedByOccm'] |
| 1046 |
current_len = len(user_tag_keys) |
| 1047 |
resp, error = self.user_tag_key_unique(user_tags, 'tag_key') |
| 1048 |
if error is not None: |
| 1049 |
return None, error |
| 1050 |
if len(user_tags) != current_len: |
| 1051 |
return True, None |
| 1052 |
# Check if tags/labels of desired configuration in current working environment |
| 1053 |
for item in user_tags: |
| 1054 |
if item['tag_key'] in current_tags and item['tag_value'] != current_tags[item['tag_key']]: |
| 1055 |
return True, None |
| 1056 |
elif item['tag_key'] not in current_tags: |
| 1057 |
return True, None |
| 1058 |
return False, None |
| 1059 |
|
| 1060 |
def is_cvo_tags_changed(self, rest_api, headers, parameters, tag_name): |
| 1061 |
''' |
| 1062 |
Since tags/laabels are CVO optional parameters, this function needs to cover with/without tags/labels on both lists |
| 1063 |
''' |
| 1064 |
# get working environment details by working environment ID |
| 1065 |
current, error = self.get_working_environment_details(rest_api, headers) |
| 1066 |
if error is not None: |
| 1067 |
return None, 'Error: Cannot find working environment %s error: %s' % (self.parameters['working_environment_id'], str(error)) |
| 1068 |
self.set_api_root_path(current, rest_api) |
| 1069 |
# compare tags |
| 1070 |
# no tags in current cvo |
| 1071 |
if 'userTags' not in current or len(current['userTags']) == 0: |
| 1072 |
return tag_name in parameters, None |
| 1073 |
|
| 1074 |
if tag_name == 'gcp_labels': |
| 1075 |
if tag_name in parameters: |
| 1076 |
return self.compare_gcp_labels(current['userTags'], parameters[tag_name], current['isHA']) |
| 1077 |
# if both are empty, no need to update |
| 1078 |
# Ignore auto generated gcp label in CVO GCP |
| 1079 |
# 'count-down', 'gcp_resource_id', and 'partner-platform-serial-number'(HA) |
| 1080 |
tag_keys = list(current['userTags'].keys()) |
| 1081 |
user_tag_keys = [key for key in tag_keys if key not in ('count-down', 'gcp_resource_id', 'partner-platform-serial-number')] |
| 1082 |
if not user_tag_keys: |
| 1083 |
return False, None |
| 1084 |
else: |
| 1085 |
return None, 'Error: Cannot remove current gcp_labels' |
| 1086 |
# no tags in input parameters |
| 1087 |
if tag_name not in parameters: |
| 1088 |
return True, None |
| 1089 |
else: |
| 1090 |
# has tags in input parameters and existing CVO |
| 1091 |
return self.compare_cvo_tags_labels(current['userTags'], parameters[tag_name]) |
| 1092 |
|
| 1093 |
def get_license_type(self, rest_api, headers, provider, region, instance_type, ontap_version, license_name): |
| 1094 |
# Permutation query example: |
| 1095 |
# aws: /metadata/permutations?region=us-east-1&instance_type=m5.xlarge&version=ONTAP-9.10.1.T1 |
| 1096 |
# azure: /metadata/permutations?region=westus&instance_type=Standard_E4s_v3&version=ONTAP-9.10.1.T1.azure |
| 1097 |
# gcp: /metadata/permutations?region=us-east1&instance_type=n2-standard-4&version=ONTAP-9.10.1.T1.gcp |
| 1098 |
# The examples of the ontapVersion in ontapClusterProperties response: |
| 1099 |
# AWS for both single and HA: 9.10.1RC1, 9.8 |
| 1100 |
# AZURE single: 9.10.1RC1.T1.azure. For HA: 9.10.1RC1.T1.azureha |
| 1101 |
# GCP for both single and HA: 9.10.1RC1.T1, 9.8.T1 |
| 1102 |
# To be used in permutation: |
| 1103 |
# AWS ontap_version format: ONTAP-x.x.x.T1 or ONTAP-x.x.x.T1.ha for Ha |
| 1104 |
# AZURE ontap_version format: ONTAP-x.x.x.T1.azure or ONTAP-x.x.x.T1.azureha for HA |
| 1105 |
# GCP ontap_version format: ONTAP-x.x.x.T1.gcp or ONTAP-x.x.x.T1.gcpha for HA |
| 1106 |
version = 'ONTAP-' + ontap_version |
| 1107 |
if provider == 'aws': |
| 1108 |
version += '.T1.ha' if self.parameters['is_ha'] else '.T1' |
| 1109 |
elif provider == 'gcp': |
| 1110 |
version += '.T1' if not ontap_version.endswith('T1') else '' |
| 1111 |
version += '.gcpha' if self.parameters['is_ha'] else '.gcp' |
| 1112 |
api = '%s/metadata/permutations' % rest_api.api_root_path |
| 1113 |
params = {'region': region, |
| 1114 |
'version': version, |
| 1115 |
'instance_type': instance_type |
| 1116 |
} |
| 1117 |
response, error, dummy = rest_api.get(api, params=params, header=headers) |
| 1118 |
if error: |
| 1119 |
return None, "Error: get_license_type %s %s" % (response, error) |
| 1120 |
for item in response: |
| 1121 |
if item['license']['name'] == license_name: |
| 1122 |
return item['license']['type'], None |
| 1123 |
|
| 1124 |
return None, "Error: get_license_type cannot get license type %s" % response |
| 1125 |
|
| 1126 |
def get_modify_cvo_params(self, rest_api, headers, desired, provider): |
| 1127 |
modified = [] |
| 1128 |
if desired['update_svm_password']: |
| 1129 |
modified = ['svm_password'] |
| 1130 |
# Get current working environment property |
| 1131 |
properties = ['status', 'ontapClusterProperties.fields(upgradeVersions)'] |
| 1132 |
# instanceType in aws case is stored in awsProperties['instances'][0]['instanceType'] |
| 1133 |
if provider == 'aws': |
| 1134 |
properties.append('awsProperties') |
| 1135 |
else: |
| 1136 |
properties.append('providerProperties') |
| 1137 |
|
| 1138 |
we, err = self.get_working_environment_property(rest_api, headers, properties) |
| 1139 |
|
| 1140 |
if err is not None: |
| 1141 |
return None, err |
| 1142 |
|
| 1143 |
if we['status'] is None or we['status']['status'] != 'ON': |
| 1144 |
return None, "Error: get_modify_cvo_params working environment %s status is not ON. Operation cannot be performed." % we['publicId'] |
| 1145 |
|
| 1146 |
tier_level = None |
| 1147 |
if we['ontapClusterProperties']['capacityTierInfo'] is not None: |
| 1148 |
tier_level = we['ontapClusterProperties']['capacityTierInfo']['tierLevel'] |
| 1149 |
|
| 1150 |
# collect changed attributes |
| 1151 |
if tier_level is not None and tier_level != desired['tier_level']: |
| 1152 |
if provider == 'azure': |
| 1153 |
if desired['capacity_tier'] == 'Blob': |
| 1154 |
modified.append('tier_level') |
| 1155 |
elif provider == 'aws': |
| 1156 |
if desired['capacity_tier'] == 'S3': |
| 1157 |
modified.append('tier_level') |
| 1158 |
elif provider == 'gcp': |
| 1159 |
if desired['capacity_tier'] == 'cloudStorage': |
| 1160 |
modified.append('tier_level') |
| 1161 |
|
| 1162 |
if 'svm_name' in desired and we['svmName'] != desired['svm_name']: |
| 1163 |
modified.append('svm_name') |
| 1164 |
|
| 1165 |
if 'writing_speed_state' in desired: |
| 1166 |
if we['ontapClusterProperties']['writingSpeedState'] != desired['writing_speed_state'].upper(): |
| 1167 |
modified.append('writing_speed_state') |
| 1168 |
|
| 1169 |
if provider == 'aws': |
| 1170 |
current_instance_type = we['awsProperties']['instances'][0]['instanceType'] |
| 1171 |
region = we['awsProperties']['regionName'] |
| 1172 |
else: |
| 1173 |
current_instance_type = we['providerProperties']['instanceType'] |
| 1174 |
region = we['providerProperties']['regionName'] |
| 1175 |
|
| 1176 |
if current_instance_type != desired['instance_type']: |
| 1177 |
modified.append('instance_type') |
| 1178 |
|
| 1179 |
# check if license type is changed |
| 1180 |
current_license_type, error = self.get_license_type(rest_api, headers, provider, region, current_instance_type, |
| 1181 |
we['ontapClusterProperties']['ontapVersion'], |
| 1182 |
we['ontapClusterProperties']['licenseType']['name']) |
| 1183 |
if err is not None: |
| 1184 |
return None, error |
| 1185 |
if current_license_type != desired['license_type']: |
| 1186 |
modified.append('license_type') |
| 1187 |
|
| 1188 |
if desired['upgrade_ontap_version'] is True: |
| 1189 |
if desired['use_latest_version'] or desired['ontap_version'] == 'latest': |
| 1190 |
return None, "Error: To upgrade ONTAP image, the ontap_version must be a specific version" |
| 1191 |
current_version = 'ONTAP-' + we['ontapClusterProperties']['ontapVersion'] |
| 1192 |
if not desired['ontap_version'].startswith(current_version): |
| 1193 |
if we['ontapClusterProperties']['upgradeVersions'] is not None: |
| 1194 |
available_versions = [] |
| 1195 |
for image_info in we['ontapClusterProperties']['upgradeVersions']: |
| 1196 |
available_versions.append(image_info['imageVersion']) |
| 1197 |
# AWS ontap_version format: ONTAP-x.x.x.Tx or ONTAP-x.x.x.Tx.ha for Ha |
| 1198 |
# AZURE ontap_version format: ONTAP-x.x.x.Tx.azure or .azureha for HA |
| 1199 |
# GCP ontap_version format: ONTAP-x.x.x.Tx.gcp or .gcpha for HA |
| 1200 |
# Tx is not relevant for ONTAP version. But it is needed for the CVO creation |
| 1201 |
# upgradeVersion imageVersion format: ONTAP-x.x.x |
| 1202 |
if desired['ontap_version'].startswith(image_info['imageVersion']): |
| 1203 |
modified.append('ontap_version') |
| 1204 |
break |
| 1205 |
else: |
| 1206 |
return None, "Error: No ONTAP image available for version %s. Available versions: %s" % (desired['ontap_version'], available_versions) |
| 1207 |
|
| 1208 |
tag_name = { |
| 1209 |
'aws': 'aws_tag', |
| 1210 |
'azure': 'azure_tag', |
| 1211 |
'gcp': 'gcp_labels' |
| 1212 |
} |
| 1213 |
|
| 1214 |
need_change, error = self.is_cvo_tags_changed(rest_api, headers, desired, tag_name[provider]) |
| 1215 |
if error is not None: |
| 1216 |
return None, error |
| 1217 |
if need_change: |
| 1218 |
modified.append(tag_name[provider]) |
| 1219 |
|
| 1220 |
# The updates of followings are not supported. Will response failure. |
| 1221 |
for key, value in desired.items(): |
| 1222 |
if key == 'project_id' and we['providerProperties']['projectName'] != value: |
| 1223 |
modified.append('project_id') |
| 1224 |
if key == 'zone' and we['providerProperties']['zoneName'][0] != value: |
| 1225 |
modified.append('zone') |
| 1226 |
if key == 'cidr' and we['providerProperties']['vnetCidr'] != value: |
| 1227 |
modified.append('cidr') |
| 1228 |
if key == 'location' and we['providerProperties']['regionName'] != value: |
| 1229 |
modified.append('location') |
| 1230 |
if key == 'availability_zone' and we['providerProperties']['availabilityZone'] != value: |
| 1231 |
modified.append('availability_zone') |
| 1232 |
|
| 1233 |
if modified: |
| 1234 |
self.changed = True |
| 1235 |
return modified, None |
| 1236 |
|
| 1237 |
def is_cvo_update_needed(self, rest_api, headers, parameters, changeable_params, provider): |
| 1238 |
modify, error = self.get_modify_cvo_params(rest_api, headers, parameters, provider) |
| 1239 |
if error is not None: |
| 1240 |
return None, error |
| 1241 |
unmodifiable = [attr for attr in modify if attr not in changeable_params] |
| 1242 |
if unmodifiable: |
| 1243 |
return None, "%s cannot be modified." % str(unmodifiable) |
| 1244 |
|
| 1245 |
return modify, None |
| 1246 |
|
| 1247 |
def wait_cvo_update_complete(self, rest_api, headers): |
| 1248 |
retry_count = 65 |
| 1249 |
if self.parameters['is_ha'] is True: |
| 1250 |
retry_count *= 2 |
| 1251 |
for count in range(retry_count): |
| 1252 |
# get CVO status |
| 1253 |
we, err = self.get_working_environment_property(rest_api, headers, ['status']) |
| 1254 |
if err is not None: |
| 1255 |
return False, 'Error: get_working_environment_property failed: %s' % (str(err)) |
| 1256 |
if we['status']['status'] != "UPDATING": |
| 1257 |
return True, None |
| 1258 |
time.sleep(60) |
| 1259 |
|
| 1260 |
return False, 'Error: Taking too long for CVO to be active after update or not properly setup' |
| 1261 |
|
| 1262 |
def update_cvo_tags(self, api_root, rest_api, headers, tag_name, tag_list): |
| 1263 |
body = {} |
| 1264 |
tags = [] |
| 1265 |
if tag_list is not None: |
| 1266 |
for tag in tag_list: |
| 1267 |
atag = { |
| 1268 |
'tagKey': tag['label_key'] if tag_name == "gcp_labels" else tag['tag_key'], |
| 1269 |
'tagValue': tag['label_value'] if tag_name == "gcp_labels" else tag['tag_value'] |
| 1270 |
} |
| 1271 |
tags.append(atag) |
| 1272 |
body['tags'] = tags |
| 1273 |
|
| 1274 |
response, err, dummy = rest_api.put(api_root + "user-tags", body, header=headers) |
| 1275 |
if err is not None: |
| 1276 |
return False, 'Error: unexpected response on modifying tags: %s, %s' % (str(err), str(response)) |
| 1277 |
|
| 1278 |
return True, None |
| 1279 |
|
| 1280 |
def update_svm_password(self, api_root, rest_api, headers, svm_password): |
| 1281 |
body = {'password': svm_password} |
| 1282 |
response, err, dummy = rest_api.put(api_root + "set-password", body, header=headers) |
| 1283 |
if err is not None: |
| 1284 |
return False, 'Error: unexpected response on modifying svm_password: %s, %s' % (str(err), str(response)) |
| 1285 |
|
| 1286 |
return True, None |
| 1287 |
|
| 1288 |
def update_svm_name(self, api_root, rest_api, headers, svm_name): |
| 1289 |
# get current svmName |
| 1290 |
we, err = self.get_working_environment_property(rest_api, headers, ['ontapClusterProperties.fields(upgradeVersions)']) |
| 1291 |
if err is not None: |
| 1292 |
return False, 'Error: get_working_environment_property failed: %s' % (str(err)) |
| 1293 |
body = {'svmNewName': svm_name, |
| 1294 |
'svmName': we['svmName']} |
| 1295 |
response, err, dummy = rest_api.put(api_root + "svm", body, header=headers) |
| 1296 |
if err is not None: |
| 1297 |
return False, "update svm_name error" |
| 1298 |
return True, None |
| 1299 |
|
| 1300 |
def update_tier_level(self, api_root, rest_api, headers, tier_level): |
| 1301 |
body = {'level': tier_level} |
| 1302 |
response, err, dummy = rest_api.post(api_root + "change-tier-level", body, header=headers) |
| 1303 |
if err is not None: |
| 1304 |
return False, 'Error: unexpected response on modify tier_level: %s, %s' % (str(err), str(response)) |
| 1305 |
|
| 1306 |
return True, None |
| 1307 |
|
| 1308 |
def update_writing_speed_state(self, api_root, rest_api, headers, writing_speed_state): |
| 1309 |
body = {'writingSpeedState': writing_speed_state.upper()} |
| 1310 |
response, err, dummy = rest_api.put(api_root + "writing-speed", body, header=headers) |
| 1311 |
if err is not None: |
| 1312 |
return False, 'Error: unexpected response on modify writing_speed_state: %s, %s' % (str(err), str(response)) |
| 1313 |
# check upgrade status |
| 1314 |
dummy, err = self.wait_cvo_update_complete(rest_api, headers) |
| 1315 |
return err is None, err |
| 1316 |
|
| 1317 |
def update_instance_license_type(self, api_root, rest_api, headers, instance_type, license_type): |
| 1318 |
body = {'instanceType': instance_type, |
| 1319 |
'licenseType': license_type} |
| 1320 |
response, err, dummy = rest_api.put(api_root + "license-instance-type", body, header=headers) |
| 1321 |
if err is not None: |
| 1322 |
return False, 'Error: unexpected response on modify instance_type and license_type: %s, %s' % (str(err), str(response)) |
| 1323 |
# check upgrade status |
| 1324 |
dummy, err = self.wait_cvo_update_complete(rest_api, headers) |
| 1325 |
return err is None, err |
| 1326 |
|
| 1327 |
def set_config_flag(self, rest_api, headers): |
| 1328 |
body = {'value': True, 'valueType': 'BOOLEAN'} |
| 1329 |
base_url = '/occm/api/occm/config/skip-eligibility-paygo-upgrade' |
| 1330 |
response, err, dummy = rest_api.put(base_url, body, header=headers) |
| 1331 |
if err is not None: |
| 1332 |
return False, "set_config_flag error" |
| 1333 |
|
| 1334 |
return True, None |
| 1335 |
|
| 1336 |
def do_ontap_image_upgrade(self, rest_api, headers, desired): |
| 1337 |
# get ONTAP image version |
| 1338 |
we, err = self.get_working_environment_property(rest_api, headers, ['ontapClusterProperties.fields(upgradeVersions)']) |
| 1339 |
if err is not None: |
| 1340 |
return False, 'Error: get_working_environment_property failed: %s' % (str(err)) |
| 1341 |
body = {'updateType': "OCCM_PROVIDED"} |
| 1342 |
for image_info in we['ontapClusterProperties']['upgradeVersions']: |
| 1343 |
if image_info['imageVersion'] in desired: |
| 1344 |
body['updateParameter'] = image_info['imageVersion'] |
| 1345 |
break |
| 1346 |
# upgrade |
| 1347 |
base_url = "%s/working-environments/%s/update-image" % (rest_api.api_root_path, self.parameters['working_environment_id']) |
| 1348 |
response, err, dummy = rest_api.post(base_url, body, header=headers) |
| 1349 |
if err is not None: |
| 1350 |
return False, 'Error: unexpected response on do_ontap_image_upgrade: %s, %s' % (str(err), str(response)) |
| 1351 |
else: |
| 1352 |
return True, None |
| 1353 |
|
| 1354 |
def wait_ontap_image_upgrade_complete(self, rest_api, headers, desired): |
| 1355 |
retry_count = 65 |
| 1356 |
if self.parameters['is_ha'] is True: |
| 1357 |
retry_count *= 2 |
| 1358 |
for count in range(retry_count): |
| 1359 |
# get CVO status |
| 1360 |
we, err = self.get_working_environment_property(rest_api, headers, ['status', 'ontapClusterProperties']) |
| 1361 |
if err is not None: |
| 1362 |
return False, 'Error: get_working_environment_property failed: %s' % (str(err)) |
| 1363 |
if we['status']['status'] != "UPDATING" and we['ontapClusterProperties']['ontapVersion'] != "": |
| 1364 |
if we['ontapClusterProperties']['ontapVersion'] in desired: |
| 1365 |
return True, None |
| 1366 |
time.sleep(60) |
| 1367 |
|
| 1368 |
return False, 'Error: Taking too long for CVO to be active or not properly setup' |
| 1369 |
|
| 1370 |
def upgrade_ontap_image(self, rest_api, headers, desired): |
| 1371 |
# set flag |
| 1372 |
dummy, err = self.set_config_flag(rest_api, headers) |
| 1373 |
if err is not None: |
| 1374 |
return False, err |
| 1375 |
# upgrade |
| 1376 |
dummy, err = self.do_ontap_image_upgrade(rest_api, headers, desired) |
| 1377 |
if err is not None: |
| 1378 |
return False, err |
| 1379 |
# check upgrade status |
| 1380 |
dummy, err = self.wait_ontap_image_upgrade_complete(rest_api, headers, desired) |
| 1381 |
return err is None, err |