| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from __future__ import (absolute_import, division, print_function) |
| 11 |
|
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
DOCUMENTATION = """ |
| 15 |
--- |
| 16 |
module: ome_template_network_vlan_info |
| 17 |
short_description: Retrieves network configuration of template. |
| 18 |
version_added: "7.2.0" |
| 19 |
description: |
| 20 |
- "This module retrieves the network configuration of a template on |
| 21 |
OpenManage Enterprise or OpenManage Enterprise Modular." |
| 22 |
extends_documentation_fragment: |
| 23 |
- dellemc.openmanage.ome_auth_options |
| 24 |
options: |
| 25 |
template_id: |
| 26 |
description: |
| 27 |
- Id of the template. |
| 28 |
- This is mutually exclusive with I(template_name). |
| 29 |
type: int |
| 30 |
template_name: |
| 31 |
description: |
| 32 |
- Name of the template. |
| 33 |
- This is mutually exclusive with I(template_id). |
| 34 |
- "C(Note) If I(template_id) or I(template_name) option is not provided, |
| 35 |
the module retrieves network VLAN info of all templates." |
| 36 |
type: str |
| 37 |
requirements: |
| 38 |
- "python >= 3.9.6" |
| 39 |
author: |
| 40 |
- Jagadeesh N V(@jagadeeshnv) |
| 41 |
- Bhavneet Sharma (@Bhavneet-Sharma) |
| 42 |
notes: |
| 43 |
- Run this module on a system that has direct access to Dell OpenManage |
| 44 |
Enterprise. |
| 45 |
- This module supports C(check_mode). |
| 46 |
""" |
| 47 |
|
| 48 |
EXAMPLES = """ |
| 49 |
--- |
| 50 |
- name: Retrieve network details of all templates. |
| 51 |
dellemc.openmanage.ome_template_network_vlan_info: |
| 52 |
hostname: "192.168.0.1" |
| 53 |
username: "username" |
| 54 |
password: "password" |
| 55 |
ca_path: "/path/to/ca_cert.pem" |
| 56 |
|
| 57 |
- name: Retrieve network details using template ID |
| 58 |
dellemc.openmanage.ome_template_network_vlan_info: |
| 59 |
hostname: "192.168.0.1" |
| 60 |
username: "username" |
| 61 |
password: "password" |
| 62 |
ca_path: "/path/to/ca_cert.pem" |
| 63 |
template_id: 1234 |
| 64 |
|
| 65 |
- name: Retrieve network details using template name |
| 66 |
dellemc.openmanage.ome_template_network_vlan_info: |
| 67 |
hostname: "192.168.0.1" |
| 68 |
username: "username" |
| 69 |
password: "password" |
| 70 |
ca_path: "/path/to/ca_cert.pem" |
| 71 |
template_name: template1 |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r''' |
| 75 |
--- |
| 76 |
msg: |
| 77 |
description: Status of template VLAN information retrieval. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: "Successfully retrieved the template network VLAN information." |
| 81 |
vlan_info: |
| 82 |
description: Information about the template network VLAN. |
| 83 |
returned: success |
| 84 |
type: list |
| 85 |
elements: dict |
| 86 |
sample: [{ |
| 87 |
"TemplateId": 58, |
| 88 |
"TemplateName": "t2", |
| 89 |
"NicBondingTechnology" : "LACP", |
| 90 |
"NicModel": { |
| 91 |
"NIC in Mezzanine 1B" : { |
| 92 |
'1' : {"Port" : 1, |
| 93 |
"Vlan Tagged" : ["25367", "32656", "32658", "26898"], |
| 94 |
"Vlan UnTagged" : "21474", |
| 95 |
"NICBondingEnabled" : "false"}, |
| 96 |
'2' : {"Port" : 2, |
| 97 |
"Vlan Tagged" : [], |
| 98 |
"Vlan UnTagged" : "32658", |
| 99 |
"NIC Bonding Enabled" : "true"} |
| 100 |
}, |
| 101 |
"NIC in Mezzanine 1A" : { |
| 102 |
'1' : {"Port" : 1, |
| 103 |
"Vlan Tagged" : ["32656", "32658"], |
| 104 |
"Vlan UnTagged" : "25367", |
| 105 |
"NIC Bonding Enabled" : "true"}, |
| 106 |
'2' : {"Port" : 2, |
| 107 |
"Vlan Tagged" : ["21474"], |
| 108 |
"Vlan UnTagged" : "32656", |
| 109 |
"NIC Bonding Enabled" : "false"} |
| 110 |
} |
| 111 |
}}] |
| 112 |
error_info: |
| 113 |
description: Details of the HTTP Error. |
| 114 |
returned: on HTTP error |
| 115 |
type: dict |
| 116 |
sample: { |
| 117 |
"error": { |
| 118 |
"code": "Base.1.0.GeneralError", |
| 119 |
"message": "A general error has occurred. See ExtendedInfo for more |
| 120 |
information.", |
| 121 |
"@Message.ExtendedInfo": [ |
| 122 |
{ |
| 123 |
"MessageId": "GEN1234", |
| 124 |
"RelatedProperties": [], |
| 125 |
"Message": "Unable to process the request because an error occurred.", |
| 126 |
"MessageArgs": [], |
| 127 |
"Severity": "Critical", |
| 128 |
"Resolution": "Retry the operation. If the issue persists, contact |
| 129 |
your system administrator." |
| 130 |
} |
| 131 |
] |
| 132 |
} |
| 133 |
} |
| 134 |
''' |
| 135 |
|
| 136 |
import json |
| 137 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 138 |
from ansible.module_utils.urls import ConnectionError |
| 139 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import ( |
| 140 |
RestOME, OmeAnsibleModule |
| 141 |
) |
| 142 |
|
| 143 |
NETWORK_HIERARCHY_VIEW = 4 |
| 144 |
TEMPLATE_ATTRIBUTE_VIEW = "TemplateService/Templates({0})/Views({1})/AttributeViewDetails" |
| 145 |
TEMPLATE_VIEW = "TemplateService/Templates" |
| 146 |
KEY_ATTR_NAME = 'DisplayName' |
| 147 |
SUB_GRP_ATTR_NAME = 'SubAttributeGroups' |
| 148 |
GRP_ATTR_NAME = 'Attributes' |
| 149 |
GRP_NAME_ID_ATTR_NAME = 'GroupNameId' |
| 150 |
CUSTOM_ID_ATTR_NAME = 'CustomId' |
| 151 |
SUCCESS_MSG = "Successfully retrieved the template network VLAN information." |
| 152 |
NO_TEMPLATES_MSG = "No templates with network info were found." |
| 153 |
|
| 154 |
|
| 155 |
def get_template_details(module, rest_obj): |
| 156 |
templt_id = module.params.get('template_id') |
| 157 |
query_param = {"$filter": "Id eq {0}".format(templt_id)} |
| 158 |
srch = 'Id' |
| 159 |
if not templt_id: |
| 160 |
templt_id = module.params.get('template_name') |
| 161 |
query_param = {"$filter": "Name eq '{0}'".format(templt_id)} |
| 162 |
srch = 'Name' |
| 163 |
resp = rest_obj.invoke_request('GET', TEMPLATE_VIEW, query_param=query_param) |
| 164 |
if resp.success and resp.json_data.get('value'): |
| 165 |
tlist = resp.json_data.get('value', []) |
| 166 |
for xtype in tlist: |
| 167 |
if xtype.get(srch) == templt_id: |
| 168 |
return xtype |
| 169 |
module.exit_json( |
| 170 |
failed=True, |
| 171 |
msg=f"Template with {srch.lower()} '{templt_id}' not found.") |
| 172 |
|
| 173 |
|
| 174 |
def get_vlan_info(attribute): |
| 175 |
vlan_key = attribute.get(KEY_ATTR_NAME).lower() |
| 176 |
vlan_value = attribute.get("Value") |
| 177 |
if attribute.get(CUSTOM_ID_ATTR_NAME) == 0: |
| 178 |
return None |
| 179 |
|
| 180 |
if vlan_key == "vlan untagged": |
| 181 |
return vlan_key, int(vlan_value) |
| 182 |
elif vlan_key == "vlan tagged": |
| 183 |
return vlan_key, list(map(int, (vlan_value).replace(" ", "").split(","))) |
| 184 |
|
| 185 |
elif vlan_key == "nic bonding enabled": |
| 186 |
return vlan_key, vlan_value |
| 187 |
return None |
| 188 |
|
| 189 |
|
| 190 |
def get_ports_info(port_list): |
| 191 |
port_data = {} |
| 192 |
for port in port_list: |
| 193 |
port_number = port.get(GRP_NAME_ID_ATTR_NAME) |
| 194 |
port_dict = {"Port": port_number} |
| 195 |
for partition in port.get(SUB_GRP_ATTR_NAME): |
| 196 |
for attribute in partition.get(GRP_ATTR_NAME): |
| 197 |
vlan_info = get_vlan_info(attribute) |
| 198 |
if vlan_info: |
| 199 |
key, value = vlan_info |
| 200 |
port_dict[key] = value |
| 201 |
port_data[port_number] = port_dict |
| 202 |
return port_data |
| 203 |
|
| 204 |
|
| 205 |
def get_nic_info(nic_model): |
| 206 |
nic_result = {} |
| 207 |
for xnic in nic_model: |
| 208 |
attribute_key = xnic.get(KEY_ATTR_NAME) |
| 209 |
if attribute_key == "NICModel": |
| 210 |
nic_group = xnic.get('SubAttributeGroups', []) |
| 211 |
nic_group_dict = {} |
| 212 |
for nic in nic_group: |
| 213 |
nic_group_dict[nic.get(KEY_ATTR_NAME)] = get_ports_info( |
| 214 |
nic.get(SUB_GRP_ATTR_NAME, [])) |
| 215 |
nic_result[attribute_key] = nic_group_dict |
| 216 |
if attribute_key == "NicBondingTechnology": |
| 217 |
nic_bonding_list = xnic.get("Attributes", []) |
| 218 |
for xbnd in nic_bonding_list: |
| 219 |
if xbnd.get(KEY_ATTR_NAME).lower() == "nic bonding technology": |
| 220 |
nic_result[attribute_key] = xbnd.get('Value') |
| 221 |
return nic_result |
| 222 |
|
| 223 |
|
| 224 |
def get_template_vlan_info(rest_obj, template_id): |
| 225 |
result = {} |
| 226 |
try: |
| 227 |
resp = rest_obj.invoke_request('GET', TEMPLATE_ATTRIBUTE_VIEW.format( |
| 228 |
template_id, NETWORK_HIERARCHY_VIEW)) |
| 229 |
if resp.json_data.get('AttributeGroups', []): |
| 230 |
nic_model = resp.json_data.get('AttributeGroups', []) |
| 231 |
if nic_model: |
| 232 |
result = get_nic_info(nic_model) |
| 233 |
except Exception: |
| 234 |
result = {} |
| 235 |
return result |
| 236 |
|
| 237 |
|
| 238 |
def main(): |
| 239 |
argument_spec = { |
| 240 |
"template_id": {"type": 'int'}, |
| 241 |
"template_name": {"type": 'str'} |
| 242 |
} |
| 243 |
module = OmeAnsibleModule( |
| 244 |
argument_spec=argument_spec, |
| 245 |
mutually_exclusive=[('template_id', 'template_name')], |
| 246 |
supports_check_mode=True) |
| 247 |
try: |
| 248 |
templates = [] |
| 249 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 250 |
if module.params.get("template_id") or module.params.get("template_name"): |
| 251 |
tmplt = get_template_details(module, rest_obj) |
| 252 |
templates.append(tmplt) |
| 253 |
else: |
| 254 |
resp = rest_obj.get_all_items_with_pagination(TEMPLATE_VIEW) |
| 255 |
templates = resp.get("value") |
| 256 |
vlan_info = [] |
| 257 |
for xtmp in templates: |
| 258 |
if xtmp.get("ViewTypeId") != 4: |
| 259 |
result = get_template_vlan_info(rest_obj, xtmp['Id']) |
| 260 |
result["TemplateId"] = xtmp['Id'] |
| 261 |
result["TemplateName"] = xtmp['Name'] |
| 262 |
vlan_info.append(result) |
| 263 |
|
| 264 |
module.exit_json(msg=SUCCESS_MSG, vlan_info=vlan_info) |
| 265 |
|
| 266 |
except HTTPError as err: |
| 267 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 268 |
except URLError as err: |
| 269 |
module.exit_json(msg=str(err), unreachable=True) |
| 270 |
except (IOError, ValueError, TypeError, ConnectionError, |
| 271 |
AttributeError, IndexError, KeyError, OSError) as err: |
| 272 |
module.exit_json(msg=str(err), failed=True) |
| 273 |
|
| 274 |
|
| 275 |
if __name__ == '__main__': |
| 276 |
main() |