From a02aede1b0cfd997e27433030300f7c5c35ae98b Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 21 Apr 2021 13:47:32 +0930 Subject: [PATCH] Use uuid5 to create new compute_id. Fixes #1641 #1887 --- gns3server/schemas/controller/computes.py | 20 ++++++++++++++++---- tests/api/routes/controller/test_computes.py | 6 +++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/gns3server/schemas/controller/computes.py b/gns3server/schemas/controller/computes.py index f84bb527..00343e64 100644 --- a/gns3server/schemas/controller/computes.py +++ b/gns3server/schemas/controller/computes.py @@ -14,9 +14,10 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import uuid + from pydantic import BaseModel, Field, SecretStr, validator from typing import List, Optional, Union -from uuid import UUID, uuid4 from enum import Enum from .nodes import NodeType @@ -49,7 +50,7 @@ class ComputeCreate(ComputeBase): Data to create a compute. """ - compute_id: Union[str, UUID] = Field(default_factory=uuid4) + compute_id: Union[str, uuid.UUID] = None password: Optional[SecretStr] = None class Config: @@ -63,7 +64,18 @@ class ComputeCreate(ComputeBase): } } - @validator("name", always=True) + @validator("compute_id", pre=True, always=True) + def default_compute_id(cls, v, values): + + 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: @@ -119,7 +131,7 @@ class Compute(DateTimeModelMixin, ComputeBase): Data returned for a compute. """ - compute_id: Union[str, UUID] + compute_id: Union[str, uuid.UUID] name: str 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) diff --git a/tests/api/routes/controller/test_computes.py b/tests/api/routes/controller/test_computes.py index 0e2c0e69..290e7461 100644 --- a/tests/api/routes/controller/test_computes.py +++ b/tests/api/routes/controller/test_computes.py @@ -84,7 +84,7 @@ class TestComputeRoutes: params = { "protocol": "http", "host": "localhost", - "port": 84, + "port": 42, "user": "julien", "password": "secure" } @@ -133,7 +133,7 @@ class TestComputeFeatures: params = { "protocol": "http", "host": "localhost", - "port": 84, + "port": 4242, "user": "julien", "password": "secure" } @@ -151,7 +151,7 @@ class TestComputeFeatures: params = { "protocol": "http", "host": "localhost", - "port": 84, + "port": 4284, "user": "julien", "password": "secure" }