Debian Codegraph @

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