1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 08:26:03 +00:00

refactor(vrtManager): new VM UUIDs come from uuid.uuid4()

randomUUID() built a version 4 UUID from 16 random bytes by hand; the clone and create jobs now take str(uuid4()) from the standard library, and the function and its format test go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-08 12:24:47 +03:00
parent f97708a7b7
commit d2383be914
4 changed files with 5 additions and 21 deletions

View file

@ -4,6 +4,7 @@ import math
import os
import re
import time
from uuid import uuid4
from django.db import transaction
from django.core.exceptions import PermissionDenied
@ -605,7 +606,7 @@ class CloneInstance(Kind):
_check_clone_state(instance.proxy, data)
return Plan(claims=keys, resource=claims.instance_key(instance.uuid),
label=f"{instance.compute.name}/{instance.name}",
intent={"compute": instance.compute_id, "uuid": util.randomUUID(), "name": data["name"],
intent={"compute": instance.compute_id, "uuid": str(uuid4()), "name": data["name"],
"clone": data, "quota": quota})
def precheck(self, op, ctx):
@ -875,7 +876,7 @@ class CreateInstance(Kind):
compute = Compute.objects.get(pk=params["compute"])
return Plan(claims=vm_claims(compute, params["name"]), resource=claims.compute_key(compute.pk),
label=f"{compute.name}/{params['name']}",
intent={"compute": compute.pk, "uuid": util.randomUUID(), "name": params["name"]})
intent={"compute": compute.pk, "uuid": str(uuid4()), "name": params["name"]})
def precheck(self, op, ctx):
if op.user is None or not op.user.is_active or not op.user.is_superuser:

View file

@ -11,7 +11,7 @@ from django.http.response import Http404
from django.shortcuts import reverse
from django.test import TestCase
from vrtManager.create import wvmCreate
from vrtManager.util import randomUUID
from uuid import uuid4
from operations.models import Operation
@ -212,7 +212,7 @@ class InstancesTestCase(TestCase):
self.assertFalse(Instance.objects.filter(pk=instance.id).exists())
def test_create_from_xml(self):
uuid = randomUUID()
uuid = str(uuid4())
xml = self.xml.replace("wvc-test-vm", "wvc-test-vm-xml")
xml = re.sub(r"\s?<uuid>.*?</uuid>", f"<uuid>{uuid}</uuid>", xml)
response = self.client.post(

View file

@ -4,7 +4,6 @@ import unittest
from vrtManager.util import (
get_xml_path,
randomPasswd,
randomUUID,
validate_macaddr,
vol_dev_type,
vol_driver_type,
@ -13,14 +12,6 @@ from vrtManager.util import (
class TestVrtManagerUtil(unittest.TestCase):
def test_random_uuid_format(self):
uuid_str = randomUUID()
# UUIDv4 format check
pattern = r"^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
self.assertRegex(uuid_str, pattern)
# Ensure successive UUIDs are distinct
self.assertNotEqual(uuid_str, randomUUID())
def test_validate_macaddr(self):
self.assertIsNone(validate_macaddr(None))
self.assertIsNone(validate_macaddr("52:54:10:aa:bb:cc"))

View file

@ -63,14 +63,6 @@ class HostBusy(ConnectionFailed):
(HOST_REQUESTS) still wait for its answers."""
def randomUUID():
"""Generate a random UUID."""
u = [secrets.randbelow(256) for _ in range(16)]
u[6] = u[6] & 15 | 4 << 4
u[8] = u[8] & 63 | 2 << 6
return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2, "%02x" * 6]) % tuple(u)
def randomPasswd(length=12, alphabet=string.ascii_letters + string.digits):
"""Generate a random password"""
return "".join([secrets.choice(alphabet) for _ in range(length)])