Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/devices_live_tools_throughput_test.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 )
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 callback=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 DevicesLiveToolsThroughputTest(object):
46 def __init__(self, params, meraki):
47 self.meraki = meraki
48 self.new_object = dict(
49 callback=params.get("callback"),
50 serial=params.get("serial"),
51 )
52
53 def create_params(self):
54 new_object_params = {}
55 if self.new_object.get('callback') is not None or self.new_object.get(
56 'callback') is not None:
57 new_object_params['callback'] = self.new_object.get(
58 'callback') or self.new_object.get('callback')
59 if self.new_object.get('serial') is not None or self.new_object.get(
60 'serial') is not None:
61 new_object_params['serial'] = self.new_object.get('serial') or \
62 self.new_object.get('serial')
63 return new_object_params
64
65 def get_object_by_name(self, name):
66 result = None
67 # NOTE: Does not have a get by name and get all
68 return result
69
70 def get_object_by_id(self, id):
71 result = None
72 # NOTE: Does not have a get by id method or it is in another action
73 return result
74
75 def exists(self):
76 prev_obj = None
77 id_exists = False
78 name_exists = False
79 o_id = self.new_object.get("serial")
80 name = self.new_object.get("name")
81 if o_id:
82 prev_obj = self.get_object_by_name(o_id)
83 id_exists = prev_obj is not None and isinstance(prev_obj, dict)
84 if not id_exists and name:
85 prev_obj = self.get_object_by_name(name)
86 name_exists = prev_obj is not None and isinstance(prev_obj, dict)
87 if name_exists:
88 _id = prev_obj.get("id")
89 if id_exists and name_exists and o_id != _id:
90 raise InconsistentParameters(
91 "The 'id' and 'name' params don't refer to the same object")
92 if _id:
93 self.new_object.update(dict(id=_id))
94 it_exists = prev_obj is not None and isinstance(prev_obj, dict)
95 return (it_exists, prev_obj)
96
97 def requires_update(self, current_obj):
98 requested_obj = self.new_object
99
100 obj_params = [
101 ("callback", "callback"),
102 ("serial", "serial"),
103 ]
104 # Method 1. Params present in request (Ansible) obj are the same as the current (ISE) params
105 # If any does not have eq params, it requires update
106 return any(
107 not meraki_compare_equality2(
108 current_obj.get(meraki_param),
109 requested_obj.get(ansible_param)) for (
110 meraki_param,
111 ansible_param) in obj_params)
112
113 def create(self):
114 result = self.meraki.exec_meraki(
115 family="devices",
116 function="createDeviceLiveToolsThroughputTest",
117 params=self.create_params(),
118 op_modifies=True,
119 )
120 return result
121
122
123 class ActionModule(ActionBase):
124 def __init__(self, *args, **kwargs):
125 if not ANSIBLE_UTILS_IS_INSTALLED:
126 raise AnsibleActionFail(
127 "ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'")
128 super(ActionModule, self).__init__(*args, **kwargs)
129 self._supports_async = False
130 self._supports_check_mode = False
131 self._result = None
132
133 # Checks the supplied parameters against the argument spec for this module
134 def _check_argspec(self):
135 aav = AnsibleArgSpecValidator(
136 data=self._task.args,
137 schema=dict(argument_spec=argument_spec),
138 schema_format="argspec",
139 schema_conditionals=dict(
140 required_if=required_if,
141 required_one_of=required_one_of,
142 mutually_exclusive=mutually_exclusive,
143 required_together=required_together,
144 ),
145 name=self._task.action,
146 )
147 valid, errors, self._task.args = aav.validate()
148 if not valid:
149 raise AnsibleActionFail(errors)
150
151 def run(self, tmp=None, task_vars=None):
152 self._task.diff = False
153 self._result = super(ActionModule, self).run(tmp, task_vars)
154 self._result["changed"] = False
155 self._check_argspec()
156
157 meraki = MERAKI(self._task.args)
158 obj = DevicesLiveToolsThroughputTest(self._task.args, meraki)
159
160 state = self._task.args.get("state")
161
162 response = None
163 if state == "present":
164 (obj_exists, prev_obj) = obj.exists()
165 if obj_exists:
166 if obj.requires_update(prev_obj):
167 response = prev_obj
168 meraki.object_present_and_different()
169 else:
170 response = prev_obj
171 meraki.object_already_present()
172 else:
173 response = obj.create()
174 meraki.object_created()
175
176 self._result.update(dict(meraki_response=response))
177 self._result.update(meraki.exit_json())
178 return self._result