Debian Codegraph @

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