Debian Codegraph @

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