Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unchangable Pulumi workspace configuration #2237

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions data_safe_haven/infrastructure/project_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,29 @@ def destroy(self) -> None:
raise DataSafeHavenPulumiError(msg) from exc

def ensure_config(self, name: str, value: str, *, secret: bool) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering whether ensure is still the best verb for this. Can you think of a better one @JimMadge ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure is what I would choose, so I might not be the best person to ask 😄

"""Ensure that config values have been set, setting them if they do not exist"""
"""
Ensure that config values have been set.

Values will be set if they do not exist.

If the value is already set and does not match the `value` argument,
`DataSafeHavenPulumiError` will be raised.
"""
try:
self.stack.get_config(name)
existing_value = self.stack.get_config(name).value
except automation.CommandError:
# Set value if it does not already exist
self.set_config(name, value, secret=secret)

# If the value does already exist, ensure it is consistent with the declared
# value
if existing_value != value:
msg = (
f"Unchangeable configuration option '{name}' not consistent, "
f"your configuration: '{value}', Pulumi workspace: '{existing_value}'."
)
raise DataSafeHavenPulumiError(msg)

def evaluate(self, result: str) -> None:
"""Evaluate a Pulumi operation."""
if result == "succeeded":
Expand Down
16 changes: 16 additions & 0 deletions tests/infrastructure/test_project_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ def test_cleanup(
)
assert "Purged Azure Key Vault shmacmedsresandbosecrets." in stdout

def test_ensure_config(self, sre_project_manager):
sre_project_manager.ensure_config(
"azure-native:location", "uksouth", secret=False
)
sre_project_manager.ensure_config("data-safe-haven:variable", "8", secret=False)

def test_ensure_config_exception(self, sre_project_manager):

with raises(
DataSafeHavenPulumiError,
match=r"Unchangeable configuration option 'azure-native:location'.*your configuration: 'ukwest', Pulumi workspace: 'uksouth'",
):
sre_project_manager.ensure_config(
"azure-native:location", "ukwest", secret=False
)

def test_new_project(
self,
context_no_secrets,
Expand Down
Loading