Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/organizations_splash_themes.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_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 baseTheme=dict(type="str"),
36 organizationId=dict(type="str"),
37 id=dict(type="str"),
38 ))
39
40 required_if = [
41 ("state", "present", ["id", "name", "organizationId"], True),
42 ("state", "absent", ["id", "name", "organizationId"], True),
43 ]
44 required_one_of = []
45 mutually_exclusive = []
46 required_together = []
47
48
49 class OrganizationsSplashThemes(object):
50 def __init__(self, params, meraki):
51 self.meraki = meraki
52 self.new_object = dict(
53 name=params.get("name"),
54 baseTheme=params.get("baseTheme"),
55 organization_id=params.get("organizationId"),
56 id=params.get("id"),
57 )
58
59 def get_all_params(self, name=None, id=None):
60 new_object_params = {}
61 if self.new_object.get('organizationId') is not None or self.new_object.get(
62 'organization_id') is not None:
63 new_object_params['organizationId'] = self.new_object.get(
64 'organizationId') or self.new_object.get('organization_id')
65 return new_object_params
66
67 def create_params(self):
68 new_object_params = {}
69 if self.new_object.get('name') is not None or self.new_object.get(
70 'name') is not None:
71 new_object_params['name'] = self.new_object.get('name') or \
72 self.new_object.get('name')
73 if self.new_object.get('baseTheme') is not None or self.new_object.get(
74 'base_theme') is not None:
75 new_object_params['baseTheme'] = self.new_object.get(
76 'baseTheme') or self.new_object.get('base_theme')
77 if self.new_object.get('organizationId') is not None or self.new_object.get(
78 'organization_id') is not None:
79 new_object_params['organizationId'] = self.new_object.get(
80 'organizationId') or self.new_object.get('organization_id')
81 return new_object_params
82
83 def delete_by_id_params(self):
84 new_object_params = {}
85 if self.new_object.get('organizationId') is not None or self.new_object.get(
86 'organization_id') is not None:
87 new_object_params['organizationId'] = self.new_object.get(
88 'organizationId') or self.new_object.get('organization_id')
89 if self.new_object.get(
90 'id') is not None or self.new_object.get('id') is not None:
91 new_object_params['id'] = self.new_object.get('id') or \
92 self.new_object.get('id')
93 return new_object_params
94
95 def get_object_by_name(self, name):
96 result = None
97 # NOTE: Does not have a get by name method, using get all
98 try:
99 items = self.meraki.exec_meraki(
100 family="organizations",
101 function="getOrganizationSplashThemes",
102 params=self.get_all_params(name=name),
103 )
104 if isinstance(items, dict):
105 if 'response' in items:
106 items = items.get('response')
107 result = get_dict_result(items, 'name', name)
108 if result is None:
109 result = items
110 except Exception as e:
111 print("Error: ", e)
112 result = None
113 return result
114
115 def get_object_by_id(self, id):
116 result = None
117 # NOTE: Does not have a get by id method or it is in another action
118 return result
119
120 def exists(self):
121 prev_obj = None
122 id_exists = False
123 name_exists = False
124 o_id = self.new_object.get("id")
125 name = self.new_object.get("name")
126 if o_id:
127 prev_obj = self.get_object_by_name(o_id)
128 id_exists = prev_obj is not None and isinstance(prev_obj, dict)
129 if not id_exists and name:
130 prev_obj = self.get_object_by_name(name)
131 name_exists = prev_obj is not None and isinstance(prev_obj, dict)
132 if name_exists:
133 _id = prev_obj.get("id")
134 if id_exists and name_exists and o_id != _id:
135 raise InconsistentParameters(
136 "The 'id' and 'name' params don't refer to the same object")
137 if _id:
138 self.new_object.update(dict(id=_id))
139 it_exists = prev_obj is not None and isinstance(prev_obj, dict)
140 return (it_exists, prev_obj)
141
142 def requires_update(self, current_obj):
143 requested_obj = self.new_object
144
145 obj_params = [
146 ("name", "name"),
147 ("baseTheme", "baseTheme"),
148 ("organizationId", "organizationId"),
149 ("id", "id"),
150 ]
151 # Method 1. Params present in request (Ansible) obj are the same as the current (ISE) params
152 # If any does not have eq params, it requires update
153 return any(
154 not meraki_compare_equality2(
155 current_obj.get(meraki_param),
156 requested_obj.get(ansible_param)) for (
157 meraki_param,
158 ansible_param) in obj_params)
159
160 def create(self):
161 result = self.meraki.exec_meraki(
162 family="organizations",
163 function="createOrganizationSplashTheme",
164 params=self.create_params(),
165 op_modifies=True,
166 )
167 return result
168
169 def delete(self):
170 id = self.new_object.get("id")
171 name = self.new_object.get("name")
172 result = None
173 if not id:
174 prev_obj_name = self.get_object_by_name(name)
175 id_ = None
176 if prev_obj_name:
177 id_ = prev_obj_name.get("id")
178 if id_:
179 self.new_object.update(dict(id=id_))
180 result = self.meraki.exec_meraki(
181 family="organizations",
182 function="deleteOrganizationSplashTheme",
183 params=self.delete_by_id_params(),
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 = OrganizationsSplashThemes(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 = prev_obj
233 meraki.object_present_and_different()
234 else:
235 response = prev_obj
236 meraki.object_already_present()
237 else:
238 response = obj.create()
239 meraki.object_created()
240 elif state == "absent":
241 (obj_exists, prev_obj) = obj.exists()
242 if obj_exists:
243 response = obj.delete()
244 meraki.object_deleted()
245 else:
246 meraki.object_already_absent()
247
248 self._result.update(dict(meraki_response=response))
249 self._result.update(meraki.exit_json())
250 return self._result