|
| 1 | +import logging |
| 2 | + |
| 3 | +import click |
| 4 | +import requests |
| 5 | + |
| 6 | +from cromshell.utilities import cromshellconfig, http_utils, io_utils |
| 7 | + |
| 8 | +LOGGER = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +@click.command(name="metadata") |
| 12 | +@click.argument("workflow_id") |
| 13 | +@click.option( |
| 14 | + "-des", |
| 15 | + "--dont-expand-subworkflows", |
| 16 | + is_flag=True, |
| 17 | + default=False, |
| 18 | + help="Do not expand subworkflow info in metadata", |
| 19 | +) |
| 20 | +@click.pass_obj |
| 21 | +def main(config, workflow_id: str, dont_expand_subworkflows: bool): |
| 22 | + """Get the full metadata of a workflow.""" |
| 23 | + |
| 24 | + LOGGER.info("metadata") |
| 25 | + |
| 26 | + check_cromwell_server(config=config, workflow_id=workflow_id) |
| 27 | + |
| 28 | + obtain_and_print_metadata( |
| 29 | + config=config, |
| 30 | + metadata_param=config.METADATA_KEYS_TO_OMIT, |
| 31 | + exclude_keys=True, |
| 32 | + dont_expand_subworkflows=dont_expand_subworkflows, |
| 33 | + ) |
| 34 | + |
| 35 | + return 0 |
| 36 | + |
| 37 | + |
| 38 | +def check_cromwell_server(config, workflow_id): |
| 39 | + """Checks for an associated cromwell server for the workflow_id |
| 40 | + and checks connection with the cromwell server""" |
| 41 | + |
| 42 | + # Overrides the default cromwell url set in the cromshell config file or |
| 43 | + # command line argument if the workflow id is found in the submission file. |
| 44 | + cromshellconfig.resolve_cromwell_config_server_address(workflow_id=workflow_id) |
| 45 | + |
| 46 | + config.cromwell_api_workflow_id = f"{config.get_cromwell_api()}/{workflow_id}" |
| 47 | + |
| 48 | + # Check if Cromwell Server Backend works |
| 49 | + http_utils.assert_can_communicate_with_server(config) |
| 50 | + |
| 51 | + |
| 52 | +def format_metadata_params( |
| 53 | + list_of_keys: list, exclude_keys: bool, expand_subworkflows: bool |
| 54 | +) -> dict: |
| 55 | + """This functions organises a list of cromwell metadata keys and flags into a |
| 56 | + dictionary that can be passed to requests library""" |
| 57 | + |
| 58 | + if not list_of_keys: |
| 59 | + LOGGER.error("No keys provided when querying metadata parameter.") |
| 60 | + raise ValueError("No keys provided when querying metadata parameter.") |
| 61 | + elif "" in list_of_keys: |
| 62 | + LOGGER.error("One of the provided metadata keys is empty.") |
| 63 | + raise ValueError("One of the provided metadata keys is empty.") |
| 64 | + else: |
| 65 | + # Determines whether the list of keys will be used to exclude or |
| 66 | + # include fields in the metadata. |
| 67 | + key_action = "excludeKey" if exclude_keys else "includeKey" |
| 68 | + |
| 69 | + final_key = {key_action: list_of_keys} |
| 70 | + |
| 71 | + if expand_subworkflows: |
| 72 | + final_key["expandSubWorkflows"] = "true" |
| 73 | + |
| 74 | + return final_key |
| 75 | + |
| 76 | + |
| 77 | +def get_workflow_metadata( |
| 78 | + meta_params: dict, |
| 79 | + api_workflow_id: str, |
| 80 | + timeout: int, |
| 81 | + verify_certs: bool, |
| 82 | +) -> str: |
| 83 | + """Uses requests to get the metadata or sub-metadata of a workflow |
| 84 | + from the cromwell server and returns a JSON formatted string.""" |
| 85 | + |
| 86 | + requests_out = requests.get( |
| 87 | + f"{api_workflow_id}/metadata", |
| 88 | + params=meta_params, |
| 89 | + timeout=timeout, |
| 90 | + verify=verify_certs, |
| 91 | + ) |
| 92 | + |
| 93 | + http_utils.check_http_request_status_code( |
| 94 | + short_error_message="Failed to get metadata", response=requests_out |
| 95 | + ) |
| 96 | + |
| 97 | + return requests_out.json() |
| 98 | + |
| 99 | + |
| 100 | +def obtain_and_print_metadata( |
| 101 | + config, metadata_param: list, exclude_keys: bool, dont_expand_subworkflows: bool |
| 102 | +): |
| 103 | + """Format metadata parameters and obtains metadata from cromwell server""" |
| 104 | + |
| 105 | + # Combine keys and flags into a dictionary |
| 106 | + formatted_metadata_parameter = format_metadata_params( |
| 107 | + list_of_keys=metadata_param, |
| 108 | + exclude_keys=exclude_keys, |
| 109 | + expand_subworkflows=not dont_expand_subworkflows, # Invert variable |
| 110 | + ) |
| 111 | + |
| 112 | + # Request workflow metadata |
| 113 | + workflow_metadata_json = get_workflow_metadata( |
| 114 | + meta_params=formatted_metadata_parameter, |
| 115 | + api_workflow_id=config.cromwell_api_workflow_id, |
| 116 | + timeout=config.requests_connect_timeout, |
| 117 | + verify_certs=config.requests_verify_certs, |
| 118 | + ) |
| 119 | + |
| 120 | + io_utils.pretty_print_json(workflow_metadata_json, add_color=True) |
0 commit comments