From aecb18ab15f061ad45c3eb6208582e6cc8dbde4c Mon Sep 17 00:00:00 2001 From: Gabor Apati-Nagy Date: Tue, 24 Nov 2015 18:31:25 +0000 Subject: [PATCH] CA-186244: Cross-pool copy of a stopped VM allows selection of the current pool Now CrossPoolMigrateCanMigrateFilter can accept a parameter to decide whether to allow same target pool or not -- if allowSameTargetPool is false, it won't. Signed-off-by: Gabor Apati-Nagy --- ...rossPoolMigrateDelayLoadingComboBoxItem.cs | 6 ++-- .../CrossPoolMigrateDestinationPage.cs | 2 +- .../CrossPoolMigrateCanMigrateFilter.cs | 31 ++++++++++++++++--- XenModel/Messages.Designer.cs | 9 ++++++ XenModel/Messages.resx | 3 ++ 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/XenAdmin/Wizards/CrossPoolMigrateWizard/CrossPoolMigrateDelayLoadingComboBoxItem.cs b/XenAdmin/Wizards/CrossPoolMigrateWizard/CrossPoolMigrateDelayLoadingComboBoxItem.cs index 0c970f64a..c2dd6554e 100644 --- a/XenAdmin/Wizards/CrossPoolMigrateWizard/CrossPoolMigrateDelayLoadingComboBoxItem.cs +++ b/XenAdmin/Wizards/CrossPoolMigrateWizard/CrossPoolMigrateDelayLoadingComboBoxItem.cs @@ -44,6 +44,7 @@ namespace XenAdmin.Wizards.CrossPoolMigrateWizard private readonly Host preSelectedHost; private readonly List preSelectedVMs; private Queue filters; + private bool allowSameTargetPool; /// /// Instantiate a delay loading combo box item for cross pool migrate @@ -51,11 +52,12 @@ namespace XenAdmin.Wizards.CrossPoolMigrateWizard /// Item being added to the list /// Host that was preselected by user prior to loading this item /// VMs selected prior to loading this item - public CrossPoolMigrateDelayLoadingComboBoxItem(IXenObject itemAddedToComboBox, Host preSelectedHost, List preSelectedVMs) + public CrossPoolMigrateDelayLoadingComboBoxItem(IXenObject itemAddedToComboBox, Host preSelectedHost, List preSelectedVMs, bool allowSameTargetPool) : base(itemAddedToComboBox) { this.preSelectedHost = preSelectedHost; this.preSelectedVMs = preSelectedVMs; + this.allowSameTargetPool = allowSameTargetPool; } /// @@ -67,7 +69,7 @@ namespace XenAdmin.Wizards.CrossPoolMigrateWizard filters = new Queue(); filters.Enqueue(new CrossPoolMigrateVersionFilter(Item)); filters.Enqueue(new ResidentHostIsSameAsSelectionFilter(Item, preSelectedVMs)); - filters.Enqueue(new CrossPoolMigrateCanMigrateFilter(Item, preSelectedVMs)); + filters.Enqueue(new CrossPoolMigrateCanMigrateFilter(Item, preSelectedVMs, allowSameTargetPool)); filters.Enqueue(new WlbEnabledFilter(Item, preSelectedVMs)); } diff --git a/XenAdmin/Wizards/CrossPoolMigrateWizard/CrossPoolMigrateDestinationPage.cs b/XenAdmin/Wizards/CrossPoolMigrateWizard/CrossPoolMigrateDestinationPage.cs index affdc787b..97a920d98 100644 --- a/XenAdmin/Wizards/CrossPoolMigrateWizard/CrossPoolMigrateDestinationPage.cs +++ b/XenAdmin/Wizards/CrossPoolMigrateWizard/CrossPoolMigrateDestinationPage.cs @@ -97,7 +97,7 @@ namespace XenAdmin.Wizards.CrossPoolMigrateWizard public override DelayLoadingOptionComboBoxItem CreateDelayLoadingOptionComboBoxItem(IXenObject xenItem) { - return new CrossPoolMigrateDelayLoadingComboBoxItem(xenItem, preSelectedHost, selectedVMs); + return new CrossPoolMigrateDelayLoadingComboBoxItem(xenItem, preSelectedHost, selectedVMs, wizardMode != WizardMode.Copy); } protected override List CreateHomeServerFilterList(IEnableableXenObjectComboBoxItem selectedItem) diff --git a/XenAdmin/Wizards/CrossPoolMigrateWizard/Filters/CrossPoolMigrateCanMigrateFilter.cs b/XenAdmin/Wizards/CrossPoolMigrateWizard/Filters/CrossPoolMigrateCanMigrateFilter.cs index dca6d1dc7..cb5563558 100644 --- a/XenAdmin/Wizards/CrossPoolMigrateWizard/Filters/CrossPoolMigrateCanMigrateFilter.cs +++ b/XenAdmin/Wizards/CrossPoolMigrateWizard/Filters/CrossPoolMigrateCanMigrateFilter.cs @@ -44,6 +44,7 @@ namespace XenAdmin.Wizards.CrossPoolMigrateWizard.Filters private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private readonly List preSelectedVMs; private readonly List failureReasons = new List(); + private readonly bool allowSameTargetPool; /// /// Helper class used for determining if the hosts in the failure @@ -62,10 +63,11 @@ namespace XenAdmin.Wizards.CrossPoolMigrateWizard.Filters } } - public CrossPoolMigrateCanMigrateFilter(IXenObject itemAddedToComboBox, List preSelectedVMs) + public CrossPoolMigrateCanMigrateFilter(IXenObject itemAddedToComboBox, List preSelectedVMs, bool allowSameTargetPool = true) : base(itemAddedToComboBox) { this.preSelectedVMs = preSelectedVMs; + this.allowSameTargetPool = allowSameTargetPool; } private struct FailureReason @@ -94,10 +96,15 @@ namespace XenAdmin.Wizards.CrossPoolMigrateWizard.Filters { try { - //Skip the resident host as there's a filter for it and - //if not then you could exclude intrapool migration - if(vm.resident_on == host.opaque_ref) - continue; + if (!allowSameTargetPool && IsVmInTargetsPool(vm, host)) + throw new Failure(Messages.ACTION_VM_CROSS_POOL_COPY_CANNOT_COPY_TO_SAME_POOL, new Exception()); + else + { + //Skip the resident host as there's a filter for it and + //if not then you could exclude intrapool migration + if (vm.resident_on == host.opaque_ref) + continue; + } PIF managementPif = host.Connection.Cache.PIFs.First(p => p.management); XenAPI.Network network = host.Connection.Cache.Resolve(managementPif.network); @@ -123,6 +130,20 @@ namespace XenAdmin.Wizards.CrossPoolMigrateWizard.Filters } } + private bool IsVmInTargetsPool(VM vm, Host target) + { + if (vm == null || target == null) + return false; + + var vmPool = Helpers.GetPoolOfOne(vm.Connection); + var targetPool = Helpers.GetPoolOfOne(target.Connection); + + if (targetPool == vmPool) + return true; + + return false; + } + /// /// If we have a pool, then we need to fail only when there are failures for /// each of the hosts. Otherwise any failure counts. diff --git a/XenModel/Messages.Designer.cs b/XenModel/Messages.Designer.cs index 7663b0947..0ff90a40f 100755 --- a/XenModel/Messages.Designer.cs +++ b/XenModel/Messages.Designer.cs @@ -2742,6 +2742,15 @@ namespace XenAdmin { } } + /// + /// Looks up a localized string similar to Cannot copy to resident host or pool. + /// + public static string ACTION_VM_CROSS_POOL_COPY_CANNOT_COPY_TO_SAME_POOL { + get { + return ResourceManager.GetString("ACTION_VM_CROSS_POOL_COPY_CANNOT_COPY_TO_SAME_POOL", resourceCulture); + } + } + /// /// Looks up a localized string similar to Copying VM '{0}' to '{1}'. /// diff --git a/XenModel/Messages.resx b/XenModel/Messages.resx index 26e35042b..8dc99b9cc 100755 --- a/XenModel/Messages.resx +++ b/XenModel/Messages.resx @@ -1023,6 +1023,9 @@ Copying {0} canceled + + Cannot copy to resident host or pool + Copying VM '{0}' to '{1}'