| 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_template_identity_pool |
| 19 |
short_description: Attach or detach an identity pool to a requested template on OpenManage Enterprise |
| 20 |
version_added: "2.0.0" |
| 21 |
description: This module allows to- |
| 22 |
- Attach an identity pool to a requested template on OpenManage Enterprise. |
| 23 |
- Detach an identity pool from a requested template on OpenManage Enterprise. |
| 24 |
extends_documentation_fragment: |
| 25 |
- dellemc.openmanage.ome_auth_options |
| 26 |
options: |
| 27 |
template_name: |
| 28 |
description: Name of the template to which an identity pool is attached or detached. |
| 29 |
type: str |
| 30 |
required: true |
| 31 |
identity_pool_name: |
| 32 |
description: Name of the identity pool. |
| 33 |
- To attach an identity pool to a template, provide the name of the identity pool. |
| 34 |
- This option is not applicable when detaching an identity pool from a template. |
| 35 |
type: str |
| 36 |
requirements: |
| 37 |
- "python >= 3.9.6" |
| 38 |
author: |
| 39 |
- "Felix Stephen (@felixs88)" |
| 40 |
- "Meenakshi Dembi(@meenakshidembi691)" |
| 41 |
notes: |
| 42 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 43 |
- This module supports C(check_mode). |
| 44 |
''' |
| 45 |
|
| 46 |
EXAMPLES = r''' |
| 47 |
--- |
| 48 |
- name: Attach an identity pool to a template |
| 49 |
dellemc.openmanage.ome_template_identity_pool: |
| 50 |
hostname: "192.168.0.1" |
| 51 |
username: "username" |
| 52 |
password: "password" |
| 53 |
ca_path: "/path/to/ca_cert.pem" |
| 54 |
template_name: template_name |
| 55 |
identity_pool_name: identity_pool_name |
| 56 |
|
| 57 |
- name: Detach an identity pool from a template |
| 58 |
dellemc.openmanage.ome_template_identity_pool: |
| 59 |
hostname: "192.168.0.1" |
| 60 |
username: "username" |
| 61 |
password: "password" |
| 62 |
ca_path: "/path/to/ca_cert.pem" |
| 63 |
template_name: template_name |
| 64 |
''' |
| 65 |
|
| 66 |
RETURN = r''' |
| 67 |
--- |
| 68 |
msg: |
| 69 |
type: str |
| 70 |
description: Overall identity pool status of the attach or detach operation. |
| 71 |
returned: always |
| 72 |
sample: Successfully attached identity pool to template. |
| 73 |
error_info: |
| 74 |
description: Details of the HTTP Error. |
| 75 |
returned: on HTTP error |
| 76 |
type: dict |
| 77 |
sample: { |
| 78 |
"error": { |
| 79 |
"code": "Base.1.0.GeneralError", |
| 80 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 81 |
"@Message.ExtendedInfo": [ |
| 82 |
{ |
| 83 |
"MessageId": "GEN1234", |
| 84 |
"RelatedProperties": [], |
| 85 |
"Message": "Unable to process the request because an error occurred.", |
| 86 |
"MessageArgs": [], |
| 87 |
"Severity": "Critical", |
| 88 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 89 |
} |
| 90 |
] |
| 91 |
} |
| 92 |
} |
| 93 |
''' |
| 94 |
|
| 95 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 96 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 97 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 98 |
from ssl import SSLError |
| 99 |
|
| 100 |
CONFIG_URI = "TemplateService/Actions/TemplateService.UpdateNetworkConfig" |
| 101 |
TEMPLATE_URI = "TemplateService/Templates" |
| 102 |
IDENTITY_URI = "IdentityPoolService/IdentityPools" |
| 103 |
TEMPLATE_ATTRIBUTE_VIEW = "TemplateService/Templates({template_id})/Views(4)/AttributeViewDetails" |
| 104 |
KEY_ATTR_NAME = 'DisplayName' |
| 105 |
CHANGES_FOUND = "Changes found to be applied." |
| 106 |
NO_CHANGES_FOUND = "No changes found to be applied." |
| 107 |
|
| 108 |
|
| 109 |
def get_template_vlan_info(rest_obj, template_id): |
| 110 |
nic_bonding_tech = "" |
| 111 |
try: |
| 112 |
resp = rest_obj.invoke_request('GET', TEMPLATE_ATTRIBUTE_VIEW.format(template_id=template_id)) |
| 113 |
if resp.success: |
| 114 |
nic_model = resp.json_data.get('AttributeGroups', []) |
| 115 |
for xnic in nic_model: |
| 116 |
if xnic.get(KEY_ATTR_NAME) == "NicBondingTechnology": |
| 117 |
nic_bonding_list = xnic.get("Attributes", []) |
| 118 |
for xbnd in nic_bonding_list: |
| 119 |
if xbnd.get(KEY_ATTR_NAME).lower() == "nic bonding technology": |
| 120 |
nic_bonding_tech = xbnd.get('Value') |
| 121 |
except Exception: |
| 122 |
nic_bonding_tech = "" |
| 123 |
return nic_bonding_tech |
| 124 |
|
| 125 |
|
| 126 |
def get_template_id(rest_obj, module): |
| 127 |
"""Get template id based on requested template name.""" |
| 128 |
template_name = module.params["template_name"] |
| 129 |
query_param = {"$filter": "Name eq '{0}'".format(template_name)} |
| 130 |
template_req = rest_obj.invoke_request("GET", TEMPLATE_URI, query_param=query_param) |
| 131 |
for each in template_req.json_data.get('value'): |
| 132 |
if each['Name'] == template_name: |
| 133 |
template = each |
| 134 |
break |
| 135 |
else: |
| 136 |
module.exit_json(msg="Unable to complete the operation because the requested template" |
| 137 |
" with name '{0}' is not present.".format(template_name), failed=True) |
| 138 |
return template |
| 139 |
|
| 140 |
|
| 141 |
def get_identity_id(rest_obj, module): |
| 142 |
"""Get identity pool id based on requested identity pool name.""" |
| 143 |
identity_name = module.params["identity_pool_name"] |
| 144 |
resp = rest_obj.get_all_report_details(IDENTITY_URI) |
| 145 |
for each in resp["report_list"]: |
| 146 |
if each['Name'] == identity_name: |
| 147 |
identity_id = each['Id'] |
| 148 |
break |
| 149 |
else: |
| 150 |
module.exit_json(msg="Unable to complete the operation because the requested identity" |
| 151 |
" pool with name '{0}' is not present.".format(identity_name), failed=True) |
| 152 |
return identity_id |
| 153 |
|
| 154 |
|
| 155 |
def main(): |
| 156 |
specs = { |
| 157 |
"template_name": {"required": True, "type": "str"}, |
| 158 |
"identity_pool_name": {"required": False, "type": "str"}, |
| 159 |
} |
| 160 |
|
| 161 |
module = OmeAnsibleModule( |
| 162 |
argument_spec=specs, |
| 163 |
supports_check_mode=True |
| 164 |
) |
| 165 |
try: |
| 166 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 167 |
template = get_template_id(rest_obj, module) |
| 168 |
template_id = template["Id"] |
| 169 |
identity_id, message = 0, "Successfully detached identity pool from template." |
| 170 |
if module.params["identity_pool_name"] is not None: |
| 171 |
identity_id = get_identity_id(rest_obj, module) |
| 172 |
message = "Successfully attached identity pool to template." |
| 173 |
nic_bonding_tech = get_template_vlan_info(rest_obj, template_id) |
| 174 |
payload = {"TemplateId": template_id, "IdentityPoolId": identity_id, "BondingTechnology": nic_bonding_tech} |
| 175 |
if template["IdentityPoolId"] == identity_id: |
| 176 |
module.exit_json(changed=True, msg=NO_CHANGES_FOUND) |
| 177 |
if module.check_mode: |
| 178 |
module.exit_json(changed=True, msg=CHANGES_FOUND) |
| 179 |
resp = rest_obj.invoke_request("POST", CONFIG_URI, data=payload) |
| 180 |
if resp.status_code == 200: |
| 181 |
module.exit_json(msg=message, changed=True) |
| 182 |
except HTTPError as err: |
| 183 |
module.exit_json(msg=str(err), failed=True) |
| 184 |
except URLError as err: |
| 185 |
module.exit_json(msg=str(err), unreachable=True) |
| 186 |
except (ValueError, TypeError, ConnectionError, SSLError, SSLValidationError, OSError) as err: |
| 187 |
module.exit_json(msg=str(err), failed=True) |
| 188 |
except Exception as err: |
| 189 |
module.exit_json(msg=str(err), failed=True) |
| 190 |
|
| 191 |
|
| 192 |
if __name__ == "__main__": |
| 193 |
main() |