| 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 = r''' |
| 16 |
--- |
| 17 |
module: ome_network_port_breakout |
| 18 |
short_description: This module allows to automate the port portioning or port breakout to logical sub ports |
| 19 |
version_added: "2.1.0" |
| 20 |
description: |
| 21 |
- This module allows to automate breaking out of IOMs in fabric mode into logical sub ports. |
| 22 |
- The port breakout operation is only supported in OpenManage Enterprise Modular. |
| 23 |
extends_documentation_fragment: |
| 24 |
- dellemc.openmanage.omem_auth_options |
| 25 |
options: |
| 26 |
target_port: |
| 27 |
required: true |
| 28 |
description: "The ID of the port in the switch to breakout. Enter the port ID in the format: service tag:port. |
| 29 |
For example, 2HB7NX2:ethernet1/1/13." |
| 30 |
type: str |
| 31 |
breakout_type: |
| 32 |
required: true |
| 33 |
description: |
| 34 |
- The preferred breakout type. For example, 4X10GE. |
| 35 |
- To revoke the default breakout configuration, enter 'HardwareDefault'. |
| 36 |
type: str |
| 37 |
requirements: |
| 38 |
- "python >= 3.9.6" |
| 39 |
author: "Felix Stephen (@felixs88)" |
| 40 |
notes: |
| 41 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise Modular. |
| 42 |
- This module supports C(check_mode). |
| 43 |
''' |
| 44 |
|
| 45 |
EXAMPLES = r''' |
| 46 |
--- |
| 47 |
- name: Port breakout configuration |
| 48 |
dellemc.openmanage.ome_network_port_breakout: |
| 49 |
hostname: "192.168.0.1" |
| 50 |
username: "username" |
| 51 |
password: "password" |
| 52 |
ca_path: "/path/to/ca_cert.pem" |
| 53 |
target_port: "2HB7NX2:phy-port1/1/11" |
| 54 |
breakout_type: "1X40GE" |
| 55 |
|
| 56 |
- name: Revoke the default breakout configuration |
| 57 |
dellemc.openmanage.ome_network_port_breakout: |
| 58 |
hostname: "192.168.0.1" |
| 59 |
username: "username" |
| 60 |
password: "password" |
| 61 |
ca_path: "/path/to/ca_cert.pem" |
| 62 |
target_port: "2HB7NX2:phy-port1/1/11" |
| 63 |
breakout_type: "HardwareDefault" |
| 64 |
''' |
| 65 |
|
| 66 |
RETURN = r''' |
| 67 |
--- |
| 68 |
msg: |
| 69 |
description: Overall status of the port configuration. |
| 70 |
returned: always |
| 71 |
type: str |
| 72 |
sample: Port breakout configuration job submitted successfully. |
| 73 |
breakout_status: |
| 74 |
description: Details of the OpenManage Enterprise jobs. |
| 75 |
returned: success |
| 76 |
type: dict |
| 77 |
sample: { |
| 78 |
"Builtin": false, |
| 79 |
"CreatedBy": "root", |
| 80 |
"Editable": true, |
| 81 |
"EndTime": null, |
| 82 |
"Id": 11111, |
| 83 |
"JobDescription": "", |
| 84 |
"JobName": "Breakout Port", |
| 85 |
"JobStatus": {"Id": 1112, "Name": "New"}, |
| 86 |
"JobType": {"Id": 3, "Internal": false, "Name": "DeviceAction_Task"}, |
| 87 |
"LastRun": null, |
| 88 |
"LastRunStatus": {"Id": 1113, "Name": "NotRun"}, |
| 89 |
"NextRun": null, |
| 90 |
"Params": [ |
| 91 |
{"JobId": 11111, "Key": "operationName", "Value": "CONFIGURE_PORT_BREAK_OUT"}, |
| 92 |
{"JobId": 11111, "Key": "interfaceId", "Value": "2HB7NX2:phy-port1/1/11"}, |
| 93 |
{"JobId": 11111, "Key": "breakoutType", "Value": "1X40GE"}], |
| 94 |
"Schedule": "startnow", |
| 95 |
"StartTime": null, |
| 96 |
"State": "Enabled", |
| 97 |
"Targets": [ |
| 98 |
{"Data": "", "Id": 11112, "JobId": 34206, "TargetType": { "Id": 1000, "Name": "DEVICE"}} |
| 99 |
], |
| 100 |
"UpdatedBy": null, |
| 101 |
"UserGenerated": true, |
| 102 |
"Visible": true |
| 103 |
} |
| 104 |
error_info: |
| 105 |
description: Details of the HTTP Error. |
| 106 |
returned: on HTTP error |
| 107 |
type: dict |
| 108 |
sample: { |
| 109 |
"error": { |
| 110 |
"code": "Base.1.0.GeneralError", |
| 111 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 112 |
"@Message.ExtendedInfo": [ |
| 113 |
{ |
| 114 |
"MessageId": "GEN1234", |
| 115 |
"RelatedProperties": [], |
| 116 |
"Message": "Unable to process the request because an error occurred.", |
| 117 |
"MessageArgs": [], |
| 118 |
"Severity": "Critical", |
| 119 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 120 |
} |
| 121 |
] |
| 122 |
} |
| 123 |
} |
| 124 |
''' |
| 125 |
|
| 126 |
import json |
| 127 |
import re |
| 128 |
from ssl import SSLError |
| 129 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 130 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 131 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 132 |
|
| 133 |
DEVICE_URI = "DeviceService/Devices" |
| 134 |
PORT_INFO_URI = "DeviceService/Devices({0})/InventoryDetails('portInformation')" |
| 135 |
JOB_URI = "JobService/Jobs" |
| 136 |
|
| 137 |
|
| 138 |
def get_device_id(module, rest_obj): |
| 139 |
""" |
| 140 |
This function returns device id. |
| 141 |
:param module: ansible module arguments. |
| 142 |
:param rest_obj: rest object for making requests. |
| 143 |
:return: device id |
| 144 |
""" |
| 145 |
regex = "^[a-z0-9A-Z]+[:][a-z0-9A-Z/-]+$" |
| 146 |
target_port = module.params["target_port"] |
| 147 |
if re.search(regex, target_port) is None: |
| 148 |
module.fail_json(msg="Invalid target port {0}.".format(target_port)) |
| 149 |
service_tag = target_port.split(":") |
| 150 |
query = "DeviceServiceTag eq '{0}'".format(service_tag[0]) |
| 151 |
device_id, failed_msg = None, "Unable to retrieve the device information because" \ |
| 152 |
" the device with the entered service tag {0} is not present." |
| 153 |
response = rest_obj.invoke_request("GET", DEVICE_URI, query_param={"$filter": query}) |
| 154 |
if response.status_code == 200 and response.json_data.get("value"): |
| 155 |
device_info = response.json_data.get("value")[0] |
| 156 |
device_id = device_info["Id"] |
| 157 |
else: |
| 158 |
module.fail_json(msg=failed_msg.format(service_tag[0])) |
| 159 |
return device_id |
| 160 |
|
| 161 |
|
| 162 |
def get_port_information(module, rest_obj, device_id): |
| 163 |
""" |
| 164 |
This function returns the existing breakout configuration details. |
| 165 |
:param module: ansible module arguments. |
| 166 |
:param rest_obj: rest object for making requests. |
| 167 |
:param device_id: device id |
| 168 |
:return: str, {}, str |
| 169 |
""" |
| 170 |
response = rest_obj.invoke_request("GET", PORT_INFO_URI.format(device_id)) |
| 171 |
breakout_config, breakout_capability, target_port = None, None, module.params["target_port"] |
| 172 |
for each in response.json_data.get("InventoryInfo"): |
| 173 |
if not each["Configuration"] == "NoBreakout" and each["Id"] == target_port: |
| 174 |
breakout_capability = each["PortBreakoutCapabilities"] |
| 175 |
breakout_config = each["Configuration"] |
| 176 |
interface_id = each["Id"] |
| 177 |
break |
| 178 |
else: |
| 179 |
module.fail_json(msg="{0} does not support port breakout" |
| 180 |
" or invalid port number entered.".format(target_port)) |
| 181 |
return breakout_config, breakout_capability, interface_id |
| 182 |
|
| 183 |
|
| 184 |
def get_breakout_payload(device_id, breakout_type, interface_id): |
| 185 |
""" |
| 186 |
Payload for breakout configuration. |
| 187 |
:param device_id: device id |
| 188 |
:param breakout_type: requested breakout type |
| 189 |
:param interface_id: port number with service tag |
| 190 |
:return: json |
| 191 |
""" |
| 192 |
payload = { |
| 193 |
"Id": 0, "JobName": "Breakout Port", "JobDescription": "", |
| 194 |
"Schedule": "startnow", "State": "Enabled", |
| 195 |
"JobType": {"Id": 3, "Name": "DeviceAction_Task"}, |
| 196 |
"Params": [ |
| 197 |
{"Key": "breakoutType", "Value": breakout_type}, |
| 198 |
{"Key": "interfaceId", "Value": interface_id}, |
| 199 |
{"Key": "operationName", "Value": "CONFIGURE_PORT_BREAK_OUT"}], |
| 200 |
"Targets": [ |
| 201 |
{"JobId": 0, "Id": device_id, "Data": "", "TargetType": {"Id": 4000, "Name": "DEVICE"}} |
| 202 |
]} |
| 203 |
return payload |
| 204 |
|
| 205 |
|
| 206 |
def check_mode(module, changes=False): |
| 207 |
""" |
| 208 |
The check mode function to check whether the changes found or not. |
| 209 |
:param module: ansible module arguments |
| 210 |
:param changes: boolean to return the appropriate message. |
| 211 |
:return: None |
| 212 |
""" |
| 213 |
if module.check_mode: |
| 214 |
message = "Changes found to commit!" if changes else "No changes found to commit!" |
| 215 |
module.exit_json(msg=message, changed=changes) |
| 216 |
|
| 217 |
|
| 218 |
def set_breakout(module, rest_obj, breakout_config, breakout_capability, interface_id, device_id): |
| 219 |
""" |
| 220 |
Configuration the breakout feature for given option. |
| 221 |
:param module: ansible module arguments. |
| 222 |
:param rest_obj: rest object for making requests. |
| 223 |
:param breakout_config: Existing breakout configuration. |
| 224 |
:param breakout_capability: Available breakout configuration. |
| 225 |
:param interface_id: port number with service tag |
| 226 |
:param device_id: device id |
| 227 |
:return: rest object |
| 228 |
""" |
| 229 |
breakout_type, response = module.params["breakout_type"], {} |
| 230 |
payload = get_breakout_payload(device_id, breakout_type, interface_id) |
| 231 |
if breakout_config == "HardwareDefault" and not breakout_type == "HardwareDefault": |
| 232 |
for config in breakout_capability: |
| 233 |
if breakout_type == config["Type"]: |
| 234 |
check_mode(module, changes=True) |
| 235 |
response = rest_obj.invoke_request("POST", JOB_URI, data=payload) |
| 236 |
break |
| 237 |
else: |
| 238 |
supported_type = ", ".join(i["Type"] for i in breakout_capability) |
| 239 |
module.fail_json(msg="Invalid breakout type: {0}, supported values are {1}.".format(breakout_type, |
| 240 |
supported_type)) |
| 241 |
elif not breakout_config == "HardwareDefault" and breakout_type == "HardwareDefault": |
| 242 |
check_mode(module, changes=True) |
| 243 |
response = rest_obj.invoke_request("POST", JOB_URI, data=payload) |
| 244 |
elif breakout_config == breakout_type: |
| 245 |
check_mode(module, changes=False) |
| 246 |
module.exit_json(msg="The port is already configured with the selected breakout configuration.") |
| 247 |
else: |
| 248 |
module.fail_json(msg="Device does not support changing a port breakout" |
| 249 |
" configuration to different breakout type. Configure the port to" |
| 250 |
" HardwareDefault and retry the operation.") |
| 251 |
return response |
| 252 |
|
| 253 |
|
| 254 |
def main(): |
| 255 |
specs = { |
| 256 |
"target_port": {"required": True, "type": 'str'}, |
| 257 |
"breakout_type": {"required": True, "type": 'str'}, |
| 258 |
} |
| 259 |
|
| 260 |
module = OmeAnsibleModule( |
| 261 |
argument_spec=specs, |
| 262 |
supports_check_mode=True |
| 263 |
) |
| 264 |
try: |
| 265 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 266 |
device_id = get_device_id(module, rest_obj) |
| 267 |
breakout_config, breakout_capability, interface_id = get_port_information(module, rest_obj, device_id) |
| 268 |
breakout_status = set_breakout(module, rest_obj, breakout_config, |
| 269 |
breakout_capability, interface_id, device_id) |
| 270 |
if breakout_status: |
| 271 |
module.exit_json(msg="Port breakout configuration job submitted successfully.", |
| 272 |
breakout_status=breakout_status.json_data, changed=True) |
| 273 |
except HTTPError as err: |
| 274 |
module.fail_json(msg=str(err), error_info=json.load(err)) |
| 275 |
except URLError as err: |
| 276 |
module.exit_json(msg=str(err), unreachable=True) |
| 277 |
except (SSLValidationError, ConnectionError, TypeError, ValueError, IndexError, SSLError, OSError) as err: |
| 278 |
module.fail_json(msg=str(err)) |
| 279 |
|
| 280 |
|
| 281 |
if __name__ == '__main__': |
| 282 |
main() |