Skip to content

Commit 5f01039

Browse files
committed
feat: POST /<vm_id>/rename endpoint
1 parent 301c7b3 commit 5f01039

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

backend/app/core/policies.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
"policy": "vms:stop",
7070
"description": "Allows the user to stop virtual machines.",
7171
},
72+
{
73+
"policy": "vms:rename",
74+
"description": "Allows the user rename a vm.",
75+
},
7276
{
7377
"policy": "vms:connect",
7478
"description": "Allows the user to connect to a virtual machine via the dashboard tunnel.",

backend/app/models/vm.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class VmCreateXML(VmBase):
3333
id: UUID
3434

3535

36+
class VmRename(BaseModel):
37+
name: str
38+
39+
3640
class VmCredentialCreateRequest(BaseModel):
3741
name: str = Field(min_length=1)
3842
password: Optional[str] = None

backend/app/routes/vm.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from fastapi.responses import Response
55
from sqlmodel import Session
66
from app.models.user_management import MissingPoliciesResponse
7-
from app.models.vm import VmCreate, VmRead, VmCredentialCreateRequest, VmCredentialRead, RecoverableVm, RecoverableVmCreate
7+
from app.models.vm import VmCreate, VmRead, VmCredentialCreateRequest, VmCredentialRead, RecoverableVm, RecoverableVmCreate, VmRename
88
from app.services.vm_service import VmService
99
from app.services.vm_screenshot import capture_screenshot
1010
from app.utils.auth import require_policy, decode_access_token, user_has_policy
@@ -137,6 +137,16 @@ def restart_vm(vm_id: str):
137137
return vm
138138

139139

140+
@router.post("/{vm_id}/rename",
141+
status_code=status.HTTP_200_OK,
142+
response_model=VmRead,
143+
dependencies=[Depends(require_policy("vms:rename"))],
144+
responses={403: {"model": MissingPoliciesResponse}})
145+
def rename_vm(vm_id: str, vm_rename: VmRename):
146+
vm = VmService.rename_vm(vm_id, vm_rename)
147+
return vm
148+
149+
140150
@router.post("/{vm_id}/start",
141151
status_code=status.HTTP_200_OK,
142152
response_model=VmRead,

backend/app/services/vm_service.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from app.utils.vm import wait_for_state
77
from typing import Optional
88
from app.core.constants import VMS_DIR, IMAGES_DIR, VM_STATE_NAMES
9-
from app.models.vm import VmCreate, VmRead, VmCredentialCreateRequest, RecoverableVm, RecoverableVmCreate, VmCreateXML
9+
from app.models.vm import VmCreate, VmRead, VmCredentialCreateRequest, RecoverableVm, RecoverableVmCreate, VmCreateXML, VmRename
1010
from app.models.image import ImageRead
1111
from app.core.xml_builder import build_xml
1212
from app.core.config import QEMUConfig, engine
@@ -422,7 +422,10 @@ def create_vm(vm_create: VmCreate):
422422
return vm
423423

424424
@staticmethod
425-
def _auto_pick_node(required_mem: int, required_vcpus: int, required_disk: int):
425+
def _auto_pick_node(
426+
required_mem: int,
427+
required_vcpus: int,
428+
required_disk: int):
426429
"""Pick the best node, prioritizing master. Returns slave UUID or None for master."""
427430
from app.services.host_service import HostService
428431
from app.services.slave_service import SlaveService
@@ -788,3 +791,12 @@ def duplicate_vm(vm_id: str):
788791
)
789792

790793
return Vm.get(str(duplicate_vm.id))
794+
795+
@staticmethod
796+
def rename_vm(vm_id: str, vm_rename: VmRename):
797+
with Session(engine) as session:
798+
vm = VmService._get_vm_or_404(session, vm_id)
799+
vm.name = vm_rename.name
800+
session.add(vm)
801+
session.commit()
802+
return Vm.get(str(vm.id))

0 commit comments

Comments
 (0)