Skip to content

Commit

Permalink
Allow bootstrapping context settings file
Browse files Browse the repository at this point in the history
  • Loading branch information
JimMadge committed Nov 3, 2023
1 parent 7805f45 commit ac99832
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
33 changes: 25 additions & 8 deletions data_safe_haven/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rich import print

from data_safe_haven.config import Config, ContextSettings
from data_safe_haven.config.context_settings import default_config_file_path
from data_safe_haven.context import Context
from data_safe_haven.functions import validate_aad_guid

Expand Down Expand Up @@ -76,14 +77,30 @@ def add(
),
],
) -> None:
settings = ContextSettings.from_file()
settings.add(
key=key,
admin_group_id=admin_group,
location=location,
name=name,
subscription_name=subscription,
)
if default_config_file_path().exists():
settings = ContextSettings.from_file()
settings.add(
key=key,
admin_group_id=admin_group,
location=location,
name=name,
subscription_name=subscription,
)
else:
# Bootstrap context settings file
settings = ContextSettings(
{
"selected": key,
"contexts": {
key: {
"admin_group_id": admin_group,
"location": location,
"name": name,
"subscription_name": subscription,
}
},
}
)
settings.write()


Expand Down
27 changes: 27 additions & 0 deletions tests_/commands/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,33 @@ def test_add_missing_ags(self, runner):
assert result.exit_code == 2
assert "Missing option" in result.stderr

def test_add_bootstrap(self, tmp_contexts, runner):
(tmp_contexts / "contexts.yaml").unlink()
result = runner.invoke(
context_command_group,
[
"add",
"acme_deployment",
"--name",
"Acme Deployment",
"--admin-group",
"d5c5c439-1115-4cb6-ab50-b8e547b6c8dd",
"--location",
"uksouth",
"--subscription",
"Data Safe Haven (Acme)",
]
)
assert result.exit_code == 0
assert (tmp_contexts / "contexts.yaml").exists()
result = runner.invoke(context_command_group, ["show"])
assert result.exit_code == 0
assert "Name: Acme Deployment" in result.stdout
result = runner.invoke(context_command_group, ["available"])
assert result.exit_code == 0
assert "acme_deployment*" in result.stdout
assert "gems" not in result.stdout


class TestUpdate:
def test_update(self, runner):
Expand Down

0 comments on commit ac99832

Please sign in to comment.