Debian Codegraph @

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