Skip to content

Commit

Permalink
Add context show command
Browse files Browse the repository at this point in the history
  • Loading branch information
JimMadge committed Oct 31, 2023
1 parent 38fd39d commit 7fe847d
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 99 deletions.
12 changes: 6 additions & 6 deletions data_safe_haven/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from data_safe_haven import __version__
from data_safe_haven.commands import (
admin_command_group,
context_command_group,
deploy_command_group,
initialise_command,
teardown_command_group,
)
from data_safe_haven.exceptions import DataSafeHavenError
Expand Down Expand Up @@ -69,6 +69,11 @@ def main() -> None:
name="admin",
help="Perform administrative tasks for a Data Safe Haven deployment.",
)
application.add_typer(
context_command_group,
name="context",
help="Manage Data Safe Haven contexts."
)
application.add_typer(
deploy_command_group,
name="deploy",
Expand All @@ -80,11 +85,6 @@ def main() -> None:
help="Tear down a Data Safe Haven component.",
)

# Register direct subcommands
application.command(name="init", help="Initialise a Data Safe Haven deployment.")(
initialise_command
)

# Start the application
try:
application()
Expand Down
4 changes: 2 additions & 2 deletions data_safe_haven/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from .admin import admin_command_group
from .context import context_command_group
from .deploy import deploy_command_group
from .init import initialise_command
from .teardown import teardown_command_group

__all__ = [
"admin_command_group",
"context_command_group",
"deploy_command_group",
"initialise_command",
"teardown_command_group",
]
36 changes: 20 additions & 16 deletions data_safe_haven/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from typing import Annotated, Optional

import typer
from rich import print

from data_safe_haven.backend import Backend
from data_safe_haven.config import BackendSettings
from data_safe_haven.config import ContextSettings
from data_safe_haven.exceptions import (
DataSafeHavenError,
DataSafeHavenInputError,
Expand All @@ -14,6 +15,23 @@
context_command_group = typer.Typer()


@context_command_group.command()
def show() -> None:
settings = ContextSettings.from_file()

current_context_key = settings.selected
current_context = settings.context
available = settings.available

print(f"Current context: [green]{current_context_key}")
print(f"\tName: {current_context.name}")
print(f"\tAdmin Group ID: {current_context.admin_group_id}")
print(f"\tSubscription name: {current_context.subscription_name}")
print(f"\tLocation: {current_context.location}")
print("\nAvailable contexts:")
print("\n".join(available))


@context_command_group.command()
def add(
admin_group: Annotated[
Expand Down Expand Up @@ -50,7 +68,7 @@ def add(
),
] = None,
) -> None:
settings = BackendSettings()
settings = ContextSettings()
settings.add(
admin_group_id=admin_group,
location=location,
Expand All @@ -64,20 +82,6 @@ def remove() -> None:
pass


@context_command_group.command()
def show() -> None:
settings = BackendSettings()
settings.summarise()


@context_command_group.command()
def switch(
name: Annotated[str, typer.Argument(help="Name of the context to switch to.")]
) -> None:
settings = BackendSettings()
settings.context = name


@context_command_group.command()
def create() -> None:
backend = Backend() # How does this get the config!?!
Expand Down
67 changes: 0 additions & 67 deletions data_safe_haven/commands/init.py

This file was deleted.

2 changes: 2 additions & 0 deletions data_safe_haven/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .config import Config
from .context_settings import ContextSettings

__all__ = [
"Config",
"ContextSettings",
]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Load global and local settings from dotfiles"""
import pathlib
from pathlib import Path
from dataclasses import dataclass
from os import getenv
from typing import Any

import appdirs
Expand All @@ -15,10 +16,15 @@
from data_safe_haven.utility import LoggingSingleton


config_directory = pathlib.Path(
appdirs.user_config_dir(appname="data_safe_haven")
).resolve()
config_file_path = config_directory / "config.yaml"
def config_file_path() -> Path:
if config_directory_env := getenv("DSH_CONFIG_DIRECTORY"):
config_directory = Path(config_directory_env).resolve()
else:
config_directory = Path(
appdirs.user_config_dir(appname="data_safe_haven")
).resolve()

return config_directory / "contexts.yaml"


@dataclass
Expand Down Expand Up @@ -153,7 +159,7 @@ def remove(self, key: str) -> None:
del self.settings["contexts"][key]

@classmethod
def from_file(cls, config_file_path: str = config_file_path) -> None:
def from_file(cls, config_file_path: str = config_file_path()) -> None:
logger = LoggingSingleton()
try:
with open(config_file_path, encoding="utf-8") as f_yaml:
Expand All @@ -170,7 +176,7 @@ def from_file(cls, config_file_path: str = config_file_path) -> None:
msg = f"Could not load settings from {config_file_path}.\n{exc}"
raise DataSafeHavenConfigError(msg) from exc

def write(self, config_file_path: str = config_file_path) -> None:
def write(self, config_file_path: str = config_file_path()) -> None:
"""Write settings to YAML file"""
# Create the parent directory if it does not exist then write YAML
config_file_path.parent.mkdir(parents=True, exist_ok=True)
Expand Down
40 changes: 40 additions & 0 deletions tests_/commands/test_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from data_safe_haven.commands.context import context_command_group

from pytest import fixture
from typer.testing import CliRunner

context_settings = """\
selected: acme_deployment
contexts:
acme_deployment:
name: Acme Deployment
admin_group_id: d5c5c439-1115-4cb6-ab50-b8e547b6c8dd
location: uksouth
subscription_name: Data Safe Haven (Acme)
gems:
name: Gems
admin_group_id: d5c5c439-1115-4cb6-ab50-b8e547b6c8dd
location: uksouth
subscription_name: Data Safe Haven (Gems)"""


@fixture
def tmp_contexts(tmp_path):
config_file_path = tmp_path / "contexts.yaml"
with open(config_file_path, "w") as f:
f.write(context_settings)
return config_file_path


@fixture
def runner(tmp_contexts):
runner = CliRunner(env={
"DSH_CONFIG_DIRECTORY": str(tmp_contexts)
})
return runner


class TestShow:
def test_show(self, runner):
result = runner.invoke(context_command_group, ["show"])
assert result.exit_code == 0
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_safe_haven.config.backend_settings import Context, ContextSettings
from data_safe_haven.config.context_settings import Context, ContextSettings
from data_safe_haven.exceptions import DataSafeHavenParameterError

import pytest
Expand Down

0 comments on commit 7fe847d

Please sign in to comment.