| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from __future__ import (absolute_import, division, print_function) |
| 14 |
__metaclass__ = type |
| 15 |
|
| 16 |
DOCUMENTATION = r''' |
| 17 |
--- |
| 18 |
module: ome_network_vlan_info |
| 19 |
short_description: Retrieves the information about networks VLAN(s) present in OpenManage Enterprise |
| 20 |
version_added: "2.1.0" |
| 21 |
description: |
| 22 |
This module allows to retrieve the following. |
| 23 |
- A list of all the network VLANs with their detailed information. |
| 24 |
- Information about a specific network VLAN using VLAN I(id) or VLAN I(name). |
| 25 |
extends_documentation_fragment: |
| 26 |
- dellemc.openmanage.ome_auth_options |
| 27 |
options: |
| 28 |
id: |
| 29 |
description: |
| 30 |
- A unique identifier of the network VLAN available in the device. |
| 31 |
- I(id) and I(name) are mutually exclusive. |
| 32 |
type: int |
| 33 |
name: |
| 34 |
description: |
| 35 |
- A unique name of the network VLAN available in the device. |
| 36 |
- I(name) and I(id) are mutually exclusive. |
| 37 |
type: str |
| 38 |
|
| 39 |
requirements: |
| 40 |
- "python >= 3.9.6" |
| 41 |
author: |
| 42 |
- "Deepak Joshi(@deepakjoshishri)" |
| 43 |
- "Meenakshi Dembi(@meenakshidembi691)" |
| 44 |
notes: |
| 45 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 46 |
- This module supports C(check_mode). |
| 47 |
''' |
| 48 |
|
| 49 |
EXAMPLES = """ |
| 50 |
--- |
| 51 |
- name: Retrieve information about all network VLANs(s) available in the device |
| 52 |
dellemc.openmanage.ome_network_vlan_info: |
| 53 |
hostname: "192.168.0.1" |
| 54 |
username: "username" |
| 55 |
password: "password" |
| 56 |
ca_path: "/path/to/ca_cert.pem" |
| 57 |
|
| 58 |
- name: Retrieve information about a network VLAN using the VLAN ID |
| 59 |
dellemc.openmanage.ome_network_vlan_info: |
| 60 |
hostname: "192.168.0.1" |
| 61 |
username: "username" |
| 62 |
password: "password" |
| 63 |
ca_path: "/path/to/ca_cert.pem" |
| 64 |
id: 12345 |
| 65 |
|
| 66 |
- name: Retrieve information about a network VLAN using the VLAN name |
| 67 |
dellemc.openmanage.ome_network_vlan_info: |
| 68 |
hostname: "192.168.0.1" |
| 69 |
username: "username" |
| 70 |
password: "password" |
| 71 |
ca_path: "/path/to/ca_cert.pem" |
| 72 |
name: "Network VLAN - 1" |
| 73 |
""" |
| 74 |
|
| 75 |
RETURN = ''' |
| 76 |
--- |
| 77 |
msg: |
| 78 |
type: str |
| 79 |
description: Detailed information of the network VLAN(s). |
| 80 |
returned: success |
| 81 |
sample: { |
| 82 |
"msg": "Successfully retrieved the network VLAN information.", |
| 83 |
"network_vlan_info": [ |
| 84 |
{ |
| 85 |
"CreatedBy": "admin", |
| 86 |
"CreationTime": "2020-09-02 18:48:42.129", |
| 87 |
"Description": "Description of Logical Network - 1", |
| 88 |
"Id": 20057, |
| 89 |
"InternalRefNWUUId": "42b9903d-93f8-4184-adcf-0772e4492f71", |
| 90 |
"Name": "Network VLAN - 1", |
| 91 |
"Type": { |
| 92 |
"Description": "This is the network for general purpose traffic. QOS Priority : Bronze.", |
| 93 |
"Id": 1, |
| 94 |
"Name": "General Purpose (Bronze)", |
| 95 |
"NetworkTrafficType": "Ethernet", |
| 96 |
"QosType": { |
| 97 |
"Id": 4, |
| 98 |
"Name": "Bronze" |
| 99 |
}, |
| 100 |
"VendorCode": "GeneralPurpose" |
| 101 |
}, |
| 102 |
"UpdatedBy": null, |
| 103 |
"UpdatedTime": "2020-09-02 18:48:42.129", |
| 104 |
"VlanMaximum": 111, |
| 105 |
"VlanMinimum": 111 |
| 106 |
}, |
| 107 |
{ |
| 108 |
"CreatedBy": "admin", |
| 109 |
"CreationTime": "2020-09-02 18:49:11.507", |
| 110 |
"Description": "Description of Logical Network - 2", |
| 111 |
"Id": 20058, |
| 112 |
"InternalRefNWUUId": "e46ccb3f-ef57-4617-ac76-46c56594005c", |
| 113 |
"Name": "Network VLAN - 2", |
| 114 |
"Type": { |
| 115 |
"Description": "This is the network for general purpose traffic. QOS Priority : Silver.", |
| 116 |
"Id": 2, |
| 117 |
"Name": "General Purpose (Silver)", |
| 118 |
"NetworkTrafficType": "Ethernet", |
| 119 |
"QosType": { |
| 120 |
"Id": 3, |
| 121 |
"Name": "Silver" |
| 122 |
}, |
| 123 |
"VendorCode": "GeneralPurpose" |
| 124 |
}, |
| 125 |
"UpdatedBy": null, |
| 126 |
"UpdatedTime": "2020-09-02 18:49:11.507", |
| 127 |
"VlanMaximum": 112, |
| 128 |
"VlanMinimum": 112 |
| 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 |
import json |
| 155 |
from ssl import SSLError |
| 156 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 157 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 158 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 159 |
|
| 160 |
|
| 161 |
NETWORK_VLAN_BASE_URI = "NetworkConfigurationService/Networks" |
| 162 |
NETWORK_TYPE_BASE_URI = "NetworkConfigurationService/NetworkTypes" |
| 163 |
QOS_TYPE_BASE_URI = "NetworkConfigurationService/QosTypes" |
| 164 |
|
| 165 |
|
| 166 |
MODULE_SUCCESS_MESSAGE = "Successfully retrieved the network VLAN information." |
| 167 |
|
| 168 |
|
| 169 |
MODULE_FAILURE_MESSAGE = "Failed to retrieve the network VLAN information." |
| 170 |
NETWORK_VLAN_NAME_NOT_FOUND = "Provided network VLAN with name - '{0}' does not exist." |
| 171 |
|
| 172 |
SAFE_MAX_LIMIT = 9999 |
| 173 |
|
| 174 |
|
| 175 |
def clean_data(data): |
| 176 |
""" |
| 177 |
data: A dictionary. |
| 178 |
return: A data dictionary after removing items that are not required for end user. |
| 179 |
""" |
| 180 |
for k in ['@odata.id', '@odata.type', '@odata.context', '@odata.count']: |
| 181 |
data.pop(k, None) |
| 182 |
return data |
| 183 |
|
| 184 |
|
| 185 |
def get_type_information(rest_obj, uri): |
| 186 |
""" |
| 187 |
rest_obj: Object containing information about connection to device. |
| 188 |
return: dict with information retrieved from URI. |
| 189 |
""" |
| 190 |
type_info_dict = {} |
| 191 |
resp = rest_obj.invoke_request('GET', uri) |
| 192 |
if resp.status_code == 200: |
| 193 |
type_info = resp.json_data.get('value') if isinstance(resp.json_data.get('value'), list) \ |
| 194 |
else [resp.json_data] |
| 195 |
for item in type_info: |
| 196 |
item = clean_data(item) |
| 197 |
type_info_dict[item['Id']] = item |
| 198 |
return type_info_dict |
| 199 |
|
| 200 |
|
| 201 |
def get_network_type_and_qos_type_information(rest_obj): |
| 202 |
""" |
| 203 |
rest_obj: Object containing information about connection to device. |
| 204 |
return: Dictionary with information for "Type" and "QosType" keys. |
| 205 |
""" |
| 206 |
|
| 207 |
network_type_dict = get_type_information(rest_obj, NETWORK_TYPE_BASE_URI) |
| 208 |
qos_type_dict = get_type_information(rest_obj, QOS_TYPE_BASE_URI) |
| 209 |
|
| 210 |
for key, item in network_type_dict.items(): |
| 211 |
item['QosType'] = qos_type_dict[item['QosType']] |
| 212 |
return network_type_dict |
| 213 |
|
| 214 |
|
| 215 |
def main(): |
| 216 |
specs = { |
| 217 |
"id": {"required": False, "type": 'int'}, |
| 218 |
"name": {"required": False, "type": 'str'} |
| 219 |
} |
| 220 |
|
| 221 |
module = OmeAnsibleModule( |
| 222 |
argument_spec=specs, |
| 223 |
mutually_exclusive=[["id", "name"]], |
| 224 |
supports_check_mode=True) |
| 225 |
try: |
| 226 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 227 |
|
| 228 |
network_vlan_uri = "{0}({1})".format(NETWORK_VLAN_BASE_URI, module.params.get("id")) if module.params.get( |
| 229 |
"id") else "{0}?$top={1}".format(NETWORK_VLAN_BASE_URI, SAFE_MAX_LIMIT) |
| 230 |
resp = rest_obj.invoke_request('GET', network_vlan_uri) |
| 231 |
if resp.status_code == 200: |
| 232 |
network_vlan_info = resp.json_data.get('value') if isinstance(resp.json_data.get('value'), list) else [ |
| 233 |
resp.json_data] |
| 234 |
if module.params.get("name"): |
| 235 |
network_vlan_name = module.params.get("name") |
| 236 |
network_vlan = [] |
| 237 |
for item in network_vlan_info: |
| 238 |
if item["Name"] == network_vlan_name.strip(): |
| 239 |
network_vlan = [item] |
| 240 |
break |
| 241 |
if not network_vlan: |
| 242 |
module.exit_json(msg=NETWORK_VLAN_NAME_NOT_FOUND.format(network_vlan_name), failed=True) |
| 243 |
network_vlan_info = network_vlan |
| 244 |
|
| 245 |
network_type_dict = get_network_type_and_qos_type_information(rest_obj) |
| 246 |
|
| 247 |
for network_vlan in network_vlan_info: |
| 248 |
network_vlan = clean_data(network_vlan) |
| 249 |
network_vlan['Type'] = network_type_dict[network_vlan['Type']] |
| 250 |
module.exit_json(msg=MODULE_SUCCESS_MESSAGE, network_vlan_info=network_vlan_info, changed=False) |
| 251 |
else: |
| 252 |
module.exit_json(msg=MODULE_FAILURE_MESSAGE, failed=True) |
| 253 |
except HTTPError as err: |
| 254 |
if err.getcode() == 404: |
| 255 |
module.exit_json(msg=str(err), failed=True) |
| 256 |
module.exit_json(msg=str(MODULE_FAILURE_MESSAGE), error_info=json.load(err), failed=True) |
| 257 |
except URLError as err: |
| 258 |
module.exit_json(msg=str(err), unreachable=True) |
| 259 |
except (IOError, ValueError, SSLError, TypeError, KeyError, ConnectionError, SSLValidationError, OSError) as err: |
| 260 |
module.exit_json(msg=str(err), failed=True) |
| 261 |
|
| 262 |
|
| 263 |
if __name__ == '__main__': |
| 264 |
main() |