Skip to content

Commit

Permalink
feat: add option to not fail if configured resource group does not ex…
Browse files Browse the repository at this point in the history
…ist (#650)

Fix #649

Co-authored-by: amimas <aver.mimas@gmail.com>
  • Loading branch information
philippe-granet and amimas committed May 15, 2024
1 parent fff1293 commit a362d8e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 15 deletions.
9 changes: 6 additions & 3 deletions docs/reference/resource_groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@

## Prerequisite

A resource group must exist before the process mode can be updated.
A resource group must exist before the process mode can be updated, except if the key name `ensure_exists` is present and is set to `false`.

Add a resource group to a project by configuring it in your project's `gitlab-ci.yml` file. For more information, visit [the appropriate GitLab's docs](https://docs.gitlab.com/ee/ci/resource_groups/#add-a-resource-group).

## Update Process Mode

This section's purpose is to manage the [resource group process modes](https://docs.gitlab.com/ee/ci/resource_groups/#process-modes).

The key names are the resource group names associated with the project.
The key name `ensure_exists` is optional - if set to `false` it will not fail when trying to process a non-existent resource group; default is `true`.

The other key names are the resource group names associated with the project.
The value for each key name is a key-value pair, with `process_mode` as the key and the value is one of the process
modes defined [here](https://docs.gitlab.com/ee/ci/resource_groups/#change-the-process-mode).


Example:

```yaml
projects_and_groups:
group_1/project_1:
resource_groups:
ensure_exists: false
staging:
process_mode: oldest_first
production:
process_mode: newest_first
resource_group_that_dont_exist:
process_mode: newest_first
```
26 changes: 16 additions & 10 deletions gitlabform/processors/project/resource_groups_processor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cli_ui import debug as verbose
from cli_ui import warning

from gitlabform.gitlab import GitLab
from gitlabform.gitlab.core import NotFoundException, UnexpectedResponseException
Expand All @@ -10,28 +11,33 @@ def __init__(self, gitlab: GitLab):
super().__init__("resource_groups", gitlab)

def _process_configuration(self, project_and_group: str, configuration: dict):
for config_resource_group_name in configuration["resource_groups"]:
config_process_mode = configuration["resource_groups"][
config_resource_group_name
]["process_mode"]
ensure_exists = configuration.get("resource_groups|ensure_exists", True)
for config_name, config in configuration["resource_groups"].items():
if config_name == "ensure_exists":
continue
config_process_mode = config["process_mode"]

try:
gitlab_resource_group = self.gitlab.get_specific_resource_group(
project_and_group, config_resource_group_name
project_and_group, config_name
)
except NotFoundException:
raise Exception(
f"Project is not configured to use resource group: {config_resource_group_name}.\n"
message = (
f"Project is not configured to use resource group: {config_name}.\n"
f"Add the resource group in your project's .gitlab-ci.yml file.\n"
f"For more information, visit https://docs.gitlab.com/ee/ci/resource_groups/#add-a-resource-group.",
f"For more information, visit https://docs.gitlab.com/ee/ci/resource_groups/#add-a-resource-group."
)

if ensure_exists:
raise Exception(message)
else:
warning(message)
continue
# compare the resource group process mode between the config entity and gitlab entity
if config_process_mode != gitlab_resource_group["process_mode"]:
try:
self.gitlab.update_resource_group(
project_and_group,
config_resource_group_name,
config_name,
{"process_mode": config_process_mode},
)
except UnexpectedResponseException:
Expand Down
57 changes: 55 additions & 2 deletions tests/acceptance/standard/test_resource_groups.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest
from tests.acceptance import run_gitlabform
import time

from tests.acceptance import run_gitlabform
from gitlabform.constants import EXIT_PROCESSING_ERROR


@pytest.fixture(scope="function")
def add_gitlab_ci_config(project):
Expand All @@ -25,6 +27,8 @@ def add_gitlab_ci_config(project):
resource_group: production
"""
run_gitlabform(add_gitlab_ci_config, project)
time.sleep(5)

return project


Expand All @@ -38,9 +42,58 @@ def test__update_resource_group_process_mode(self, project, add_gitlab_ci_config
process_mode: newest_first
"""

time.sleep(5)
run_gitlabform(update_resource_group_config, project)

resource_group = project.resource_groups.get("production")
assert resource_group.key == "production"
assert resource_group.process_mode == "newest_first"

def test__ensure_exists_default_true(self, project, capsys):
update_resource_group_config = f"""
projects_and_groups:
{project.path_with_namespace}:
resource_groups:
resource_group_that_dont_exist:
process_mode: newest_first
"""

with pytest.raises(SystemExit) as exception:
run_gitlabform(update_resource_group_config, project)
assert exception.type == SystemExit
assert exception.value.code == EXIT_PROCESSING_ERROR
captured = capsys.readouterr()
assert "Project is not configured to use resource group" in captured.err

def test__ensure_exists_equals_true(self, project, capsys):
update_resource_group_config = f"""
projects_and_groups:
{project.path_with_namespace}:
resource_groups:
ensure_exists: true
resource_group_that_dont_exist:
process_mode: newest_first
"""

with pytest.raises(SystemExit) as exception:
run_gitlabform(update_resource_group_config, project)
assert exception.type == SystemExit
assert exception.value.code == EXIT_PROCESSING_ERROR
captured = capsys.readouterr()
assert "Project is not configured to use resource group" in captured.err

def test__ensure_exists_equals_false(self, project):
update_resource_group_config = f"""
projects_and_groups:
{project.path_with_namespace}:
resource_groups:
ensure_exists: false
resource_group_that_dont_exist:
process_mode: newest_first
"""

try:
run_gitlabform(update_resource_group_config, project)
except Exception as exception:
assert (
False
), f"Test disabling `ensure_exists` raised an exception {exception}, but it shouldn't."

0 comments on commit a362d8e

Please sign in to comment.