| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from __future__ import (absolute_import, division, print_function) |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: ome_identity_pool |
| 16 |
short_description: Manages identity pool settings on OpenManage Enterprise |
| 17 |
version_added: "2.1.0" |
| 18 |
description: This module allows to create, modify, or delete a single identity pool on OpenManage Enterprise. |
| 19 |
extends_documentation_fragment: |
| 20 |
- dellemc.openmanage.ome_auth_options |
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- C(present) modifies an existing identity pool. If the provided I (pool_name) does not exist, |
| 25 |
it creates an identity pool. |
| 26 |
- C(absent) deletes an existing identity pool. |
| 27 |
type: str |
| 28 |
default: present |
| 29 |
choices: [present, absent] |
| 30 |
pool_name: |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
description: |
| 34 |
- This option is mandatory for I(state) when creating, modifying and deleting an identity pool. |
| 35 |
new_pool_name: |
| 36 |
type: str |
| 37 |
description: |
| 38 |
- After creating an identity pool, I(pool_name) can be changed to I(new_pool_name). |
| 39 |
- This option is ignored when creating an identity pool. |
| 40 |
pool_description: |
| 41 |
type: str |
| 42 |
description: |
| 43 |
- Description of the identity pool. |
| 44 |
ethernet_settings: |
| 45 |
type: dict |
| 46 |
description: |
| 47 |
- Applicable for creating and modifying an identity pool using Ethernet settings. |
| 48 |
- I(starting_mac_address) and I(identity_count) are required to create an identity pool. |
| 49 |
suboptions: |
| 50 |
starting_mac_address: |
| 51 |
description: Starting MAC address of the ethernet setting. |
| 52 |
type: str |
| 53 |
identity_count: |
| 54 |
description: Number of MAC addresses. |
| 55 |
type: int |
| 56 |
fcoe_settings: |
| 57 |
type: dict |
| 58 |
description: |
| 59 |
- Applicable for creating and modifying an identity pool using FCoE settings. |
| 60 |
- I(starting_mac_address) and I(identity_count) are required to create an identity pool. |
| 61 |
suboptions: |
| 62 |
starting_mac_address: |
| 63 |
description: Starting MAC Address of the FCoE setting. |
| 64 |
type: str |
| 65 |
identity_count: |
| 66 |
description: Number of MAC addresses. |
| 67 |
type: int |
| 68 |
iscsi_settings: |
| 69 |
type: dict |
| 70 |
description: |
| 71 |
- Applicable for creating and modifying an identity pool using ISCSI settings. |
| 72 |
- I(starting_mac_address), I(identity_count), I(iqn_prefix), I(ip_range) and I(subnet_mask) are |
| 73 |
required to create an identity pool. |
| 74 |
suboptions: |
| 75 |
starting_mac_address: |
| 76 |
description: Starting MAC address of the iSCSI setting.This is required option for iSCSI setting. |
| 77 |
type: str |
| 78 |
identity_count: |
| 79 |
description: Number of MAC addresses. |
| 80 |
type: int |
| 81 |
initiator_config: |
| 82 |
type: dict |
| 83 |
description: |
| 84 |
- Applicable for creating and modifying an identity pool using iSCSI Initiator settings. |
| 85 |
suboptions: |
| 86 |
iqn_prefix: |
| 87 |
description: IQN prefix addresses. |
| 88 |
type: str |
| 89 |
initiator_ip_pool_settings: |
| 90 |
type: dict |
| 91 |
description: |
| 92 |
- Applicable for creating and modifying an identity pool using ISCSI Initiator IP pool settings. |
| 93 |
suboptions: |
| 94 |
ip_range: |
| 95 |
description: Range of non-multicast IP addresses. |
| 96 |
type: str |
| 97 |
subnet_mask: |
| 98 |
description: Subnet mask for I(ip_range). |
| 99 |
type: str |
| 100 |
gateway: |
| 101 |
description: IP address of gateway. |
| 102 |
type: str |
| 103 |
primary_dns_server: |
| 104 |
description: IP address of the primary DNS server. |
| 105 |
type: str |
| 106 |
secondary_dns_server: |
| 107 |
description: IP address of the secondary DNS server. |
| 108 |
type: str |
| 109 |
fc_settings: |
| 110 |
type: dict |
| 111 |
description: |
| 112 |
- Applicable for creating and modifying an identity pool using fibre channel(FC) settings. |
| 113 |
- This option allows OpenManage Enterprise to generate a Worldwide port name (WWPN) and Worldwide node name (WWNN) address. |
| 114 |
- The value 0x2001 is beginning to the starting address for the generation of a WWPN, and 0x2000 for a WWNN. |
| 115 |
- I(starting_address) and I(identity_count) are required to create an identity pool. |
| 116 |
suboptions: |
| 117 |
starting_address: |
| 118 |
description: Starting MAC Address of FC setting.I(starting_address) is required to option to create FC settings. |
| 119 |
type: str |
| 120 |
identity_count: |
| 121 |
description: Number of MAC addresses.I(identity_count) is required to option to create FC settings. |
| 122 |
type: int |
| 123 |
requirements: |
| 124 |
- "python >= 3.9.6" |
| 125 |
author: |
| 126 |
- "Sajna Shetty(@Sajna-Shetty)" |
| 127 |
- "Deepak Joshi(@Dell-Deepak-Joshi))" |
| 128 |
notes: |
| 129 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 130 |
- This module supports C(check_mode). |
| 131 |
''' |
| 132 |
|
| 133 |
EXAMPLES = r''' |
| 134 |
--- |
| 135 |
- name: Create an identity pool using ethernet, FCoE, iSCSI and FC settings |
| 136 |
dellemc.openmanage.ome_identity_pool: |
| 137 |
hostname: "192.168.0.1" |
| 138 |
username: "username" |
| 139 |
password: "password" |
| 140 |
ca_path: "/path/to/ca_cert.pem" |
| 141 |
state: present |
| 142 |
pool_name: "pool1" |
| 143 |
pool_description: "Identity pool with Ethernet, FCoE, iSCSI and FC settings" |
| 144 |
ethernet_settings: |
| 145 |
starting_mac_address: "50:50:50:50:50:00" |
| 146 |
identity_count: 60 |
| 147 |
fcoe_settings: |
| 148 |
starting_mac_address: "70:70:70:70:70:00" |
| 149 |
identity_count: 75 |
| 150 |
iscsi_settings: |
| 151 |
starting_mac_address: "60:60:60:60:60:00" |
| 152 |
identity_count: 30 |
| 153 |
initiator_config: |
| 154 |
iqn_prefix: "iqn.myprefix." |
| 155 |
initiator_ip_pool_settings: |
| 156 |
ip_range: "10.33.0.1-10.33.0.255" |
| 157 |
subnet_mask: "255.255.255.0" |
| 158 |
gateway: "192.168.4.1" |
| 159 |
primary_dns_server: "10.8.8.8" |
| 160 |
secondary_dns_server: "8.8.8.8" |
| 161 |
fc_settings: |
| 162 |
starting_address: "30:30:30:30:30:00" |
| 163 |
identity_count: 45 |
| 164 |
|
| 165 |
- name: Create an identity pool using only ethernet settings |
| 166 |
dellemc.openmanage.ome_identity_pool: |
| 167 |
hostname: "192.168.0.1" |
| 168 |
username: "username" |
| 169 |
password: "password" |
| 170 |
ca_path: "/path/to/ca_cert.pem" |
| 171 |
pool_name: "pool2" |
| 172 |
pool_description: "create identity pool with ethernet" |
| 173 |
ethernet_settings: |
| 174 |
starting_mac_address: "aa-bb-cc-dd-ee-aa" |
| 175 |
identity_count: 80 |
| 176 |
|
| 177 |
- name: Modify an identity pool |
| 178 |
dellemc.openmanage.ome_identity_pool: |
| 179 |
hostname: "192.168.0.1" |
| 180 |
username: "username" |
| 181 |
password: "password" |
| 182 |
ca_path: "/path/to/ca_cert.pem" |
| 183 |
pool_name: "pool2" |
| 184 |
new_pool_name: "pool3" |
| 185 |
pool_description: "modifying identity pool with ethernet and fcoe settings" |
| 186 |
ethernet_settings: |
| 187 |
starting_mac_address: "90-90-90-90-90-90" |
| 188 |
identity_count: 61 |
| 189 |
fcoe_settings: |
| 190 |
starting_mac_address: "aabb.ccdd.5050" |
| 191 |
identity_count: 77 |
| 192 |
|
| 193 |
- name: Modify an identity pool using iSCSI and FC settings |
| 194 |
dellemc.openmanage.ome_identity_pool: |
| 195 |
hostname: "{{hostname}}" |
| 196 |
username: "{{username}}" |
| 197 |
password: "{{password}}" |
| 198 |
ca_path: "/path/to/ca_cert.pem" |
| 199 |
pool_name: "pool_new" |
| 200 |
new_pool_name: "pool_new2" |
| 201 |
pool_description: "modifying identity pool with iscsi and fc settings" |
| 202 |
iscsi_settings: |
| 203 |
identity_count: 99 |
| 204 |
initiator_config: |
| 205 |
iqn_prefix: "iqn1.myprefix2." |
| 206 |
initiator_ip_pool_settings: |
| 207 |
gateway: "192.168.4.5" |
| 208 |
fc_settings: |
| 209 |
starting_address: "10:10:10:10:10:10" |
| 210 |
identity_count: 98 |
| 211 |
|
| 212 |
- name: Delete an identity pool |
| 213 |
dellemc.openmanage.ome_identity_pool: |
| 214 |
hostname: "192.168.0.1" |
| 215 |
username: "username" |
| 216 |
password: "password" |
| 217 |
ca_path: "/path/to/ca_cert.pem" |
| 218 |
state: "absent" |
| 219 |
pool_name: "pool2" |
| 220 |
''' |
| 221 |
|
| 222 |
RETURN = r''' |
| 223 |
--- |
| 224 |
msg: |
| 225 |
type: str |
| 226 |
description: Overall status of the identity pool operation. |
| 227 |
returned: always |
| 228 |
sample: "Successfully created an identity pool." |
| 229 |
pool_status: |
| 230 |
type: dict |
| 231 |
description: Details of the user operation, when I(state) is C(present). |
| 232 |
returned: success |
| 233 |
sample: { |
| 234 |
"Id":29, |
| 235 |
"IsSuccessful":True, |
| 236 |
"Issues":[] |
| 237 |
} |
| 238 |
error_info: |
| 239 |
description: Details of the HTTP Error. |
| 240 |
returned: on HTTP error |
| 241 |
type: dict |
| 242 |
sample: { |
| 243 |
"error": { |
| 244 |
"@Message.ExtendedInfo": [{ |
| 245 |
"Message": "Unable to process the request because an error occurred: |
| 246 |
Ethernet-MAC Range overlap found (in this Identity Pool or in a different one) .", |
| 247 |
"MessageArgs": [Ethernet-MAC Range overlap found (in this Identity Pool or in a different one)"], |
| 248 |
"MessageId": "CGEN6001", |
| 249 |
"RelatedProperties": [], |
| 250 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator.", |
| 251 |
"Severity": "Critical" |
| 252 |
}], |
| 253 |
"code": "Base.1.0.GeneralError", |
| 254 |
"message": "A general error has occurred. See ExtendedInfo for more information." |
| 255 |
}} |
| 256 |
''' |
| 257 |
|
| 258 |
import re |
| 259 |
import json |
| 260 |
import codecs |
| 261 |
import binascii |
| 262 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 263 |
from ansible.module_utils.urls import ConnectionError |
| 264 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 265 |
|
| 266 |
IDENTITY_URI = "IdentityPoolService/IdentityPools" |
| 267 |
CHANGES_FOUND = "Changes found to be applied." |
| 268 |
NO_CHANGES_FOUND = "No changes found to be applied." |
| 269 |
|
| 270 |
|
| 271 |
def get_identity_pool_id_by_name(pool_name, rest_obj): |
| 272 |
pool_id = 0 |
| 273 |
attributes = None |
| 274 |
identity_list = rest_obj.get_all_report_details(IDENTITY_URI)["report_list"] |
| 275 |
for item in identity_list: |
| 276 |
if pool_name == item["Name"]: |
| 277 |
pool_id = item["Id"] |
| 278 |
attributes = item |
| 279 |
break |
| 280 |
return pool_id, attributes |
| 281 |
|
| 282 |
|
| 283 |
def mac_validation(mac_input): |
| 284 |
match_found = re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$|" |
| 285 |
"([0-9a-f]{4}([.])[0-9a-f]{4}([.])[0-9a-f]{4})$", |
| 286 |
mac_input.lower()) |
| 287 |
return match_found |
| 288 |
|
| 289 |
|
| 290 |
def mac_to_base64_conversion(mac_address, module): |
| 291 |
try: |
| 292 |
if mac_address: |
| 293 |
allowed_mac_separators = [':', '-', '.'] |
| 294 |
for sep in allowed_mac_separators: |
| 295 |
if sep in mac_address: |
| 296 |
b64_mac_address = codecs.encode(codecs.decode( |
| 297 |
mac_address.replace(sep, ''), 'hex'), 'base64') |
| 298 |
address = codecs.decode(b64_mac_address, 'utf-8').rstrip() |
| 299 |
return address |
| 300 |
except binascii.Error: |
| 301 |
module.exit_json(msg='Encoding of MAC address {0} to base64 ' |
| 302 |
'failed'.format(mac_address), failed=True) |
| 303 |
|
| 304 |
|
| 305 |
def update_modify_setting(modify_payload, existing_payload, setting_type, sub_keys): |
| 306 |
"""update current pool sub setting to modify payload if not provided |
| 307 |
in the options to avoid the null update from ome""" |
| 308 |
for sub_key in sub_keys: |
| 309 |
if sub_key not in modify_payload[setting_type] and sub_key in existing_payload[setting_type]: |
| 310 |
modify_payload[setting_type][sub_key] = existing_payload[setting_type][sub_key] |
| 311 |
elif existing_payload[setting_type]: |
| 312 |
if modify_payload[setting_type].get(sub_key) and existing_payload[setting_type].get(sub_key): |
| 313 |
modify_setting = modify_payload[setting_type][sub_key] |
| 314 |
existing_setting_payload = existing_payload[setting_type][sub_key] |
| 315 |
diff_item = list(set(existing_setting_payload) - set(modify_setting)) |
| 316 |
for key in diff_item: |
| 317 |
modify_payload[setting_type][sub_key][key] = existing_setting_payload[key] |
| 318 |
|
| 319 |
|
| 320 |
def get_updated_modify_payload(modify_payload, existing_payload): |
| 321 |
"""update current pool setting to modify payload if not provided |
| 322 |
in the options to avoid the null update from ome""" |
| 323 |
remove_unwanted_key_list = ['@odata.type', '@odata.id', 'CreatedBy', 'CreationTime', 'LastUpdatedBy', |
| 324 |
'LastUpdateTime', 'UsageCounts', '[email protected]'] |
| 325 |
[existing_payload.pop(key) for key in remove_unwanted_key_list if key in existing_payload] |
| 326 |
for key, val in existing_payload.items(): |
| 327 |
if key not in modify_payload: |
| 328 |
modify_payload[key] = val |
| 329 |
else: |
| 330 |
if existing_payload.get(key) and key == "EthernetSettings" or key == "FcoeSettings": |
| 331 |
update_modify_setting(modify_payload, existing_payload, key, ["Mac"]) |
| 332 |
elif existing_payload.get(key) and key == "FcSettings": |
| 333 |
update_modify_setting(modify_payload, existing_payload, key, ["Wwnn", "Wwpn"]) |
| 334 |
elif existing_payload.get(key) and key == "IscsiSettings": |
| 335 |
update_modify_setting(modify_payload, existing_payload, key, |
| 336 |
["Mac", "InitiatorConfig", "InitiatorIpPoolSettings"]) |
| 337 |
modify_payload = {k: v for k, v in modify_payload.items() if v is not None} |
| 338 |
return modify_payload |
| 339 |
|
| 340 |
|
| 341 |
def update_mac_settings(payload, settings_params, setting_type, module): |
| 342 |
"""payload update for ethernet and fcoe settings and isci settings |
| 343 |
and convert to MAC address to base 64 format""" |
| 344 |
mac_address = settings_params.get("starting_mac_address") |
| 345 |
mac_base_64_format = None |
| 346 |
if mac_address: |
| 347 |
match_found = mac_validation(mac_address) |
| 348 |
if match_found: |
| 349 |
mac_base_64_format = mac_to_base64_conversion(mac_address, module) |
| 350 |
else: |
| 351 |
module.exit_json(msg="Please provide the valid MAC address format for {0} settings." |
| 352 |
.format(setting_type.split('Settings')[0]), failed=True) |
| 353 |
sub_setting_mapper = {"StartingMacAddress": mac_base_64_format, |
| 354 |
"IdentityCount": settings_params.get("identity_count")} |
| 355 |
sub_settings_payload = {k: v for k, v in sub_setting_mapper.items() if v is not None} |
| 356 |
if any(sub_settings_payload): |
| 357 |
payload.update({setting_type: {"Mac": sub_settings_payload}}) |
| 358 |
|
| 359 |
|
| 360 |
def update_iscsi_specific_settings(payload, settings_params, setting_type): |
| 361 |
"""payload update for Iscsi specific settings""" |
| 362 |
sub_setting_mapper = {} |
| 363 |
initiator_config = settings_params.get("initiator_config") |
| 364 |
if initiator_config and initiator_config.get("iqn_prefix"): |
| 365 |
sub_setting_mapper.update({ |
| 366 |
"InitiatorConfig": {"IqnPrefix": initiator_config.get("iqn_prefix")}}) |
| 367 |
if settings_params.get("initiator_ip_pool_settings"): |
| 368 |
initiator_ip_pool_settings = settings_params["initiator_ip_pool_settings"] |
| 369 |
initiator_ip_pool_settings = {"IpRange": initiator_ip_pool_settings.get("ip_range"), |
| 370 |
"SubnetMask": initiator_ip_pool_settings.get("subnet_mask"), |
| 371 |
"Gateway": initiator_ip_pool_settings.get("gateway"), |
| 372 |
"PrimaryDnsServer": initiator_ip_pool_settings.get("primary_dns_server"), |
| 373 |
"SecondaryDnsServer": initiator_ip_pool_settings.get("secondary_dns_server")} |
| 374 |
initiator_ip_pool_settings = {k: v for k, v in initiator_ip_pool_settings.items() if v is not None} |
| 375 |
sub_setting_mapper.update({ |
| 376 |
"InitiatorIpPoolSettings": initiator_ip_pool_settings}) |
| 377 |
if any(sub_setting_mapper): |
| 378 |
if "IscsiSettings" in payload: |
| 379 |
"""update MAC address setting""" |
| 380 |
sub_setting_mapper.update(payload[setting_type]) |
| 381 |
sub_setting_mapper = {key: val for key, val in sub_setting_mapper.items() if any(val)} |
| 382 |
payload.update({setting_type: sub_setting_mapper}) |
| 383 |
|
| 384 |
|
| 385 |
def get_wwn_address_prefix(starting_address): |
| 386 |
"""Prefix wwnn and wwpn MAC address with 20x00 and 20x01 respectively""" |
| 387 |
delimiter, wwnn_prefix, wwpn_prefix = None, None, None |
| 388 |
if "." in starting_address: |
| 389 |
delimiter = "." |
| 390 |
elif ":" in starting_address: |
| 391 |
delimiter = ":" |
| 392 |
elif "-" in starting_address: |
| 393 |
delimiter = "-" |
| 394 |
length = len(starting_address.split(delimiter)[0]) |
| 395 |
if length == 4: |
| 396 |
wwnn_prefix = "2000{0}".format(delimiter) |
| 397 |
wwpn_prefix = "2001{0}".format(delimiter) |
| 398 |
else: |
| 399 |
wwnn_prefix = "20{0}00{0}".format(delimiter) |
| 400 |
wwpn_prefix = "20{0}01{0}".format(delimiter) |
| 401 |
return wwnn_prefix, wwpn_prefix |
| 402 |
|
| 403 |
|
| 404 |
def update_fc_settings(payload, settings_params, setting_type, module): |
| 405 |
"""payload update for Fibre Channel specific settings |
| 406 |
payload: other setting payload |
| 407 |
settings_params: fc setting parameters |
| 408 |
setting_type: "FcSettings" |
| 409 |
""" |
| 410 |
sub_setting_mapper = {} |
| 411 |
starting_address = settings_params.get("starting_address") |
| 412 |
identity_count = settings_params.get("identity_count") |
| 413 |
wwnn_payload = {} |
| 414 |
wwpn_payload = {} |
| 415 |
if starting_address: |
| 416 |
if not mac_validation(starting_address): |
| 417 |
module.exit_json(msg="Please provide the valid starting address format for FC settings.", |
| 418 |
failed=True) |
| 419 |
wwnn_prefix, wwpn_prefix = get_wwn_address_prefix(starting_address) |
| 420 |
wwnn_address = mac_to_base64_conversion(wwnn_prefix + starting_address, module) |
| 421 |
wwpn_address = mac_to_base64_conversion(wwpn_prefix + starting_address, module) |
| 422 |
wwnn_payload.update({"StartingAddress": wwnn_address}) |
| 423 |
wwpn_payload.update({"StartingAddress": wwpn_address}) |
| 424 |
if identity_count is not None: |
| 425 |
wwnn_payload.update({"IdentityCount": identity_count}) |
| 426 |
wwpn_payload.update({"IdentityCount": identity_count}) |
| 427 |
sub_setting_mapper.update({"Wwnn": wwnn_payload, |
| 428 |
"Wwpn": wwpn_payload}) |
| 429 |
sub_setting_mapper = {key: val for key, val in sub_setting_mapper.items() if any(val)} |
| 430 |
if any(sub_setting_mapper): |
| 431 |
payload.update({setting_type: sub_setting_mapper}) |
| 432 |
|
| 433 |
|
| 434 |
def get_payload(module, pool_id=None): |
| 435 |
"""create payload for create and modify operations""" |
| 436 |
module_params = module.params |
| 437 |
setting_payload = { |
| 438 |
"Description": module_params.get("pool_description"), |
| 439 |
"Name": module_params["pool_name"] |
| 440 |
} |
| 441 |
fcoe_settings_params = module_params.get("fcoe_settings") |
| 442 |
ethernet_settings_params = module_params.get("ethernet_settings") |
| 443 |
iscsi_settings_params = module_params.get("iscsi_settings") |
| 444 |
fc_settings_params = module_params.get("fc_settings") |
| 445 |
if fcoe_settings_params: |
| 446 |
update_mac_settings(setting_payload, fcoe_settings_params, "FcoeSettings", module) |
| 447 |
if ethernet_settings_params: |
| 448 |
update_mac_settings(setting_payload, ethernet_settings_params, "EthernetSettings", module) |
| 449 |
if iscsi_settings_params: |
| 450 |
update_mac_settings(setting_payload, iscsi_settings_params, "IscsiSettings", module) |
| 451 |
update_iscsi_specific_settings(setting_payload, iscsi_settings_params, "IscsiSettings") |
| 452 |
if fc_settings_params: |
| 453 |
update_fc_settings(setting_payload, fc_settings_params, "FcSettings", module) |
| 454 |
if pool_id: |
| 455 |
new_name = module_params.get("new_pool_name") |
| 456 |
if new_name is not None: |
| 457 |
setting_payload.update({"Name": new_name}) |
| 458 |
setting_payload["Id"] = pool_id |
| 459 |
payload = {k: v for k, v in setting_payload.items() if v is not None} |
| 460 |
return payload |
| 461 |
|
| 462 |
|
| 463 |
def compare_nested_dict(modify_setting_payload, existing_setting_payload): |
| 464 |
"""compare existing and requested setting values of identity pool in case of modify operations |
| 465 |
if both are same return True""" |
| 466 |
for key, val in modify_setting_payload.items(): |
| 467 |
if existing_setting_payload is None or existing_setting_payload.get(key) is None: |
| 468 |
return False |
| 469 |
elif isinstance(val, dict): |
| 470 |
if not compare_nested_dict(val, existing_setting_payload.get(key)): |
| 471 |
return False |
| 472 |
elif val != existing_setting_payload.get(key): |
| 473 |
return False |
| 474 |
return True |
| 475 |
|
| 476 |
|
| 477 |
def validate_modify_create_payload(setting_payload, module, action): |
| 478 |
for key, val in setting_payload.items(): |
| 479 |
if key in ["EthernetSettings", "FcoeSettings"] and val: |
| 480 |
sub_config = val.get("Mac") |
| 481 |
if sub_config is None or not all([sub_config.get("IdentityCount"), sub_config.get("StartingMacAddress")]): |
| 482 |
module.exit_json(msg="Both starting MAC address and identity " |
| 483 |
"count is required to {0} an identity pool " |
| 484 |
"using {1} settings.".format(action, ''.join(key.split('Settings'))), |
| 485 |
failed=True) |
| 486 |
elif key == "FcSettings" and val: |
| 487 |
sub_config = val.get("Wwnn") |
| 488 |
validate_fc_settings(sub_config, module, action) |
| 489 |
elif key == "IscsiSettings" and val: |
| 490 |
validate_iscsi_settings(val, module, action, key) |
| 491 |
|
| 492 |
|
| 493 |
def validate_fc_settings(sub_config, module, action): |
| 494 |
if sub_config is None or not all([sub_config.get("IdentityCount"), sub_config.get("StartingAddress")]): |
| 495 |
module.exit_json(msg="Both starting MAC address and identity count is " |
| 496 |
"required to {0} an identity pool using Fc settings.".format(action), |
| 497 |
failed=True) |
| 498 |
|
| 499 |
|
| 500 |
def validate_iscsi_settings(val, module, action, key): |
| 501 |
""" |
| 502 |
Validate iscsi settings for modify and create operations. |
| 503 |
""" |
| 504 |
sub_config1 = val.get("Mac") |
| 505 |
sub_config2 = val.get("InitiatorIpPoolSettings") |
| 506 |
if sub_config1 is None or not all([sub_config1.get("IdentityCount"), sub_config1.get("StartingMacAddress")]): |
| 507 |
module.exit_json(msg="Both starting MAC address and identity count is " |
| 508 |
"required to {0} an identity pool using {1} " |
| 509 |
"settings.".format(action, ''.join(key.split('Settings'))), |
| 510 |
failed=True) |
| 511 |
elif sub_config2: |
| 512 |
if not all([sub_config2.get("IpRange"), sub_config2.get("SubnetMask")]): |
| 513 |
module.exit_json(msg="Both ip range and subnet mask in required to" |
| 514 |
" {0} an identity pool using iSCSI settings.".format(action), |
| 515 |
failed=True) |
| 516 |
|
| 517 |
|
| 518 |
def pool_create_modify(module, rest_obj): |
| 519 |
pool_name = module.params["pool_name"] |
| 520 |
pool_id, existing_payload = get_identity_pool_id_by_name(pool_name, rest_obj) |
| 521 |
method = "POST" |
| 522 |
uri = IDENTITY_URI |
| 523 |
action = "create" |
| 524 |
setting_payload = get_payload(module, pool_id) |
| 525 |
if pool_id: |
| 526 |
action = "modify" |
| 527 |
method = "PUT" |
| 528 |
uri = uri + "({0})".format(pool_id) |
| 529 |
if compare_nested_dict(setting_payload, existing_payload): |
| 530 |
module.exit_json(msg=NO_CHANGES_FOUND) |
| 531 |
else: |
| 532 |
setting_payload = get_updated_modify_payload(setting_payload, existing_payload) |
| 533 |
validate_modify_create_payload(setting_payload, module, action) |
| 534 |
if module.check_mode: |
| 535 |
module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 536 |
resp = rest_obj.invoke_request(method, uri, data=setting_payload) |
| 537 |
msg = get_success_message(action, resp.json_data) |
| 538 |
return msg |
| 539 |
|
| 540 |
|
| 541 |
def pool_delete(module, rest_obj): |
| 542 |
pool_name = module.params["pool_name"] |
| 543 |
pool_id, _ = get_identity_pool_id_by_name(pool_name, rest_obj) |
| 544 |
if not pool_id: |
| 545 |
message = "The identity pool '{0}' is not present in the system.".format(pool_name) |
| 546 |
module.exit_json(msg=message) |
| 547 |
method = "DELETE" |
| 548 |
uri = IDENTITY_URI + "({0})".format(pool_id) |
| 549 |
if module.check_mode: |
| 550 |
module.exit_json(msg=CHANGES_FOUND, changed=True) |
| 551 |
rest_obj.invoke_request(method, uri) |
| 552 |
return {"msg": "Successfully deleted the identity pool."} |
| 553 |
|
| 554 |
|
| 555 |
def get_success_message(action, resp_data): |
| 556 |
message = { |
| 557 |
"create": "Successfully created an identity pool.", |
| 558 |
"modify": "Successfully modified the identity pool." |
| 559 |
} |
| 560 |
return {"msg": message[action], "result": resp_data} |
| 561 |
|
| 562 |
|
| 563 |
def main(): |
| 564 |
settings_options = {"starting_mac_address": {"type": 'str'}, |
| 565 |
"identity_count": {"type": 'int'}} |
| 566 |
iscsi_specific_settings = {"starting_mac_address": {"type": 'str'}, |
| 567 |
"identity_count": {"type": 'int'}, |
| 568 |
"initiator_config": {"options": {"iqn_prefix": {"type": 'str'}}, "type": "dict"}, |
| 569 |
"initiator_ip_pool_settings": {"options": {"ip_range": {"type": 'str'}, |
| 570 |
"subnet_mask": {"type": 'str'}, |
| 571 |
"gateway": {"type": 'str'}, |
| 572 |
"primary_dns_server": {"type": 'str'}, |
| 573 |
"secondary_dns_server": {"type": 'str'}}, |
| 574 |
"type": "dict"}} |
| 575 |
fc_settings = {"starting_address": {"type": "str"}, "identity_count": {"type": "int"}} |
| 576 |
|
| 577 |
specs = { |
| 578 |
"state": {"type": "str", "required": False, "default": "present", "choices": ['present', 'absent']}, |
| 579 |
"pool_name": {"required": True, "type": "str"}, |
| 580 |
"new_pool_name": {"required": False, "type": "str"}, |
| 581 |
"pool_description": {"required": False, "type": "str"}, |
| 582 |
"ethernet_settings": {"required": False, "type": "dict", |
| 583 |
"options": settings_options}, |
| 584 |
"fcoe_settings": {"required": False, "type": "dict", "options": settings_options}, |
| 585 |
"iscsi_settings": {"required": False, "type": "dict", |
| 586 |
"options": iscsi_specific_settings}, |
| 587 |
"fc_settings": {"required": False, "type": "dict", "options": fc_settings}, |
| 588 |
} |
| 589 |
|
| 590 |
module = OmeAnsibleModule( |
| 591 |
argument_spec=specs, |
| 592 |
supports_check_mode=True |
| 593 |
) |
| 594 |
try: |
| 595 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 596 |
state = module.params["state"] |
| 597 |
if state == "present": |
| 598 |
message = pool_create_modify(module, rest_obj) |
| 599 |
module.exit_json(msg=message["msg"], pool_status=message["result"], changed=True) |
| 600 |
else: |
| 601 |
message = pool_delete(module, rest_obj) |
| 602 |
module.exit_json(msg=message["msg"], changed=True) |
| 603 |
except HTTPError as err: |
| 604 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 605 |
except URLError as err: |
| 606 |
module.exit_json(msg=str(err), unreachable=True, failed=True) |
| 607 |
except (IOError, ValueError, TypeError, ConnectionError, OSError) as err: |
| 608 |
module.exit_json(msg=str(err), failed=True) |
| 609 |
|
| 610 |
|
| 611 |
if __name__ == "__main__": |
| 612 |
main() |