Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/networks_appliance_firewall_settings.py

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