| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2023, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
''' |
| 7 |
na_ontap_license |
| 8 |
''' |
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
|
| 14 |
module: na_ontap_license |
| 15 |
|
| 16 |
short_description: NetApp ONTAP protocol and feature license packages |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: 2.6.0 |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
|
| 22 |
description: |
| 23 |
- Add or remove license packages on NetApp ONTAP. |
| 24 |
- Note that the module is asymmetrical. |
| 25 |
- It requires license codes to add packages and the package name is not visible. |
| 26 |
- It requires package names and as serial number to remove packages. |
| 27 |
|
| 28 |
options: |
| 29 |
state: |
| 30 |
description: |
| 31 |
- Whether the specified license packages should be installed or removed. |
| 32 |
choices: ['present', 'absent'] |
| 33 |
type: str |
| 34 |
default: present |
| 35 |
|
| 36 |
remove_unused: |
| 37 |
description: |
| 38 |
- Remove license packages that have no controller affiliation in the cluster. |
| 39 |
- Not supported with REST. |
| 40 |
type: bool |
| 41 |
|
| 42 |
remove_expired: |
| 43 |
description: |
| 44 |
- Remove license packages that have expired in the cluster. |
| 45 |
- Not supported with REST. |
| 46 |
type: bool |
| 47 |
|
| 48 |
serial_number: |
| 49 |
description: |
| 50 |
- Serial number of the node or cluster associated with the license package. |
| 51 |
- This parameter is required when removing a license package. |
| 52 |
- With REST, '*' is accepted and matches any serial number. |
| 53 |
type: str |
| 54 |
|
| 55 |
license_names: |
| 56 |
type: list |
| 57 |
elements: str |
| 58 |
description: |
| 59 |
- List of license package names to remove. |
| 60 |
suboptions: |
| 61 |
base: |
| 62 |
description: |
| 63 |
- Cluster Base License |
| 64 |
nfs: |
| 65 |
description: |
| 66 |
- NFS License |
| 67 |
cifs: |
| 68 |
description: |
| 69 |
- CIFS License |
| 70 |
iscsi: |
| 71 |
description: |
| 72 |
- iSCSI License |
| 73 |
fcp: |
| 74 |
description: |
| 75 |
- FCP License |
| 76 |
cdmi: |
| 77 |
description: |
| 78 |
- CDMI License |
| 79 |
snaprestore: |
| 80 |
description: |
| 81 |
- SnapRestore License |
| 82 |
snapmirror: |
| 83 |
description: |
| 84 |
- SnapMirror License |
| 85 |
flexclone: |
| 86 |
description: |
| 87 |
- FlexClone License |
| 88 |
snapvault: |
| 89 |
description: |
| 90 |
- SnapVault License |
| 91 |
snaplock: |
| 92 |
description: |
| 93 |
- SnapLock License |
| 94 |
snapmanagersuite: |
| 95 |
description: |
| 96 |
- SnapManagerSuite License |
| 97 |
snapprotectapps: |
| 98 |
description: |
| 99 |
- SnapProtectApp License |
| 100 |
v_storageattach: |
| 101 |
description: |
| 102 |
- Virtual Attached Storage License |
| 103 |
|
| 104 |
license_codes: |
| 105 |
description: |
| 106 |
- List of license codes to be installed. |
| 107 |
type: list |
| 108 |
elements: str |
| 109 |
|
| 110 |
notes: |
| 111 |
- Partially supports check_mode - some changes cannot be detected until an add or remove action is performed. |
| 112 |
- Supports 28 character key licenses with ZAPI and REST. |
| 113 |
- Supports NetApp License File Version 2 (NLFv2) with REST. |
| 114 |
- NetApp License File Version 1 (NLFv1) with REST is not supported at present but may work. |
| 115 |
- Ansible attempts to reformat license files as the contents are python-like. |
| 116 |
Use the string filter in case of problem to disable this behavior. |
| 117 |
- This module requires the python ast and json packages when the string filter is not used. |
| 118 |
- This module requires the json package to check for idempotency, and to remove licenses using a NLFv2 file. |
| 119 |
- This module requires the deepdiff package to check for idempotency. |
| 120 |
- None of these packages are required when the string filter is used, but the module will not be idempotent. |
| 121 |
''' |
| 122 |
|
| 123 |
|
| 124 |
EXAMPLES = """ |
| 125 |
- name: Add licenses - 28 character keys |
| 126 |
netapp.ontap.na_ontap_license: |
| 127 |
state: present |
| 128 |
serial_number: ################# |
| 129 |
license_codes: CODE1,CODE2 |
| 130 |
|
| 131 |
- name: Remove licenses |
| 132 |
netapp.ontap.na_ontap_license: |
| 133 |
state: absent |
| 134 |
remove_unused: false |
| 135 |
remove_expired: true |
| 136 |
serial_number: ################# |
| 137 |
license_names: nfs,cifs |
| 138 |
|
| 139 |
- name: Add NLF licenses |
| 140 |
netapp.ontap.na_ontap_license: |
| 141 |
state: present |
| 142 |
license_codes: |
| 143 |
- "{{ lookup('file', nlf_filepath) | string }}" |
| 144 |
|
| 145 |
- name: Remove NLF license bundle - using license file |
| 146 |
netapp.ontap.na_ontap_license: |
| 147 |
state: absent |
| 148 |
license_codes: |
| 149 |
- "{{ lookup('file', nlf_filepath) | string }}" |
| 150 |
|
| 151 |
- name: Remove NLF license bundle - using bundle name |
| 152 |
netapp.ontap.na_ontap_license: |
| 153 |
state: absent |
| 154 |
remove_unused: false |
| 155 |
remove_expired: true |
| 156 |
serial_number: ################# |
| 157 |
license_names: "Enterprise Edition" |
| 158 |
""" |
| 159 |
|
| 160 |
RETURN = """ |
| 161 |
updated_licenses: |
| 162 |
description: return list of updated package names |
| 163 |
returned: always |
| 164 |
type: dict |
| 165 |
sample: "['nfs']" |
| 166 |
""" |
| 167 |
|
| 168 |
HAS_AST = True |
| 169 |
HAS_DEEPDIFF = True |
| 170 |
HAS_JSON = True |
| 171 |
IMPORT_ERRORS = [] |
| 172 |
|
| 173 |
try: |
| 174 |
import ast |
| 175 |
except ImportError as exc: |
| 176 |
HAS_AST = False |
| 177 |
IMPORT_ERRORS.append(exc) |
| 178 |
|
| 179 |
try: |
| 180 |
from deepdiff import DeepDiff |
| 181 |
except (ImportError, SyntaxError) as exc: |
| 182 |
# With Ansible 2.9, python 2.6 reports a SyntaxError |
| 183 |
HAS_DEEPDIFF = False |
| 184 |
IMPORT_ERRORS.append(exc) |
| 185 |
|
| 186 |
try: |
| 187 |
import json |
| 188 |
except ImportError as exc: |
| 189 |
HAS_JSON = False |
| 190 |
IMPORT_ERRORS.append(exc) |
| 191 |
|
| 192 |
import re |
| 193 |
import sys |
| 194 |
import time |
| 195 |
import traceback |
| 196 |
|
| 197 |
from ansible.module_utils.basic import AnsibleModule |
| 198 |
from ansible.module_utils._text import to_native |
| 199 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 200 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 201 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 202 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 203 |
|
| 204 |
|
| 205 |
if sys.version_info < (3, 5): |
| 206 |
# not defined in earlier versions |
| 207 |
RecursionError = RuntimeError |
| 208 |
|
| 209 |
|
| 210 |
def local_cmp(a, b): |
| 211 |
""" |
| 212 |
compares with only values and not keys, keys should be the same for both dicts |
| 213 |
:param a: dict 1 |
| 214 |
:param b: dict 2 |
| 215 |
:return: difference of values in both dicts |
| 216 |
""" |
| 217 |
return [key for key in a if a[key] != b[key]] |
| 218 |
|
| 219 |
|
| 220 |
class NetAppOntapLicense: |
| 221 |
'''ONTAP license class''' |
| 222 |
|
| 223 |
def __init__(self): |
| 224 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 225 |
self.argument_spec.update(dict( |
| 226 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 227 |
serial_number=dict(required=False, type='str'), |
| 228 |
remove_unused=dict(default=None, type='bool'), |
| 229 |
remove_expired=dict(default=None, type='bool'), |
| 230 |
license_codes=dict(default=None, type='list', elements='str'), |
| 231 |
license_names=dict(default=None, type='list', elements='str'), |
| 232 |
)) |
| 233 |
self.module = AnsibleModule( |
| 234 |
argument_spec=self.argument_spec, |
| 235 |
supports_check_mode=False, |
| 236 |
required_if=[ |
| 237 |
('state', 'absent', ['license_codes', 'license_names'], True)], |
| 238 |
required_together=[ |
| 239 |
('serial_number', 'license_names')], |
| 240 |
) |
| 241 |
self.na_helper = NetAppModule() |
| 242 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 243 |
self.license_status = {} |
| 244 |
# list of tuples - original licenses (license_code or NLF contents), and dict of NLF contents (empty dict for legacy codes) |
| 245 |
self.nlfs = [] |
| 246 |
# when using REST, just keep a list as returned by GET to use with deepdiff |
| 247 |
self.previous_records = [] |
| 248 |
|
| 249 |
# Set up REST API |
| 250 |
self.rest_api = OntapRestAPI(self.module) |
| 251 |
unsupported_rest_properties = ['remove_unused', 'remove_expired'] |
| 252 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties) |
| 253 |
if not self.use_rest: |
| 254 |
if not netapp_utils.has_netapp_lib(): |
| 255 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 256 |
else: |
| 257 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 258 |
self.validate_nlfs() |
| 259 |
|
| 260 |
def get_licensing_status(self): |
| 261 |
""" |
| 262 |
Check licensing status |
| 263 |
|
| 264 |
:return: package (key) and licensing status (value) |
| 265 |
:rtype: dict |
| 266 |
""" |
| 267 |
if self.use_rest: |
| 268 |
return self.get_licensing_status_rest() |
| 269 |
license_status = netapp_utils.zapi.NaElement( |
| 270 |
'license-v2-status-list-info') |
| 271 |
result = None |
| 272 |
try: |
| 273 |
result = self.server.invoke_successfully(license_status, |
| 274 |
enable_tunneling=False) |
| 275 |
except netapp_utils.zapi.NaApiError as error: |
| 276 |
self.module.fail_json(msg="Error checking license status: %s" % |
| 277 |
to_native(error), exception=traceback.format_exc()) |
| 278 |
|
| 279 |
return_dictionary = {} |
| 280 |
license_v2_status = result.get_child_by_name('license-v2-status') |
| 281 |
if license_v2_status: |
| 282 |
for license_v2_status_info in license_v2_status.get_children(): |
| 283 |
package = license_v2_status_info.get_child_content('package') |
| 284 |
status = license_v2_status_info.get_child_content('method') |
| 285 |
return_dictionary[package] = status |
| 286 |
return return_dictionary, None |
| 287 |
|
| 288 |
def get_licensing_status_rest(self): |
| 289 |
api = 'cluster/licensing/licenses' |
| 290 |
# By default, the GET method only returns licensed packages. |
| 291 |
# To retrieve all the available package state details, below query is used. |
| 292 |
query = {'state': 'compliant, noncompliant, unlicensed, unknown'} |
| 293 |
fields = 'name,state,licenses' |
| 294 |
records, error = rest_generic.get_0_or_more_records(self.rest_api, api, query, fields) |
| 295 |
if error: |
| 296 |
self.module.fail_json(msg=error) |
| 297 |
current = {'installed_licenses': {}} |
| 298 |
if records: |
| 299 |
for package in records: |
| 300 |
current[package['name']] = package['state'] |
| 301 |
if 'licenses' in package: |
| 302 |
for license in package['licenses']: |
| 303 |
installed_license = license.get('installed_license') |
| 304 |
serial_number = license.get('serial_number') |
| 305 |
if serial_number and installed_license: |
| 306 |
if serial_number not in current: |
| 307 |
current['installed_licenses'][serial_number] = set() |
| 308 |
current['installed_licenses'][serial_number].add(installed_license) |
| 309 |
return current, records |
| 310 |
|
| 311 |
def remove_licenses(self, package_name, nlf_dict=None): |
| 312 |
""" |
| 313 |
Remove requested licenses |
| 314 |
:param: |
| 315 |
package_name: Name of the license to be deleted |
| 316 |
""" |
| 317 |
if self.use_rest: |
| 318 |
return self.remove_licenses_rest(package_name, nlf_dict or {}) |
| 319 |
license_delete = netapp_utils.zapi.NaElement('license-v2-delete') |
| 320 |
license_delete.add_new_child('serial-number', self.parameters['serial_number']) |
| 321 |
license_delete.add_new_child('package', package_name) |
| 322 |
try: |
| 323 |
self.server.invoke_successfully(license_delete, |
| 324 |
enable_tunneling=False) |
| 325 |
return True |
| 326 |
except netapp_utils.zapi.NaApiError as error: |
| 327 |
# Error 15661 - Object not found |
| 328 |
if to_native(error.code) == "15661": |
| 329 |
return False |
| 330 |
else: |
| 331 |
self.module.fail_json(msg="Error removing license %s" % |
| 332 |
to_native(error), exception=traceback.format_exc()) |
| 333 |
|
| 334 |
def remove_licenses_rest(self, package_name, nlf_dict): |
| 335 |
""" |
| 336 |
This is called either with a package name or a NLF dict |
| 337 |
We already validated product and serialNumber are present in nlf_dict |
| 338 |
""" |
| 339 |
p_serial_number = self.parameters.get('serial_number') |
| 340 |
n_serial_number = nlf_dict.get('serialNumber') |
| 341 |
n_product = nlf_dict.get('product') |
| 342 |
serial_number = n_serial_number or p_serial_number |
| 343 |
if not serial_number: |
| 344 |
self.module.fail_json(msg='Error: serial_number is required to delete a license.') |
| 345 |
if n_product: |
| 346 |
error = self.remove_one_license_rest(None, n_product, serial_number) |
| 347 |
elif package_name.endswith(('Bundle', 'Edition')): |
| 348 |
error = self.remove_one_license_rest(None, package_name, serial_number) |
| 349 |
else: |
| 350 |
error = self.remove_one_license_rest(package_name, None, serial_number) |
| 351 |
if error and "entry doesn't exist" in error: |
| 352 |
return False |
| 353 |
if error: |
| 354 |
self.module.fail_json(msg="Error removing license for serial number %s and %s: %s" |
| 355 |
% (serial_number, n_product or package_name, error)) |
| 356 |
return True |
| 357 |
|
| 358 |
def remove_one_license_rest(self, package_name, product, serial_number): |
| 359 |
api = 'cluster/licensing/licenses' |
| 360 |
query = {'serial_number': serial_number} |
| 361 |
if product: |
| 362 |
query['licenses.installed_license'] = product.replace(' ', '*') |
| 363 |
# since this is a query, we need to specify state, or only active licenses are removed |
| 364 |
query['state'] = '*' |
| 365 |
dummy, error = rest_generic.delete_async(self.rest_api, api, package_name, query) |
| 366 |
return error |
| 367 |
|
| 368 |
def remove_unused_licenses(self): |
| 369 |
""" |
| 370 |
Remove unused licenses |
| 371 |
""" |
| 372 |
remove_unused = netapp_utils.zapi.NaElement('license-v2-delete-unused') |
| 373 |
try: |
| 374 |
self.server.invoke_successfully(remove_unused, |
| 375 |
enable_tunneling=False) |
| 376 |
except netapp_utils.zapi.NaApiError as error: |
| 377 |
self.module.fail_json(msg="Error removing unused licenses: %s" % |
| 378 |
to_native(error), exception=traceback.format_exc()) |
| 379 |
|
| 380 |
def remove_expired_licenses(self): |
| 381 |
""" |
| 382 |
Remove expired licenses |
| 383 |
""" |
| 384 |
remove_expired = netapp_utils.zapi.NaElement( |
| 385 |
'license-v2-delete-expired') |
| 386 |
try: |
| 387 |
self.server.invoke_successfully(remove_expired, |
| 388 |
enable_tunneling=False) |
| 389 |
except netapp_utils.zapi.NaApiError as error: |
| 390 |
self.module.fail_json(msg="Error removing expired licenses: %s" % |
| 391 |
to_native(error), exception=traceback.format_exc()) |
| 392 |
|
| 393 |
def add_licenses(self): |
| 394 |
""" |
| 395 |
Add licenses |
| 396 |
""" |
| 397 |
if self.use_rest: |
| 398 |
return self.add_licenses_rest() |
| 399 |
license_add = netapp_utils.zapi.NaElement('license-v2-add') |
| 400 |
codes = netapp_utils.zapi.NaElement('codes') |
| 401 |
for code in self.parameters['license_codes']: |
| 402 |
codes.add_new_child('license-code-v2', str(code.strip().lower())) |
| 403 |
license_add.add_child_elem(codes) |
| 404 |
try: |
| 405 |
self.server.invoke_successfully(license_add, |
| 406 |
enable_tunneling=False) |
| 407 |
except netapp_utils.zapi.NaApiError as error: |
| 408 |
self.module.fail_json(msg="Error adding licenses: %s" % |
| 409 |
to_native(error), exception=traceback.format_exc()) |
| 410 |
|
| 411 |
def add_licenses_rest(self): |
| 412 |
api = 'cluster/licensing/licenses' |
| 413 |
body = {'keys': [x[0] for x in self.nlfs]} |
| 414 |
headers = None |
| 415 |
if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 9, 1): |
| 416 |
# request nested errors |
| 417 |
headers = {'X-Dot-Error-Arguments': 'true'} |
| 418 |
dummy, error = rest_generic.post_async(self.rest_api, api, body, headers=headers) |
| 419 |
if error: |
| 420 |
error = self.format_post_error(error, body) |
| 421 |
if 'conflicts' in error: |
| 422 |
return error |
| 423 |
self.module.fail_json(msg="Error adding license: %s - previous license status: %s" % (error, self.license_status)) |
| 424 |
return None |
| 425 |
|
| 426 |
def compare_license_status(self, previous_license_status): |
| 427 |
changed_keys = [] |
| 428 |
for __ in range(5): |
| 429 |
error = None |
| 430 |
new_license_status, records = self.get_licensing_status() |
| 431 |
try: |
| 432 |
changed_keys = local_cmp(previous_license_status, new_license_status) |
| 433 |
break |
| 434 |
except KeyError as exc: |
| 435 |
# when a new license is added, it seems REST may not report all licenses |
| 436 |
# wait for things to stabilize |
| 437 |
error = exc |
| 438 |
time.sleep(5) |
| 439 |
if error: |
| 440 |
self.module.fail_json(msg='Error: mismatch in license package names: %s. Expected: %s, found: %s.' |
| 441 |
% (error, previous_license_status.keys(), new_license_status.keys())) |
| 442 |
if 'installed_licenses' in changed_keys: |
| 443 |
changed_keys.remove('installed_licenses') |
| 444 |
if records and self.previous_records: |
| 445 |
deep_changed_keys = self.deep_compare(records) |
| 446 |
for key in deep_changed_keys: |
| 447 |
if key not in changed_keys: |
| 448 |
changed_keys.append(key) |
| 449 |
return changed_keys |
| 450 |
|
| 451 |
def deep_compare(self, records): |
| 452 |
""" look for any change in license details, capacity, expiration, ... |
| 453 |
this is run after apply, so we don't know for sure in check_mode |
| 454 |
""" |
| 455 |
if not HAS_DEEPDIFF: |
| 456 |
self.module.warn('deepdiff is required to identify detailed changes') |
| 457 |
return [] |
| 458 |
diffs = DeepDiff(self.previous_records, records) |
| 459 |
self.rest_api.log_debug('diffs', diffs) |
| 460 |
roots = set(re.findall(r'root\[(\d+)\]', str(diffs))) |
| 461 |
result = [records[int(index)]['name'] for index in roots] |
| 462 |
self.rest_api.log_debug('deep_changed_keys', result) |
| 463 |
return result |
| 464 |
|
| 465 |
def reformat_nlf(self, license_code): |
| 466 |
# Ansible converts double quotes into single quotes if the input is python-like |
| 467 |
# and we can't use json loads with single quotes! |
| 468 |
if not HAS_AST or not HAS_JSON: |
| 469 |
return None, "ast and json packages are required to install NLF license files. Import error(s): %s." % IMPORT_ERRORS |
| 470 |
try: |
| 471 |
nlf_dict = ast.literal_eval(license_code) |
| 472 |
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError) as exc: |
| 473 |
return None, "malformed input: %s, exception: %s" % (license_code, exc) |
| 474 |
try: |
| 475 |
license_code = json.dumps(nlf_dict, separators=(',', ':')) |
| 476 |
except Exception as exc: |
| 477 |
return None, "unable to encode input: %s - evaluated as %s, exception: %s" % (license_code, nlf_dict, exc) |
| 478 |
return license_code, None |
| 479 |
|
| 480 |
def get_nlf_dict(self, license_code): |
| 481 |
nlf_dict = {} |
| 482 |
is_nlf = False |
| 483 |
if '"statusResp"' in license_code: |
| 484 |
if license_code.count('"statusResp"') > 1: |
| 485 |
self.module.fail_json(msg="Error: NLF license files with multiple licenses are not supported, found %d in %s." |
| 486 |
% (license_code.count('"statusResp"'), license_code)) |
| 487 |
if license_code.count('"serialNumber"') > 1: |
| 488 |
self.module.fail_json(msg="Error: NLF license files with multiple serial numbers are not supported, found %d in %s." |
| 489 |
% (license_code.count('"serialNumber"'), license_code)) |
| 490 |
is_nlf = True |
| 491 |
if not HAS_JSON: |
| 492 |
return nlf_dict, is_nlf, "the json package is required to process NLF license files. Import error(s): %s." % IMPORT_ERRORS |
| 493 |
try: |
| 494 |
nlf_dict = json.loads(license_code) |
| 495 |
except Exception as exc: |
| 496 |
return nlf_dict, is_nlf, "the license contents cannot be read. Unable to decode input: %s - exception: %s." % (license_code, exc) |
| 497 |
return nlf_dict, is_nlf, None |
| 498 |
|
| 499 |
def scan_license_codes_for_nlf(self, license_code): |
| 500 |
more_info = "You %s seeing this error because the original NLF contents were modified by Ansible. You can use the string filter to keep the original." |
| 501 |
transformed = False |
| 502 |
original_license_code = license_code |
| 503 |
|
| 504 |
if "'statusResp'" in license_code: |
| 505 |
license_code, error = self.reformat_nlf(license_code) |
| 506 |
if error: |
| 507 |
error = 'Error: %s %s' % (error, more_info % 'are') |
| 508 |
self.module.fail_json(msg=error) |
| 509 |
transformed = True |
| 510 |
|
| 511 |
# For an NLF license, extract fields, to later collect serial number and bundle name (product) |
| 512 |
nlf_dict, is_nlf, error = self.get_nlf_dict(license_code) |
| 513 |
if error and transformed: |
| 514 |
error = 'Error: %s. Ansible input: %s %s' % (error, original_license_code, more_info % 'may be') |
| 515 |
self.module.fail_json(msg=error) |
| 516 |
|
| 517 |
if error: |
| 518 |
msg = "The license " + ( |
| 519 |
"will be installed without checking for idempotency." if self.parameters['state'] == 'present' else "cannot be removed.") |
| 520 |
msg += " You are seeing this warning because " + error |
| 521 |
self.module.warn(msg) |
| 522 |
|
| 523 |
return license_code, nlf_dict, is_nlf |
| 524 |
|
| 525 |
def split_nlf(self, license_code): |
| 526 |
""" A NLF file may contain several licenses |
| 527 |
One license per line |
| 528 |
Return a list of 1 or more licenses |
| 529 |
""" |
| 530 |
licenses = license_code.count('"statusResp"') |
| 531 |
if licenses <= 1: |
| 532 |
return [license_code] |
| 533 |
nlfs = license_code.splitlines() |
| 534 |
if len(nlfs) != licenses: |
| 535 |
self.module.fail_json(msg="Error: unexpected format found %d entries and %d lines in %s" |
| 536 |
% (licenses, len(nlfs), license_code)) |
| 537 |
return nlfs |
| 538 |
|
| 539 |
def split_nlfs(self): |
| 540 |
""" A NLF file may contain several licenses |
| 541 |
Return a flattened list of license codes |
| 542 |
""" |
| 543 |
license_codes = [] |
| 544 |
for license in self.parameters.get('license_codes', []): |
| 545 |
license_codes.extend(self.split_nlf(license)) |
| 546 |
return license_codes |
| 547 |
|
| 548 |
def validate_nlfs(self): |
| 549 |
self.parameters['license_codes'] = self.split_nlfs() |
| 550 |
nlf_count = 0 |
| 551 |
for license in self.parameters['license_codes']: |
| 552 |
nlf, nlf_dict, is_nlf = self.scan_license_codes_for_nlf(license) |
| 553 |
if is_nlf and not self.use_rest: |
| 554 |
self.module.fail_json(msg="Error: NLF license format is not supported with ZAPI.") |
| 555 |
self.nlfs.append((nlf, nlf_dict)) |
| 556 |
if is_nlf: |
| 557 |
nlf_count += 1 |
| 558 |
if nlf_count and nlf_count != len(self.parameters['license_codes']): |
| 559 |
self.module.fail_json(msg="Error: cannot mix legacy licenses and NLF licenses; found %d NLF licenses out of %d license_codes." |
| 560 |
% (nlf_count, len(self.parameters['license_codes']))) |
| 561 |
|
| 562 |
def get_key(self, error, body): |
| 563 |
needle = r'Failed to install the license at index (\d+)' |
| 564 |
matched = re.search(needle, error) |
| 565 |
if matched: |
| 566 |
index = int(matched.group(1)) |
| 567 |
return body['keys'][index] |
| 568 |
return None |
| 569 |
|
| 570 |
def format_post_error(self, error, body): |
| 571 |
if 'The system received a licensing request with an invalid digital signature.' in error: |
| 572 |
key = self.get_key(error, body) |
| 573 |
if key and "'statusResp'" in key: |
| 574 |
error = 'Original NLF contents were modified by Ansible. Make sure to use the string filter. REST error: %s' % error |
| 575 |
return error |
| 576 |
|
| 577 |
def nlf_is_installed(self, nlf_dict): |
| 578 |
""" return True if NLF with same SN, product (bundle) name and package list is present |
| 579 |
return False otherwise |
| 580 |
Even when present, the NLF may not be active, so this is only useful for delete |
| 581 |
""" |
| 582 |
n_serial_number, n_product = self.get_sn_and_product(nlf_dict) |
| 583 |
if not n_product or not n_serial_number: |
| 584 |
return False |
| 585 |
if 'installed_licenses' not in self.license_status: |
| 586 |
# nothing is installed |
| 587 |
return False |
| 588 |
if n_serial_number == '*' and self.parameters['state'] == 'absent': |
| 589 |
# force a delete |
| 590 |
return True |
| 591 |
if n_serial_number not in self.license_status['installed_licenses']: |
| 592 |
return False |
| 593 |
return n_product in self.license_status['installed_licenses'][n_serial_number] |
| 594 |
|
| 595 |
def get_sn_and_product(self, nlf_dict): |
| 596 |
# V2 and V1 formats |
| 597 |
n_serial_number = self.na_helper.safe_get(nlf_dict, ['statusResp', 'serialNumber'])\ |
| 598 |
or self.na_helper.safe_get(nlf_dict, ['statusResp', 'licenses', 'serialNumber']) |
| 599 |
n_product = self.na_helper.safe_get(nlf_dict, ['statusResp', 'product'])\ |
| 600 |
or self.na_helper.safe_get(nlf_dict, ['statusResp', 'licenses', 'product']) |
| 601 |
return n_serial_number, n_product |
| 602 |
|
| 603 |
def validate_delete_action(self, nlf_dict): |
| 604 |
""" make sure product and serialNumber are set at the top level (V2 format) """ |
| 605 |
# product is required for delete |
| 606 |
n_serial_number, n_product = self.get_sn_and_product(nlf_dict) |
| 607 |
if nlf_dict and not n_product: |
| 608 |
self.module.fail_json(msg='Error: product not found in NLF file %s.' % nlf_dict) |
| 609 |
# if serial number is not present in the NLF, we could use a module parameter |
| 610 |
p_serial_number = self.parameters.get('serial_number') |
| 611 |
if p_serial_number and n_serial_number and p_serial_number != n_serial_number: |
| 612 |
self.module.fail_json(msg='Error: mismatch is serial numbers %s vs %s' % (p_serial_number, n_serial_number)) |
| 613 |
if nlf_dict and not n_serial_number and not p_serial_number: |
| 614 |
self.module.fail_json(msg='Error: serialNumber not found in NLF file. It can be set in the module parameter.') |
| 615 |
nlf_dict['serialNumber'] = n_serial_number or p_serial_number |
| 616 |
nlf_dict['product'] = n_product |
| 617 |
|
| 618 |
def get_delete_actions(self): |
| 619 |
packages_to_delete = [] |
| 620 |
if self.parameters.get('license_names') is not None: |
| 621 |
for package in list(self.parameters['license_names']): |
| 622 |
if 'installed_licenses' in self.license_status and self.parameters['serial_number'] != '*'\ |
| 623 |
and self.parameters['serial_number'] in self.license_status['installed_licenses']\ |
| 624 |
and package in self.license_status['installed_licenses'][self.parameters['serial_number']]: |
| 625 |
packages_to_delete.append(package) |
| 626 |
if package in self.license_status: |
| 627 |
packages_to_delete.append(package) |
| 628 |
|
| 629 |
for dummy, nlf_dict in self.nlfs: |
| 630 |
if nlf_dict: |
| 631 |
self.validate_delete_action(nlf_dict) |
| 632 |
nlfs_to_delete = [ |
| 633 |
nlf_dict |
| 634 |
for dummy, nlf_dict in self.nlfs |
| 635 |
if self.nlf_is_installed(nlf_dict) |
| 636 |
] |
| 637 |
return bool(nlfs_to_delete) or bool(self.parameters.get('license_names')), packages_to_delete, nlfs_to_delete |
| 638 |
|
| 639 |
def get_add_actions(self): |
| 640 |
""" add licenses unconditionally |
| 641 |
for legacy licenses we don't know if they are already installed |
| 642 |
for NLF licenses we don't know if some details have changed (eg capacity, expiration date) |
| 643 |
""" |
| 644 |
return bool(self.nlfs), [license_code for license_code, dummy in self.nlfs] |
| 645 |
|
| 646 |
def get_actions(self): |
| 647 |
changed = False |
| 648 |
licenses_to_add = [] |
| 649 |
nlfs_to_delete = [] |
| 650 |
remove_license = False |
| 651 |
packages_to_delete = [] |
| 652 |
nlfs_to_delete = [] |
| 653 |
# Add / Update licenses. |
| 654 |
self.license_status, self.previous_records = self.get_licensing_status() |
| 655 |
if self.parameters['state'] == 'absent': # delete |
| 656 |
changed, packages_to_delete, nlfs_to_delete = self.get_delete_actions() |
| 657 |
else: # add or update |
| 658 |
changed, licenses_to_add = self.get_add_actions() |
| 659 |
if self.parameters.get('remove_unused') is not None: |
| 660 |
remove_license = True |
| 661 |
changed = True |
| 662 |
if self.parameters.get('remove_expired') is not None: |
| 663 |
remove_license = True |
| 664 |
changed = True |
| 665 |
return changed, licenses_to_add, remove_license, packages_to_delete, nlfs_to_delete |
| 666 |
|
| 667 |
def apply(self): |
| 668 |
'''Call add, delete or modify methods''' |
| 669 |
changed, licenses_to_add, remove_license, packages_to_delete, nlfs_to_delete = self.get_actions() |
| 670 |
error, changed_keys = None, [] |
| 671 |
if changed and not self.module.check_mode: |
| 672 |
if self.parameters['state'] == 'present': # execute create |
| 673 |
if licenses_to_add: |
| 674 |
error = self.add_licenses() |
| 675 |
if self.parameters.get('remove_unused') is not None: |
| 676 |
self.remove_unused_licenses() |
| 677 |
if self.parameters.get('remove_expired') is not None: |
| 678 |
self.remove_expired_licenses() |
| 679 |
# not able to detect that a new license is required until we try to install it. |
| 680 |
if licenses_to_add or remove_license: |
| 681 |
changed_keys = self.compare_license_status(self.license_status) |
| 682 |
# delete actions |
| 683 |
else: |
| 684 |
if nlfs_to_delete: |
| 685 |
changed_keys.extend([nlf_dict.get("product") for nlf_dict in nlfs_to_delete if self.remove_licenses(None, nlf_dict)]) |
| 686 |
if packages_to_delete: |
| 687 |
changed_keys.extend([package for package in self.parameters['license_names'] if self.remove_licenses(package)]) |
| 688 |
if not changed_keys: |
| 689 |
changed = False |
| 690 |
|
| 691 |
if error: |
| 692 |
error = 'Error: ' + ( |
| 693 |
'some licenses were updated, but others were in conflict: ' |
| 694 |
if changed_keys |
| 695 |
else 'adding licenses: ' |
| 696 |
) + error |
| 697 |
self.module.fail_json(msg=error, changed=changed, updated_licenses=changed_keys) |
| 698 |
self.module.exit_json(changed=changed, updated_licenses=changed_keys) |
| 699 |
|
| 700 |
|
| 701 |
def main(): |
| 702 |
'''Apply license operations''' |
| 703 |
obj = NetAppOntapLicense() |
| 704 |
obj.apply() |
| 705 |
|
| 706 |
|
| 707 |
if __name__ == '__main__': |
| 708 |
main() |