| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2021, Cisco Systems |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or |
| 6 |
# https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
from __future__ import (absolute_import, division, print_function) |
| 9 |
__metaclass__ = type |
| 10 |
from ansible.plugins.action import ActionBase |
| 11 |
try: |
| 12 |
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import ( |
| 13 |
AnsibleArgSpecValidator, ) |
| 14 |
except ImportError: |
| 15 |
ANSIBLE_UTILS_IS_INSTALLED = False |
| 16 |
else: |
| 17 |
ANSIBLE_UTILS_IS_INSTALLED = True |
| 18 |
from ansible.errors import AnsibleActionFail |
| 19 |
from ansible_collections.cisco.meraki.plugins.plugin_utils.meraki import ( |
| 20 |
MERAKI, |
| 21 |
meraki_argument_spec, |
| 22 |
meraki_compare_equality2, |
| 23 |
get_dict_result, |
| 24 |
) |
| 25 |
from ansible_collections.cisco.meraki.plugins.plugin_utils.exceptions import ( |
| 26 |
InconsistentParameters, |
| 27 |
) |
| 28 |
|
| 29 |
# Get common arguments specification |
| 30 |
argument_spec = meraki_argument_spec() |
| 31 |
# Add arguments specific for this module |
| 32 |
argument_spec.update(dict( |
| 33 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 34 |
switchPorts=dict(type="list"), |
| 35 |
switchProfilePorts=dict(type="list"), |
| 36 |
networkId=dict(type="str"), |
| 37 |
linkAggregationId=dict(type="str"), |
| 38 |
)) |
| 39 |
|
| 40 |
required_if = [ |
| 41 |
("state", "present", ["linkAggregationId", "networkId"], True), |
| 42 |
("state", "absent", ["linkAggregationId", "networkId"], True), |
| 43 |
] |
| 44 |
required_one_of = [] |
| 45 |
mutually_exclusive = [] |
| 46 |
required_together = [] |
| 47 |
|
| 48 |
|
| 49 |
class NetworksSwitchLinkAggregations(object): |
| 50 |
def __init__(self, params, meraki): |
| 51 |
self.meraki = meraki |
| 52 |
self.new_object = dict( |
| 53 |
switchPorts=params.get("switchPorts"), |
| 54 |
switchProfilePorts=params.get("switchProfilePorts"), |
| 55 |
networkId=params.get("networkId"), |
| 56 |
linkAggregationId=params.get("linkAggregationId"), |
| 57 |
) |
| 58 |
|
| 59 |
def get_all_params(self, name=None, id=None): |
| 60 |
new_object_params = {} |
| 61 |
if self.new_object.get('networkId') is not None or self.new_object.get( |
| 62 |
'network_id') is not None: |
| 63 |
new_object_params['networkId'] = self.new_object.get( |
| 64 |
'networkId') or self.new_object.get('network_id') |
| 65 |
return new_object_params |
| 66 |
|
| 67 |
def create_params(self): |
| 68 |
new_object_params = {} |
| 69 |
if self.new_object.get('switchPorts') is not None or self.new_object.get( |
| 70 |
'switch_ports') is not None: |
| 71 |
new_object_params['switchPorts'] = self.new_object.get( |
| 72 |
'switchPorts') or self.new_object.get('switch_ports') |
| 73 |
if self.new_object.get('switchProfilePorts') is not None or self.new_object.get( |
| 74 |
'switch_profile_ports') is not None: |
| 75 |
new_object_params['switchProfilePorts'] = self.new_object.get( |
| 76 |
'switchProfilePorts') or self.new_object.get('switch_profile_ports') |
| 77 |
if self.new_object.get('networkId') is not None or self.new_object.get( |
| 78 |
'network_id') is not None: |
| 79 |
new_object_params['networkId'] = self.new_object.get( |
| 80 |
'networkId') or self.new_object.get('network_id') |
| 81 |
return new_object_params |
| 82 |
|
| 83 |
def delete_by_id_params(self): |
| 84 |
new_object_params = {} |
| 85 |
if self.new_object.get('networkId') is not None or self.new_object.get( |
| 86 |
'network_id') is not None: |
| 87 |
new_object_params['networkId'] = self.new_object.get( |
| 88 |
'networkId') or self.new_object.get('network_id') |
| 89 |
if self.new_object.get('linkAggregationId') is not None or self.new_object.get( |
| 90 |
'link_aggregation_id') is not None: |
| 91 |
new_object_params['linkAggregationId'] = self.new_object.get( |
| 92 |
'linkAggregationId') or self.new_object.get('link_aggregation_id') |
| 93 |
return new_object_params |
| 94 |
|
| 95 |
def update_by_id_params(self): |
| 96 |
new_object_params = {} |
| 97 |
if self.new_object.get('switchPorts') is not None or self.new_object.get( |
| 98 |
'switch_ports') is not None: |
| 99 |
new_object_params['switchPorts'] = self.new_object.get( |
| 100 |
'switchPorts') or self.new_object.get('switch_ports') |
| 101 |
if self.new_object.get('switchProfilePorts') is not None or self.new_object.get( |
| 102 |
'switch_profile_ports') is not None: |
| 103 |
new_object_params['switchProfilePorts'] = self.new_object.get( |
| 104 |
'switchProfilePorts') or self.new_object.get('switch_profile_ports') |
| 105 |
if self.new_object.get('networkId') is not None or self.new_object.get( |
| 106 |
'network_id') is not None: |
| 107 |
new_object_params['networkId'] = self.new_object.get( |
| 108 |
'networkId') or self.new_object.get('network_id') |
| 109 |
if self.new_object.get('linkAggregationId') is not None or self.new_object.get( |
| 110 |
'link_aggregation_id') is not None: |
| 111 |
new_object_params['linkAggregationId'] = self.new_object.get( |
| 112 |
'linkAggregationId') or self.new_object.get('link_aggregation_id') |
| 113 |
return new_object_params |
| 114 |
|
| 115 |
def get_object_by_name(self, name): |
| 116 |
result = None |
| 117 |
# NOTE: Does not have a get by name method or it is in another action |
| 118 |
try: |
| 119 |
items = self.meraki.exec_meraki( |
| 120 |
family="switch", |
| 121 |
function="getNetworkSwitchLinkAggregations", |
| 122 |
params=self.get_all_params(name=name), |
| 123 |
) |
| 124 |
if isinstance(items, dict): |
| 125 |
if 'response' in items: |
| 126 |
items = items.get('response') |
| 127 |
result = get_dict_result(items, 'name', name) |
| 128 |
if result is None: |
| 129 |
result = items |
| 130 |
except Exception as e: |
| 131 |
print("Error: ", e) |
| 132 |
result = None |
| 133 |
return result |
| 134 |
|
| 135 |
def get_object_by_id(self, id): |
| 136 |
result = None |
| 137 |
# NOTE: Does not have a get by id method or it is in another action |
| 138 |
try: |
| 139 |
items = self.meraki.exec_meraki( |
| 140 |
family="switch", |
| 141 |
function="getNetworkSwitchLinkAggregations", |
| 142 |
params=self.get_all_params(id=id), |
| 143 |
) |
| 144 |
if isinstance(items, dict): |
| 145 |
if 'response' in items: |
| 146 |
items = items.get('response') |
| 147 |
result = get_dict_result(items, 'id', id) |
| 148 |
except Exception as e: |
| 149 |
print("Error: ", e) |
| 150 |
result = None |
| 151 |
return result |
| 152 |
|
| 153 |
def exists(self): |
| 154 |
id_exists = False |
| 155 |
name_exists = False |
| 156 |
prev_obj = None |
| 157 |
o_id = self.new_object.get("id") |
| 158 |
o_id = o_id or self.new_object.get( |
| 159 |
"link_aggregation_id") or self.new_object.get("linkAggregationId") |
| 160 |
name = self.new_object.get("name") |
| 161 |
if o_id: |
| 162 |
prev_obj = self.get_object_by_id(o_id) |
| 163 |
id_exists = prev_obj is not None and isinstance(prev_obj, dict) |
| 164 |
if not id_exists and name: |
| 165 |
prev_obj = self.get_object_by_name(name) |
| 166 |
name_exists = prev_obj is not None and isinstance(prev_obj, dict) |
| 167 |
if name_exists: |
| 168 |
_id = prev_obj.get("id") |
| 169 |
_id = _id or prev_obj.get("linkAggregationId") |
| 170 |
if id_exists and name_exists and o_id != _id: |
| 171 |
raise InconsistentParameters( |
| 172 |
"The 'id' and 'name' params don't refer to the same object") |
| 173 |
if _id: |
| 174 |
self.new_object.update(dict(id=_id)) |
| 175 |
self.new_object.update(dict(linkAggregationId=_id)) |
| 176 |
it_exists = prev_obj is not None and isinstance(prev_obj, dict) |
| 177 |
return (it_exists, prev_obj) |
| 178 |
|
| 179 |
def requires_update(self, current_obj): |
| 180 |
requested_obj = self.new_object |
| 181 |
|
| 182 |
obj_params = [ |
| 183 |
("switchPorts", "switchPorts"), |
| 184 |
("switchProfilePorts", "switchProfilePorts"), |
| 185 |
("networkId", "networkId"), |
| 186 |
("linkAggregationId", "linkAggregationId"), |
| 187 |
] |
| 188 |
# Method 1. Params present in request (Ansible) obj are the same as the current (DNAC) params |
| 189 |
# If any does not have eq params, it requires update |
| 190 |
return any( |
| 191 |
not meraki_compare_equality2( |
| 192 |
current_obj.get(meraki_param), |
| 193 |
requested_obj.get(ansible_param)) for ( |
| 194 |
meraki_param, |
| 195 |
ansible_param) in obj_params) |
| 196 |
|
| 197 |
def create(self): |
| 198 |
result = self.meraki.exec_meraki( |
| 199 |
family="switch", |
| 200 |
function="createNetworkSwitchLinkAggregation", |
| 201 |
params=self.create_params(), |
| 202 |
op_modifies=True, |
| 203 |
) |
| 204 |
return result |
| 205 |
|
| 206 |
def update(self): |
| 207 |
id = self.new_object.get("id") |
| 208 |
id = id or self.new_object.get("linkAggregationId") |
| 209 |
name = self.new_object.get("name") |
| 210 |
result = None |
| 211 |
if not id: |
| 212 |
prev_obj_name = self.get_object_by_name(name) |
| 213 |
id_ = None |
| 214 |
if prev_obj_name: |
| 215 |
id_ = prev_obj_name.get("id") |
| 216 |
id_ = id_ or prev_obj_name.get("linkAggregationId") |
| 217 |
if id_: |
| 218 |
self.new_object.update(dict(linkAggregationId=id_)) |
| 219 |
result = self.meraki.exec_meraki( |
| 220 |
family="switch", |
| 221 |
function="updateNetworkSwitchLinkAggregation", |
| 222 |
params=self.update_by_id_params(), |
| 223 |
op_modifies=True, |
| 224 |
) |
| 225 |
return result |
| 226 |
|
| 227 |
def delete(self): |
| 228 |
id = self.new_object.get("id") |
| 229 |
id = id or self.new_object.get("linkAggregationId") |
| 230 |
name = self.new_object.get("name") |
| 231 |
result = None |
| 232 |
if not id: |
| 233 |
prev_obj_name = self.get_object_by_name(name) |
| 234 |
id_ = None |
| 235 |
if prev_obj_name: |
| 236 |
id_ = prev_obj_name.get("id") |
| 237 |
id_ = id_ or prev_obj_name.get("linkAggregationId") |
| 238 |
if id_: |
| 239 |
self.new_object.update(dict(linkAggregationId=id_)) |
| 240 |
result = self.meraki.exec_meraki( |
| 241 |
family="switch", |
| 242 |
function="deleteNetworkSwitchLinkAggregation", |
| 243 |
params=self.delete_by_id_params(), |
| 244 |
) |
| 245 |
return result |
| 246 |
|
| 247 |
|
| 248 |
class ActionModule(ActionBase): |
| 249 |
def __init__(self, *args, **kwargs): |
| 250 |
if not ANSIBLE_UTILS_IS_INSTALLED: |
| 251 |
raise AnsibleActionFail( |
| 252 |
"ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'") |
| 253 |
super(ActionModule, self).__init__(*args, **kwargs) |
| 254 |
self._supports_async = False |
| 255 |
self._supports_check_mode = False |
| 256 |
self._result = None |
| 257 |
|
| 258 |
# Checks the supplied parameters against the argument spec for this module |
| 259 |
def _check_argspec(self): |
| 260 |
aav = AnsibleArgSpecValidator( |
| 261 |
data=self._task.args, |
| 262 |
schema=dict(argument_spec=argument_spec), |
| 263 |
schema_format="argspec", |
| 264 |
schema_conditionals=dict( |
| 265 |
required_if=required_if, |
| 266 |
required_one_of=required_one_of, |
| 267 |
mutually_exclusive=mutually_exclusive, |
| 268 |
required_together=required_together, |
| 269 |
), |
| 270 |
name=self._task.action, |
| 271 |
) |
| 272 |
valid, errors, self._task.args = aav.validate() |
| 273 |
if not valid: |
| 274 |
raise AnsibleActionFail(errors) |
| 275 |
|
| 276 |
def run(self, tmp=None, task_vars=None): |
| 277 |
self._task.diff = False |
| 278 |
self._result = super(ActionModule, self).run(tmp, task_vars) |
| 279 |
self._result["changed"] = False |
| 280 |
self._check_argspec() |
| 281 |
|
| 282 |
meraki = MERAKI(self._task.args) |
| 283 |
obj = NetworksSwitchLinkAggregations(self._task.args, meraki) |
| 284 |
|
| 285 |
state = self._task.args.get("state") |
| 286 |
|
| 287 |
response = None |
| 288 |
|
| 289 |
if state == "present": |
| 290 |
(obj_exists, prev_obj) = obj.exists() |
| 291 |
if obj_exists: |
| 292 |
if obj.requires_update(prev_obj): |
| 293 |
response = obj.update() |
| 294 |
meraki.object_updated() |
| 295 |
else: |
| 296 |
response = prev_obj |
| 297 |
meraki.object_already_present() |
| 298 |
else: |
| 299 |
response = obj.create() |
| 300 |
meraki.object_created() |
| 301 |
|
| 302 |
elif state == "absent": |
| 303 |
(obj_exists, prev_obj) = obj.exists() |
| 304 |
if obj_exists: |
| 305 |
response = obj.delete() |
| 306 |
meraki.object_deleted() |
| 307 |
else: |
| 308 |
meraki.object_already_absent() |
| 309 |
|
| 310 |
self._result.update(dict(meraki_response=response)) |
| 311 |
self._result.update(meraki.exit_json()) |
| 312 |
return self._result |