Debian Codegraph @

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