Skip to content

Commit

Permalink
Merge pull request #158 from seqeralabs/148-add-targets-feature
Browse files Browse the repository at this point in the history
Add targets flag to specify resources for creation in input YAML
  • Loading branch information
ejseqera authored Aug 2, 2024
2 parents c006f67 + 3a87f44 commit 57ffa7f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
11 changes: 10 additions & 1 deletion seqerakit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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(
Expand Down
7 changes: 6 additions & 1 deletion seqerakit/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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",
Expand Down
70 changes: 70 additions & 0 deletions tests/unit/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 57ffa7f

Please sign in to comment.