Handling PKS Actions Flow
This document explains the flow of handling various PKS-related actions in the plugins/modules/hdcrypt_pks.py
module. The process starts with initializing the Ansible module and retrieving the action parameter. Depending on the user-specified action, the appropriate function is called to handle operations such as adding, showing, cleaning, importing, or exporting PKS keys.
The flow begins by initializing the Ansible module and getting the action parameter from the user. If PKS is not enabled or activated, an error message is returned. If PKS is enabled, the flow proceeds based on the action specified by the user. For example, if the action is 'clean', the pksclean
pksshow
Flow drill down
Handling different actions based on user input
First, the main
def main():
module = AnsibleModule(
supports_check_mode=True,
argument_spec=dict(
action=dict(type='str', choices=['addpks', 'show', 'clean', 'import', 'export'], required=True),
device=dict(type='str', default=""),
method_name=dict(type='str', default="initpks"),
pks_label=dict(type='str'),
location=dict(type='str'),
passphrase=dict(type='str', default=""),
),
)
action = module.params['action']
if not is_pks_enabled(module):
results['msg'] = "PKS is not supported or PKS is not activated."
module.fail_json(**results)
version = find_version(module)
Cleaning invalid PKS keys
Next, the pksclean
def pksclean(module):
"""
Cleans invalid PKS keys
arguments:
module - The generic ansible module
returns:
success_msg (str) - In case of success
fail_msg (str) - In case of failure
"""
success_msg = "Successfully cleaned invalid PKS keys"
fail_msg = "Could not clean invalid PKS keys"
pks_label = module.params['pks_label']
if not pks_label:
results['msg'] = "You must specify the PKS label that is associated with the invalid key that you want to remove."
module.fail_json(**results)
validate_label(module, pks_label)
cmd = "hdcryptmgr pksclean " + pks_label
Validating PKS label
Then, the validate_label
def validate_label(module, id):
"""
Utility function to check if the provided id is valid or not
arguments:
module (dict) - The Ansible module
id (str) - id that needs to be validated
returns:
Nothing
Note:
Fails if the key is not present in PKS storage or is a valid key
"""
results['msg'] = pksshow(module)
pksshow_res = results['pksshow_results']
if id not in pksshow_res["PKS_Label (LVid)"].keys():
results['msg'] = "The provided id is not present in PKS Storage."
module.fail_json(**results)
Displaying PKS labels and status
Finally, the pksshow
def pksshow(module):
"""
Displays the PKS label of volume that is associated with the PKS keys and the status of the PKS keys.
arguements:
module - The generic ansible module
returns:
success_msg: If the command runs successfully, success message is returned.
"""
cmd = "hdcryptmgr pksshow"
success_msg = "Successfully fetched PKS keys, labels and their status, check pksshow_results"
fail_msg = "Could not fetch pksshow's output"
rc, stdout, stderr = module.run_command(cmd)
if not rc:
results['stdout'] = stdout
results['msg'] = success_msg
else:
results['stderr'] = stderr
results['msg'] = fail_msg
This is an auto-generated document by Swimm 🌊 and has not yet been verified by a human