Debian Codegraph @

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