Debian Codegraph @

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