Debian Codegraph @

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