Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/networks_switch_storm_control.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 broadcastThreshold=dict(type="int"),
35 multicastThreshold=dict(type="int"),
36 unknownUnicastThreshold=dict(type="int"),
37 treatTheseTrafficTypesAsOneThreshold=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 NetworksSwitchStormControl(object):
50 def __init__(self, params, meraki):
51 self.meraki = meraki
52 self.new_object = dict(
53 broadcastThreshold=params.get("broadcastThreshold"),
54 multicastThreshold=params.get("multicastThreshold"),
55 unknownUnicastThreshold=params.get("unknownUnicastThreshold"),
56 treatTheseTrafficTypesAsOneThreshold=params.get("treatTheseTrafficTypesAsOneThreshold"),
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('broadcastThreshold') is not None or self.new_object.get(
71 'broadcast_threshold') is not None:
72 new_object_params['broadcastThreshold'] = self.new_object.get(
73 'broadcastThreshold') or self.new_object.get('broadcast_threshold')
74 if self.new_object.get('multicastThreshold') is not None or self.new_object.get(
75 'multicast_threshold') is not None:
76 new_object_params['multicastThreshold'] = self.new_object.get(
77 'multicastThreshold') or self.new_object.get('multicast_threshold')
78 if self.new_object.get('unknownUnicastThreshold') is not None or self.new_object.get(
79 'unknown_unicast_threshold') is not None:
80 new_object_params['unknownUnicastThreshold'] = self.new_object.get(
81 'unknownUnicastThreshold') or self.new_object.get('unknown_unicast_threshold')
82 if self.new_object.get('treatTheseTrafficTypesAsOneThreshold') is not None or self.new_object.get(
83 'treat_these_traffic_types_as_one_threshold') is not None:
84 new_object_params['treatTheseTrafficTypesAsOneThreshold'] = self.new_object.get(
85 'treatTheseTrafficTypesAsOneThreshold') or self.new_object.get('treat_these_traffic_types_as_one_threshold')
86 if self.new_object.get('networkId') is not None or self.new_object.get(
87 'network_id') is not None:
88 new_object_params['networkId'] = self.new_object.get(
89 'networkId') or self.new_object.get('network_id')
90 return new_object_params
91
92 def get_object_by_name(self, name):
93 result = None
94 # NOTE: Does not have a get by name method, using get all
95 try:
96 items = self.meraki.exec_meraki(
97 family="switch",
98 function="getNetworkSwitchStormControl",
99 params=self.get_all_params(name=name),
100 )
101 if isinstance(items, dict):
102 if 'response' in items:
103 items = items.get('response')
104 result = get_dict_result(items, 'name', name)
105 if result is None:
106 result = items
107 except Exception as e:
108 print("Error: ", e)
109 result = None
110 return result
111
112 def get_object_by_id(self, id):
113 result = None
114 # NOTE: Does not have a get by id method or it is in another action
115 return result
116
117 def exists(self):
118 prev_obj = None
119 id_exists = False
120 name_exists = False
121 o_id = self.new_object.get(
122 "networkId") or self.new_object.get("network_id")
123 name = self.new_object.get("name")
124 if o_id:
125 prev_obj = self.get_object_by_name(o_id)
126 id_exists = prev_obj is not None and isinstance(prev_obj, dict)
127 if not id_exists and name:
128 prev_obj = self.get_object_by_name(name)
129 name_exists = prev_obj is not None and isinstance(prev_obj, dict)
130 if name_exists:
131 _id = prev_obj.get("id")
132 if id_exists and name_exists and o_id != _id:
133 raise InconsistentParameters(
134 "The 'id' and 'name' params don't refer to the same object")
135 if _id:
136 self.new_object.update(dict(id=_id))
137 it_exists = prev_obj is not None and isinstance(prev_obj, dict)
138 return (it_exists, prev_obj)
139
140 def requires_update(self, current_obj):
141 requested_obj = self.new_object
142
143 obj_params = [
144 ("broadcastThreshold", "broadcastThreshold"),
145 ("multicastThreshold", "multicastThreshold"),
146 ("unknownUnicastThreshold", "unknownUnicastThreshold"),
147 ("treatTheseTrafficTypesAsOneThreshold", "treatTheseTrafficTypesAsOneThreshold"),
148 ("networkId", "networkId"),
149 ]
150 # Method 1. Params present in request (Ansible) obj are the same as the current (ISE) params
151 # If any does not have eq params, it requires update
152 return any(
153 not meraki_compare_equality2(
154 current_obj.get(meraki_param),
155 requested_obj.get(ansible_param)) for (
156 meraki_param,
157 ansible_param) in obj_params)
158
159 def update(self):
160 id = self.new_object.get("id")
161 name = self.new_object.get("name")
162 result = None
163 result = self.meraki.exec_meraki(
164 family="switch",
165 function="updateNetworkSwitchStormControl",
166 params=self.update_all_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 = NetworksSwitchStormControl(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