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