Debian Codegraph @

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