mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 14:01:15 +02:00
Pydantic v2 migration
This commit is contained in:
1 parent
11f51b3809
commit
9c456532d0
46 files changed
+221
-277
No files matched your search
@@ -16,8 +16,15 @@
|
||||
|
||||
import uuid
|
||||
|
||||
from pydantic import BaseModel, Field, SecretStr, validator
|
||||
from typing import List, Optional, Union
|
||||
from pydantic import (
|
||||
ConfigDict,
|
||||
BaseModel,
|
||||
Field,
|
||||
SecretStr,
|
||||
field_validator,
|
||||
model_validator
|
||||
)
|
||||
from typing import List, Optional, Union, Any
|
||||
from enum import Enum
|
||||
|
||||
from .nodes import NodeType
|
||||
@@ -44,9 +51,7 @@ class ComputeBase(BaseModel):
|
||||
user: str = None
|
||||
password: Optional[SecretStr] = None
|
||||
name: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
use_enum_values = True
|
||||
model_config = ConfigDict(use_enum_values=True)
|
||||
|
||||
|
||||
class ComputeCreate(ComputeBase):
|
||||
@@ -55,46 +60,28 @@ class ComputeCreate(ComputeBase):
|
||||
"""
|
||||
|
||||
compute_id: Union[str, uuid.UUID] = None
|
||||
|
||||
class Config:
|
||||
schema_extra = {
|
||||
"example": {
|
||||
"name": "My compute",
|
||||
"host": "127.0.0.1",
|
||||
"port": 3080,
|
||||
"user": "user",
|
||||
"password": "password"
|
||||
}
|
||||
model_config = ConfigDict(json_schema_extra={
|
||||
"example": {
|
||||
"name": "My compute",
|
||||
"host": "127.0.0.1",
|
||||
"port": 3080,
|
||||
"user": "user",
|
||||
"password": "password"
|
||||
}
|
||||
})
|
||||
|
||||
@validator("compute_id", pre=True, always=True)
|
||||
def default_compute_id(cls, v, values):
|
||||
@model_validator(mode='before')
|
||||
@classmethod
|
||||
def set_default_compute_id_and_name(cls, data: Any) -> Any:
|
||||
|
||||
if v is not None:
|
||||
return v
|
||||
else:
|
||||
protocol = values.get("protocol")
|
||||
host = values.get("host")
|
||||
port = values.get("port")
|
||||
return uuid.uuid5(uuid.NAMESPACE_URL, f"{protocol}://{host}:{port}")
|
||||
|
||||
@validator("name", pre=True, always=True)
|
||||
def generate_name(cls, name, values):
|
||||
|
||||
if name is not None:
|
||||
return name
|
||||
else:
|
||||
protocol = values.get("protocol")
|
||||
host = values.get("host")
|
||||
port = values.get("port")
|
||||
user = values.get("user")
|
||||
if user:
|
||||
# due to random user generated by 1.4 it's common to have a very long user
|
||||
if len(user) > 14:
|
||||
user = user[:11] + "..."
|
||||
return f"{protocol}://{user}@{host}:{port}"
|
||||
else:
|
||||
return f"{protocol}://{host}:{port}"
|
||||
if "compute_id" not in data:
|
||||
data['compute_id'] = uuid.uuid5(
|
||||
uuid.NAMESPACE_URL,
|
||||
f"{data.get('protocol')}://{data.get('host')}:{data.get('port')}"
|
||||
)
|
||||
if "name" not in data:
|
||||
data['name'] = f"{data.get('protocol')}://{data.get('user', '')}@{data.get('host')}:{data.get('port')}"
|
||||
return data
|
||||
|
||||
|
||||
class ComputeUpdate(ComputeBase):
|
||||
@@ -107,14 +94,12 @@ class ComputeUpdate(ComputeBase):
|
||||
port: Optional[int] = Field(None, gt=0, le=65535)
|
||||
user: Optional[str] = None
|
||||
password: Optional[SecretStr] = None
|
||||
|
||||
class Config:
|
||||
schema_extra = {
|
||||
"example": {
|
||||
"host": "10.0.0.1",
|
||||
"port": 8080,
|
||||
}
|
||||
model_config = ConfigDict(json_schema_extra={
|
||||
"example": {
|
||||
"host": "10.0.0.1",
|
||||
"port": 8080,
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
class Capabilities(BaseModel):
|
||||
@@ -143,9 +128,7 @@ class Compute(DateTimeModelMixin, ComputeBase):
|
||||
disk_usage_percent: Optional[float] = Field(None, description="Disk usage of the compute", ge=0, le=100)
|
||||
last_error: Optional[str] = Field(None, description="Last error found on the compute")
|
||||
capabilities: Optional[Capabilities] = None
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class ComputeVirtualBoxVM(BaseModel):
|
||||
@@ -182,6 +165,4 @@ class AutoIdlePC(BaseModel):
|
||||
platform: str = Field(..., description="Cisco platform")
|
||||
image: str = Field(..., description="Image path")
|
||||
ram: int = Field(..., description="Amount of RAM in MB")
|
||||
|
||||
class Config:
|
||||
schema_extra = {"example": {"platform": "c7200", "image": "/path/to/c7200_image.bin", "ram": 256}}
|
||||
model_config = ConfigDict(json_schema_extra={"example": {"platform": "c7200", "image": "/path/to/c7200_image.bin", "ram": 256}})
|
||||
Reference in new issue
Block a user