diff --git a/instances/jobs.py b/instances/jobs.py index ffaaeff7..c8a2c926 100644 --- a/instances/jobs.py +++ b/instances/jobs.py @@ -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: diff --git a/instances/tests/test_live_views.py b/instances/tests/test_live_views.py index 5cd96ea8..689e12f4 100644 --- a/instances/tests/test_live_views.py +++ b/instances/tests/test_live_views.py @@ -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?.*?", f"{uuid}", xml) response = self.client.post( diff --git a/vrtManager/tests/test_util.py b/vrtManager/tests/test_util.py index 42d77e25..a1378dea 100644 --- a/vrtManager/tests/test_util.py +++ b/vrtManager/tests/test_util.py @@ -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")) diff --git a/vrtManager/util.py b/vrtManager/util.py index 84c2c24a..aee68908 100644 --- a/vrtManager/util.py +++ b/vrtManager/util.py @@ -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)])