Debian Codegraph @

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