|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Validated contracts for Studio workspaces.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from datetime import datetime |
| 20 | + |
| 21 | +from pydantic import BaseModel, ConfigDict, Field, model_validator |
| 22 | + |
| 23 | + |
| 24 | +class WorkspaceInput(BaseModel): |
| 25 | + model_config = ConfigDict(extra="forbid", populate_by_name=True) |
| 26 | + |
| 27 | + name: str = Field(min_length=1, max_length=128) |
| 28 | + description: str = Field(default="", max_length=2000) |
| 29 | + environment_ids: list[str] = Field( |
| 30 | + default_factory=list, alias="environmentIds", max_length=100 |
| 31 | + ) |
| 32 | + |
| 33 | + @model_validator(mode="after") |
| 34 | + def normalize(self) -> WorkspaceInput: |
| 35 | + self.name = self.name.strip() |
| 36 | + self.description = self.description.strip() |
| 37 | + if not self.name: |
| 38 | + raise ValueError("工作区名称不能为空。") |
| 39 | + self.environment_ids = list( |
| 40 | + dict.fromkeys(item.strip() for item in self.environment_ids if item.strip()) |
| 41 | + ) |
| 42 | + return self |
| 43 | + |
| 44 | + |
| 45 | +class WorkspacePatch(BaseModel): |
| 46 | + model_config = ConfigDict(extra="forbid", populate_by_name=True) |
| 47 | + |
| 48 | + name: str | None = Field(default=None, min_length=1, max_length=128) |
| 49 | + description: str | None = Field(default=None, max_length=2000) |
| 50 | + environment_ids: list[str] | None = Field( |
| 51 | + default=None, alias="environmentIds", max_length=100 |
| 52 | + ) |
| 53 | + |
| 54 | + @model_validator(mode="after") |
| 55 | + def normalize(self) -> WorkspacePatch: |
| 56 | + if not self.model_fields_set: |
| 57 | + raise ValueError("至少需要更新一个工作区字段。") |
| 58 | + if self.name is not None: |
| 59 | + self.name = self.name.strip() |
| 60 | + if not self.name: |
| 61 | + raise ValueError("工作区名称不能为空。") |
| 62 | + if self.description is not None: |
| 63 | + self.description = self.description.strip() |
| 64 | + if self.environment_ids is not None: |
| 65 | + self.environment_ids = list( |
| 66 | + dict.fromkeys( |
| 67 | + item.strip() for item in self.environment_ids if item.strip() |
| 68 | + ) |
| 69 | + ) |
| 70 | + return self |
| 71 | + |
| 72 | + |
| 73 | +class WorkspaceRecord(WorkspaceInput): |
| 74 | + id: str = Field(min_length=32, max_length=32) |
| 75 | + owner_id: str = Field(alias="ownerId", min_length=1, max_length=1024) |
| 76 | + created_at: datetime = Field(alias="createdAt") |
| 77 | + updated_at: datetime = Field(alias="updatedAt") |
| 78 | + |
| 79 | + |
| 80 | +__all__ = ["WorkspaceInput", "WorkspacePatch", "WorkspaceRecord"] |
0 commit comments