| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
""" |
| 6 |
Utility functions for Intersight vHBA-related modules. |
| 7 |
|
| 8 |
This module contains common functions used by both intersight_san_connectivity_policy |
| 9 |
and intersight_vhba_template modules to prevent code duplication. |
| 10 |
""" |
| 11 |
|
| 12 |
from __future__ import absolute_import, division, print_function |
| 13 |
__metaclass__ = type |
| 14 |
|
| 15 |
import re |
| 16 |
|
| 17 |
|
| 18 |
def validate_wwn_address(wwn_address, address_type='WWPN'): |
| 19 |
""" |
| 20 |
Validate WWN (WWNN or WWPN) address format and range. |
| 21 |
""" |
| 22 |
wwn_pattern = r'^([0-9a-fA-F]{2}:){7}[0-9a-fA-F]{2}$' |
| 23 |
if not re.match(wwn_pattern, wwn_address): |
| 24 |
raise ValueError(f"Invalid {address_type} format '{wwn_address}'. Expected format: XX:XX:XX:XX:XX:XX:XX:XX (hex values)") |
| 25 |
first_octet = int(wwn_address.split(':')[0], 16) |
| 26 |
if not ((0x20 <= first_octet <= 0x2F) or (0x50 <= first_octet <= 0x5F)): |
| 27 |
raise ValueError(f"Invalid {address_type} address range. First octet must be 20-2F or 50-5F") |
| 28 |
return True |
| 29 |
|
| 30 |
|
| 31 |
def get_policy_moid_with_org(intersight, policy_cache, module, resource_path, policy_name, organization_name, policy_type="Policy"): |
| 32 |
""" |
| 33 |
Get policy MOID with caching and organization scoping to avoid redundant API calls. |
| 34 |
""" |
| 35 |
if not policy_name: |
| 36 |
return None |
| 37 |
|
| 38 |
cache_key = f"{resource_path}:{policy_name}" |
| 39 |
if cache_key in policy_cache: |
| 40 |
return policy_cache[cache_key] |
| 41 |
|
| 42 |
policy_moid = intersight.get_moid_by_name_and_org( |
| 43 |
resource_path=resource_path, |
| 44 |
resource_name=policy_name, |
| 45 |
organization_name=organization_name |
| 46 |
) |
| 47 |
if not policy_moid: |
| 48 |
module.fail_json(msg=f"{policy_type} '{policy_name}' not found in organization '{organization_name}'") |
| 49 |
|
| 50 |
policy_cache[cache_key] = policy_moid |
| 51 |
return policy_moid |
| 52 |
|
| 53 |
|
| 54 |
def get_common_fc_policy_mappings(): |
| 55 |
""" |
| 56 |
Get common Fibre Channel policy mappings shared between vHBA template and SAN connectivity policy. |
| 57 |
""" |
| 58 |
return { |
| 59 |
'fibre_channel_network_policy_name': ('/vnic/FcNetworkPolicies', 'FcNetworkPolicy', 'Fibre Channel Network Policy'), |
| 60 |
'fibre_channel_qos_policy_name': ('/vnic/FcQosPolicies', 'FcQosPolicy', 'Fibre Channel QoS Policy'), |
| 61 |
'fibre_channel_adapter_policy_name': ('/vnic/FcAdapterPolicies', 'FcAdapterPolicy', 'Fibre Channel Adapter Policy') |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
def get_wwpn_pool_policy_mapping(): |
| 66 |
""" |
| 67 |
Get WWPN pool policy mapping. |
| 68 |
""" |
| 69 |
return { |
| 70 |
'wwpn_pool_name': ('/fcpool/Pools', 'WwpnPool', 'WWPN Pool') |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
def get_fc_zone_policy_mapping(): |
| 75 |
""" |
| 76 |
Get Fibre Channel Zone policy mapping (optional policy). |
| 77 |
""" |
| 78 |
return { |
| 79 |
'fibre_channel_zone_policy_names': ('/fabric/FcZonePolicies', 'FcZonePolicies', 'Fibre Channel Zone Policy') |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
def validate_wwpn_address_config(module, config, config_name='vHBA'): |
| 84 |
""" |
| 85 |
Validate WWPN address configuration. |
| 86 |
""" |
| 87 |
wwpn_address_type = config.get('wwpn_address_type', 'pool') |
| 88 |
wwpn_pool = config.get('wwpn_pool') or config.get('wwpn_pool_name') |
| 89 |
static_wwpn_address = config.get('static_wwpn_address') |
| 90 |
|
| 91 |
if wwpn_address_type == 'pool' and not wwpn_pool: |
| 92 |
module.fail_json(msg=f"wwpn_pool_name is required when wwpn_address_type is 'pool' for {config_name}") |
| 93 |
|
| 94 |
if wwpn_address_type == 'static' and not static_wwpn_address: |
| 95 |
module.fail_json(msg=f"static_wwpn_address is required when wwpn_address_type is 'static' for {config_name}") |
| 96 |
|
| 97 |
if wwpn_address_type != 'pool' and wwpn_pool: |
| 98 |
module.fail_json(msg=f"wwpn_pool_name should only be specified when wwpn_address_type is 'pool' for {config_name}") |
| 99 |
|
| 100 |
if wwpn_address_type != 'static' and static_wwpn_address: |
| 101 |
module.fail_json(msg=f"static_wwpn_address should only be specified when wwpn_address_type is 'static' for {config_name}") |
| 102 |
|
| 103 |
# Validate static WWPN address format if provided |
| 104 |
if static_wwpn_address: |
| 105 |
try: |
| 106 |
validate_wwn_address(static_wwpn_address, 'WWPN') |
| 107 |
except ValueError as e: |
| 108 |
module.fail_json(msg=f"{config_name}: {str(e)}") |
| 109 |
|
| 110 |
|
| 111 |
def validate_wwnn_address_config(module, policy_params): |
| 112 |
""" |
| 113 |
Validate WWNN address configuration for SAN Connectivity Policy. |
| 114 |
""" |
| 115 |
wwnn_address_type = policy_params.get('wwnn_address_type') |
| 116 |
static_wwnn_address = policy_params.get('static_wwnn_address') |
| 117 |
wwnn_pool = policy_params.get('wwnn_pool') |
| 118 |
|
| 119 |
if wwnn_address_type == 'static' and not static_wwnn_address: |
| 120 |
module.fail_json(msg="static_wwnn_address is required when wwnn_address_type is 'static'") |
| 121 |
|
| 122 |
if wwnn_address_type == 'pool' and not wwnn_pool: |
| 123 |
module.fail_json(msg="wwnn_pool is required when wwnn_address_type is 'pool'") |
| 124 |
|
| 125 |
if wwnn_address_type != 'static' and static_wwnn_address: |
| 126 |
module.fail_json(msg="static_wwnn_address should only be specified when wwnn_address_type is 'static'") |
| 127 |
|
| 128 |
if wwnn_address_type != 'pool' and wwnn_pool: |
| 129 |
module.fail_json(msg="wwnn_pool should only be specified when wwnn_address_type is 'pool'") |
| 130 |
|
| 131 |
if static_wwnn_address: |
| 132 |
try: |
| 133 |
validate_wwn_address(static_wwnn_address, 'WWNN') |
| 134 |
except ValueError as e: |
| 135 |
module.fail_json(msg=str(e)) |
| 136 |
|
| 137 |
|
| 138 |
def validate_fi_attached_placement_config(module, vhba_config): |
| 139 |
""" |
| 140 |
Validate FIAttached placement configuration for vHBA. |
| 141 |
""" |
| 142 |
vhba_name = vhba_config.get('name', 'unknown') |
| 143 |
auto_slot_id = vhba_config.get('auto_slot_id', True) |
| 144 |
slot_id = vhba_config.get('slot_id') |
| 145 |
auto_pci_link = vhba_config.get('auto_pci_link', True) |
| 146 |
pci_link_assignment_mode = vhba_config.get('pci_link_assignment_mode') |
| 147 |
pci_link = vhba_config.get('pci_link') |
| 148 |
|
| 149 |
if not auto_slot_id and not slot_id: |
| 150 |
module.fail_json(msg=f"slot_id is required when auto_slot_id is false for vHBA '{vhba_name}'") |
| 151 |
|
| 152 |
if auto_slot_id and slot_id: |
| 153 |
module.fail_json(msg=f"slot_id should not be specified when auto_slot_id is true for vHBA '{vhba_name}'") |
| 154 |
|
| 155 |
if not auto_pci_link and not pci_link_assignment_mode: |
| 156 |
module.fail_json(msg=f"pci_link_assignment_mode is required when auto_pci_link is false for vHBA '{vhba_name}'") |
| 157 |
|
| 158 |
if auto_pci_link and pci_link_assignment_mode: |
| 159 |
module.fail_json(msg=f"pci_link_assignment_mode should not be specified when auto_pci_link is true for vHBA '{vhba_name}'") |
| 160 |
|
| 161 |
if pci_link_assignment_mode == 'custom' and pci_link is None: |
| 162 |
module.fail_json(msg=f"pci_link is required when pci_link_assignment_mode is 'custom' for vHBA '{vhba_name}'") |
| 163 |
|
| 164 |
if pci_link_assignment_mode != 'custom' and pci_link is not None and pci_link != 0: |
| 165 |
module.fail_json(msg=f"pci_link should only be specified when pci_link_assignment_mode is 'custom' for vHBA '{vhba_name}'") |
| 166 |
|
| 167 |
|
| 168 |
def validate_standalone_vhba_config(module, vhba_config): |
| 169 |
""" |
| 170 |
Validate Standalone specific vHBA configuration. |
| 171 |
""" |
| 172 |
vhba_name = vhba_config.get('name', 'unknown') |
| 173 |
slot_id = vhba_config.get('slot_id') |
| 174 |
|
| 175 |
if not slot_id: |
| 176 |
module.fail_json(msg=f"slot_id is required for standalone vHBA '{vhba_name}'") |
| 177 |
|
| 178 |
|
| 179 |
def resolve_policy_moids_from_mappings(intersight, policy_cache, module, config_source, policy_mappings, organization_name): |
| 180 |
""" |
| 181 |
Resolve policy MOIDs from configuration using provided policy mappings. |
| 182 |
""" |
| 183 |
policy_moids = {} |
| 184 |
|
| 185 |
for param_name, (resource_path, api_field, policy_type) in policy_mappings.items(): |
| 186 |
policy_name = config_source.get(param_name) |
| 187 |
if policy_name: |
| 188 |
policy_moid = get_policy_moid_with_org( |
| 189 |
intersight, policy_cache, module, resource_path, policy_name, organization_name, policy_type |
| 190 |
) |
| 191 |
policy_moids[api_field] = policy_moid |
| 192 |
|
| 193 |
return policy_moids |
| 194 |
|
| 195 |
|
| 196 |
def resolve_fc_zone_policies(intersight, policy_cache, module, zone_policy_names, organization_name): |
| 197 |
""" |
| 198 |
Resolve Fibre Channel Zone Policy MOIDs from list of policy names. |
| 199 |
""" |
| 200 |
fc_zone_policy_moids = [] |
| 201 |
|
| 202 |
if zone_policy_names: |
| 203 |
for fc_zone_policy_name in zone_policy_names: |
| 204 |
fc_zone_policy_moid = get_policy_moid_with_org( |
| 205 |
intersight, policy_cache, module, '/fabric/FcZonePolicies', |
| 206 |
fc_zone_policy_name, organization_name, 'Fibre Channel Zone Policy' |
| 207 |
) |
| 208 |
fc_zone_policy_moids.append(fc_zone_policy_moid) |
| 209 |
|
| 210 |
return fc_zone_policy_moids |
| 211 |
|
| 212 |
|
| 213 |
def build_wwpn_address_config(vhba_config, intersight, policy_cache, module, organization_name): |
| 214 |
""" |
| 215 |
Build WWPN address configuration for API body. |
| 216 |
""" |
| 217 |
wwpn_address_type = vhba_config.get('wwpn_address_type', 'pool') |
| 218 |
wwpn_address_type_map = {'pool': 'POOL', 'static': 'STATIC'} |
| 219 |
api_wwpn_address_type = wwpn_address_type_map[wwpn_address_type] |
| 220 |
|
| 221 |
wwpn_config = { |
| 222 |
'WwpnAddressType': api_wwpn_address_type |
| 223 |
} |
| 224 |
|
| 225 |
if wwpn_address_type == 'pool': |
| 226 |
wwpn_pool_name = vhba_config.get('wwpn_pool') or vhba_config.get('wwpn_pool_name') |
| 227 |
wwpn_pool_moid = get_policy_moid_with_org( |
| 228 |
intersight, policy_cache, module, '/fcpool/Pools', |
| 229 |
wwpn_pool_name, organization_name, 'WWPN Pool' |
| 230 |
) |
| 231 |
wwpn_config['WwpnPool'] = wwpn_pool_moid |
| 232 |
wwpn_config['StaticWwpnAddress'] = '' |
| 233 |
else: |
| 234 |
wwpn_config['StaticWwpnAddress'] = vhba_config['static_wwpn_address'] |
| 235 |
wwpn_config['WwpnPool'] = '' |
| 236 |
|
| 237 |
return wwpn_config |
| 238 |
|
| 239 |
|
| 240 |
def build_fi_attached_placement(vhba_config): |
| 241 |
""" |
| 242 |
Build FI-Attached placement configuration for API body. |
| 243 |
""" |
| 244 |
auto_slot_id = vhba_config.get('auto_slot_id', True) |
| 245 |
auto_pci_link = vhba_config.get('auto_pci_link', True) |
| 246 |
switch_id = vhba_config.get('switch_id', 'a') |
| 247 |
api_switch_id = switch_id.upper() |
| 248 |
|
| 249 |
placement = { |
| 250 |
'SwitchId': api_switch_id, |
| 251 |
'AutoSlotId': auto_slot_id, |
| 252 |
'AutoPciLink': auto_pci_link |
| 253 |
} |
| 254 |
|
| 255 |
if not auto_slot_id: |
| 256 |
placement['Id'] = vhba_config['slot_id'] |
| 257 |
|
| 258 |
if not auto_pci_link: |
| 259 |
pci_link_mode = vhba_config['pci_link_assignment_mode'] |
| 260 |
pci_link_mode_map = {'custom': 'Custom', 'load-balanced': 'Load-Balanced'} |
| 261 |
api_pci_link_mode = pci_link_mode_map[pci_link_mode] |
| 262 |
placement['PciLinkAssignmentMode'] = api_pci_link_mode |
| 263 |
if pci_link_mode == 'custom': |
| 264 |
placement['PciLink'] = vhba_config.get('pci_link', 0) |
| 265 |
|
| 266 |
return placement |
| 267 |
|
| 268 |
|
| 269 |
def build_standalone_placement(vhba_config): |
| 270 |
""" |
| 271 |
Build Standalone placement configuration for API body. |
| 272 |
""" |
| 273 |
return { |
| 274 |
'Id': vhba_config['slot_id'], |
| 275 |
'Uplink': vhba_config.get('uplink_port', 0), |
| 276 |
'PciLink': vhba_config.get('pci_link', 0) |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
def get_vhba_template_policy_mappings(): |
| 281 |
""" |
| 282 |
Get policy mappings for vHBA Template configuration. |
| 283 |
""" |
| 284 |
policy_mappings = get_common_fc_policy_mappings() |
| 285 |
policy_mappings.update(get_wwpn_pool_policy_mapping()) |
| 286 |
return policy_mappings |
| 287 |
|
| 288 |
|
| 289 |
def get_san_connectivity_vhba_policy_mappings(target_platform): |
| 290 |
""" |
| 291 |
Get policy mappings for vHBA in SAN Connectivity Policy based on target platform. |
| 292 |
""" |
| 293 |
policy_mappings = { |
| 294 |
'fibre_channel_network_policy': ('/vnic/FcNetworkPolicies', 'FcNetworkPolicy', 'Fibre Channel Network Policy'), |
| 295 |
'fibre_channel_qos_policy': ('/vnic/FcQosPolicies', 'FcQosPolicy', 'Fibre Channel QoS Policy'), |
| 296 |
'fibre_channel_adapter_policy': ('/vnic/FcAdapterPolicies', 'FcAdapterPolicy', 'Fibre Channel Adapter Policy') |
| 297 |
} |
| 298 |
|
| 299 |
# WWPN pool only for FI-Attached |
| 300 |
if target_platform == 'fiattached': |
| 301 |
policy_mappings['wwpn_pool'] = ('/fcpool/Pools', 'WwpnPool', 'WWPN Pool') |
| 302 |
|
| 303 |
return policy_mappings |