Further refactoring of CopyVM- and CopyTemplateCommand to reduce repetitive code.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2021-11-22 21:23:01 +00:00 committed by Danilo Del Busso
parent e265e86cfa
commit a327390e4a
4 changed files with 104 additions and 180 deletions

View File

@ -1,118 +0,0 @@
/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System.Collections.Generic;
using System.Linq;
using XenAdmin.Actions;
using XenAdmin.Actions.VMActions;
using XenAdmin.Core;
using XenAdmin.Dialogs;
using XenAPI;
namespace XenAdmin.Commands
{
/// <summary>
/// Launches the Copy-Template dialog for the specified VM.
/// </summary>
internal class CopyTemplateCommand : 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 CopyTemplateCommand()
{
}
public CopyTemplateCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
: base(mainWindow, selection)
{
}
private bool CheckRbacPermissions(VM vm, RbacMethodList methodList, string warningMessage)
{
if (vm.Connection.Session.IsLocalSuperuser)
return true;
var currentRoles = vm.Connection.Session.Roles;
var validRoles = Role.ValidRoleList(methodList, vm.Connection);
if (currentRoles.Any(currentRole => validRoles.Contains(currentRole)))
return true;
currentRoles.Sort();
using (var dlg = new ErrorDialog(string.Format(warningMessage, currentRoles[0].FriendlyName())))
dlg.ShowDialog(Parent);
return false;
}
protected override void RunCore(SelectedItemCollection selection)
{
var template = (VM)selection[0].XenObject;
if (CrossPoolCopyTemplateCommand.CanRun(template, null))
{
new CrossPoolCopyTemplateCommand(MainWindowCommandInterface, selection).Run();
}
else
{
var rbac = new RbacMethodList();
rbac.AddRange(SrRefreshAction.StaticRBACDependencies);
rbac.AddRange(VMCopyAction.StaticRBACDependencies);
rbac.AddRange(VMCloneAction.StaticRBACDependencies);
if (CheckRbacPermissions(template, rbac, Messages.RBAC_INTRA_POOL_COPY_TEMPLATE_BLOCKED))
new CopyVMDialog(template).ShowPerXenObject(template, Program.MainWindow);
}
}
protected override bool CanRunCore(SelectedItemCollection selection)
{
return selection.ContainsOneItemOfType<VM>() && selection.AtLeastOneXenObjectCan<VM>(CanRun);
}
private static bool CanRun(VM vm)
{
if (vm != null && vm.is_a_template && !vm.is_a_snapshot && !vm.Locked && vm.allowed_operations != null && !vm.InternalTemplate())
{
if (CrossPoolCopyTemplateCommand.CanRun(vm, null))
return true;
if (vm.allowed_operations.Contains(vm_operations.clone) || vm.allowed_operations.Contains(vm_operations.copy))
return true;
}
return false;
}
public override string MenuText => Messages.MAINWINDOW_COPY_TEMPLATE;
}
}

View File

@ -35,19 +35,75 @@ using XenAdmin.Actions;
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
{
protected CopyVmTemplateCommandBase()
{
}
protected CopyVmTemplateCommandBase(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
: base(mainWindow, selection)
{
}
protected override bool CanRunCore(SelectedItemCollection selection)
{
return selection.ContainsOneItemOfType<VM>() && selection.AtLeastOneXenObjectCan<VM>(CanRun);
}
protected override void RunCore(SelectedItemCollection selection)
{
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 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(RbacMessage, currentRoles[0].FriendlyName())))
dlg.ShowDialog(Parent);
return false;
}
protected abstract string RbacMessage { get; }
protected abstract bool CanRun(VM vm);
protected abstract bool CanLaunchMigrateWizard(VM vm);
}
internal class CopyVMCommand : CopyVmTemplateCommandBase
{
/// <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()
{
}
@ -57,55 +113,54 @@ namespace XenAdmin.Commands
{
}
private bool CheckRbacPermissions(VM vm, RbacMethodList methodList, string warningMessage)
protected override bool CanRun(VM vm)
{
if (vm.Connection.Session.IsLocalSuperuser)
if (vm == null || vm.is_a_template || vm.Locked || vm.allowed_operations == null)
return false;
if (CanLaunchMigrateWizard(vm))
return true;
var currentRoles = vm.Connection.Session.Roles;
var validRoles = Role.ValidRoleList(methodList, vm.Connection);
if (currentRoles.Any(currentRole => validRoles.Contains(currentRole)))
return true;
currentRoles.Sort();
using (var dlg = new ErrorDialog(string.Format(warningMessage, currentRoles[0].FriendlyName())))
dlg.ShowDialog(Parent);
return false;
return vm.allowed_operations.Contains(vm_operations.export) && vm.power_state != vm_power_state.Suspended;
}
protected override void RunCore(SelectedItemCollection selection)
protected override bool CanLaunchMigrateWizard(VM vm)
{
var vm = (VM)selection[0].XenObject;
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);
}
}
protected override bool CanRunCore(SelectedItemCollection selection)
{
return selection.ContainsOneItemOfType<VM>() && selection.AtLeastOneXenObjectCan<VM>(CanRun);
}
private static bool CanRun(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;
}
}

View File

@ -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" />

View File

@ -1639,18 +1639,6 @@ namespace XenAPI
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