Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/ome_smart_fabric_uplink_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Modules
6 # Version 9.3.0
7 # Copyright (C) 2022-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
8
9 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
10 #
11
12
13 from __future__ import (absolute_import, division, print_function)
14
15 __metaclass__ = type
16
17 DOCUMENTATION = r'''
18 ---
19 module: ome_smart_fabric_uplink_info
20 short_description: Retrieve details of fabric uplink on OpenManage Enterprise Modular.
21 version_added: "7.1.0"
22 description: This module retrieve details of fabric uplink on OpenManage Enterprise Modular.
23 extends_documentation_fragment:
24 - dellemc.openmanage.omem_auth_options
25 options:
26 fabric_id:
27 type: str
28 description:
29 - Unique id of the fabric.
30 - I(fabric_id) is mutually exclusive with I(fabric_name).
31 fabric_name:
32 type: str
33 description:
34 - Unique name of the fabric.
35 - I(fabric_name) is mutually exclusive with I(fabric_id).
36 uplink_id:
37 type: str
38 description:
39 - Unique id of the uplink.
40 - I(uplink_id) is mutually exclusive with I(uplink_name).
41 - I(fabric_id) or I(fabric_name) is required along with I(uplink_id).
42 uplink_name:
43 type: str
44 description:
45 - Unique name of the uplink.
46 - I(uplink_name) is mutually exclusive with I(uplink_id).
47 - I(fabric_id) or I(fabric_name) is required along with I(uplink_name).
48 requirements:
49 - "python >= 3.9.6"
50 author:
51 - "Husniya Hameed(@husniya_hameed)"
52 notes:
53 - Run this module from a system that has direct access to Dell OpenManage Enterprise Modular.
54 - This module supports C(check_mode).
55 '''
56
57 EXAMPLES = r'''
58 ---
59 - name: Retrieve all fabric uplink information using fabric_id.
60 dellemc.openmanage.ome_smart_fabric_uplink_info:
61 hostname: "192.168.0.1"
62 username: "username"
63 password: "password"
64 ca_path: "/path/to/ca_cert.pem"
65 fabric_id: "61c20a59-9ed5-4ae5-b850-5e5acf42d2f2"
66
67 - name: Retrieve all fabric uplink information using fabric_name.
68 dellemc.openmanage.ome_smart_fabric_uplink_info:
69 hostname: "192.168.0.1"
70 username: "username"
71 password: "password"
72 ca_path: "/path/to/ca_cert.pem"
73 fabric_name: "f1"
74
75 - name: Retrieve specific fabric information using uplink_id.
76 dellemc.openmanage.ome_smart_fabric_uplink_info:
77 hostname: "192.168.0.1"
78 username: "username"
79 password: "password"
80 ca_path: "/path/to/ca_cert.pem"
81 fabric_id: "61c20a59-9ed5-4ae5-b850-5e5acf42d2f2"
82 uplink_id: "1ad54420-b145-49a1-9779-21a579ef6f2d"
83
84 - name: Retrieve specific fabric information using uplink_name.
85 dellemc.openmanage.ome_smart_fabric_uplink_info:
86 hostname: "192.168.0.1"
87 username: "username"
88 password: "password"
89 ca_path: "/path/to/ca_cert.pem"
90 fabric_id: "61c20a59-9ed5-4ae5-b850-5e5acf42d2f2"
91 uplink_name: "u1"
92 '''
93
94 RETURN = r'''
95 ---
96 msg:
97 type: str
98 description: Status of fabric uplink information retrieval.
99 returned: always
100 sample: "Successfully retrieved the fabric uplink information."
101 uplink_info:
102 type: list
103 description: Information about the fabric uplink.
104 returned: on success
105 sample: [{
106 "Description": "",
107 "Id": "1ad54420-b145-49a1-9779-21a579ef6f2d",
108 "MediaType": "Ethernet",
109 "Name": "u1",
110 "NativeVLAN": 1,
111 "Networks": [{
112 "CreatedBy": "system",
113 "CreationTime": "2018-09-25 14:46:12.374",
114 "Description": null,
115 "Id": 10155,
116 "InternalRefNWUUId": "f15a36b6-e3d3-46b2-9e7d-bf9cd66e180d",
117 "Name": "testvlan",
118 "Type": 1,
119 "UpdatedBy": "root",
120 "UpdatedTime": "2019-06-27 15:06:22.836",
121 "VlanMaximum": 143,
122 "VlanMinimum": 143
123 }],
124 "Ports": [{
125 "AdminStatus": "Enabled",
126 "BlinkStatus": "OFF",
127 "ConfiguredSpeed": "0",
128 "CurrentSpeed": "0",
129 "Description": "",
130 "Id": "SVCTAG1:ethernet1/1/35",
131 "MaxSpeed": "0",
132 "MediaType": "Ethernet",
133 "Name": "",
134 "NodeServiceTag": "SVCTAG1",
135 "OpticsType": "NotPresent",
136 "PortNumber": "ethernet1/1/35",
137 "Role": "Uplink",
138 "Status": "Down",
139 "Type": "PhysicalEthernet"
140 },
141 {
142 "AdminStatus": "Enabled",
143 "BlinkStatus": "OFF",
144 "ConfiguredSpeed": "0",
145 "CurrentSpeed": "0",
146 "Description": "",
147 "Id": "SVCTAG1:ethernet1/1/35",
148 "MaxSpeed": "0",
149 "MediaType": "Ethernet",
150 "Name": "",
151 "NodeServiceTag": "SVCTAG1",
152 "OpticsType": "NotPresent",
153 "PortNumber": "ethernet1/1/35",
154 "Role": "Uplink",
155 "Status": "Down",
156 "Type": "PhysicalEthernet"
157 }],
158 "Summary": {
159 "NetworkCount": 1,
160 "PortCount": 2
161 },
162 "UfdEnable": "Disabled"
163 }]
164 error_info:
165 description: Details of the HTTP Error.
166 returned: on HTTP error
167 type: dict
168 sample: {
169 "error": {
170 "code": "Base.1.0.GeneralError",
171 "message": "A general error has occurred. See ExtendedInfo for more information.",
172 "@Message.ExtendedInfo": [
173 {
174 "MessageId": "CGEN1006",
175 "RelatedProperties": [],
176 "Message": "Unable to complete the request because the resource URI does not exist or is not implemented.",
177 "MessageArgs": [],
178 "Severity": "Critical",
179 "Resolution": "Check the request resource URI. Refer to the OpenManage Enterprise-Modular User's Guide
180 for more information about resource URI and its properties."
181 }
182 ]
183 }
184 }
185 '''
186
187 import json
188 from ssl import SSLError
189 from ansible.module_utils.urls import ConnectionError, SSLValidationError
190 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
191 from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule
192 from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import strip_substr_dict
193
194 ALL_UPLINKS_URI = "NetworkService/Fabrics('{0}')/Uplinks?$expand=Networks,Ports"
195 FABRIC_URI = "NetworkService/Fabrics"
196 UPLINK_URI = "NetworkService/Fabrics('{0}')/Uplinks('{1}')?$expand=Networks,Ports"
197 # Messages
198 SUCCESS_MSG = "Successfully retrieved the fabric uplink information."
199 UNSUCCESS_MSG = "Unable to retrieve smart fabric uplink information."
200 INVALID_FABRIC_ID = "Unable to retrieve smart fabric uplink information with fabric ID {0}."
201 INVALID_FABRIC_NAME = "Unable to retrieve smart fabric uplink information with fabric name {0}."
202 INVALID_UPLINK_ID = "Unable to retrieve smart fabric uplink information with uplink ID {0}."
203 INVALID_UPLINK_NAME = "Unable to retrieve smart fabric uplink information with uplink name {0}."
204 ID_UNAVAILABLE = "fabric_id or fabric_name is required along with uplink_id."
205 NAME_UNAVAILABLE = "fabric_id or fabric_name is required along with uplink_name."
206
207
208 def get_all_uplink_details(module, rest_obj):
209 resp = []
210 try:
211 fabric_det = rest_obj.invoke_request("GET", FABRIC_URI)
212 fabric_resp = fabric_det.json_data.get("value")
213 for each in fabric_resp:
214 if each.get("[email protected]"):
215 uplink_det = each.get("[email protected]")
216 uplink = uplink_det[5:] + "?$expand=Networks,Ports"
217 uplink_details = rest_obj.invoke_request("GET", uplink)
218 for val in uplink_details.json_data.get("value"):
219 resp.append(val)
220 except HTTPError:
221 module.exit_json(msg=UNSUCCESS_MSG, failed=True)
222 return resp
223
224
225 def get_uplink_details_from_fabric_id(module, rest_obj, fabric_id):
226 resp = []
227 try:
228 resp_det = rest_obj.invoke_request("GET", ALL_UPLINKS_URI.format(fabric_id))
229 resp = resp_det.json_data.get("value")
230 except HTTPError:
231 module.exit_json(msg=INVALID_FABRIC_ID.format(fabric_id), failed=True)
232 return resp
233
234
235 def get_fabric_id_from_name(module, rest_obj, fabric_name):
236 fabric_id = ""
237 try:
238 resp_det = rest_obj.invoke_request("GET", FABRIC_URI)
239 resp = resp_det.json_data.get("value")
240 for each in resp:
241 if each["Name"] == fabric_name:
242 fabric_id = each["Id"]
243 break
244 except HTTPError:
245 module.exit_json(msg=UNSUCCESS_MSG, failed=True)
246 if not fabric_id:
247 module.exit_json(msg=INVALID_FABRIC_NAME.format(fabric_name), failed=True)
248 return fabric_id
249
250
251 def get_uplink_details(module, rest_obj, fabric_id, uplink_id):
252 resp = []
253 try:
254 resp_det = rest_obj.invoke_request("GET", UPLINK_URI.format(fabric_id, uplink_id))
255 resp = [resp_det.json_data]
256 except HTTPError:
257 module.exit_json(msg=INVALID_UPLINK_ID.format(uplink_id), failed=True)
258 return resp
259
260
261 def get_uplink_id_from_name(module, rest_obj, uplink_name, fabric_id):
262 uplink_id = ""
263 try:
264 resp_det = rest_obj.invoke_request("GET", ALL_UPLINKS_URI.format(fabric_id))
265 resp = resp_det.json_data.get("value")
266 for each in resp:
267 if each["Name"] == uplink_name:
268 uplink_id = each["Id"]
269 break
270 except HTTPError:
271 module.exit_json(msg=UNSUCCESS_MSG, failed=True)
272 if not uplink_id:
273 module.exit_json(msg=INVALID_UPLINK_NAME.format(uplink_name), failed=True)
274 return uplink_id
275
276
277 def strip_uplink_info(uplink_info):
278 for item in uplink_info:
279 item = strip_substr_dict(item)
280 if item["Networks"]:
281 for net in item["Networks"]:
282 net = strip_substr_dict(net)
283 if item["Ports"]:
284 for port in item["Ports"]:
285 port = strip_substr_dict(port)
286 return uplink_info
287
288
289 def main():
290 specs = {
291 "fabric_id": {"type": "str"},
292 "fabric_name": {"type": "str"},
293 "uplink_id": {"type": "str"},
294 "uplink_name": {"type": "str"}
295 }
296
297 module = OmeAnsibleModule(
298 argument_spec=specs,
299 mutually_exclusive=[('fabric_id', 'fabric_name'), ('uplink_id', 'uplink_name')],
300 supports_check_mode=True
301 )
302 try:
303 with RestOME(module.params, req_session=True) as rest_obj:
304 uplink_info = []
305 fabric_id = module.params["fabric_id"]
306 fabric_name = module.params["fabric_name"]
307 uplink_id = module.params["uplink_id"]
308 uplink_name = module.params["uplink_name"]
309
310 if fabric_id:
311 uplink_info = get_uplink_details_from_fabric_id(module, rest_obj, fabric_id)
312 elif fabric_name:
313 fabric_id = get_fabric_id_from_name(module, rest_obj, fabric_name)
314 if fabric_id:
315 uplink_info = get_uplink_details_from_fabric_id(module, rest_obj, fabric_id)
316
317 if uplink_id and not (fabric_id or fabric_name):
318 module.exit_json(msg=ID_UNAVAILABLE, failed=True)
319 elif uplink_id:
320 uplink_info = get_uplink_details(module, rest_obj, fabric_id, uplink_id)
321 elif uplink_name and not (fabric_id or fabric_name):
322 module.exit_json(msg=NAME_UNAVAILABLE, failed=True)
323 elif uplink_name:
324 uplink_id = get_uplink_id_from_name(module, rest_obj, uplink_name, fabric_id)
325 if uplink_id:
326 uplink_info = get_uplink_details(module, rest_obj, fabric_id, uplink_id)
327
328 if fabric_id is None and fabric_name is None and uplink_id is None and uplink_name is None:
329 uplink_info = get_all_uplink_details(module, rest_obj)
330 if not bool(uplink_info):
331 module.exit_json(msg=SUCCESS_MSG, uplink_info=uplink_info)
332
333 uplink_info_strip = strip_uplink_info(uplink_info)
334 module.exit_json(msg=SUCCESS_MSG, uplink_info=uplink_info_strip)
335
336 except HTTPError as err:
337 module.fail_json(msg=str(err), error_info=json.load(err))
338 except URLError as err:
339 module.exit_json(msg=str(err), unreachable=True)
340 except (IOError, ValueError, TypeError, ConnectionError, SSLValidationError, SSLError, OSError) as err:
341 module.fail_json(msg=str(err))
342
343
344 if __name__ == "__main__":
345 main()