Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/devices_sensor_commands_info.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 )
23
24 # Get common arguments specification
25 argument_spec = meraki_argument_spec()
26 # Add arguments specific for this module
27 argument_spec.update(dict(
28 serial=dict(type="str"),
29 operations=dict(type="list"),
30 perPage=dict(type="int"),
31 total_pages=dict(type="int"),
32 direction=dict(type="str"),
33 startingAfter=dict(type="str"),
34 endingBefore=dict(type="str"),
35 sortOrder=dict(type="str"),
36 t0=dict(type="str"),
37 t1=dict(type="str"),
38 timespan=dict(type="float"),
39 commandId=dict(type="str"),
40 ))
41
42 required_if = []
43 required_one_of = []
44 mutually_exclusive = []
45 required_together = []
46
47
48 class ActionModule(ActionBase):
49 def __init__(self, *args, **kwargs):
50 if not ANSIBLE_UTILS_IS_INSTALLED:
51 raise AnsibleActionFail(
52 "ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'")
53 super(ActionModule, self).__init__(*args, **kwargs)
54 self._supports_async = False
55 self._supports_check_mode = True
56 self._result = None
57
58 # Checks the supplied parameters against the argument spec for this module
59 def _check_argspec(self):
60 aav = AnsibleArgSpecValidator(
61 data=self._task.args,
62 schema=dict(argument_spec=argument_spec),
63 schema_format="argspec",
64 schema_conditionals=dict(
65 required_if=required_if,
66 required_one_of=required_one_of,
67 mutually_exclusive=mutually_exclusive,
68 required_together=required_together,
69 ),
70 name=self._task.action,
71 )
72 valid, errors, self._task.args = aav.validate()
73 if not valid:
74 raise AnsibleActionFail(errors)
75
76 def get_object(self, params):
77 new_object = {}
78 if params.get("serial") is not None:
79 new_object["serial"] = params.get(
80 "serial")
81 if params.get("commandId") is not None:
82 new_object["commandId"] = params.get(
83 "commandId")
84 return new_object
85
86 def get_all(self, params):
87 new_object = {}
88 if params.get("serial") is not None:
89 new_object["serial"] = params.get(
90 "serial")
91 if params.get("operations") is not None:
92 new_object["operations"] = params.get(
93 "operations")
94 if params.get("perPage") is not None:
95 new_object["perPage"] = params.get(
96 "perPage")
97 new_object['total_pages'] = params.get(
98 "total_pages") or 1
99 new_object['direction'] = params.get(
100 "direction") or "next"
101 if params.get("startingAfter") is not None:
102 new_object["startingAfter"] = params.get(
103 "startingAfter")
104 if params.get("endingBefore") is not None:
105 new_object["endingBefore"] = params.get(
106 "endingBefore")
107 if params.get("sortOrder") is not None:
108 new_object["sortOrder"] = params.get(
109 "sortOrder")
110 if params.get("t0") is not None:
111 new_object["t0"] = params.get(
112 "t0")
113 if params.get("t1") is not None:
114 new_object["t1"] = params.get(
115 "t1")
116 if params.get("timespan") is not None:
117 new_object["timespan"] = params.get(
118 "timespan")
119
120 return new_object
121
122 def run(self, tmp=None, task_vars=None):
123 self._task.diff = False
124 self._result = super(ActionModule, self).run(tmp, task_vars)
125 self._result["changed"] = False
126 self._check_argspec()
127
128 self._result.update(dict(meraki_response={}))
129
130 meraki = MERAKI(params=self._task.args)
131
132 id = self._task.args.get("commandId")
133 if id:
134 response = meraki.exec_meraki(
135 family="sensor",
136 function='getDeviceSensorCommand',
137 params=self.get_object(self._task.args),
138 )
139 self._result.update(dict(meraki_response=response))
140 self._result.update(meraki.exit_json())
141 return self._result
142 if not id:
143 response = meraki.exec_meraki(
144 family="sensor",
145 function='getDeviceSensorCommands',
146 params=self.get_all(self._task.args),
147 )
148 self._result.update(dict(meraki_response=response))
149 self._result.update(meraki.exit_json())
150 return self._result