Skip to content

Commit 6966f43

Browse files
authored
Clean up models backward compatibility code (#2408)
1 parent 5bc5fc1 commit 6966f43

File tree

9 files changed

+51
-57
lines changed

9 files changed

+51
-57
lines changed

src/dstack/_internal/cli/commands/gateway.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
)
1414
from dstack._internal.cli.utils.gateway import get_gateways_table, print_gateways_table
1515
from dstack._internal.core.models.backends.base import BackendType
16+
from dstack._internal.core.models.gateways import GatewayConfiguration
17+
from dstack._internal.utils.logging import get_logger
18+
19+
logger = get_logger(__name__)
1620

1721

1822
class GatewayCommand(APIBaseCommand):
@@ -41,7 +45,9 @@ def _register(self):
4145
)
4246

4347
create_parser = subparsers.add_parser(
44-
"create", help="Add a gateway", formatter_class=self._parser.formatter_class
48+
"create",
49+
help="Add a gateway. Deprecated in favor of `dstack apply` with gateway configuration.",
50+
formatter_class=self._parser.formatter_class,
4551
)
4652
create_parser.set_defaults(subfunc=self._create)
4753
create_parser.add_argument(
@@ -100,10 +106,16 @@ def _list(self, args: argparse.Namespace):
100106
pass
101107

102108
def _create(self, args: argparse.Namespace):
109+
logger.warning(
110+
"`dstack gateway create` is deperecated in favor of `dstack apply` with gateway configurations."
111+
)
103112
with console.status("Creating gateway..."):
104-
gateway = self.api.client.gateways.create(
105-
self.api.project, args.name, BackendType(args.backend), args.region
113+
configuration = GatewayConfiguration(
114+
name=args.name,
115+
backend=BackendType(args.backend),
116+
region=args.region,
106117
)
118+
gateway = self.api.client.gateways.create(self.api.project, configuration)
107119
if args.set_default:
108120
self.api.client.gateways.set_default(self.api.project, gateway.name)
109121
if args.domain:

src/dstack/_internal/cli/utils/gateway.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def get_gateways_table(
3434
for gateway in gateways:
3535
row = {
3636
"NAME": gateway.name,
37-
"BACKEND": f"{gateway.backend.value} ({gateway.region})",
37+
"BACKEND": f"{gateway.configuration.backend.value} ({gateway.configuration.region})",
3838
"HOSTNAME": gateway.hostname,
3939
"DOMAIN": gateway.wildcard_domain,
4040
"DEFAULT": "✓" if gateway.default else "",

src/dstack/_internal/core/models/fleets.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,7 @@ def _merged_profile(cls, values) -> Dict:
270270

271271

272272
class Fleet(CoreModel):
273-
# id is Optional for backward compatibility within 0.18.x
274-
id: Optional[uuid.UUID] = None
273+
id: uuid.UUID
275274
name: str
276275
project_name: str
277276
spec: FleetSpec

src/dstack/_internal/core/models/gateways.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ class Gateway(CoreModel):
7676
# The ip address of the gateway instance
7777
ip_address: Optional[str]
7878
instance_id: Optional[str]
79+
wildcard_domain: Optional[str]
80+
default: bool
7981
# TODO: configuration fields are duplicated on top-level for backward compatibility with 0.18.x
80-
# Remove in 0.19
82+
# Remove after 0.19
8183
backend: BackendType
8284
region: str
83-
default: bool
84-
wildcard_domain: Optional[str]
8585

8686

8787
class GatewayPlan(CoreModel):

src/dstack/_internal/core/models/runs.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,7 @@ class RunPlan(CoreModel):
457457
run_spec: RunSpec
458458
job_plans: List[JobPlan]
459459
current_resource: Optional[Run] = None
460-
# Optional for backward-compatibility with 0.18.x servers
461-
# TODO: make required in 0.19
462-
action: Optional[ApplyAction] = None
460+
action: ApplyAction
463461

464462

465463
class ApplyRunPlanInput(CoreModel):

src/dstack/_internal/core/models/volumes.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ class VolumeAttachment(CoreModel):
8686
class Volume(CoreModel):
8787
id: uuid.UUID
8888
name: str
89-
# Default user to "" for client backward compatibility (old 0.18 servers).
90-
# TODO: Remove in 0.19
91-
user: str = ""
89+
user: str
9290
project_name: str
9391
configuration: VolumeConfiguration
9492
external: bool

src/dstack/_internal/server/schemas/gateways.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
1-
from typing import Dict, List, Optional
1+
from typing import List
22

3-
from pydantic import root_validator
4-
5-
from dstack._internal.core.models.backends.base import BackendType
63
from dstack._internal.core.models.common import CoreModel
74
from dstack._internal.core.models.gateways import GatewayConfiguration
85

96

107
class CreateGatewayRequest(CoreModel):
11-
name: Optional[str]
12-
backend_type: Optional[BackendType]
13-
region: Optional[str]
14-
configuration: Optional[GatewayConfiguration]
15-
16-
@root_validator
17-
def fill_configuration(cls, values: Dict) -> Dict:
18-
if values.get("configuration", None) is not None:
19-
return values
20-
backend_type = values.get("backend_type", None)
21-
region = values.get("region", None)
22-
if backend_type is None:
23-
raise ValueError("backend_type must be specified")
24-
if region is None:
25-
raise ValueError("region must be specified")
26-
values["configuration"] = GatewayConfiguration(
27-
name=values.get("name", None),
28-
backend=backend_type,
29-
region=region,
30-
)
31-
return values
8+
configuration: GatewayConfiguration
329

3310

3411
class GetGatewayRequest(CoreModel):

src/dstack/api/server/_gateways.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from typing import List, Optional
1+
from typing import List
22

33
from pydantic import parse_obj_as
44

5-
from dstack._internal.core.models.backends.base import BackendType
65
from dstack._internal.core.models.gateways import Gateway, GatewayConfiguration
76
from dstack._internal.server.schemas.gateways import (
87
CreateGatewayRequest,
@@ -24,22 +23,12 @@ def get(self, project_name: str, gateway_name: str) -> Gateway:
2423
resp = self._request(f"/api/project/{project_name}/gateways/get", body=body.json())
2524
return parse_obj_as(Gateway.__response__, resp.json())
2625

27-
# gateway_name, backend_type, region are left for backward-compatibility with 0.18.x
28-
# TODO: Remove in 0.19
2926
def create(
3027
self,
3128
project_name: str,
32-
gateway_name: Optional[str] = None,
33-
backend_type: Optional[BackendType] = None,
34-
region: Optional[str] = None,
35-
configuration: Optional[GatewayConfiguration] = None,
29+
configuration: GatewayConfiguration,
3630
) -> Gateway:
37-
body = CreateGatewayRequest(
38-
name=gateway_name,
39-
backend_type=backend_type,
40-
region=region,
41-
configuration=configuration,
42-
)
31+
body = CreateGatewayRequest(configuration=configuration)
4332
resp = self._request(f"/api/project/{project_name}/gateways/create", body=body.json())
4433
return parse_obj_as(Gateway.__response__, resp.json())
4534

src/tests/_internal/server/routers/test_gateways.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,14 @@ async def test_create_gateway(self, test_db, session: AsyncSession, client: Asyn
175175
backend = await create_backend(session, project.id, backend_type=BackendType.AWS)
176176
response = await client.post(
177177
f"/api/project/{project.name}/gateways/create",
178-
json={"name": "test", "backend_type": "aws", "region": "us"},
178+
json={
179+
"configuration": {
180+
"type": "gateway",
181+
"name": "test",
182+
"backend": "aws",
183+
"region": "us",
184+
},
185+
},
179186
headers=get_auth_headers(user.token),
180187
)
181188
assert response.status_code == 200
@@ -218,7 +225,14 @@ async def test_create_gateway_without_name(
218225
g.return_value = "random-name"
219226
response = await client.post(
220227
f"/api/project/{project.name}/gateways/create",
221-
json={"name": None, "backend_type": "aws", "region": "us"},
228+
json={
229+
"configuration": {
230+
"type": "gateway",
231+
"name": None,
232+
"backend": "aws",
233+
"region": "us",
234+
},
235+
},
222236
headers=get_auth_headers(user.token),
223237
)
224238
g.assert_called_once()
@@ -259,7 +273,14 @@ async def test_create_gateway_missing_backend(
259273
)
260274
response = await client.post(
261275
f"/api/project/{project.name}/gateways/create",
262-
json={"name": "test", "backend_type": "aws", "region": "us"},
276+
json={
277+
"configuration": {
278+
"type": "gateway",
279+
"name": "test",
280+
"backend": "aws",
281+
"region": "us",
282+
},
283+
},
263284
headers=get_auth_headers(user.token),
264285
)
265286
assert response.status_code == 400

0 commit comments

Comments
 (0)