| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 11 |
'status': ['preview'], |
| 12 |
'supported_by': 'community'} |
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: intersight_uuid_pool |
| 17 |
short_description: UUID Pool configuration for Cisco Intersight |
| 18 |
description: |
| 19 |
- UUID Pool configuration for Cisco Intersight. |
| 20 |
- Used to configure UUID pools settings on Cisco Intersight managed devices. |
| 21 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs). |
| 22 |
extends_documentation_fragment: intersight |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- If C(present), will verify the resource is present and will create if needed. |
| 27 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 28 |
type: str |
| 29 |
choices: [present, absent] |
| 30 |
default: present |
| 31 |
organization: |
| 32 |
description: |
| 33 |
- The name of the Organization this resource is assigned to. |
| 34 |
- Profiles, Policies, and Pools that are created within a Custom Organization are applicable only to devices in the same Organization. |
| 35 |
type: str |
| 36 |
default: default |
| 37 |
name: |
| 38 |
description: |
| 39 |
- The name assigned to the UUID Pool. |
| 40 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
tags: |
| 44 |
description: |
| 45 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 46 |
type: list |
| 47 |
elements: dict |
| 48 |
description: |
| 49 |
description: |
| 50 |
- The user-defined description of the UUID Pool. |
| 51 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 52 |
type: str |
| 53 |
aliases: [descr] |
| 54 |
assignment_order: |
| 55 |
description: |
| 56 |
- Assignment order decides the order in which the next identifier is allocated. |
| 57 |
- If C(sequential), identifiers are assigned in a sequential order. |
| 58 |
- If C(default), the system determines the assignment order. |
| 59 |
type: str |
| 60 |
choices: [default, sequential] |
| 61 |
default: default |
| 62 |
prefix: |
| 63 |
description: |
| 64 |
- The UUID prefix used for all UUID suffix blocks. |
| 65 |
- UUID must be in uppercase in order to support idempotency. |
| 66 |
- The prefix must be in the format XXXXXXXX-XXXX-XXXX (8-4-4) where X is a hexadecimal character (0-9, A-F). |
| 67 |
- This prefix will be combined with the suffix blocks to form complete UUIDs. |
| 68 |
- Required for C(state=present) |
| 69 |
type: str |
| 70 |
uuid_suffix_blocks: |
| 71 |
description: |
| 72 |
- List of UUID suffix blocks that define the range of UUIDs available in the pool. |
| 73 |
- Each block defines a starting suffix and the number of consecutive UUIDs in that block. |
| 74 |
- Required for C(state=present) |
| 75 |
type: list |
| 76 |
elements: dict |
| 77 |
suboptions: |
| 78 |
from: |
| 79 |
description: |
| 80 |
- The starting UUID suffix for this block. |
| 81 |
- UUID must be in uppercase in order to support idempotency. |
| 82 |
- The suffix must be in the format XXXX-XXXXXXXXXXXX (4-12) where X is a hexadecimal character (0-9, A-F). |
| 83 |
- This suffix will be combined with the pool prefix to form complete UUIDs. |
| 84 |
type: str |
| 85 |
size: |
| 86 |
description: |
| 87 |
- The number of consecutive UUIDs in this block. |
| 88 |
- Must be a positive integer representing how many UUIDs are available starting from the 'from' suffix. |
| 89 |
type: int |
| 90 |
author: |
| 91 |
- Shahar Golshani (@sgolshan) |
| 92 |
''' |
| 93 |
|
| 94 |
EXAMPLES = r''' |
| 95 |
- name: Configure UUID Pool |
| 96 |
cisco.intersight.intersight_uuid_pool: |
| 97 |
api_private_key: "{{ api_private_key }}" |
| 98 |
api_key_id: "{{ api_key_id }}" |
| 99 |
organization: DevNet |
| 100 |
name: lab-uuid-pool |
| 101 |
description: UUID Pool for lab use |
| 102 |
assignment_order: sequential |
| 103 |
prefix: "550E8400-E29B-41D4" |
| 104 |
uuid_suffix_blocks: |
| 105 |
- from: "A716-446655440000" |
| 106 |
size: 100 |
| 107 |
- from: "A716-446655441000" |
| 108 |
size: 200 |
| 109 |
tags: |
| 110 |
- Key: Site |
| 111 |
Value: RCDN |
| 112 |
|
| 113 |
- name: Delete UUID Pool |
| 114 |
cisco.intersight.intersight_uuid_pool: |
| 115 |
api_private_key: "{{ api_private_key }}" |
| 116 |
api_key_id: "{{ api_key_id }}" |
| 117 |
organization: DevNet |
| 118 |
name: lab-uuid-pool |
| 119 |
state: absent |
| 120 |
''' |
| 121 |
|
| 122 |
RETURN = r''' |
| 123 |
api_response: |
| 124 |
description: The API response output returned by the specified resource. |
| 125 |
returned: always |
| 126 |
type: dict |
| 127 |
sample: |
| 128 |
"api_response": { |
| 129 |
"AccountMoid": "", |
| 130 |
"Ancestors": [], |
| 131 |
"Assigned": 0, |
| 132 |
"AssignmentOrder": "sequential", |
| 133 |
"ClassId": "uuidpool.Pool", |
| 134 |
"CreateTime": "", |
| 135 |
"Description": "UUID Pool for lab use", |
| 136 |
"DomainGroupMoid": "", |
| 137 |
"ModTime": "", |
| 138 |
"Moid": "", |
| 139 |
"Name": "lab-uuid-pool", |
| 140 |
"ObjectType": "uuidpool.Pool", |
| 141 |
"Organization": {}, |
| 142 |
"Owners": [], |
| 143 |
"PermissionResources": [], |
| 144 |
"Prefix": "550E8400-E29B-41D4", |
| 145 |
"Reservations": [], |
| 146 |
"Reserved": 0, |
| 147 |
"ShadowPools": [], |
| 148 |
"SharedScope": "", |
| 149 |
"Size": 300, |
| 150 |
"Tags": [ |
| 151 |
{ |
| 152 |
"Key": "Site", |
| 153 |
"Value": "RCDN" |
| 154 |
} |
| 155 |
], |
| 156 |
"UuidSuffixBlocks": [ |
| 157 |
{ |
| 158 |
"From": "A716-446655440000", |
| 159 |
"Size": 100 |
| 160 |
}, |
| 161 |
{ |
| 162 |
"From": "A716-446655441000", |
| 163 |
"Size": 200 |
| 164 |
} |
| 165 |
] |
| 166 |
} |
| 167 |
''' |
| 168 |
|
| 169 |
|
| 170 |
import re |
| 171 |
from ansible.module_utils.basic import AnsibleModule |
| 172 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 173 |
|
| 174 |
|
| 175 |
def validate_uuid_format(uuid_string, field_sizes): |
| 176 |
""" |
| 177 |
Validate UUID format with flexible field sizes |
| 178 |
where x represents a hexadecimal character (0-9, A-F) |
| 179 |
Args: |
| 180 |
uuid_string (str): The UUID string to validate |
| 181 |
field_sizes (list): List of integers representing the size of each field |
| 182 |
(e.g., [4, 4, 8] for XXXX-XXXX-XXXXXXXX format) |
| 183 |
Returns: |
| 184 |
bool: True if valid, False otherwise |
| 185 |
""" |
| 186 |
if not uuid_string or not field_sizes: |
| 187 |
return False |
| 188 |
|
| 189 |
# Build pattern dynamically based on field sizes |
| 190 |
pattern_parts = [] |
| 191 |
for size in field_sizes: |
| 192 |
pattern_parts.append(f'[0-9A-F]{{{size}}}') |
| 193 |
|
| 194 |
pattern = f'^{"-".join(pattern_parts)}$' |
| 195 |
return bool(re.match(pattern, uuid_string)) |
| 196 |
|
| 197 |
|
| 198 |
def main(): |
| 199 |
argument_spec = intersight_argument_spec.copy() |
| 200 |
argument_spec.update( |
| 201 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 202 |
organization=dict(type='str', default='default'), |
| 203 |
name=dict(type='str', required=True), |
| 204 |
description=dict(type='str', aliases=['descr']), |
| 205 |
tags=dict(type='list', elements='dict'), |
| 206 |
assignment_order=dict(type='str', choices=['default', 'sequential'], default='default'), |
| 207 |
prefix=dict(type='str'), |
| 208 |
uuid_suffix_blocks=dict(type='list', elements='dict'), |
| 209 |
) |
| 210 |
|
| 211 |
module = AnsibleModule( |
| 212 |
argument_spec, |
| 213 |
supports_check_mode=True, |
| 214 |
required_if=[ |
| 215 |
('state', 'present', ['prefix', 'uuid_suffix_blocks']), |
| 216 |
], |
| 217 |
) |
| 218 |
|
| 219 |
# Validate UUID prefix format (xxxx-xxxx-xxxxxxxx) |
| 220 |
if module.params['prefix'] and not validate_uuid_format(module.params['prefix'], [8, 4, 4]): |
| 221 |
module.fail_json(msg="Invalid UUID prefix format. Expected: XXXXXXXX-XXXX-XXXX (where X is a hexadecimal character)") |
| 222 |
|
| 223 |
# Validate UUID suffix blocks (xxxx-xxxxxxxxxxxx) |
| 224 |
if module.params['uuid_suffix_blocks']: |
| 225 |
for uuid_block in module.params['uuid_suffix_blocks']: |
| 226 |
if 'from' in uuid_block and uuid_block['from'] and not validate_uuid_format(uuid_block['from'], [4, 12]): |
| 227 |
module.fail_json(msg="Invalid UUID suffix format in uuid_suffix_blocks. Expected: XXXX-XXXXXXXXXXXX (where X is a hexadecimal character)") |
| 228 |
|
| 229 |
intersight = IntersightModule(module) |
| 230 |
intersight.result['api_response'] = {} |
| 231 |
intersight.result['trace_id'] = '' |
| 232 |
|
| 233 |
# Resource path used to configure pool |
| 234 |
resource_path = '/uuidpool/Pools' |
| 235 |
# Define API body used in compares or create |
| 236 |
intersight.api_body = { |
| 237 |
'Organization': { |
| 238 |
'Name': intersight.module.params['organization'], |
| 239 |
}, |
| 240 |
'Name': intersight.module.params['name'], |
| 241 |
|
| 242 |
} |
| 243 |
if module.params['state'] == 'present': |
| 244 |
intersight.set_tags_and_description() |
| 245 |
intersight.api_body['AssignmentOrder'] = intersight.module.params['assignment_order'] |
| 246 |
intersight.api_body['Prefix'] = intersight.module.params['prefix'] |
| 247 |
UuidSuffixBlocks = [] |
| 248 |
for uuid_block in intersight.module.params['uuid_suffix_blocks']: |
| 249 |
block = {} |
| 250 |
block['From'] = uuid_block['from'] |
| 251 |
block['Size'] = uuid_block['size'] |
| 252 |
UuidSuffixBlocks.append(block) |
| 253 |
intersight.api_body['UuidSuffixBlocks'] = UuidSuffixBlocks |
| 254 |
|
| 255 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 256 |
|
| 257 |
module.exit_json(**intersight.result) |
| 258 |
|
| 259 |
|
| 260 |
if __name__ == '__main__': |
| 261 |
main() |