| 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 vNIC-related modules. |
| 7 |
|
| 8 |
This module contains common functions used by both intersight_lan_connectivity_policy |
| 9 |
and intersight_vnic_template modules to prevent code duplication. |
| 10 |
""" |
| 11 |
|
| 12 |
from __future__ import absolute_import, division, print_function |
| 13 |
__metaclass__ = type |
| 14 |
|
| 15 |
|
| 16 |
def get_policy_moid_with_org(intersight, policy_cache, module, resource_path, policy_name, organization_name, policy_type="Policy"): |
| 17 |
""" |
| 18 |
Get policy MOID with caching and organization scoping to avoid redundant API calls. |
| 19 |
|
| 20 |
This function scopes the policy lookup to a specific organization, preventing |
| 21 |
conflicts when multiple organizations have policies with the same name. |
| 22 |
|
| 23 |
Args: |
| 24 |
intersight: IntersightModule instance |
| 25 |
policy_cache: Dictionary for caching policy MOIDs |
| 26 |
module: AnsibleModule instance |
| 27 |
resource_path: API resource path for the policy |
| 28 |
policy_name: Name of the policy to resolve |
| 29 |
organization_name: Name of the organization to scope the search to |
| 30 |
policy_type: Type of policy for error messages (default: "Policy") |
| 31 |
|
| 32 |
Returns: |
| 33 |
MOID of the policy or fails with error message if not found |
| 34 |
""" |
| 35 |
|
| 36 |
if not policy_name: |
| 37 |
return None |
| 38 |
|
| 39 |
cache_key = f"{resource_path}:{policy_name}" |
| 40 |
if cache_key in policy_cache: |
| 41 |
return policy_cache[cache_key] |
| 42 |
|
| 43 |
policy_moid = intersight.get_moid_by_name_and_org( |
| 44 |
resource_path=resource_path, |
| 45 |
resource_name=policy_name, |
| 46 |
organization_name=organization_name |
| 47 |
) |
| 48 |
if not policy_moid: |
| 49 |
module.fail_json(msg=f"{policy_type} '{policy_name}' not found in organization '{organization_name}'") |
| 50 |
|
| 51 |
policy_cache[cache_key] = policy_moid |
| 52 |
return policy_moid |
| 53 |
|
| 54 |
|
| 55 |
def validate_cdn_config(module, vnic_config): |
| 56 |
""" |
| 57 |
Validate CDN (Consistent Device Naming) configuration. |
| 58 |
""" |
| 59 |
cdn_source = vnic_config.get('cdn_source', 'vnic') |
| 60 |
cdn_value = vnic_config.get('cdn_value') |
| 61 |
vnic_name = vnic_config.get('name', 'unknown') |
| 62 |
|
| 63 |
if cdn_source == 'user' and not cdn_value: |
| 64 |
module.fail_json(msg=f"cdn_value is required when cdn_source is set to 'user' for {vnic_name}") |
| 65 |
|
| 66 |
if cdn_source == 'vnic' and cdn_value: |
| 67 |
module.fail_json(msg=f"cdn_value should not be provided when cdn_source is set to 'vnic' for {vnic_name}") |
| 68 |
|
| 69 |
|
| 70 |
def validate_usnic_settings(module, usnic_settings, name=None): |
| 71 |
""" |
| 72 |
Validate USNIC connection type settings. |
| 73 |
""" |
| 74 |
error_suffix = f" for {name}" if name else "" |
| 75 |
|
| 76 |
if not usnic_settings: |
| 77 |
module.fail_json(msg=f"usnic_settings is required when connection_type is 'usnic'{error_suffix}") |
| 78 |
|
| 79 |
usnic_adapter_policy_name = usnic_settings.get('usnic_adapter_policy_name') |
| 80 |
if not usnic_adapter_policy_name: |
| 81 |
module.fail_json(msg=f"usnic_adapter_policy_name is required in usnic_settings{error_suffix}") |
| 82 |
|
| 83 |
count = usnic_settings.get('count', 0) |
| 84 |
cos = usnic_settings.get('cos', 5) |
| 85 |
|
| 86 |
if count < 0 or count > 225: |
| 87 |
module.fail_json(msg=f"USNIC count must be between 0 and 225{error_suffix}") |
| 88 |
if cos < 0 or cos > 6: |
| 89 |
module.fail_json(msg=f"USNIC CoS must be between 0 and 6{error_suffix}") |
| 90 |
|
| 91 |
|
| 92 |
def validate_vmq_settings(module, vmq_settings, name=None): |
| 93 |
""" |
| 94 |
Validate VMQ connection type settings. |
| 95 |
""" |
| 96 |
error_suffix = f" for {name}" if name else "" |
| 97 |
multi_queue_support = vmq_settings.get('multi_queue_support', False) |
| 98 |
|
| 99 |
if not multi_queue_support: |
| 100 |
num_interrupts = vmq_settings.get('num_interrupts', 16) |
| 101 |
num_vmqs = vmq_settings.get('num_vmqs', 4) |
| 102 |
|
| 103 |
if num_interrupts < 1 or num_interrupts > 514: |
| 104 |
module.fail_json(msg=f"VMQ num_interrupts must be between 1 and 514{error_suffix}") |
| 105 |
if num_vmqs < 1 or num_vmqs > 128: |
| 106 |
module.fail_json(msg=f"VMQ num_vmqs must be between 1 and 128{error_suffix}") |
| 107 |
else: |
| 108 |
num_sub_vnics = vmq_settings.get('num_sub_vnics', 64) |
| 109 |
if num_sub_vnics < 0 or num_sub_vnics > 64: |
| 110 |
module.fail_json(msg=f"VMQ num_sub_vnics must be between 0 and 64{error_suffix}") |
| 111 |
|
| 112 |
if not vmq_settings.get('vmmq_adapter_policy_name'): |
| 113 |
module.fail_json(msg=f"vmmq_adapter_policy_name is required when multi_queue_support is true{error_suffix}") |
| 114 |
|
| 115 |
|
| 116 |
def validate_sriov_settings(module, sriov_settings, name=None): |
| 117 |
""" |
| 118 |
Validate SR-IOV connection type settings. |
| 119 |
""" |
| 120 |
error_suffix = f" for {name}" if name else "" |
| 121 |
vf_count = sriov_settings.get('vf_count', 64) |
| 122 |
rx_count_per_vf = sriov_settings.get('rx_count_per_vf', 4) |
| 123 |
tx_count_per_vf = sriov_settings.get('tx_count_per_vf', 1) |
| 124 |
comp_count_per_vf = sriov_settings.get('comp_count_per_vf', 5) |
| 125 |
int_count_per_vf = sriov_settings.get('int_count_per_vf', 8) |
| 126 |
|
| 127 |
if vf_count < 1 or vf_count > 64: |
| 128 |
module.fail_json(msg=f"SR-IOV vf_count must be between 1 and 64{error_suffix}") |
| 129 |
if rx_count_per_vf < 1 or rx_count_per_vf > 8: |
| 130 |
module.fail_json(msg=f"SR-IOV rx_count_per_vf must be between 1 and 8{error_suffix}") |
| 131 |
if tx_count_per_vf < 1 or tx_count_per_vf > 8: |
| 132 |
module.fail_json(msg=f"SR-IOV tx_count_per_vf must be between 1 and 8{error_suffix}") |
| 133 |
if comp_count_per_vf < 1 or comp_count_per_vf > 16: |
| 134 |
module.fail_json(msg=f"SR-IOV comp_count_per_vf must be between 1 and 16{error_suffix}") |
| 135 |
if int_count_per_vf < 1 or int_count_per_vf > 16: |
| 136 |
module.fail_json(msg=f"SR-IOV int_count_per_vf must be between 1 and 16{error_suffix}") |
| 137 |
|
| 138 |
|
| 139 |
def build_usnic_settings(intersight, policy_cache, module, usnic_settings, organization_name): |
| 140 |
""" |
| 141 |
Build USNIC settings for API body. |
| 142 |
""" |
| 143 |
usnic_adapter_policy_name = usnic_settings.get('usnic_adapter_policy_name') |
| 144 |
usnic_adapter_policy_moid = get_policy_moid_with_org( |
| 145 |
intersight, policy_cache, module, '/vnic/EthAdapterPolicies', |
| 146 |
usnic_adapter_policy_name, organization_name, 'USNIC Adapter Policy' |
| 147 |
) |
| 148 |
|
| 149 |
return { |
| 150 |
'Count': usnic_settings.get('count', 0), |
| 151 |
'UsnicAdapterPolicy': usnic_adapter_policy_moid, |
| 152 |
'Cos': usnic_settings.get('cos', 5) |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
def build_vmq_settings(intersight, policy_cache, module, vmq_settings, organization_name): |
| 157 |
""" |
| 158 |
Build VMQ settings for API body. |
| 159 |
""" |
| 160 |
multi_queue_support = vmq_settings.get('multi_queue_support', False) |
| 161 |
|
| 162 |
if multi_queue_support: |
| 163 |
vmmq_adapter_policy_name = vmq_settings.get('vmmq_adapter_policy_name') |
| 164 |
vmmq_adapter_policy_moid = get_policy_moid_with_org( |
| 165 |
intersight, policy_cache, module, '/vnic/EthAdapterPolicies', |
| 166 |
vmmq_adapter_policy_name, organization_name, 'VMMQ Adapter Policy' |
| 167 |
) |
| 168 |
|
| 169 |
return { |
| 170 |
'Enabled': vmq_settings.get('enabled', True), |
| 171 |
'MultiQueueSupport': True, |
| 172 |
'NumSubVnics': vmq_settings.get('num_sub_vnics', 64), |
| 173 |
'VmmqAdapterPolicy': vmmq_adapter_policy_moid |
| 174 |
} |
| 175 |
else: |
| 176 |
return { |
| 177 |
'Enabled': vmq_settings.get('enabled', True), |
| 178 |
'MultiQueueSupport': False, |
| 179 |
'NumInterrupts': vmq_settings.get('num_interrupts', 16), |
| 180 |
'NumVmqs': vmq_settings.get('num_vmqs', 4) |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
def build_sriov_settings(sriov_settings): |
| 185 |
""" |
| 186 |
Build SR-IOV settings for API body. |
| 187 |
""" |
| 188 |
return { |
| 189 |
'VfCount': sriov_settings.get('vf_count', 64), |
| 190 |
'RxCountPerVf': sriov_settings.get('rx_count_per_vf', 4), |
| 191 |
'TxCountPerVf': sriov_settings.get('tx_count_per_vf', 1), |
| 192 |
'CompCountPerVf': sriov_settings.get('comp_count_per_vf', 5), |
| 193 |
'IntCountPerVf': sriov_settings.get('int_count_per_vf', 8), |
| 194 |
'Enabled': sriov_settings.get('enabled', True) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
def build_cdn_config(config_source): |
| 199 |
""" |
| 200 |
Build CDN configuration for API body. |
| 201 |
""" |
| 202 |
cdn_config = { |
| 203 |
'Source': config_source.get('cdn_source', 'vnic') |
| 204 |
} |
| 205 |
|
| 206 |
# Add CDN value if specified |
| 207 |
if config_source.get('cdn_value'): |
| 208 |
cdn_config['Value'] = config_source['cdn_value'] |
| 209 |
|
| 210 |
return cdn_config |
| 211 |
|
| 212 |
|
| 213 |
def get_common_policy_mappings(): |
| 214 |
""" |
| 215 |
Get common policy mappings shared between vNIC template and LAN connectivity policy. |
| 216 |
""" |
| 217 |
return { |
| 218 |
'eth_qos_policy_name': ('/vnic/EthQosPolicies', 'EthQosPolicy', 'Ethernet QoS Policy'), |
| 219 |
'eth_adapter_policy_name': ('/vnic/EthAdapterPolicies', 'EthAdapterPolicy', 'Ethernet Adapter Policy') |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
def get_iscsi_boot_policy_mapping(): |
| 224 |
""" |
| 225 |
Get iSCSI boot policy mapping (optional policy). |
| 226 |
""" |
| 227 |
return { |
| 228 |
'iscsi_boot_policy_name': ('/vnic/IscsiBootPolicies', 'IscsiBootPolicy', 'iSCSI Boot Policy') |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
def get_fabric_network_policy_mappings(): |
| 233 |
""" |
| 234 |
Get fabric network policy mappings for FI-attached configurations. |
| 235 |
""" |
| 236 |
return { |
| 237 |
'fabric_eth_network_group_policy_name': ( |
| 238 |
'/fabric/EthNetworkGroupPolicies', 'FabricEthNetworkGroupPolicy', 'Fabric Ethernet Network Group Policy' |
| 239 |
), |
| 240 |
'fabric_eth_network_control_policy_name': ( |
| 241 |
'/fabric/EthNetworkControlPolicies', 'FabricEthNetworkControlPolicy', 'Fabric Ethernet Network Control Policy' |
| 242 |
) |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
def get_mac_pool_policy_mapping(): |
| 247 |
""" |
| 248 |
Get MAC pool policy mapping. |
| 249 |
""" |
| 250 |
return { |
| 251 |
'mac_pool_name': ('/macpool/Pools', 'MacPool', 'MAC Pool') |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
def get_common_settings_argument_spec(): |
| 256 |
""" |
| 257 |
Get common connection settings argument specifications. |
| 258 |
""" |
| 259 |
return { |
| 260 |
'connection_type': dict(type='str', choices=['none', 'usnic', 'vmq', 'sriov'], default='none'), |
| 261 |
'iscsi_boot_policy_name': dict(type='str'), |
| 262 |
'fabric_eth_network_control_policy_name': dict(type='str'), |
| 263 |
'fabric_eth_network_group_policy_name': dict(type='str'), |
| 264 |
'failover_enabled': dict(type='bool', default=False), |
| 265 |
'switch_id': dict(type='str', choices=['A', 'B'], default='A'), |
| 266 |
'mac_pool_name': dict(type='str'), |
| 267 |
'cdn_value': dict(type='str'), |
| 268 |
'cdn_source': dict(type='str', choices=['vnic', 'user'], default='vnic'), |
| 269 |
'usnic_settings': dict(type='dict', options=dict( |
| 270 |
count=dict(type='int', default=0), |
| 271 |
cos=dict(type='int', choices=[0, 1, 2, 3, 4, 5, 6], default=5), |
| 272 |
usnic_adapter_policy_name=dict(type='str') |
| 273 |
)), |
| 274 |
'vmq_settings': dict(type='dict', options=dict( |
| 275 |
enabled=dict(type='bool', default=True), |
| 276 |
multi_queue_support=dict(type='bool', default=False), |
| 277 |
num_interrupts=dict(type='int', default=16), |
| 278 |
num_vmqs=dict(type='int', default=4), |
| 279 |
num_sub_vnics=dict(type='int', default=64), |
| 280 |
vmmq_adapter_policy_name=dict(type='str') |
| 281 |
)), |
| 282 |
'sriov_settings': dict(type='dict', options=dict( |
| 283 |
enabled=dict(type='bool', default=True), |
| 284 |
vf_count=dict(type='int', default=64), |
| 285 |
rx_count_per_vf=dict(type='int', default=4), |
| 286 |
tx_count_per_vf=dict(type='int', default=1), |
| 287 |
comp_count_per_vf=dict(type='int', default=5), |
| 288 |
int_count_per_vf=dict(type='int', default=8) |
| 289 |
)) |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
def resolve_policy_moids_from_mappings(intersight, policy_cache, module, config_source, policy_mappings, organization_name): |
| 294 |
""" |
| 295 |
Resolve policy MOIDs from configuration using provided policy mappings. |
| 296 |
""" |
| 297 |
policy_moids = {} |
| 298 |
|
| 299 |
for param_name, (resource_path, api_field, policy_type) in policy_mappings.items(): |
| 300 |
policy_name = config_source.get(param_name) |
| 301 |
if policy_name: |
| 302 |
policy_moid = get_policy_moid_with_org( |
| 303 |
intersight, policy_cache, module, resource_path, policy_name, organization_name, policy_type |
| 304 |
) |
| 305 |
# Special handling for FabricEthNetworkGroupPolicy which needs to be an array for the API |
| 306 |
if api_field == 'FabricEthNetworkGroupPolicy': |
| 307 |
policy_moids[api_field] = [policy_moid] |
| 308 |
else: |
| 309 |
policy_moids[api_field] = policy_moid |
| 310 |
|
| 311 |
return policy_moids |
| 312 |
|
| 313 |
|
| 314 |
def build_connection_settings(intersight, policy_cache, module, config_source, organization_name): |
| 315 |
""" |
| 316 |
Build connection type specific settings for API body. |
| 317 |
""" |
| 318 |
connection_settings = {} |
| 319 |
connection_type = config_source.get('connection_type', 'none') |
| 320 |
|
| 321 |
if connection_type == 'usnic': |
| 322 |
usnic_settings = config_source.get('usnic_settings', {}) |
| 323 |
connection_settings['UsnicSettings'] = build_usnic_settings(intersight, policy_cache, module, usnic_settings, organization_name) |
| 324 |
elif connection_type == 'vmq': |
| 325 |
vmq_settings = config_source.get('vmq_settings', {}) |
| 326 |
connection_settings['VmqSettings'] = build_vmq_settings(intersight, policy_cache, module, vmq_settings, organization_name) |
| 327 |
elif connection_type == 'sriov': |
| 328 |
sriov_settings = config_source.get('sriov_settings', {}) |
| 329 |
connection_settings['SriovSettings'] = build_sriov_settings(sriov_settings) |
| 330 |
|
| 331 |
return connection_settings |