-
Notifications
You must be signed in to change notification settings - Fork 45
[Filter][2165]filter_for_data_set_name #2390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
b8c982b
411f8d0
400c79a
cc2bc29
491c971
2580b14
98c9410
3d1b82b
c07d41f
c63ded2
8ae1f98
97069f2
eff0c5e
0577d52
196f559
1f8f9d0
e7bd73f
3002125
65e19a3
4042f97
31f455b
bfb4709
31b0a9d
8ddc145
5be31c4
13cc71b
c36eb5e
9d183fd
5785fac
de45adf
40924ee
2cbc016
e6b71d2
77064ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # Copyright (c) IBM Corporation 2025 | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import secrets | ||
| import string | ||
| import re | ||
| from ansible.errors import AnsibleFilterError | ||
|
|
||
|
|
||
| def generate_data_set_name(value, generations=1): | ||
| """Filter to generate valid data set names | ||
|
|
||
| Args: | ||
| value {str} -- value of high level qualifier to use on data set names | ||
| generations {int, optional} -- number of dataset names to generate. Defaults to 1. | ||
|
|
||
| Returns: | ||
| list -- the total dataset names valid | ||
| """ | ||
| if len(value) > 8: | ||
| raise AnsibleFilterError("The high level qualifier is too long for the data set name") | ||
|
|
||
| if generations > 1: | ||
| dataset_names = [] | ||
| for generation in range(generations): | ||
| name = value + get_tmp_ds_name() | ||
| dataset_names.append(name) | ||
| else: | ||
| dataset_names = value + get_tmp_ds_name() | ||
|
|
||
| return dataset_names | ||
|
|
||
|
|
||
| def get_tmp_ds_name(): | ||
| """Unify the random qualifiers generate in one name. | ||
AndreMarcel99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Returns: | ||
| str: valid data set name | ||
| """ | ||
| ds = "." | ||
| ds += "P" + get_random_q() + "." | ||
| ds += "T" + get_random_q() + "." | ||
| ds += "C" + get_random_q() | ||
| return ds | ||
|
|
||
|
|
||
| def get_random_q(): | ||
| """ Function or test to ensure random hlq of datasets""" | ||
| # Generate the first random hlq of size pass as parameter | ||
| letters = string.ascii_uppercase + string.digits | ||
| random_q = ''.join(secrets.choice(letters)for iteration in range(7)) | ||
| count = 0 | ||
| # Generate a random HLQ and verify if is valid, if not, repeat the process | ||
| while count < 5 and not re.fullmatch( | ||
| r"^(?:[A-Z$#@]{1}[A-Z0-9$#@-]{0,7})", | ||
| random_q, | ||
| re.IGNORECASE, | ||
| ): | ||
| random_q = ''.join(secrets.choice(letters)for iteration in range(7)) | ||
| count += 1 | ||
| return random_q | ||
|
|
||
|
|
||
| class FilterModule(object): | ||
| """ Jinja2 filter for the returned list or string by the collection module. """ | ||
| def filters(self): | ||
| return { | ||
| 'generate_data_set_name': generate_data_set_name | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Copyright (c) IBM Corporation 2025 | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| DOCUMENTATION: | ||
| name: generate_data_set_name | ||
| author: Marcel Gutierrez (@AndreMarcel99) | ||
| version_added: "2.0.0" | ||
| short_description: Filter returned valid data set names | ||
| description: | ||
| - Provide a valid temporary data set name. | ||
| options: | ||
| value: | ||
| description: | ||
| - High level qualifier. | ||
| type: str | ||
| required: true | ||
| generations: | ||
| description: | ||
| - Number of data set names that you require to generate. | ||
| type: int | ||
| required: false | ||
|
|
||
| EXAMPLES: | | ||
| # Get only one data set name. | ||
| clean_output: "{{ hlq | ibm.ibm_zos_core.generate_data_set_name}}" | ||
|
||
| # Get 10 data set names. | ||
| clean_output: "{{ hlq | ibm.ibm_zos_core.generate_data_set_name(10)}}" | ||
|
|
||
| RETURN: | ||
| _value: | ||
| description: Name or names generated by the filter. | ||
| type: list | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this!!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update the copyright year |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good, would only add one testing when is empty (if possible even) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # Copyright (c) IBM Corporation 2025 | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import absolute_import, division, print_function | ||
|
|
||
| __metaclass__ = type | ||
|
|
||
| import pytest | ||
|
|
||
| def test_generate_data_set_name_filter(ansible_zos_module): | ||
| hosts = ansible_zos_module | ||
| input_string = "OMVSADM" | ||
| hosts.all.set_fact(input_string=input_string) | ||
| results = hosts.all.debug(msg="{{ input_string | generate_data_set_name }}") | ||
|
|
||
| for result in results.contacted.values(): | ||
| assert result.get('msg') is not None | ||
| assert input_string in result.get('msg') | ||
|
|
||
| def test_generate_data_set_name_filter_multiple_generations(ansible_zos_module): | ||
| hosts = ansible_zos_module | ||
| input_string = "OMVSADM" | ||
| hosts.all.set_fact(input_string=input_string) | ||
| results = hosts.all.debug(msg="{{ input_string | generate_data_set_name(10) }}") | ||
|
|
||
| for result in results.contacted.values(): | ||
| assert result.get('msg') is not None | ||
| assert input_string in result.get('msg')[0] | ||
| assert len(result.get('msg')) == 10 | ||
|
|
||
| def test_generate_data_set_name_filter_bad_hl1(ansible_zos_module): | ||
| hosts = ansible_zos_module | ||
| input_string = "OMVSADMONE" | ||
| hosts.all.set_fact(input_string=input_string) | ||
| results = hosts.all.debug(msg="{{ input_string | generate_data_set_name }}") | ||
|
|
||
| for result in results.contacted.values(): | ||
| assert result.get('failed') is True | ||
| assert result.get('msg') == "The high level qualifier is too long for the data set name" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first sight generations might seem to be referring to a different thing, it will be better to use something like
countornum_names