From ac99832cfe304dd7fd80e1fa0f7f9dc6458555c3 Mon Sep 17 00:00:00 2001 From: Jim Madge Date: Fri, 3 Nov 2023 11:25:34 +0000 Subject: [PATCH] Allow bootstrapping context settings file --- data_safe_haven/commands/context.py | 33 ++++++++++++++++++++++------- tests_/commands/test_context.py | 27 +++++++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/data_safe_haven/commands/context.py b/data_safe_haven/commands/context.py index 8bc1de6706..89eac06dee 100644 --- a/data_safe_haven/commands/context.py +++ b/data_safe_haven/commands/context.py @@ -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 @@ -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() diff --git a/tests_/commands/test_context.py b/tests_/commands/test_context.py index 51e902f445..0cdf878c44 100644 --- a/tests_/commands/test_context.py +++ b/tests_/commands/test_context.py @@ -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):