Skip to content

Commit

Permalink
Further refactoring of CopyVM- and CopyTemplateCommand to reduce repe…
Browse files Browse the repository at this point in the history
…titive code.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
  • Loading branch information
kc284 authored and danilo-delbusso committed Nov 24, 2021
1 parent e265e86 commit a327390
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 166 deletions.
118 changes: 0 additions & 118 deletions XenAdmin/Commands/CopyTemplateCommand.cs

This file was deleted.

125 changes: 90 additions & 35 deletions XenAdmin/Commands/CopyVMCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,77 +35,132 @@
using XenAdmin.Actions.VMActions;
using XenAdmin.Core;
using XenAdmin.Dialogs;
using XenAdmin.Network;
using XenAdmin.Wizards.CrossPoolMigrateWizard;
using XenAPI;

namespace XenAdmin.Commands
{
/// <summary>
/// Launches the Copy-VM dialog for the selected VM.
/// </summary>
internal class CopyVMCommand : Command
internal abstract class CopyVmTemplateCommandBase : Command
{
/// <summary>
/// Initializes a new instance of this Command. The parameter-less constructor is required in the derived
/// class if it is to be attached to a ToolStrip menu item or button. It should not be used in any other scenario.
/// </summary>
public CopyVMCommand()
protected CopyVmTemplateCommandBase()
{
}

public CopyVMCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
protected CopyVmTemplateCommandBase(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
: base(mainWindow, selection)
{
}

private bool CheckRbacPermissions(VM vm, RbacMethodList methodList, string warningMessage)
protected override bool CanRunCore(SelectedItemCollection selection)
{
return selection.ContainsOneItemOfType<VM>() && selection.AtLeastOneXenObjectCan<VM>(CanRun);
}

protected override void RunCore(SelectedItemCollection selection)
{
if (vm.Connection.Session.IsLocalSuperuser)
var vm = (VM)selection[0].XenObject;

if (CanLaunchMigrateWizard(vm))
{
MainWindowCommandInterface.ShowPerConnectionWizard(vm.Connection,
new CrossPoolMigrateWizard(vm.Connection, selection, null, WizardMode.Copy));
return;
}

if (CheckRbacPermissions(vm.Connection))
new CopyVMDialog(vm).ShowPerXenObject(vm, Program.MainWindow);
}

private bool CheckRbacPermissions(IXenConnection connection)
{
if (connection.Session.IsLocalSuperuser)
return true;

var currentRoles = vm.Connection.Session.Roles;
var validRoles = Role.ValidRoleList(methodList, vm.Connection);
var methodList = new RbacMethodList();
methodList.AddRange(SrRefreshAction.StaticRBACDependencies);
methodList.AddRange(VMCopyAction.StaticRBACDependencies);
methodList.AddRange(VMCloneAction.StaticRBACDependencies);

var currentRoles = connection.Session.Roles;
var validRoles = Role.ValidRoleList(methodList, connection);

if (currentRoles.Any(currentRole => validRoles.Contains(currentRole)))
return true;

currentRoles.Sort();

using (var dlg = new ErrorDialog(string.Format(warningMessage, currentRoles[0].FriendlyName())))
using (var dlg = new ErrorDialog(string.Format(RbacMessage, currentRoles[0].FriendlyName())))
dlg.ShowDialog(Parent);

return false;
}

protected override void RunCore(SelectedItemCollection selection)
{
var vm = (VM)selection[0].XenObject;
protected abstract string RbacMessage { get; }
protected abstract bool CanRun(VM vm);
protected abstract bool CanLaunchMigrateWizard(VM vm);
}

if (CrossPoolCopyVMCommand.CanRun(vm, null))
{
new CrossPoolCopyVMCommand(MainWindowCommandInterface, selection).Run();
}
else
{
var rbac = new RbacMethodList();
rbac.AddRange(SrRefreshAction.StaticRBACDependencies);
rbac.AddRange(VMCopyAction.StaticRBACDependencies);
rbac.AddRange(VMCloneAction.StaticRBACDependencies);

if (CheckRbacPermissions(vm, rbac, Messages.RBAC_INTRA_POOL_COPY_VM_BLOCKED))
new CopyVMDialog(vm).ShowPerXenObject(vm, Program.MainWindow);
}
internal class CopyVMCommand : CopyVmTemplateCommandBase
{
public CopyVMCommand()
{
}

protected override bool CanRunCore(SelectedItemCollection selection)
public CopyVMCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
: base(mainWindow, selection)
{
return selection.ContainsOneItemOfType<VM>() && selection.AtLeastOneXenObjectCan<VM>(CanRun);
}

private static bool CanRun(VM vm)
protected override bool CanRun(VM vm)
{
if (vm == null || vm.is_a_template || vm.Locked || vm.allowed_operations == null)
return false;

if (CanLaunchMigrateWizard(vm))
return true;

return vm.allowed_operations.Contains(vm_operations.export) && vm.power_state != vm_power_state.Suspended;
}

protected override bool CanLaunchMigrateWizard(VM vm)
{
return vm != null && (CrossPoolCopyVMCommand.CanRun(vm, null) || vm.CanBeCopied());
return vm.power_state == vm_power_state.Halted && CrossPoolMigrateCommand.CanRun(vm, null);
}

public override string MenuText => Messages.MAINWINDOW_COPY_VM;
protected override string RbacMessage => Messages.RBAC_INTRA_POOL_COPY_VM_BLOCKED;
}


internal class CopyTemplateCommand : CopyVmTemplateCommandBase
{
public CopyTemplateCommand()
{
}

public CopyTemplateCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
: base(mainWindow, selection)
{
}

protected override bool CanRun(VM vm)
{
if (vm == null || !vm.is_a_template || vm.is_a_snapshot || vm.Locked || vm.allowed_operations == null || vm.InternalTemplate()) return false;

if (CanLaunchMigrateWizard(vm))
return true;

return vm.allowed_operations.Contains(vm_operations.clone) || vm.allowed_operations.Contains(vm_operations.copy);
}

protected override bool CanLaunchMigrateWizard(VM vm)
{
return !vm.DefaultTemplate() && CrossPoolMigrateCommand.CanRun(vm, null);
}

public override string MenuText => Messages.MAINWINDOW_COPY_TEMPLATE;
protected override string RbacMessage => Messages.RBAC_INTRA_POOL_COPY_TEMPLATE_BLOCKED;
}
}
1 change: 0 additions & 1 deletion XenAdmin/XenAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3360,7 +3360,6 @@
<Compile Include="Commands\DisconnectHostsAndPoolsCommand.cs" />
<Compile Include="Commands\DisconnectPoolCommand.cs">
</Compile>
<Compile Include="Commands\CopyTemplateCommand.cs" />
<Compile Include="Commands\DragDropCommand.cs" />
<Compile Include="Commands\DragDropAddHostToPoolCommand.cs" />
<Compile Include="Commands\DragDropMigrateVMCommand.cs" />
Expand Down
12 changes: 0 additions & 12 deletions XenModel/XenAPI-Extensions/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,18 +1639,6 @@ public bool CanBeMoved()
return false;
}

/// <summary>
/// Whether the VM can be copied inside the pool (vm.copy)
/// </summary>
public bool CanBeCopied()
{
if (!is_a_template && !Locked && allowed_operations != null && allowed_operations.Contains(vm_operations.export) && power_state != vm_power_state.Suspended)
{
return true;
}
return false;
}

/// <summary>
/// Returns whether this is a Windows VM by checking the distro value in the
/// guest_metrics before falling back to the viridian flag. The result may not be
Expand Down

0 comments on commit a327390

Please sign in to comment.