Debian Codegraph @

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