| 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) 2018, 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 |
|
| 33 |
from copy import deepcopy |
| 34 |
|
| 35 |
__metaclass__ = type |
| 36 |
|
| 37 |
|
| 38 |
def cmp(obj1, obj2): |
| 39 |
""" |
| 40 |
Python 3 does not have a cmp function, this will do the cmp. |
| 41 |
:param obj1: first object to check |
| 42 |
:param obj2: second object to check |
| 43 |
:return: |
| 44 |
""" |
| 45 |
# convert to lower case for string comparison. |
| 46 |
if obj1 is None: |
| 47 |
return -1 |
| 48 |
if isinstance(obj1, str) and isinstance(obj2, str): |
| 49 |
obj1 = obj1.lower() |
| 50 |
obj2 = obj2.lower() |
| 51 |
# if list has string element, convert string to lower case. |
| 52 |
if isinstance(obj1, list) and isinstance(obj2, list): |
| 53 |
obj1 = [x.lower() if isinstance(x, str) else x for x in obj1] |
| 54 |
obj2 = [x.lower() if isinstance(x, str) else x for x in obj2] |
| 55 |
obj1.sort() |
| 56 |
obj2.sort() |
| 57 |
return (obj1 > obj2) - (obj1 < obj2) |
| 58 |
|
| 59 |
|
| 60 |
class NetAppModule(object): |
| 61 |
""" |
| 62 |
Common class for NetApp modules |
| 63 |
set of support functions to derive actions based |
| 64 |
on the current state of the system, and a desired state |
| 65 |
""" |
| 66 |
|
| 67 |
def __init__(self): |
| 68 |
self.log = list() |
| 69 |
self.changed = False |
| 70 |
self.parameters = {"name": "not initialized"} |
| 71 |
|
| 72 |
def set_parameters(self, ansible_params): |
| 73 |
self.parameters = dict() |
| 74 |
for param in ansible_params: |
| 75 |
if ansible_params[param] is not None: |
| 76 |
self.parameters[param] = ansible_params[param] |
| 77 |
return self.parameters |
| 78 |
|
| 79 |
def get_cd_action(self, current, desired): |
| 80 |
"""takes a desired state and a current state, and return an action: |
| 81 |
create, delete, None |
| 82 |
eg: |
| 83 |
is_present = 'absent' |
| 84 |
some_object = self.get_object(source) |
| 85 |
if some_object is not None: |
| 86 |
is_present = 'present' |
| 87 |
action = cd_action(current=is_present, desired = self.desired.state()) |
| 88 |
""" |
| 89 |
if "state" in desired: |
| 90 |
desired_state = desired["state"] |
| 91 |
else: |
| 92 |
desired_state = "present" |
| 93 |
|
| 94 |
if current is None and desired_state == "absent": |
| 95 |
return None |
| 96 |
if current is not None and desired_state == "present": |
| 97 |
return None |
| 98 |
# change in state |
| 99 |
self.changed = True |
| 100 |
if current is not None: |
| 101 |
return "delete" |
| 102 |
return "create" |
| 103 |
|
| 104 |
def compare_and_update_values(self, current, desired, keys_to_compare): |
| 105 |
updated_values = dict() |
| 106 |
is_changed = False |
| 107 |
for key in keys_to_compare: |
| 108 |
if key in current: |
| 109 |
if key in desired and desired[key] is not None: |
| 110 |
if current[key] != desired[key]: |
| 111 |
updated_values[key] = desired[key] |
| 112 |
is_changed = True |
| 113 |
else: |
| 114 |
updated_values[key] = current[key] |
| 115 |
else: |
| 116 |
updated_values[key] = current[key] |
| 117 |
|
| 118 |
return updated_values, is_changed |
| 119 |
|
| 120 |
@staticmethod |
| 121 |
def check_keys(current, desired): |
| 122 |
"""TODO: raise an error if keys do not match |
| 123 |
with the exception of: |
| 124 |
new_name, state in desired |
| 125 |
""" |
| 126 |
|
| 127 |
def is_rename_action(self, source, target): |
| 128 |
"""takes a source and target object, and returns True |
| 129 |
if a rename is required |
| 130 |
eg: |
| 131 |
source = self.get_object(source_name) |
| 132 |
target = self.get_object(target_name) |
| 133 |
action = is_rename_action(source, target) |
| 134 |
:return: None for error, True for rename action, False otherwise |
| 135 |
""" |
| 136 |
if source is None and target is None: |
| 137 |
# error, do nothing |
| 138 |
# cannot rename an non existent resource |
| 139 |
# alternatively we could create B |
| 140 |
return None |
| 141 |
if source is not None and target is not None: |
| 142 |
# error, do nothing |
| 143 |
# idempotency (or) new_name_is_already_in_use |
| 144 |
# alternatively we could delete B and rename A to B |
| 145 |
return False |
| 146 |
if source is None and target is not None: |
| 147 |
# do nothing, maybe the rename was already done |
| 148 |
return False |
| 149 |
# source is not None and target is None: |
| 150 |
# rename is in order |
| 151 |
self.changed = True |
| 152 |
return True |
| 153 |
|
| 154 |
@staticmethod |
| 155 |
def compare_lists(current, desired, get_list_diff): |
| 156 |
"""compares two lists and return a list of elements that are either the desired elements or elements that are |
| 157 |
modified from the current state depending on the get_list_diff flag |
| 158 |
:param: current: current item attribute in ONTAP |
| 159 |
:param: desired: attributes from playbook |
| 160 |
:param: get_list_diff: specifies whether to have a diff of desired list w.r.t current list for an attribute |
| 161 |
:return: list of attributes to be modified |
| 162 |
:rtype: list |
| 163 |
""" |
| 164 |
current_copy = deepcopy(current) |
| 165 |
desired_copy = deepcopy(desired) |
| 166 |
|
| 167 |
# get what in desired and not in current |
| 168 |
desired_diff_list = list() |
| 169 |
for item in desired: |
| 170 |
if item in current_copy: |
| 171 |
current_copy.remove(item) |
| 172 |
else: |
| 173 |
desired_diff_list.append(item) |
| 174 |
|
| 175 |
# get what in current but not in desired |
| 176 |
current_diff_list = [] |
| 177 |
for item in current: |
| 178 |
if item in desired_copy: |
| 179 |
desired_copy.remove(item) |
| 180 |
else: |
| 181 |
current_diff_list.append(item) |
| 182 |
|
| 183 |
if desired_diff_list or current_diff_list: |
| 184 |
# there are changes |
| 185 |
if get_list_diff: |
| 186 |
return desired_diff_list |
| 187 |
else: |
| 188 |
return desired |
| 189 |
else: |
| 190 |
return None |
| 191 |
|
| 192 |
def get_modified_attributes(self, current, desired, get_list_diff=False): |
| 193 |
"""takes two dicts of attributes and return a dict of attributes that are |
| 194 |
not in the current state |
| 195 |
It is expected that all attributes of interest are listed in current and |
| 196 |
desired. |
| 197 |
:param: current: current attributes on StorageGRID |
| 198 |
:param: desired: attributes from playbook |
| 199 |
:param: get_list_diff: specifies whether to have a diff of desired list w.r.t current list for an attribute |
| 200 |
:return: dict of attributes to be modified |
| 201 |
:rtype: dict |
| 202 |
NOTE: depending on the attribute, the caller may need to do a modify or a |
| 203 |
different operation (eg move volume if the modified attribute is an |
| 204 |
aggregate name) |
| 205 |
""" |
| 206 |
# if the object does not exist, we can't modify it |
| 207 |
modified = {} |
| 208 |
if current is None: |
| 209 |
return modified |
| 210 |
|
| 211 |
# error out if keys do not match |
| 212 |
self.check_keys(current, desired) |
| 213 |
|
| 214 |
# collect changed attributes |
| 215 |
for key, value in current.items(): |
| 216 |
if key in desired and desired[key] is not None: |
| 217 |
if isinstance(value, list): |
| 218 |
modified_list = self.compare_lists( |
| 219 |
value, desired[key], get_list_diff |
| 220 |
) # get modified list from current and desired |
| 221 |
if modified_list is not None: |
| 222 |
modified[key] = modified_list |
| 223 |
elif isinstance(value, dict): |
| 224 |
modified_dict = self.get_modified_attributes(value, desired[key]) |
| 225 |
if modified_dict: |
| 226 |
modified[key] = modified_dict |
| 227 |
else: |
| 228 |
try: |
| 229 |
result = cmp(value, desired[key]) |
| 230 |
except TypeError as exc: |
| 231 |
raise TypeError( |
| 232 |
"%s, key: %s, value: %s, desired: %s" % (repr(exc), key, repr(value), repr(desired[key])) |
| 233 |
) |
| 234 |
else: |
| 235 |
if result != 0: |
| 236 |
modified[key] = desired[key] |
| 237 |
if modified: |
| 238 |
self.changed = True |
| 239 |
return modified |