Skip to content
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

Handle data type or empty string in module_utils #1143

Merged
merged 5 commits into from
Dec 19, 2023

Conversation

rjeffman
Copy link
Member

Some parameters, in modules, have a specific data type, but allow the use of an empty string to clear the parameter.

By providing a method to retrieve the parameter with the correct data type, or optionally an empty string, allows for consistency of parameter handling between different modules.

Currently, ipapwpolicy and ipaidoverrideuser requires such method, and both were modified to use the module_utils implementation.

Comment on lines 297 to 324
maxlife = ansible_module.params_get_tipe(
"maxlife", int, allow_empty_string=True)
minlife = ansible_module.params_get_tipe(
"minlife", int, allow_empty_string=True)
history = ansible_module.params_get_tipe(
"history", int, allow_empty_string=True)
minclasses = ansible_module.params_get_tipe(
"minclasses", int, allow_empty_string=True)
minlength = ansible_module.params_get_tipe(
"minlength", int, allow_empty_string=True)
priority = ansible_module.params_get_tipe(
"priority", int, allow_empty_string=True)
maxfail = ansible_module.params_get_tipe(
"maxfail", int, allow_empty_string=True)
failinterval = ansible_module.params_get_tipe(
"failinterval", int, allow_empty_string=True)
lockouttime = ansible_module.params_get_tipe(
"lockouttime", int, allow_empty_string=True)
maxrepeat = ansible_module.params_get_tipe(
"maxrepeat", int, allow_empty_string=True)
maxsequence = ansible_module.params_get_tipe(
"maxsequence", int, allow_empty_string=True)
dictcheck = ansible_module.params_get_tipe(
"dictcheck", bool, allow_empty_string=True)
usercheck = ansible_module.params_get_tipe(
"usercheck", bool, allow_empty_string=True)
gracelimit = ansible_module.params_get_tipe(
"gracelimit", int, allow_empty_string=True)
Copy link
Member

@t-woerner t-woerner Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these should be ansible_module.params_get_type(..). About: allow_empty_string=True this is the wrong parameter right now. The current allow_empty_string needs to be renamed to something like allow_empty_list_item.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@@ -497,6 +497,49 @@ def module_params_get_lowercase(module, name, allow_empty_list_item=False):
return value


def module_params_get_type(
Copy link
Member

@t-woerner t-woerner Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module_params_get_type is misleading. Maybe something like module_params_get_with_type or module_params_get_converted or module_params_get_convert_to_type would be better.

@@ -1072,6 +1115,28 @@ def params_get_lowercase(self, name, allow_empty_list_item=False):
"""
return module_params_get_lowercase(self, name, allow_empty_list_item)

def params_get_type(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

The parameter 'allow_empty_string' in 'module_params_get' is used to
allow an item in a list to be an empty string. The problem is that the
naming is misleading, as it is checking a list item rather than a
string.

This patch rename the parameter to 'allow_empty_list_item' so that it
more clearly refers to list itens instead of standalone strings, and do
not collide with future parameters that may test for empty strings which
are not part of lists.
Copy link
Collaborator

@varunmylaraiah varunmylaraiah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Downstream tests are passed

@rjeffman rjeffman marked this pull request as draft December 12, 2023 20:53
@rjeffman
Copy link
Member Author

@t-woerner what do you think about calling the method "params_get_with_type_cast"?

Not only this is more explicitly tells what the method is doing, but it also alows for new usage, such as params_get_with_type_cast("aduser", ad_user_type), or params_get_with_type_cast("service", list_of(service(ipa_realm)).

I have the second example in a local "playground branch", and I'm getting fond of the solution for ensuring idempotence in the face of different value types.

(Note: I converted the PR to "draft" so that it is not merged before we are aligned on the method name.)

Some parameters, in modules, have a specific data type, but allow the
use of an empty string to clear the parameter.

By providing a method to retrieve the parameter with the correct data
type, or optionally an empty string, allows for consistency of parameter
handling between different modules.
Use the commom parameter type handling method for parameters that accept
a value or an empty string.
Use the commom parameter type handling method for parameters that accept
a value or an empty string.
Use the commom parameter type handling method for parameters that accept
a value or an empty string.
@rjeffman rjeffman marked this pull request as ready for review December 15, 2023 13:48
@rjeffman
Copy link
Member Author

rjeffman commented Dec 15, 2023

As discussed offline, I renamed the new method/function to *_get_with_type_cast, as it explicity says what it is doing, specially in the cases used by PR #1190 (which is based on these changes)

@t-woerner t-woerner merged commit 4e831b0 into freeipa:master Dec 19, 2023
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants