| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
from __future__ import (absolute_import, division, print_function) |
| 13 |
__metaclass__ = type |
| 14 |
|
| 15 |
DOCUMENTATION = """ |
| 16 |
--- |
| 17 |
module: ome_server_interface_profile_info |
| 18 |
short_description: Retrieves the information of server interface profile on OpenManage Enterprise Modular. |
| 19 |
description: This module allows to retrieves the information of server interface profile |
| 20 |
on OpenManage Enterprise Modular. |
| 21 |
version_added: "5.1.0" |
| 22 |
extends_documentation_fragment: |
| 23 |
- dellemc.openmanage.omem_auth_options |
| 24 |
options: |
| 25 |
device_id: |
| 26 |
type: list |
| 27 |
description: |
| 28 |
- The ID of the device. |
| 29 |
- I(device_id) is mutually exclusive with I(device_service_tag). |
| 30 |
elements: int |
| 31 |
device_service_tag: |
| 32 |
type: list |
| 33 |
description: |
| 34 |
- The service tag of the device. |
| 35 |
- I(device_service_tag) is mutually exclusive with I(device_id). |
| 36 |
elements: str |
| 37 |
requirements: |
| 38 |
- "python >= 3.9.6" |
| 39 |
author: |
| 40 |
- "Felix Stephen (@felixs88)" |
| 41 |
notes: |
| 42 |
- Run this module from a system that has direct access to OpenManage Enterprise Modular. |
| 43 |
- This module supports C(check_mode). |
| 44 |
""" |
| 45 |
|
| 46 |
EXAMPLES = """ |
| 47 |
--- |
| 48 |
- name: Retrieves the server interface profiles of all the device using device ID. |
| 49 |
dellemc.openmanage.ome_server_interface_profile_info: |
| 50 |
hostname: "192.168.0.1" |
| 51 |
username: "username" |
| 52 |
password: "password" |
| 53 |
ca_path: "/path/to/ca_cert.pem" |
| 54 |
device_id: |
| 55 |
- 10001 |
| 56 |
- 10002 |
| 57 |
|
| 58 |
- name: Retrieves the server interface profiles of all the device using device service tag. |
| 59 |
dellemc.openmanage.ome_server_interface_profile_info: |
| 60 |
hostname: "192.168.0.1" |
| 61 |
username: "username" |
| 62 |
password: "password" |
| 63 |
ca_path: "/path/to/ca_cert.pem" |
| 64 |
device_service_tag: |
| 65 |
- 6GHH6H2 |
| 66 |
- 6KHH6H3 |
| 67 |
""" |
| 68 |
|
| 69 |
RETURN = """ |
| 70 |
--- |
| 71 |
msg: |
| 72 |
type: str |
| 73 |
description: Overall status of the server interface profile information. |
| 74 |
returned: on success |
| 75 |
sample: "Successfully retrieved the server interface profile information." |
| 76 |
server_profiles: |
| 77 |
type: list |
| 78 |
description: Returns the information of collected server interface profile information. |
| 79 |
returned: success |
| 80 |
sample: [ |
| 81 |
{ |
| 82 |
"BondingTechnology": "LACP", |
| 83 |
"Id": "6KZK6K2", |
| 84 |
"ServerInterfaceProfile": [ |
| 85 |
{ |
| 86 |
"FabricId": "1ea6bf64-3cf0-4e06-a136-5046d874d1e7", |
| 87 |
"Id": "NIC.Mezzanine.1A-1-1", |
| 88 |
"NativeVLAN": 0, |
| 89 |
"Networks": [ |
| 90 |
{ |
| 91 |
"CreatedBy": "system", |
| 92 |
"CreationTime": "2018-11-27 10:22:14.140", |
| 93 |
"Description": "VLAN 1", |
| 94 |
"Id": 10001, |
| 95 |
"InternalRefNWUUId": "add035b9-a971-400d-a3fa-bb365df1d476", |
| 96 |
Name": "VLAN 1", |
| 97 |
"Type": 2, |
| 98 |
"UpdatedBy": null, |
| 99 |
"UpdatedTime": "2018-11-27 10:22:14.140", |
| 100 |
"VlanMaximum": 1, |
| 101 |
"VlanMinimum": 1 |
| 102 |
} |
| 103 |
], |
| 104 |
"NicBonded": true, |
| 105 |
"OnboardedPort": "59HW8X2:ethernet1/1/1" |
| 106 |
}, |
| 107 |
{ |
| 108 |
"FabricId": "3ea6be04-5cf0-4e05-a136-5046d874d1e6", |
| 109 |
"Id": "NIC.Mezzanine.1A-2-1", |
| 110 |
"NativeVLAN": 0, |
| 111 |
"Networks": [ |
| 112 |
{ |
| 113 |
"CreatedBy": "system", |
| 114 |
"CreationTime": "2018-09-25 14:46:12.374", |
| 115 |
"Description": null, |
| 116 |
"Id": 10155, |
| 117 |
"InternalRefNWUUId": "f15a36b6-e3d3-46b2-9e7d-bf9cd66e180d", |
| 118 |
"Name": "jagvlan", |
| 119 |
"Type": 1, |
| 120 |
"UpdatedBy": null, |
| 121 |
"UpdatedTime": "2018-09-25 14:46:12.374", |
| 122 |
"VlanMaximum": 143, |
| 123 |
"VlanMinimum": 143 |
| 124 |
} |
| 125 |
], |
| 126 |
"NicBonded": false, |
| 127 |
"OnboardedPort": "6H7J6Z2:ethernet1/1/1" |
| 128 |
} |
| 129 |
] |
| 130 |
} |
| 131 |
] |
| 132 |
error_info: |
| 133 |
description: Details of the HTTP Error. |
| 134 |
returned: on HTTP error |
| 135 |
type: dict |
| 136 |
sample: { |
| 137 |
"error": { |
| 138 |
"code": "Base.1.0.GeneralError", |
| 139 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 140 |
"@Message.ExtendedInfo": [ |
| 141 |
{ |
| 142 |
"MessageId": "GEN1234", |
| 143 |
"RelatedProperties": [], |
| 144 |
"Message": "Unable to process the request because an error occurred.", |
| 145 |
"MessageArgs": [], |
| 146 |
"Severity": "Critical", |
| 147 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 148 |
} |
| 149 |
] |
| 150 |
} |
| 151 |
} |
| 152 |
""" |
| 153 |
|
| 154 |
|
| 155 |
import json |
| 156 |
from ssl import SSLError |
| 157 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 158 |
from ansible.module_utils.urls import ConnectionError |
| 159 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 160 |
|
| 161 |
DOMAIN_URI = "ManagementDomainService/Domains" |
| 162 |
PROFILE_URI = "NetworkService/ServerProfiles" |
| 163 |
DEVICE_URI = "DeviceService/Devices" |
| 164 |
NETWORK_PROFILE_URI = "NetworkService/ServerProfiles('{0}')/ServerInterfaceProfiles" |
| 165 |
|
| 166 |
DOMAIN_FAIL_MSG = "The information retrieval operation of server interface profile is supported only on " \ |
| 167 |
"OpenManage Enterprise Modular." |
| 168 |
CONFIG_FAIL_MSG = "one of the following is required: device_id, device_service_tag." |
| 169 |
INVALID_DEVICE = "Unable to complete the operation because the entered " \ |
| 170 |
"target device {0}(s) '{1}' are invalid." |
| 171 |
PROFILE_ERR_MSG = "Unable to complete the operation because the server " \ |
| 172 |
"profile(s) for {0} do not exist in the Fabric Manager." |
| 173 |
SUCCESS_MSG = "Successfully retrieved the server interface profile information." |
| 174 |
|
| 175 |
|
| 176 |
def check_domain_service(module, rest_obj): |
| 177 |
try: |
| 178 |
rest_obj.invoke_request("GET", DOMAIN_URI, api_timeout=5) |
| 179 |
except HTTPError as err: |
| 180 |
err_message = json.load(err) |
| 181 |
if err_message["error"]["@Message.ExtendedInfo"][0]["MessageId"] == "CGEN1006": |
| 182 |
module.exit_json(msg=DOMAIN_FAIL_MSG, failed=True) |
| 183 |
return |
| 184 |
|
| 185 |
|
| 186 |
def get_sip_info(module, rest_obj): |
| 187 |
invalid, valid_service_tag, device_map = [], [], {} |
| 188 |
device_id, tag = module.params.get("device_id"), module.params.get("device_service_tag") |
| 189 |
key, value = ("Id", device_id) if device_id is not None else ("DeviceServiceTag", tag) |
| 190 |
resp_data = rest_obj.get_all_report_details(DEVICE_URI) |
| 191 |
if resp_data['report_list']: |
| 192 |
for each in value: |
| 193 |
each_device = list(filter(lambda d: d[key] in [each], resp_data["report_list"])) |
| 194 |
if each_device and key == "DeviceServiceTag": |
| 195 |
valid_service_tag.append(each) |
| 196 |
elif each_device and key == "Id": |
| 197 |
valid_service_tag.append(each_device[0]["DeviceServiceTag"]) |
| 198 |
device_map[each_device[0]["DeviceServiceTag"]] = each |
| 199 |
if not each_device: |
| 200 |
invalid.append(each) |
| 201 |
if invalid: |
| 202 |
err_value = "id" if key == "Id" else "service tag" |
| 203 |
module.exit_json(msg=INVALID_DEVICE.format(err_value, ",".join(map(str, set(invalid)))), failed=True) |
| 204 |
|
| 205 |
invalid_fabric_tag, sip_info = [], [] |
| 206 |
for pro_id in valid_service_tag: |
| 207 |
profile_dict = {} |
| 208 |
try: |
| 209 |
profile_resp = rest_obj.invoke_request("GET", "{0}('{1}')".format(PROFILE_URI, pro_id)) |
| 210 |
except HTTPError as err: |
| 211 |
err_message = json.load(err) |
| 212 |
if err_message.get('error', {}).get('@Message.ExtendedInfo')[0]["MessageId"] == "CDEV5008": |
| 213 |
if key == "Id": |
| 214 |
invalid_fabric_tag.append(device_map[pro_id]) |
| 215 |
else: |
| 216 |
invalid_fabric_tag.append(pro_id) |
| 217 |
else: |
| 218 |
profile_data = rest_obj.strip_substr_dict(profile_resp.json_data) |
| 219 |
profile_dict.update(profile_data) |
| 220 |
np_resp = rest_obj.invoke_request("GET", NETWORK_PROFILE_URI.format(pro_id)) |
| 221 |
sip_strip = [] |
| 222 |
for each in np_resp.json_data["value"]: |
| 223 |
np_strip_data = rest_obj.strip_substr_dict(each) |
| 224 |
np_strip_data["Networks"] = [rest_obj.strip_substr_dict(each) for each in np_strip_data["Networks"]] |
| 225 |
sip_strip.append(np_strip_data) |
| 226 |
profile_dict["ServerInterfaceProfile"] = sip_strip |
| 227 |
sip_info.append(profile_dict) |
| 228 |
|
| 229 |
if invalid_fabric_tag: |
| 230 |
module.exit_json(msg=PROFILE_ERR_MSG.format(", ".join(set(map(str, invalid_fabric_tag)))), failed=True) |
| 231 |
return sip_info |
| 232 |
|
| 233 |
|
| 234 |
def main(): |
| 235 |
argument_spec = { |
| 236 |
"device_id": {"required": False, "type": "list", "elements": "int"}, |
| 237 |
"device_service_tag": {"required": False, "type": "list", "elements": "str"}, |
| 238 |
} |
| 239 |
module = OmeAnsibleModule(argument_spec=argument_spec, |
| 240 |
mutually_exclusive=[('device_id', 'device_service_tag')], |
| 241 |
required_one_of=[["device_id", "device_service_tag"]], |
| 242 |
supports_check_mode=True) |
| 243 |
if not any([module.params.get("device_id"), module.params.get("device_service_tag")]): |
| 244 |
module.exit_json(msg=CONFIG_FAIL_MSG, failed=True) |
| 245 |
try: |
| 246 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 247 |
check_domain_service(module, rest_obj) |
| 248 |
sip_info = get_sip_info(module, rest_obj) |
| 249 |
module.exit_json(msg=SUCCESS_MSG, server_profiles=sip_info) |
| 250 |
except HTTPError as err: |
| 251 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 252 |
except URLError as err: |
| 253 |
module.exit_json(msg=str(err), unreachable=True) |
| 254 |
except (IOError, ValueError, SSLError, TypeError, ConnectionError, |
| 255 |
AttributeError, IndexError, KeyError, OSError) as err: |
| 256 |
module.exit_json(msg=str(err), failed=True) |
| 257 |
|
| 258 |
|
| 259 |
if __name__ == '__main__': |
| 260 |
main() |