Skip to content

Commit

Permalink
Merge pull request #1853 from jemrobinson/1852-fix-circular-import
Browse files Browse the repository at this point in the history
Break circular dependency in Context
  • Loading branch information
jemrobinson authored May 2, 2024
2 parents 1cbb819 + 26dcb49 commit 387ece4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
3 changes: 2 additions & 1 deletion data_safe_haven/context/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from data_safe_haven import __version__
from data_safe_haven.external import AzureApi
from data_safe_haven.functions import alphanumeric
from data_safe_haven.serialisers import ContextBase
from data_safe_haven.types import (
AzureLocation,
AzureLongName,
Expand All @@ -18,7 +19,7 @@
from data_safe_haven.utility import config_dir


class Context(BaseModel, validate_assignment=True):
class Context(ContextBase, BaseModel, validate_assignment=True):
admin_group_id: Guid
location: AzureLocation
name: str
Expand Down
2 changes: 2 additions & 0 deletions data_safe_haven/serialisers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from .azure_serialisable_model import AzureSerialisableModel
from .context_base import ContextBase
from .yaml_serialisable_model import YAMLSerialisableModel

__all__ = [
"AzureSerialisableModel",
"ContextBase",
"YAMLSerialisableModel",
]
8 changes: 4 additions & 4 deletions data_safe_haven/serialisers/azure_serialisable_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from typing import Any, ClassVar, TypeVar

from data_safe_haven.context.context import Context
from data_safe_haven.external import AzureApi

from .context_base import ContextBase
from .yaml_serialisable_model import YAMLSerialisableModel

T = TypeVar("T", bound="AzureSerialisableModel")
Expand All @@ -17,7 +17,7 @@ class AzureSerialisableModel(YAMLSerialisableModel):
filename: ClassVar[str] = "config.yaml"

@classmethod
def from_remote(cls: type[T], context: Context) -> T:
def from_remote(cls: type[T], context: ContextBase) -> T:
"""Construct an AzureSerialisableModel from a YAML file in Azure storage."""
azure_api = AzureApi(subscription_name=context.subscription_name)
config_yaml = azure_api.download_blob(
Expand All @@ -30,7 +30,7 @@ def from_remote(cls: type[T], context: Context) -> T:

@classmethod
def from_remote_or_create(
cls: type[T], context: Context, **default_args: dict[Any, Any]
cls: type[T], context: ContextBase, **default_args: dict[Any, Any]
) -> T:
"""
Construct an AzureSerialisableModel from a YAML file in Azure storage, or from
Expand All @@ -47,7 +47,7 @@ def from_remote_or_create(
else:
return cls(**default_args)

def upload(self, context: Context) -> None:
def upload(self, context: ContextBase) -> None:
"""Serialise an AzureSerialisableModel to a YAML file in Azure storage."""
azure_api = AzureApi(subscription_name=context.subscription_name)
azure_api.upload_blob(
Expand Down
19 changes: 19 additions & 0 deletions data_safe_haven/serialisers/context_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from abc import ABC, abstractmethod
from typing import ClassVar

from data_safe_haven.types import AzureLongName


class ContextBase(ABC):
subscription_name: AzureLongName
storage_container_name: ClassVar[str]

@property
@abstractmethod
def resource_group_name(self) -> str:
pass

@property
@abstractmethod
def storage_account_name(self) -> str:
pass
2 changes: 1 addition & 1 deletion tests/commands/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_safe_haven.commands.config import config_command_group
from data_safe_haven.commands import config_command_group
from data_safe_haven.config import Config
from data_safe_haven.external import AzureApi

Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_safe_haven.commands.context import context_command_group
from data_safe_haven.commands import context_command_group
from data_safe_haven.context_infrastructure import ContextInfrastructure


Expand Down

0 comments on commit 387ece4

Please sign in to comment.