-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a08116
commit 02ad362
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright (c) 2024 Paul Arthur | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
DOCUMENTATION = ''' | ||
name: to_uuid | ||
short_description: Convert a binary UUID to the string representation | ||
version_added: "1.1.0" | ||
author: | ||
- Paul Arthur (@flowerysong) | ||
options: | ||
_input: | ||
description: A base64 encoded binary value | ||
type: string | ||
required: true | ||
''' | ||
|
||
from base64 import b64decode | ||
from uuid import UUID | ||
|
||
from ansible.errors import AnsibleFilterError | ||
from ansible.module_utils.common.collections import is_string | ||
|
||
|
||
def convert_to_uuid(value): | ||
if not is_string(value): | ||
raise AnsibleFilterError(f'Invalid value type ({type(value)}) for to_uuid ({repr(value)})') | ||
|
||
try: | ||
uuid_bytes = b64decode(value) | ||
except ValueError as e: | ||
raise AnsibleFilterError(f'Unable to decode "{value}" as base64 in to_uuid: {e}') | ||
|
||
try: | ||
uuid = UUID(bytes=uuid_bytes) | ||
except ValueError as e: | ||
raise AnsibleFilterError(f'Invalid value ({value}) for to_uuid: {e}') | ||
|
||
return str(uuid) | ||
|
||
|
||
class FilterModule: | ||
def filters(self): | ||
return { | ||
'to_uuid': convert_to_uuid, | ||
} |