Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/organizations_wireless_zigbee_devices.py

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