| 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"]), |
| 34 |
alerts=dict(type="dict"), |
| 35 |
defaultPolicy=dict(type="str"), |
| 36 |
allowedServers=dict(type="list"), |
| 37 |
blockedServers=dict(type="list"), |
| 38 |
arpInspection=dict(type="dict"), |
| 39 |
networkId=dict(type="str"), |
| 40 |
)) |
| 41 |
|
| 42 |
required_if = [ |
| 43 |
("state", "present", ["networkId"], True), |
| 44 |
] |
| 45 |
required_one_of = [] |
| 46 |
mutually_exclusive = [] |
| 47 |
required_together = [] |
| 48 |
|
| 49 |
|
| 50 |
class NetworksSwitchDhcpServerPolicy(object): |
| 51 |
def __init__(self, params, meraki): |
| 52 |
self.meraki = meraki |
| 53 |
self.new_object = dict( |
| 54 |
alerts=params.get("alerts"), |
| 55 |
defaultPolicy=params.get("defaultPolicy"), |
| 56 |
allowedServers=params.get("allowedServers"), |
| 57 |
blockedServers=params.get("blockedServers"), |
| 58 |
arpInspection=params.get("arpInspection"), |
| 59 |
network_id=params.get("networkId"), |
| 60 |
) |
| 61 |
|
| 62 |
def get_all_params(self, name=None, id=None): |
| 63 |
new_object_params = {} |
| 64 |
if self.new_object.get('networkId') is not None or self.new_object.get( |
| 65 |
'network_id') is not None: |
| 66 |
new_object_params['networkId'] = self.new_object.get( |
| 67 |
'networkId') or self.new_object.get('network_id') |
| 68 |
return new_object_params |
| 69 |
|
| 70 |
def update_all_params(self): |
| 71 |
new_object_params = {} |
| 72 |
if self.new_object.get('alerts') is not None or self.new_object.get( |
| 73 |
'alerts') is not None: |
| 74 |
new_object_params['alerts'] = self.new_object.get('alerts') or \ |
| 75 |
self.new_object.get('alerts') |
| 76 |
if self.new_object.get('defaultPolicy') is not None or self.new_object.get( |
| 77 |
'default_policy') is not None: |
| 78 |
new_object_params['defaultPolicy'] = self.new_object.get( |
| 79 |
'defaultPolicy') or self.new_object.get('default_policy') |
| 80 |
if self.new_object.get('allowedServers') is not None or self.new_object.get( |
| 81 |
'allowed_servers') is not None: |
| 82 |
new_object_params['allowedServers'] = self.new_object.get( |
| 83 |
'allowedServers') or self.new_object.get('allowed_servers') |
| 84 |
if self.new_object.get('blockedServers') is not None or self.new_object.get( |
| 85 |
'blocked_servers') is not None: |
| 86 |
new_object_params['blockedServers'] = self.new_object.get( |
| 87 |
'blockedServers') or self.new_object.get('blocked_servers') |
| 88 |
if self.new_object.get('arpInspection') is not None or self.new_object.get( |
| 89 |
'arp_inspection') is not None: |
| 90 |
new_object_params['arpInspection'] = self.new_object.get( |
| 91 |
'arpInspection') or self.new_object.get('arp_inspection') |
| 92 |
if self.new_object.get('networkId') is not None or self.new_object.get( |
| 93 |
'network_id') is not None: |
| 94 |
new_object_params['networkId'] = self.new_object.get( |
| 95 |
'networkId') or self.new_object.get('network_id') |
| 96 |
return new_object_params |
| 97 |
|
| 98 |
def get_object_by_name(self, name): |
| 99 |
result = None |
| 100 |
# NOTE: Does not have a get by name method, using get all |
| 101 |
try: |
| 102 |
items = self.meraki.exec_meraki( |
| 103 |
family="switch", |
| 104 |
function="getNetworkSwitchDhcpServerPolicy", |
| 105 |
params=self.get_all_params(name=name), |
| 106 |
) |
| 107 |
if isinstance(items, dict): |
| 108 |
if 'response' in items: |
| 109 |
items = items.get('response') |
| 110 |
result = get_dict_result(items, 'name', name) |
| 111 |
if result is None: |
| 112 |
result = items |
| 113 |
except Exception as e: |
| 114 |
print("Error: ", e) |
| 115 |
result = None |
| 116 |
return result |
| 117 |
|
| 118 |
def get_object_by_id(self, id): |
| 119 |
result = None |
| 120 |
# NOTE: Does not have a get by id method or it is in another action |
| 121 |
return result |
| 122 |
|
| 123 |
def exists(self): |
| 124 |
prev_obj = None |
| 125 |
id_exists = False |
| 126 |
name_exists = False |
| 127 |
o_id = self.new_object.get( |
| 128 |
"networkId") or self.new_object.get("network_id") |
| 129 |
name = self.new_object.get("name") |
| 130 |
if o_id: |
| 131 |
prev_obj = self.get_object_by_name(o_id) |
| 132 |
id_exists = prev_obj is not None and isinstance(prev_obj, dict) |
| 133 |
if not id_exists and name: |
| 134 |
prev_obj = self.get_object_by_name(name) |
| 135 |
name_exists = prev_obj is not None and isinstance(prev_obj, dict) |
| 136 |
if name_exists: |
| 137 |
_id = prev_obj.get("id") |
| 138 |
if id_exists and name_exists and o_id != _id: |
| 139 |
raise InconsistentParameters( |
| 140 |
"The 'id' and 'name' params don't refer to the same object") |
| 141 |
if _id: |
| 142 |
self.new_object.update(dict(id=_id)) |
| 143 |
it_exists = prev_obj is not None and isinstance(prev_obj, dict) |
| 144 |
return (it_exists, prev_obj) |
| 145 |
|
| 146 |
def requires_update(self, current_obj): |
| 147 |
requested_obj = self.new_object |
| 148 |
|
| 149 |
obj_params = [ |
| 150 |
("alerts", "alerts"), |
| 151 |
("defaultPolicy", "defaultPolicy"), |
| 152 |
("allowedServers", "allowedServers"), |
| 153 |
("blockedServers", "blockedServers"), |
| 154 |
("arpInspection", "arpInspection"), |
| 155 |
("networkId", "networkId"), |
| 156 |
] |
| 157 |
# Method 1. Params present in request (Ansible) obj are the same as the current (ISE) params |
| 158 |
# If any does not have eq params, it requires update |
| 159 |
return any( |
| 160 |
not meraki_compare_equality2( |
| 161 |
current_obj.get(meraki_param), |
| 162 |
requested_obj.get(ansible_param)) for ( |
| 163 |
meraki_param, |
| 164 |
ansible_param) in obj_params) |
| 165 |
|
| 166 |
def update(self): |
| 167 |
id = self.new_object.get("id") |
| 168 |
name = self.new_object.get("name") |
| 169 |
result = None |
| 170 |
result = self.meraki.exec_meraki( |
| 171 |
family="switch", |
| 172 |
function="updateNetworkSwitchDhcpServerPolicy", |
| 173 |
params=self.update_all_params(), |
| 174 |
op_modifies=True, |
| 175 |
) |
| 176 |
return result |
| 177 |
|
| 178 |
|
| 179 |
class ActionModule(ActionBase): |
| 180 |
def __init__(self, *args, **kwargs): |
| 181 |
if not ANSIBLE_UTILS_IS_INSTALLED: |
| 182 |
raise AnsibleActionFail( |
| 183 |
"ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'") |
| 184 |
super(ActionModule, self).__init__(*args, **kwargs) |
| 185 |
self._supports_async = False |
| 186 |
self._supports_check_mode = False |
| 187 |
self._result = None |
| 188 |
|
| 189 |
# Checks the supplied parameters against the argument spec for this module |
| 190 |
def _check_argspec(self): |
| 191 |
aav = AnsibleArgSpecValidator( |
| 192 |
data=self._task.args, |
| 193 |
schema=dict(argument_spec=argument_spec), |
| 194 |
schema_format="argspec", |
| 195 |
schema_conditionals=dict( |
| 196 |
required_if=required_if, |
| 197 |
required_one_of=required_one_of, |
| 198 |
mutually_exclusive=mutually_exclusive, |
| 199 |
required_together=required_together, |
| 200 |
), |
| 201 |
name=self._task.action, |
| 202 |
) |
| 203 |
valid, errors, self._task.args = aav.validate() |
| 204 |
if not valid: |
| 205 |
raise AnsibleActionFail(errors) |
| 206 |
|
| 207 |
def run(self, tmp=None, task_vars=None): |
| 208 |
self._task.diff = False |
| 209 |
self._result = super(ActionModule, self).run(tmp, task_vars) |
| 210 |
self._result["changed"] = False |
| 211 |
self._check_argspec() |
| 212 |
|
| 213 |
meraki = MERAKI(self._task.args) |
| 214 |
obj = NetworksSwitchDhcpServerPolicy(self._task.args, meraki) |
| 215 |
|
| 216 |
state = self._task.args.get("state") |
| 217 |
|
| 218 |
response = None |
| 219 |
if state == "present": |
| 220 |
(obj_exists, prev_obj) = obj.exists() |
| 221 |
if obj_exists: |
| 222 |
if obj.requires_update(prev_obj): |
| 223 |
response = obj.update() |
| 224 |
meraki.object_updated() |
| 225 |
else: |
| 226 |
response = prev_obj |
| 227 |
meraki.object_already_present() |
| 228 |
else: |
| 229 |
meraki.fail_json( |
| 230 |
"Object does not exists, plugin only has update") |
| 231 |
|
| 232 |
self._result.update(dict(meraki_response=response)) |
| 233 |
self._result.update(meraki.exit_json()) |
| 234 |
return self._result |