Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/networks_clients_info.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 )
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 networkId=dict(type="str"),
29 t0=dict(type="str"),
30 timespan=dict(type="float"),
31 perPage=dict(type="int"),
32 total_pages=dict(type="int"),
33 direction=dict(type="str"),
34 startingAfter=dict(type="str"),
35 endingBefore=dict(type="str"),
36 statuses=dict(type="list"),
37 ip=dict(type="str"),
38 ip6=dict(type="str"),
39 ip6Local=dict(type="str"),
40 mac=dict(type="str"),
41 os=dict(type="str"),
42 pskGroup=dict(type="str"),
43 description=dict(type="str"),
44 vlan=dict(type="str"),
45 namedVlan=dict(type="str"),
46 recentDeviceConnections=dict(type="list"),
47 clientId=dict(type="str"),
48 ))
49
50 required_if = []
51 required_one_of = []
52 mutually_exclusive = []
53 required_together = []
54
55
56 class ActionModule(ActionBase):
57 def __init__(self, *args, **kwargs):
58 if not ANSIBLE_UTILS_IS_INSTALLED:
59 raise AnsibleActionFail(
60 "ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'")
61 super(ActionModule, self).__init__(*args, **kwargs)
62 self._supports_async = False
63 self._supports_check_mode = True
64 self._result = None
65
66 # Checks the supplied parameters against the argument spec for this module
67 def _check_argspec(self):
68 aav = AnsibleArgSpecValidator(
69 data=self._task.args,
70 schema=dict(argument_spec=argument_spec),
71 schema_format="argspec",
72 schema_conditionals=dict(
73 required_if=required_if,
74 required_one_of=required_one_of,
75 mutually_exclusive=mutually_exclusive,
76 required_together=required_together,
77 ),
78 name=self._task.action,
79 )
80 valid, errors, self._task.args = aav.validate()
81 if not valid:
82 raise AnsibleActionFail(errors)
83
84 def get_object(self, params):
85 new_object = {}
86 if params.get("networkId") is not None:
87 new_object["networkId"] = params.get(
88 "networkId")
89 if params.get("clientId") is not None:
90 new_object["clientId"] = params.get(
91 "clientId")
92 return new_object
93
94 def get_all(self, params):
95 new_object = {}
96 if params.get("networkId") is not None:
97 new_object["networkId"] = params.get(
98 "networkId")
99 if params.get("t0") is not None:
100 new_object["t0"] = params.get(
101 "t0")
102 if params.get("timespan") is not None:
103 new_object["timespan"] = params.get(
104 "timespan")
105 if params.get("perPage") is not None:
106 new_object["perPage"] = params.get(
107 "perPage")
108 new_object['total_pages'] = params.get(
109 "total_pages") or 1
110 new_object['direction'] = params.get(
111 "direction") or "next"
112 if params.get("startingAfter") is not None:
113 new_object["startingAfter"] = params.get(
114 "startingAfter")
115 if params.get("endingBefore") is not None:
116 new_object["endingBefore"] = params.get(
117 "endingBefore")
118 if params.get("statuses") is not None:
119 new_object["statuses"] = params.get(
120 "statuses")
121 if params.get("ip") is not None:
122 new_object["ip"] = params.get(
123 "ip")
124 if params.get("ip6") is not None:
125 new_object["ip6"] = params.get(
126 "ip6")
127 if params.get("ip6Local") is not None:
128 new_object["ip6Local"] = params.get(
129 "ip6Local")
130 if params.get("mac") is not None:
131 new_object["mac"] = params.get(
132 "mac")
133 if params.get("os") is not None:
134 new_object["os"] = params.get(
135 "os")
136 if params.get("pskGroup") is not None:
137 new_object["pskGroup"] = params.get(
138 "pskGroup")
139 if params.get("description") is not None:
140 new_object["description"] = params.get(
141 "description")
142 if params.get("vlan") is not None:
143 new_object["vlan"] = params.get(
144 "vlan")
145 if params.get("namedVlan") is not None:
146 new_object["namedVlan"] = params.get(
147 "namedVlan")
148 if params.get("recentDeviceConnections") is not None:
149 new_object["recentDeviceConnections"] = params.get(
150 "recentDeviceConnections")
151
152 return new_object
153
154 def run(self, tmp=None, task_vars=None):
155 self._task.diff = False
156 self._result = super(ActionModule, self).run(tmp, task_vars)
157 self._result["changed"] = False
158 self._check_argspec()
159
160 self._result.update(dict(meraki_response={}))
161
162 meraki = MERAKI(params=self._task.args)
163
164 id = self._task.args.get("clientId")
165 if id:
166 response = meraki.exec_meraki(
167 family="networks",
168 function='getNetworkClient',
169 params=self.get_object(self._task.args),
170 )
171 self._result.update(dict(meraki_response=response))
172 self._result.update(meraki.exit_json())
173 return self._result
174 if not id:
175 response = meraki.exec_meraki(
176 family="networks",
177 function='getNetworkClients',
178 params=self.get_all(self._task.args),
179 )
180 self._result.update(dict(meraki_response=response))
181 self._result.update(meraki.exit_json())
182 return self._result