CA-339370: Assign the new VM's name at a late stage to reduce duplicate names. Some simplifications.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2020-05-12 13:27:33 +01:00 committed by Mihaela Stoica
parent 7b7e388bb8
commit 8a478011d9
7 changed files with 49 additions and 68 deletions

View File

@ -244,8 +244,8 @@ namespace XenAdmin.Dialogs
private void Scan()
{
DelegatedAsyncAction saveVMsAction = new DelegatedAsyncAction(connection, Messages.SAVING_VMS_ACTION_TITLE,
Messages.SAVING_VMS_ACTION_DESC, Messages.COMPLETED, delegate(Session session)
DelegatedAsyncAction saveVMsAction = new DelegatedAsyncAction(connection, Messages.SAVING_VM_PROPERTIES_ACTION_TITLE,
Messages.SAVING_VM_PROPERTIES_ACTION_DESC, Messages.COMPLETED, delegate(Session session)
{
//Save Evacuated VMs for later
host.SaveEvacuatedVMs(session);

View File

@ -206,14 +206,14 @@ namespace XenAdmin.Actions.VMActions
// given the choice of a fast-clone (VM.clone) or a full-copy (VM.copy) on the storage page of the wizard. If the
// user chose a VM.clone, then FullCopySR will be null.
RelatedTask = VM.async_copy(Session, Template.opaque_ref, HiddenVmName, FullCopySR.opaque_ref);
RelatedTask = VM.async_copy(Session, Template.opaque_ref, Helpers.MakeHiddenName(NameLabel), FullCopySR.opaque_ref);
}
else
{
// if the target disks are on mixed storage or the user chose to a do a fast-clone on the storage
// page then we end up here.
RelatedTask = VM.async_clone(Session, Template.opaque_ref, HiddenVmName);
RelatedTask = VM.async_clone(Session, Template.opaque_ref, Helpers.MakeHiddenName(NameLabel));
}
Description = string.Format(Messages.CLONING_TEMPLATE, Helpers.GetName(Template));
@ -728,14 +728,6 @@ namespace XenAdmin.Actions.VMActions
}
}
private string HiddenVmName
{
get
{
return Helpers.MakeHiddenName(NameLabel);
}
}
private List<string> AllowedVBDs
{
get

View File

@ -37,19 +37,18 @@ using XenAPI;
namespace XenAdmin.Actions.VMActions
{
/// <summary>
/// This is for the "Instant VM From Template" Command.
/// </summary>
public class CreateVMFastAction : AsyncAction
{
private readonly String _nameLabel;
private readonly bool _markVmAsBeingCreated;
public CreateVMFastAction(IXenConnection connection, VM template, bool markVmAsBeingCreated = true)
: base(connection, Messages.INSTANT_VM_CREATE_TITLE, string.Format(Messages.INSTANT_VM_CREATE_DESCRIPTION, Helpers.DefaultVMName(Helpers.GetName(template), connection), Helpers.GetName(template)))
: base(connection, string.Format(Messages.INSTANT_VM_CREATE_TITLE, Helpers.GetName(template)), "")
{
//CA-339370: the VM's name is calculated at a later stage to avoid duplicate
//names in the case of creating multiple VMs in quick succession;
//it comes with the downside that no VM name is shown on the title
Template = template;
_nameLabel = Helpers.DefaultVMName(Helpers.GetName(Template), Connection);
_markVmAsBeingCreated = markVmAsBeingCreated;
ApiMethodsToRoleCheck.AddRange(Role.CommonTaskApiList);
@ -62,7 +61,10 @@ namespace XenAdmin.Actions.VMActions
protected override void Run()
{
RelatedTask = XenAPI.VM.async_clone(Session, Template.opaque_ref, Helpers.MakeHiddenName(_nameLabel));
var originalName = Helpers.GetName(Template);
Description = string.Format(Messages.CLONING_TEMPLATE, originalName);
RelatedTask = VM.async_clone(Session, Template.opaque_ref, Helpers.MakeHiddenName(originalName));
PollToCompletion(0, 80);
string new_vm_ref = Result;
@ -75,19 +77,23 @@ namespace XenAdmin.Actions.VMActions
XenAdminConfigManager.Provider.HideObject(new_vm_ref);
RelatedTask = XenAPI.VM.async_provision(Session, new_vm_ref);
Description = Messages.PROVISIONING_VM;
RelatedTask = VM.async_provision(Session, new_vm_ref);
PollToCompletion(80, 90);
VM.set_name_label(Session, new_vm_ref, _nameLabel);
Description = Messages.SAVING_VM_PROPERTIES_ACTION_TITLE;
var newName = Helpers.DefaultVMName(originalName, Connection);
VM.set_name_label(Session, new_vm_ref, newName);
XenAdminConfigManager.Provider.ShowObject(new_vm_ref);
if (_markVmAsBeingCreated)
{
VM.name_label = _nameLabel; //the set_name_label method takes some time, we want to show the VM right away
VM.name_label = newName; //the set_name_label method takes some time, we want to show the VM right away
VM.IsBeingCreated = false;
}
Result = new_vm_ref;
Description = string.Format(Messages.INSTANT_VM_CREATE_DESC_COMPLETED, newName);
}
}
}

View File

@ -43,7 +43,7 @@ namespace XenAdmin.Actions.VMActions
private string Val;
public SetVMOtherConfigAction(IXenConnection connection, VM vm, string key, string val)
: base(connection, Messages.ACTION_SET_VM_OTHER_CONFIG_TITLE, true)
: base(connection, Messages.SAVING_VM_PROPERTIES_ACTION_TITLE, true)
{
VM = vm;
Key = key;

View File

@ -19,7 +19,7 @@ namespace XenAdmin {
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class Messages {
@ -2076,15 +2076,6 @@ namespace XenAdmin {
}
}
/// <summary>
/// Looks up a localized string similar to Saving VM Configuration.
/// </summary>
public static string ACTION_SET_VM_OTHER_CONFIG_TITLE {
get {
return ResourceManager.GetString("ACTION_SET_VM_OTHER_CONFIG_TITLE", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Shut down.
/// </summary>
@ -21218,16 +21209,16 @@ namespace XenAdmin {
}
/// <summary>
/// Looks up a localized string similar to Creating VM &apos;{0}&apos; from template &apos;{1}&apos;.
/// Looks up a localized string similar to Created VM &apos;{0}&apos;.
/// </summary>
public static string INSTANT_VM_CREATE_DESCRIPTION {
public static string INSTANT_VM_CREATE_DESC_COMPLETED {
get {
return ResourceManager.GetString("INSTANT_VM_CREATE_DESCRIPTION", resourceCulture);
return ResourceManager.GetString("INSTANT_VM_CREATE_DESC_COMPLETED", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Instant VM create.
/// Looks up a localized string similar to Creating VM quickly from template &apos;{0}&apos;.
/// </summary>
public static string INSTANT_VM_CREATE_TITLE {
get {
@ -32992,18 +32983,18 @@ namespace XenAdmin {
/// <summary>
/// Looks up a localized string similar to Saving configuration....
/// </summary>
public static string SAVING_VMS_ACTION_DESC {
public static string SAVING_VM_PROPERTIES_ACTION_DESC {
get {
return ResourceManager.GetString("SAVING_VMS_ACTION_DESC", resourceCulture);
return ResourceManager.GetString("SAVING_VM_PROPERTIES_ACTION_DESC", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Saving VM Configuration.
/// Looks up a localized string similar to Saving VM properties.
/// </summary>
public static string SAVING_VMS_ACTION_TITLE {
public static string SAVING_VM_PROPERTIES_ACTION_TITLE {
get {
return ResourceManager.GetString("SAVING_VMS_ACTION_TITLE", resourceCulture);
return ResourceManager.GetString("SAVING_VM_PROPERTIES_ACTION_TITLE", resourceCulture);
}
}

View File

@ -789,9 +789,6 @@
<data name="ACTION_SET_SECONDARY_MANAGEMENT_PURPOSE_TITLE" xml:space="preserve">
<value>Renaming server network interface</value>
</data>
<data name="ACTION_SET_VM_OTHER_CONFIG_TITLE" xml:space="preserve">
<value>Saving VM Configuration</value>
</data>
<data name="ACTION_SHUTDOWN_AND_DESTROY_VMS_TITLE" xml:space="preserve">
<value>Delete VMs</value>
</data>
@ -7368,11 +7365,11 @@ This might result in failure to migrate VMs to this server during the RPU or to
<data name="INSTALL_XENSERVER_TOOLS_BUTTON" xml:space="preserve">
<value>&amp;Install [Citrix VM Tools]</value>
</data>
<data name="INSTANT_VM_CREATE_DESCRIPTION" xml:space="preserve">
<value>Creating VM '{0}' from template '{1}'</value>
<data name="INSTANT_VM_CREATE_DESC_COMPLETED" xml:space="preserve">
<value>Created VM '{0}'</value>
</data>
<data name="INSTANT_VM_CREATE_TITLE" xml:space="preserve">
<value>Instant VM create</value>
<value>Creating VM quickly from template '{0}'</value>
</data>
<data name="INTEGRATED_GPU_PASSTHROUGH_DISABLED" xml:space="preserve">
<value>This server is currently not using the integrated GPU.</value>
@ -11445,11 +11442,11 @@ The master must be upgraded first, so if you skip the master, the rolling pool u
<data name="SAVING_SEARCH" xml:space="preserve">
<value>Saving search '{0}'...</value>
</data>
<data name="SAVING_VMS_ACTION_DESC" xml:space="preserve">
<value>Saving configuration...</value>
<data name="SAVING_VM_PROPERTIES_ACTION_TITLE" xml:space="preserve">
<value>Saving VM properties</value>
</data>
<data name="SAVING_VMS_ACTION_TITLE" xml:space="preserve">
<value>Saving VM Configuration</value>
<data name="SAVING_VM_PROPERTIES_ACTION_DESC" xml:space="preserve">
<value>Saving configuration...</value>
</data>
<data name="SAVING_WLB_CONFIGURATION" xml:space="preserve">
<value>Saving Workload Balancing configuration.</value>
@ -14575,4 +14572,4 @@ Any disk in your VM's DVD drive will be ejected when installing [Citrix VM Tools
<data name="YOU_ARE_HERE" xml:space="preserve">
<value>You are here</value>
</data>
</root>
</root>

View File

@ -1587,22 +1587,17 @@ namespace XenAdmin.Core
public static string DefaultVMName(string p, IXenConnection connection)
{
for (int i = 1; true; i++)
int i = 0;
do
{
bool willDo = true;
string name = string.Format(Messages.NEWVM_DEFAULTNAME, p, i);
string hiddenName = Helpers.GuiTempObjectPrefix + name;
// Check to see if name is in use
foreach (VM v in connection.Cache.VMs)
{
if (v.name_label == name || v.name_label == hiddenName)
{
willDo = false;
break;
}
}
if (willDo) return name;
}
string name = string.Format(Messages.NEWVM_DEFAULTNAME, p, ++i);
string hiddenName = MakeHiddenName(name);
if (connection.Cache.VMs.Any(v => v.name_label == name || v.name_label == hiddenName))
continue;
return name;
} while (true);
}
public static bool CDsExist(IXenConnection connection)