Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/organizations_wireless_mqtt_settings.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_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 network=dict(type="dict"),
35 mqtt=dict(type="dict"),
36 ble=dict(type="dict"),
37 wifi=dict(type="dict"),
38 organizationId=dict(type="str"),
39 ))
40
41 required_if = [
42 ("state", "present", ["organizationId"], True),
43 ]
44 required_one_of = []
45 mutually_exclusive = []
46 required_together = []
47
48
49 class OrganizationsWirelessMqttSettings(object):
50 def __init__(self, params, meraki):
51 self.meraki = meraki
52 self.new_object = dict(
53 network=params.get("network"),
54 mqtt=params.get("mqtt"),
55 ble=params.get("ble"),
56 wifi=params.get("wifi"),
57 organization_id=params.get("organizationId"),
58 )
59
60 def get_all_params(self, name=None, id=None):
61 new_object_params = {}
62 if self.new_object.get('perPage') is not None or self.new_object.get(
63 'per_page') is not None:
64 new_object_params['perPage'] = self.new_object.get('perPage') or \
65 self.new_object.get('per_page')
66 new_object_params['total_pages'] = -1
67 if self.new_object.get('startingAfter') is not None or self.new_object.get(
68 'starting_after') is not None:
69 new_object_params['startingAfter'] = self.new_object.get(
70 'startingAfter') or self.new_object.get('starting_after')
71 if self.new_object.get('endingBefore') is not None or self.new_object.get(
72 'ending_before') is not None:
73 new_object_params['endingBefore'] = self.new_object.get(
74 'endingBefore') or self.new_object.get('ending_before')
75 if self.new_object.get('networkIds') is not None or self.new_object.get(
76 'network_ids') is not None:
77 new_object_params['networkIds'] = self.new_object.get(
78 'networkIds') or self.new_object.get('network_ids')
79 if self.new_object.get('organizationId') is not None or self.new_object.get(
80 'organization_id') is not None:
81 new_object_params['organizationId'] = self.new_object.get(
82 'organizationId') or self.new_object.get('organization_id')
83 return new_object_params
84
85 def update_all_params(self):
86 new_object_params = {}
87 if self.new_object.get('network') is not None or self.new_object.get(
88 'network') is not None:
89 new_object_params['network'] = self.new_object.get('network') or \
90 self.new_object.get('network')
91 if self.new_object.get('mqtt') is not None or self.new_object.get(
92 'mqtt') is not None:
93 new_object_params['mqtt'] = self.new_object.get('mqtt') or \
94 self.new_object.get('mqtt')
95 if self.new_object.get(
96 'ble') is not None or self.new_object.get('ble') is not None:
97 new_object_params['ble'] = self.new_object.get('ble') or \
98 self.new_object.get('ble')
99 if self.new_object.get('wifi') is not None or self.new_object.get(
100 'wifi') is not None:
101 new_object_params['wifi'] = self.new_object.get('wifi') or \
102 self.new_object.get('wifi')
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 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="getOrganizationWirelessMqttSettings",
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 ("network", "network"),
161 ("mqtt", "mqtt"),
162 ("ble", "ble"),
163 ("wifi", "wifi"),
164 ("organizationId", "organizationId"),
165 ]
166 # Method 1. Params present in request (Ansible) obj are the same as the current (ISE) params
167 # If any does not have eq params, it requires update
168 return any(
169 not meraki_compare_equality(
170 current_obj.get(meraki_param),
171 requested_obj.get(ansible_param)) for (
172 meraki_param,
173 ansible_param) in obj_params)
174
175 def update(self):
176 id = self.new_object.get("id")
177 name = self.new_object.get("name")
178 result = None
179 result = self.meraki.exec_meraki(
180 family="wireless",
181 function="updateOrganizationWirelessMqttSettings",
182 params=self.update_all_params(),
183 op_modifies=True,
184 )
185 return result
186
187
188 class ActionModule(ActionBase):
189 def __init__(self, *args, **kwargs):
190 if not ANSIBLE_UTILS_IS_INSTALLED:
191 raise AnsibleActionFail(
192 "ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'")
193 super(ActionModule, self).__init__(*args, **kwargs)
194 self._supports_async = False
195 self._supports_check_mode = False
196 self._result = None
197
198 # Checks the supplied parameters against the argument spec for this module
199 def _check_argspec(self):
200 aav = AnsibleArgSpecValidator(
201 data=self._task.args,
202 schema=dict(argument_spec=argument_spec),
203 schema_format="argspec",
204 schema_conditionals=dict(
205 required_if=required_if,
206 required_one_of=required_one_of,
207 mutually_exclusive=mutually_exclusive,
208 required_together=required_together,
209 ),
210 name=self._task.action,
211 )
212 valid, errors, self._task.args = aav.validate()
213 if not valid:
214 raise AnsibleActionFail(errors)
215
216 def run(self, tmp=None, task_vars=None):
217 self._task.diff = False
218 self._result = super(ActionModule, self).run(tmp, task_vars)
219 self._result["changed"] = False
220 self._check_argspec()
221
222 meraki = MERAKI(self._task.args)
223 obj = OrganizationsWirelessMqttSettings(self._task.args, meraki)
224
225 state = self._task.args.get("state")
226
227 response = None
228 if state == "present":
229 (obj_exists, prev_obj) = obj.exists()
230 if obj_exists:
231 if obj.requires_update(prev_obj):
232 response = obj.update()
233 meraki.object_updated()
234 else:
235 response = prev_obj
236 meraki.object_already_present()
237 else:
238 meraki.fail_json(
239 "Object does not exists, plugin only has update")
240
241 self._result.update(dict(meraki_response=response))
242 self._result.update(meraki.exit_json())
243 return self._result