Debian Codegraph @

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