Debian Codegraph @

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