forked from ansible-collections/community.crypto
-
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.
Add conversion filters for serial numbers (ansible-collections#713)
* Refactoring. * Add parse_filter and to_filter plugins. * Mention filters when serial numbers are accepted or returned.
- Loading branch information
1 parent
5159189
commit 6b1a3d6
Showing
27 changed files
with
500 additions
and
55 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
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
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
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,66 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2024, Felix Fontein <[email protected]> | ||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
DOCUMENTATION = """ | ||
name: parse_serial | ||
short_description: Convert a serial number as a colon-separated list of hex numbers to an integer | ||
author: Felix Fontein (@felixfontein) | ||
version_added: 2.18.0 | ||
description: | ||
- "Parses a colon-separated list of hex numbers of the form C(00:11:22:33) and returns the corresponding integer." | ||
options: | ||
_input: | ||
description: | ||
- A serial number represented as a colon-separated list of hex numbers between 0 and 255. | ||
- These numbers are interpreted as the byte presentation of an unsigned integer in network byte order. | ||
That is, C(01:00) is interpreted as the integer 256. | ||
type: string | ||
required: true | ||
seealso: | ||
- plugin: community.crypto.to_serial | ||
plugin_type: filter | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: Parse serial number | ||
ansible.builtin.debug: | ||
msg: "{{ '11:22:33' | community.crypto.parse_serial }}" | ||
""" | ||
|
||
RETURN = """ | ||
_value: | ||
description: | ||
- The serial number as an integer. | ||
type: int | ||
""" | ||
|
||
from ansible.errors import AnsibleFilterError | ||
from ansible.module_utils.common.text.converters import to_native | ||
from ansible.module_utils.six import string_types | ||
|
||
from ansible_collections.community.crypto.plugins.module_utils.serial import parse_serial | ||
|
||
|
||
def parse_serial_filter(input): | ||
if not isinstance(input, string_types): | ||
raise AnsibleFilterError( | ||
'The input for the community.crypto.parse_serial filter must be a string; got {type} instead'.format(type=type(input)) | ||
) | ||
try: | ||
return parse_serial(to_native(input)) | ||
except ValueError as exc: | ||
raise AnsibleFilterError(to_native(exc)) | ||
|
||
|
||
class FilterModule(object): | ||
'''Ansible jinja2 filters''' | ||
|
||
def filters(self): | ||
return { | ||
'parse_serial': parse_serial_filter, | ||
} |
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,68 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2024, Felix Fontein <[email protected]> | ||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
DOCUMENTATION = """ | ||
name: to_serial | ||
short_description: Convert an integer to a colon-separated list of hex numbers | ||
author: Felix Fontein (@felixfontein) | ||
version_added: 2.18.0 | ||
description: | ||
- "Converts an integer to a colon-separated list of hex numbers of the form C(00:11:22:33)." | ||
options: | ||
_input: | ||
description: | ||
- The non-negative integer to convert. | ||
type: int | ||
required: true | ||
seealso: | ||
- plugin: community.crypto.to_serial | ||
plugin_type: filter | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: Convert integer to serial number | ||
ansible.builtin.debug: | ||
msg: "{{ 1234567 | community.crypto.to_serial }}" | ||
""" | ||
|
||
RETURN = """ | ||
_value: | ||
description: | ||
- A colon-separated list of hexadecimal numbers. | ||
- Letters are upper-case, and all numbers have exactly two digits. | ||
- The string is never empty. The representation of C(0) is C("00"). | ||
type: string | ||
""" | ||
|
||
from ansible.errors import AnsibleFilterError | ||
from ansible.module_utils.common.text.converters import to_native | ||
from ansible.module_utils.six import integer_types | ||
|
||
from ansible_collections.community.crypto.plugins.module_utils.serial import to_serial | ||
|
||
|
||
def to_serial_filter(input): | ||
if not isinstance(input, integer_types): | ||
raise AnsibleFilterError( | ||
'The input for the community.crypto.to_serial filter must be an integer; got {type} instead'.format(type=type(input)) | ||
) | ||
if input < 0: | ||
raise AnsibleFilterError('The input for the community.crypto.to_serial filter must not be negative') | ||
try: | ||
return to_serial(input) | ||
except ValueError as exc: | ||
raise AnsibleFilterError(to_native(exc)) | ||
|
||
|
||
class FilterModule(object): | ||
'''Ansible jinja2 filters''' | ||
|
||
def filters(self): | ||
return { | ||
'to_serial': to_serial_filter, | ||
} |
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
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
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
Oops, something went wrong.