Debian Codegraph @

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