mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
refactor(instances): a migration merges owners with move_ownership
record_migration() repeated computes.utils._move_ownership() line by line: owners move to the record the destination already has, a user's rights are merged and the template mark is kept. It now calls that function, renamed move_ownership as it is used from both apps; the owners left on the source record go with it, by cascade. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
9ec3c09358
commit
cc104716e0
3 changed files with 13 additions and 25 deletions
|
|
@ -64,7 +64,7 @@ def _log(inst, what):
|
|||
write("-", inst.compute.name, inst.name, f"VM {inst.name} ({inst.uuid}): {what}", True, uuid=inst.uuid)
|
||||
|
||||
|
||||
def _move_ownership(source, target):
|
||||
def move_ownership(source, target):
|
||||
"""Give target the owners and template flag of source; a user's rights are merged."""
|
||||
for ui in UserInstance.objects.filter(instance=source):
|
||||
existing = UserInstance.objects.filter(instance=target, user=ui.user).first()
|
||||
|
|
@ -88,7 +88,7 @@ def forget(inst):
|
|||
comes back."""
|
||||
elsewhere = Instance.objects.filter(uuid=inst.uuid).exclude(compute_id=inst.compute_id).first()
|
||||
if elsewhere:
|
||||
_move_ownership(inst, elsewhere)
|
||||
move_ownership(inst, elsewhere)
|
||||
_log(inst, f"owners moved to {elsewhere.compute.name}")
|
||||
else:
|
||||
_bury(inst)
|
||||
|
|
|
|||
|
|
@ -68,3 +68,12 @@ class MigrationBookkeepingTests(TestCase):
|
|||
found.refresh_from_db()
|
||||
self.assertTrue(found.is_template)
|
||||
self.assertEqual(UserInstance.objects.get(user=owner).instance_id, found.id)
|
||||
|
||||
def test_an_owner_of_both_records_keeps_the_rights_of_both(self):
|
||||
owner = get_user_model().objects.create_user("mb-both")
|
||||
UserInstance.objects.create(user=owner, instance=self.vm, is_change=True)
|
||||
found = Instance.objects.create(compute=self.dest, name="vm", uuid="u-mb")
|
||||
UserInstance.objects.create(user=owner, instance=found, is_vnc=True)
|
||||
self.migrate()
|
||||
ui = UserInstance.objects.get(user=owner)
|
||||
self.assertEqual((ui.instance_id, ui.is_change, ui.is_delete, ui.is_vnc), (found.id, True, False, True))
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ from collections import namedtuple
|
|||
from accounts.models import UserInstance, UserAttributes
|
||||
from appsettings.settings import app_settings
|
||||
from computes.models import Compute
|
||||
from computes.utils import lock_ownership
|
||||
from computes.utils import lock_ownership, move_ownership
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
|
@ -351,28 +351,7 @@ def record_migration(instance, new_compute):
|
|||
lock_ownership()
|
||||
target_inst = Instance.objects.filter(compute=new_compute, uuid=instance.uuid).first()
|
||||
if target_inst and target_inst.id != instance.id:
|
||||
for ui in UserInstance.objects.filter(instance=instance):
|
||||
existing_ui = UserInstance.objects.filter(instance=target_inst, user=ui.user).first()
|
||||
if not existing_ui:
|
||||
ui.instance = target_inst
|
||||
ui.save()
|
||||
else:
|
||||
updated = False
|
||||
if ui.is_change and not existing_ui.is_change:
|
||||
existing_ui.is_change = True
|
||||
updated = True
|
||||
if ui.is_delete and not existing_ui.is_delete:
|
||||
existing_ui.is_delete = True
|
||||
updated = True
|
||||
if ui.is_vnc and not existing_ui.is_vnc:
|
||||
existing_ui.is_vnc = True
|
||||
updated = True
|
||||
if updated:
|
||||
existing_ui.save()
|
||||
ui.delete()
|
||||
if instance.is_template and not target_inst.is_template:
|
||||
target_inst.is_template = True
|
||||
target_inst.save(update_fields=["is_template"])
|
||||
move_ownership(instance, target_inst)
|
||||
instance.delete()
|
||||
instance.id = target_inst.id
|
||||
instance.compute = new_compute
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue