From c34bcf2b3ca4d8646907e8c11f56b636b6ef265e Mon Sep 17 00:00:00 2001 From: ejseqera Date: Thu, 1 Aug 2024 21:32:20 -0400 Subject: [PATCH 1/2] feat: add cli flag to specify targets re #148 --- seqerakit/cli.py | 11 ++++++++++- seqerakit/helper.py | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/seqerakit/cli.py b/seqerakit/cli.py index 81798a0..e9bcbdf 100644 --- a/seqerakit/cli.py +++ b/seqerakit/cli.py @@ -80,6 +80,13 @@ def parse_args(args=None): help="Additional Seqera Platform CLI specific options to be passed," " enclosed in double quotes (e.g. '--cli=\"--insecure\"').", ) + yaml_processing.add_argument( + "--targets", + dest="targets", + type=str, + help="Specify the resources to be targeted for creation in a YAML file through " + "a comma-separated list (e.g. '--targets=teams,participants').", + ) return parser.parse_args(args) @@ -183,7 +190,9 @@ def main(args=None): # Parse the YAML file(s) by blocks # and get a dictionary of command line arguments try: - cmd_args_dict = helper.parse_all_yaml(options.yaml, destroy=options.delete) + cmd_args_dict = helper.parse_all_yaml( + options.yaml, destroy=options.delete, targets=options.targets + ) for block, args_list in cmd_args_dict.items(): for args in args_list: block_manager.handle_block( diff --git a/seqerakit/helper.py b/seqerakit/helper.py index c4792af..86a71a1 100644 --- a/seqerakit/helper.py +++ b/seqerakit/helper.py @@ -55,7 +55,7 @@ def parse_yaml_block(yaml_data, block_name): return block_name, cmd_args_list -def parse_all_yaml(file_paths, destroy=False): +def parse_all_yaml(file_paths, destroy=False, targets=None): # If multiple yamls, merge them into one dictionary merged_data = {} @@ -108,6 +108,11 @@ def parse_all_yaml(file_paths, destroy=False): block_names = list(merged_data.keys()) + # Filter blocks based on targets if provided + if targets: + target_blocks = set(targets.split(",")) + block_names = [block for block in block_names if block in target_blocks] + # Define the order in which the resources should be created. resource_order = [ "organizations", From 3a87f4435bc1705abdb342497e96df432106fee6 Mon Sep 17 00:00:00 2001 From: ejseqera Date: Thu, 1 Aug 2024 21:37:39 -0400 Subject: [PATCH 2/2] test: add unit tests --- tests/unit/test_helper.py | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/unit/test_helper.py b/tests/unit/test_helper.py index 76f5b55..73ce6d5 100644 --- a/tests/unit/test_helper.py +++ b/tests/unit/test_helper.py @@ -442,3 +442,73 @@ def test_error_duplicate_name_yaml_file(mock_yaml_file): "compute-envs: test_computeenv. Please specify " "a unique value." in str(e.value) ) + + +def test_targets_specified(): + # mock YAML data + yaml_data = """ +organizations: + - name: org1 + description: Organization 1 +workspaces: + - name: workspace1 + organization: org1 + description: Workspace 1 +pipelines: + - name: pipeline1 + workspace: workspace1 + description: Pipeline 1 +""" + with patch("builtins.open", lambda f, _: StringIO(yaml_data)): + result = helper.parse_all_yaml( + ["dummy_path.yaml"], targets="organizations,workspaces" + ) + + expected_organizations_output = [ + { + "cmd_args": ["--name", "org1", "--description", "Organization 1"], + "overwrite": False, + } + ] + expected_workspaces_output = [ + { + "cmd_args": [ + "--name", + "workspace1", + "--organization", + "org1", + "--description", + "Workspace 1", + ], + "overwrite": False, + } + ] + # Check that only 'organizations' and 'workspaces' are in the result + assert "organizations" in result + assert result["organizations"] == expected_organizations_output + assert "workspaces" in result + assert result["workspaces"] == expected_workspaces_output + assert "pipelines" not in result + + +def test_no_targets_specified(): + yaml_data = """ +organizations: + - name: org1 + description: Organization 1 +workspaces: + - name: workspace1 + organization: org1 + description: Workspace 1 +pipelines: + - name: pipeline1 + workspace: workspace1 + description: Pipeline 1 +""" + with patch("builtins.open", lambda f, _: StringIO(yaml_data)): + result = helper.parse_all_yaml(["dummy_path.yaml"]) + + # Check that all blocks are in the result + assert "organizations" in result + assert "workspaces" in result + assert "pipelines" in result