Debian Codegraph @

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