Debian Codegraph @

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