Debian Codegraph @

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