11from abc import ABC , abstractmethod
2- from typing import Any , ClassVar , List , Literal , Optional , overload
2+ from typing import Any , ClassVar , Generic , List , Optional , TypeVar
33from uuid import UUID
44
55from dstack ._internal .core .backends .base .backend import Backend
66from dstack ._internal .core .backends .models import (
7- AnyBackendConfig ,
87 AnyBackendConfigWithCreds ,
98 AnyBackendConfigWithoutCreds ,
109)
1615# We'll introduce our own base limit that can be customized per backend if required.
1716TAGS_MAX_NUM = 25
1817
18+ BackendConfigWithoutCredsT = TypeVar (
19+ "BackendConfigWithoutCredsT" , bound = AnyBackendConfigWithoutCreds
20+ )
21+ BackendConfigWithCredsT = TypeVar ("BackendConfigWithCredsT" , bound = AnyBackendConfigWithCreds )
22+
1923
2024class BackendRecord (CoreModel ):
2125 """
@@ -40,7 +44,7 @@ class StoredBackendRecord(BackendRecord):
4044 backend_id : UUID
4145
4246
43- class Configurator (ABC ):
47+ class Configurator (ABC , Generic [ BackendConfigWithoutCredsT , BackendConfigWithCredsT ] ):
4448 """
4549 `Configurator` is responsible for configuring backends
4650 and initializing `Backend` instances from backend configs.
@@ -53,7 +57,7 @@ class Configurator(ABC):
5357 BACKEND_CLASS : ClassVar [type [Backend ]]
5458
5559 @abstractmethod
56- def validate_config (self , config : AnyBackendConfigWithCreds , default_creds_enabled : bool ):
60+ def validate_config (self , config : BackendConfigWithCredsT , default_creds_enabled : bool ):
5761 """
5862 Validates backend config including backend creds and other parameters.
5963 Raises `ServerClientError` or its subclass if config is invalid.
@@ -62,9 +66,7 @@ def validate_config(self, config: AnyBackendConfigWithCreds, default_creds_enabl
6266 pass
6367
6468 @abstractmethod
65- def create_backend (
66- self , project_name : str , config : AnyBackendConfigWithCreds
67- ) -> BackendRecord :
69+ def create_backend (self , project_name : str , config : BackendConfigWithCredsT ) -> BackendRecord :
6870 """
6971 Sets up backend given backend config and returns
7072 text-encoded config and creds to be stored in the DB.
@@ -78,26 +80,23 @@ def create_backend(
7880 """
7981 pass
8082
81- @overload
82- def get_backend_config (
83- self , record : StoredBackendRecord , include_creds : Literal [False ]
84- ) -> AnyBackendConfigWithoutCreds :
85- pass
86-
87- @overload
88- def get_backend_config (
89- self , record : StoredBackendRecord , include_creds : Literal [True ]
90- ) -> AnyBackendConfigWithCreds :
83+ @abstractmethod
84+ def get_backend_config_with_creds (
85+ self , record : StoredBackendRecord
86+ ) -> BackendConfigWithCredsT :
87+ """
88+ Constructs `BackendConfig` with credentials included.
89+ Used internally and when project admins need to see backend's creds.
90+ """
9191 pass
9292
9393 @abstractmethod
94- def get_backend_config (
95- self , record : StoredBackendRecord , include_creds : bool
96- ) -> AnyBackendConfig :
94+ def get_backend_config_without_creds (
95+ self , record : StoredBackendRecord
96+ ) -> BackendConfigWithoutCredsT :
9797 """
98- Constructs `BackendConfig` to be returned in API responses.
99- Project admins may need to see backend's creds. In this case `include_creds` will be `True`.
100- Otherwise, no sensitive information should be included.
98+ Constructs `BackendConfig` without sensitive information.
99+ Used for API responses where creds should not be exposed.
101100 """
102101 pass
103102
0 commit comments