Debian Codegraph @

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

raw symbols SCIP
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 confirmed=dict(type="bool"),
35 synchronous=dict(type="bool"),
36 actions=dict(type="list"),
37 callback=dict(type="dict"),
38 organizationId=dict(type="str"),
39 actionBatchId=dict(type="str"),
40 ))
41
42 required_if = [
43 ("state", "present", ["actionBatchId", "organizationId"], True),
44 ("state", "absent", ["actionBatchId", "organizationId"], True),
45 ]
46 required_one_of = []
47 mutually_exclusive = []
48 required_together = []
49
50
51 class OrganizationsActionBatches(object):
52 def __init__(self, params, meraki):
53 self.meraki = meraki
54 self.new_object = dict(
55 confirmed=params.get("confirmed"),
56 synchronous=params.get("synchronous"),
57 actions=params.get("actions"),
58 callback=params.get("callback"),
59 organizationId=params.get("organizationId"),
60 actionBatchId=params.get("actionBatchId"),
61 )
62
63 def get_all_params(self, name=None, id=None):
64 new_object_params = {}
65 if self.new_object.get('status') is not None or self.new_object.get(
66 'status') is not None:
67 new_object_params['status'] = self.new_object.get('status')
68 if self.new_object.get('organizationId') is not None or self.new_object.get(
69 'organization_id') is not None:
70 new_object_params['organizationId'] = self.new_object.get(
71 'organizationId') or self.new_object.get('organization_id')
72 return new_object_params
73
74 def get_params_by_id(self, name=None, id=None):
75 new_object_params = {}
76 if self.new_object.get('organizationId') is not None or self.new_object.get(
77 'organization_id') is not None:
78 new_object_params['organizationId'] = self.new_object.get(
79 'organizationId') or self.new_object.get('organization_id')
80 if self.new_object.get('actionBatchId') is not None or self.new_object.get(
81 'action_batch_id') is not None:
82 new_object_params['actionBatchId'] = self.new_object.get(
83 'actionBatchId') or self.new_object.get('action_batch_id')
84 return new_object_params
85
86 def create_params(self):
87 new_object_params = {}
88 if self.new_object.get('confirmed') is not None or self.new_object.get(
89 'confirmed') is not None:
90 new_object_params['confirmed'] = self.new_object.get('confirmed')
91 if self.new_object.get('synchronous') is not None or self.new_object.get(
92 'synchronous') is not None:
93 new_object_params['synchronous'] = self.new_object.get(
94 'synchronous')
95 if self.new_object.get('actions') is not None or self.new_object.get(
96 'actions') is not None:
97 new_object_params['actions'] = self.new_object.get('actions') or \
98 self.new_object.get('actions')
99 if self.new_object.get('callback') is not None or self.new_object.get(
100 'callback') is not None:
101 new_object_params['callback'] = self.new_object.get(
102 'callback') or self.new_object.get('callback')
103 if self.new_object.get('organizationId') is not None or self.new_object.get(
104 'organization_id') is not None:
105 new_object_params['organizationId'] = self.new_object.get(
106 'organizationId') or self.new_object.get('organization_id')
107 return new_object_params
108
109 def delete_by_id_params(self):
110 new_object_params = {}
111 if self.new_object.get('organizationId') is not None or self.new_object.get(
112 'organization_id') is not None:
113 new_object_params['organizationId'] = self.new_object.get(
114 'organizationId') or self.new_object.get('organization_id')
115 if self.new_object.get('actionBatchId') is not None or self.new_object.get(
116 'action_batch_id') is not None:
117 new_object_params['actionBatchId'] = self.new_object.get(
118 'actionBatchId') or self.new_object.get('action_batch_id')
119 return new_object_params
120
121 def update_by_id_params(self):
122 new_object_params = {}
123 if self.new_object.get('confirmed') is not None or self.new_object.get(
124 'confirmed') is not None:
125 new_object_params['confirmed'] = self.new_object.get('confirmed')
126 if self.new_object.get('synchronous') is not None or self.new_object.get(
127 'synchronous') is not None:
128 new_object_params['synchronous'] = self.new_object.get(
129 'synchronous')
130 if self.new_object.get('organizationId') is not None or self.new_object.get(
131 'organization_id') is not None:
132 new_object_params['organizationId'] = self.new_object.get(
133 'organizationId') or self.new_object.get('organization_id')
134 if self.new_object.get('actionBatchId') is not None or self.new_object.get(
135 'action_batch_id') is not None:
136 new_object_params['actionBatchId'] = self.new_object.get(
137 'actionBatchId') or self.new_object.get('action_batch_id')
138 return new_object_params
139
140 def get_object_by_name(self, name):
141 result = None
142 # NOTE: Does not have a get by name method or it is in another action
143 try:
144 items = self.meraki.exec_meraki(
145 family="organizations",
146 function="getOrganizationActionBatches",
147 params=self.get_all_params(name=name),
148 )
149 if isinstance(items, dict):
150 if 'response' in items:
151 items = items.get('response')
152 result = get_dict_result(items, 'name', name)
153 if result is None:
154 result = items
155 except Exception as e:
156 print("Error: ", e)
157 result = None
158 return result
159
160 def get_object_by_id(self, id):
161 result = None
162 try:
163 items = self.meraki.exec_meraki(
164 family="organizations",
165 function="getOrganizationActionBatch",
166 params=self.get_params_by_id()
167 )
168 if isinstance(items, dict):
169 if 'response' in items:
170 items = items.get('response')
171 result = get_dict_result(items, 'actionBatchId', id)
172 except Exception as e:
173 print("Error: ", e)
174 result = None
175 return result
176
177 def exists(self):
178 id_exists = False
179 name_exists = False
180 prev_obj = None
181 o_id = self.new_object.get("id")
182 o_id = o_id or self.new_object.get(
183 "action_batch_id") or self.new_object.get("actionBatchId")
184 name = self.new_object.get("name")
185 if o_id:
186 prev_obj = self.get_object_by_id(o_id)
187 id_exists = prev_obj is not None and isinstance(prev_obj, dict)
188 if not id_exists and name:
189 prev_obj = self.get_object_by_name(name)
190 name_exists = prev_obj is not None and isinstance(prev_obj, dict)
191 if name_exists:
192 _id = prev_obj.get("id")
193 _id = _id or prev_obj.get("actionBatchId")
194 if id_exists and name_exists and o_id != _id:
195 raise InconsistentParameters(
196 "The 'id' and 'name' params don't refer to the same object")
197 if _id:
198 self.new_object.update(dict(id=_id))
199 self.new_object.update(dict(actionBatchId=_id))
200 if _id:
201 prev_obj = self.get_object_by_id(_id)
202 it_exists = prev_obj is not None and isinstance(prev_obj, dict)
203 return (it_exists, prev_obj)
204
205 def requires_update(self, current_obj):
206 requested_obj = self.new_object
207
208 obj_params = [
209 ("confirmed", "confirmed"),
210 ("synchronous", "synchronous"),
211 ("actions", "actions"),
212 ("callback", "callback"),
213 ("organizationId", "organizationId"),
214 ("actionBatchId", "actionBatchId"),
215 ]
216 # Method 1. Params present in request (Ansible) obj are the same as the current (DNAC) params
217 # If any does not have eq params, it requires update
218 return any(
219 not meraki_compare_equality2(
220 current_obj.get(meraki_param),
221 requested_obj.get(ansible_param)) for (
222 meraki_param,
223 ansible_param) in obj_params)
224
225 def create(self):
226 result = self.meraki.exec_meraki(
227 family="organizations",
228 function="createOrganizationActionBatch",
229 params=self.create_params(),
230 op_modifies=True,
231 )
232 return result
233
234 def update(self):
235 id = self.new_object.get("id")
236 id = id or self.new_object.get("actionBatchId")
237 name = self.new_object.get("name")
238 result = None
239 if not id:
240 prev_obj_name = self.get_object_by_name(name)
241 id_ = None
242 if prev_obj_name:
243 id_ = prev_obj_name.get("id")
244 id_ = id_ or prev_obj_name.get("actionBatchId")
245 if id_:
246 self.new_object.update(dict(actionBatchId=id_))
247 result = self.meraki.exec_meraki(
248 family="organizations",
249 function="updateOrganizationActionBatch",
250 params=self.update_by_id_params(),
251 op_modifies=True,
252 )
253 return result
254
255 def delete(self):
256 id = self.new_object.get("id")
257 id = id or self.new_object.get("actionBatchId")
258 name = self.new_object.get("name")
259 result = None
260 if not id:
261 prev_obj_name = self.get_object_by_name(name)
262 id_ = None
263 if prev_obj_name:
264 id_ = prev_obj_name.get("id")
265 id_ = id_ or prev_obj_name.get("actionBatchId")
266 if id_:
267 self.new_object.update(dict(actionBatchId=id_))
268 result = self.meraki.exec_meraki(
269 family="organizations",
270 function="deleteOrganizationActionBatch",
271 params=self.delete_by_id_params(),
272 )
273 return result
274
275
276 class ActionModule(ActionBase):
277 def __init__(self, *args, **kwargs):
278 if not ANSIBLE_UTILS_IS_INSTALLED:
279 raise AnsibleActionFail(
280 "ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'")
281 super(ActionModule, self).__init__(*args, **kwargs)
282 self._supports_async = False
283 self._supports_check_mode = False
284 self._result = None
285
286 # Checks the supplied parameters against the argument spec for this module
287 def _check_argspec(self):
288 aav = AnsibleArgSpecValidator(
289 data=self._task.args,
290 schema=dict(argument_spec=argument_spec),
291 schema_format="argspec",
292 schema_conditionals=dict(
293 required_if=required_if,
294 required_one_of=required_one_of,
295 mutually_exclusive=mutually_exclusive,
296 required_together=required_together,
297 ),
298 name=self._task.action,
299 )
300 valid, errors, self._task.args = aav.validate()
301 if not valid:
302 raise AnsibleActionFail(errors)
303
304 def run(self, tmp=None, task_vars=None):
305 self._task.diff = False
306 self._result = super(ActionModule, self).run(tmp, task_vars)
307 self._result["changed"] = False
308 self._check_argspec()
309
310 meraki = MERAKI(self._task.args)
311 obj = OrganizationsActionBatches(self._task.args, meraki)
312
313 state = self._task.args.get("state")
314
315 response = None
316
317 if state == "present":
318 (obj_exists, prev_obj) = obj.exists()
319 if obj_exists:
320 if obj.requires_update(prev_obj):
321 response = obj.update()
322 meraki.object_updated()
323 else:
324 response = prev_obj
325 meraki.object_already_present()
326 else:
327 response = obj.create()
328 meraki.object_created()
329
330 elif state == "absent":
331 (obj_exists, prev_obj) = obj.exists()
332 if obj_exists:
333 response = obj.delete()
334 meraki.object_deleted()
335 else:
336 meraki.object_already_absent()
337
338 self._result.update(dict(meraki_response=response))
339 self._result.update(meraki.exit_json())
340 return self._result