Skip to content

Commit

Permalink
chore: Reducing complexity of qpc/cred/utils.py get_password
Browse files Browse the repository at this point in the history
Reducing the complexity of get_password by breaking out the logic
for the individual credential attributes into their own functions.
  • Loading branch information
abellotti committed Jun 15, 2023
1 parent f3e4039 commit 272a570
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
5 changes: 4 additions & 1 deletion qpc/cred/test_openshift_cred_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def test_add_no_token(
CLI().main()
out, err = capsys.readouterr()
assert out == ""
assert "one of the arguments --password --sshkeyfile --token is required" in err
assert (
"one of the arguments --password --sshkeyfile"
" --sshkeyvalue --token is required" in err
)

def test_add_no_name(
self,
Expand Down
45 changes: 39 additions & 6 deletions qpc/cred/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,76 @@


def get_password(args, req_payload, add_none=True):
"""Collect the password value and place in credential dictionary.
"""Collect the credential attribute values and place in credential dictionary.
:param args: the command line arguments
:param req_payload: the dictionary for the request
:param add_none: add None for a key if True vs. not in dictionary
:returns: the updated dictionary
"""
req_payload = _get_password_value(args, req_payload, add_none)
req_payload = _get_ssh_keyvalue_value(args, req_payload, add_none)
req_payload = _get_ssh_passphrase_value(args, req_payload, add_none)
req_payload = _get_become_password_value(args, req_payload, add_none)
req_payload = _get_token_value(args, req_payload, add_none)

return req_payload


def _get_password_value(args, req_payload, add_none):
"""Collect the password value and place in credential dictionary."""
if "password" in args and args.password:
print(_(messages.CONN_PASSWORD))
pass_prompt = getpass()
check_if_prompt_is_not_empty(pass_prompt)
req_payload["password"] = pass_prompt
elif add_none:
req_payload["password"] = None

return req_payload


def _get_ssh_keyvalue_value(args, req_payload, add_none):
"""Collect the ssh_keyvalue value and place in credential dictionary."""
if "ssh_keyvalue" in args and args.ssh_keyvalue:
print(_(messages.SSH_KEYVALUE))
ssh_keyvalue_prompt = get_multiline_pass(
prompt=f"{messages.SSH_KEYVALUE_PROMPT}"
)
check_if_prompt_is_not_empty(ssh_keyvalue_prompt)
req_payload["ssh_keyvalue"] = ssh_keyvalue_prompt
pass_prompt = get_multiline_pass(prompt=f"{messages.SSH_KEYVALUE_PROMPT}")
check_if_prompt_is_not_empty(pass_prompt)
req_payload["ssh_keyvalue"] = pass_prompt
elif add_none:
req_payload["ssh_keyvalue"] = None

return req_payload


def _get_ssh_passphrase_value(args, req_payload, add_none):
"""Collect the ssh_passphrase value and place in credential dictionary."""
if "ssh_passphrase" in args and args.ssh_passphrase:
print(_(messages.SSH_PASSPHRASE))
pass_prompt = getpass()
check_if_prompt_is_not_empty(pass_prompt)
req_payload["ssh_passphrase"] = pass_prompt
elif add_none:
req_payload["ssh_passphrase"] = None

return req_payload


def _get_become_password_value(args, req_payload, add_none):
"""Collect the become_password value and place in credential dictionary."""
if "become_password" in args and args.become_password:
print(_(messages.BECOME_PASSWORD))
pass_prompt = getpass()
check_if_prompt_is_not_empty(pass_prompt)
req_payload["become_password"] = pass_prompt
elif add_none:
req_payload["become_password"] = None

return req_payload


def _get_token_value(args, req_payload, add_none):
"""Collect the token value and place in credential dictionary."""
if getattr(args, "token", None):
token_prompt = getpass(messages.AUTH_TOKEN)
check_if_prompt_is_not_empty(token_prompt)
Expand Down

0 comments on commit 272a570

Please sign in to comment.