Debian Codegraph @

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