Debian Codegraph @

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