Files
gns3-server/gns3server/schemas/controller/computes.py
T
YueGuobin c1e03194d3 api: list Docker image details and delete images
GET /computes/{compute_id}/docker/images now returns the daemon image
id, size and creation date in addition to the tag, so a UI can show
and compare images instead of bare names. The fields are optional to
keep the response compatible with older computes.

Add DELETE /computes/{compute_id}/docker/images/{image} which removes
the image from the compute Docker daemon. The image name is passed as
a path so names with a slash or a tag separator work. A daemon that
refuses because a container uses the image answers a conflict; the
force query parameter removes it anyway.

The compute also drops the controller digest to local id translation
recorded when the image was synced, since it no longer matches
anything on the daemon.
2026-11-06 14:22:22 +08:00

173 lines
5.5 KiB
Python

#
# Copyright (C) 2020 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import uuid
from enum import StrEnum
from typing import Any, Optional, Union
from pydantic import BaseModel, ConfigDict, Field, SecretStr, model_validator
from .base import DateTimeModelMixin
# The capabilities payload has one definition (controller.capabilities): a
# second, private copy here silently stripped every field it did not know
# about from GET /computes/{id} — which is how ubridge_tap and
# ubridge_iol_tap went missing while the compute returned them.
from .capabilities import Capabilities
class Protocol(StrEnum):
"""
Protocol supported to communicate with a compute.
"""
http = "http"
https = "https"
class ComputeBase(BaseModel):
"""
Data to create a compute.
"""
protocol: Optional[Protocol] = None
host: Optional[str] = None
port: Optional[int] = Field(None, gt=0, le=65535)
user: Optional[str] = None
password: Optional[SecretStr] = None
name: Optional[str] = None
auto_connect: Optional[bool] = Field(
False, description="Whether to automatically connect to the compute when the controller starts"
)
model_config = ConfigDict(use_enum_values=True)
class ComputeCreate(ComputeBase):
"""
Data to create a compute.
"""
protocol: Protocol
host: str
port: int = Field(..., gt=0, le=65535)
compute_id: Optional[Union[str, uuid.UUID]] = None
model_config = ConfigDict(
json_schema_extra={
"example": {"name": "My compute", "host": "127.0.0.1", "port": 3080, "user": "user", "password": "password"}
}
)
@model_validator(mode="before")
@classmethod
def set_default_compute_id_and_name(cls, data: Any) -> Any:
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):
"""
Data to update a compute.
"""
user: Optional[str] = None
password: Optional[SecretStr] = None
auto_connect: Optional[bool] = None # no default: update schemas must not publish defaults
model_config = ConfigDict(
json_schema_extra={
"example": {
"host": "10.0.0.1",
"port": 8080,
}
}
)
class Compute(DateTimeModelMixin, ComputeBase):
"""
Data returned for a compute.
"""
protocol: Protocol
host: str
port: int = Field(..., gt=0, le=65535)
compute_id: Union[str, uuid.UUID]
name: str
# defaulted: topology files and the local compute dict may not carry it
auto_connect: bool = False
connected: Optional[bool] = Field(None, description="Whether the controller is connected to the compute or not")
cpu_usage_percent: Optional[float] = Field(None, description="CPU usage of the compute", ge=0, le=100)
memory_usage_percent: Optional[float] = Field(None, description="Memory usage of the compute", ge=0, le=100)
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
model_config = ConfigDict(from_attributes=True)
class ComputeVirtualBoxVM(BaseModel):
"""
VirtualBox VM from compute.
"""
vmname: str = Field(..., description="VirtualBox VM name")
ram: int = Field(..., description="VirtualBox VM memory")
class ComputeVMwareVM(BaseModel):
"""
VMware VM from compute.
"""
vmname: str = Field(..., description="VMware VM name")
vmx_path: str = Field(..., description="Path to the vmx file")
class ComputeDockerImage(BaseModel):
"""
Docker image from compute.
"""
image: str = Field(..., description="Docker image name")
id: Optional[str] = Field(None, description="Docker image id reported by the daemon")
size: Optional[int] = Field(None, description="Docker image size in bytes")
created: Optional[Union[int, str]] = Field(None, description="Docker image creation date (unix timestamp)")
class AutoIdlePC(BaseModel):
"""
Data for auto Idle-PC request.
"""
platform: str = Field(..., description="Cisco platform")
image: str = Field(..., description="Image path")
ram: int = Field(..., description="Amount of RAM in MB")
model_config = ConfigDict(
json_schema_extra={"example": {"platform": "c7200", "image": "/path/to/c7200_image.bin", "ram": 256}}
)
class IdlePC(BaseModel):
"""
Result of an Idle-PC computation.
"""
idlepc: Optional[str] = Field(None, description="Idle-PC value, null when none was found")