2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* 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;
|
|
|
|
|
using System.Collections.Generic;
|
2018-10-09 12:03:36 +02:00
|
|
|
|
using XenAdmin.Actions.VMActions;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Controls;
|
|
|
|
|
using XenAdmin.Core;
|
2018-10-09 12:03:36 +02:00
|
|
|
|
using XenAPI;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Dialogs
|
|
|
|
|
{
|
|
|
|
|
public partial class CopyVMDialog : XenDialogBase
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
private readonly VM TheVM;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private readonly bool IsRealVm;
|
|
|
|
|
|
|
|
|
|
public CopyVMDialog(VM vm)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
this.MinimumSize = this.Size;
|
|
|
|
|
IsRealVm = !vm.is_a_template;
|
|
|
|
|
TheVM = vm;
|
|
|
|
|
srPicker1.Usage = SrPicker.SRPickerType.MoveOrCopy;
|
|
|
|
|
Host affinity = TheVM.Home();
|
|
|
|
|
srPicker1.Connection = TheVM.Connection;
|
2017-09-03 04:33:29 +02:00
|
|
|
|
srPicker1.DiskSize = vm.TotalVMSize();
|
2018-10-09 12:03:36 +02:00
|
|
|
|
labelSrHint.Text = IsRealVm ? Messages.COPY_VM_SELECT_SR : Messages.COPY_TEMPLATE_SELECT_SR;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
srPicker1.SetAffinity(affinity);
|
|
|
|
|
Pool pool = Helpers.GetPoolOfOne(vm.Connection);
|
|
|
|
|
if (pool != null)
|
|
|
|
|
srPicker1.DefaultSR = vm.Connection.Resolve(pool.default_SR);
|
|
|
|
|
|
|
|
|
|
NameTextBox.Text = GetDefaultCopyName(TheVM);
|
|
|
|
|
|
|
|
|
|
Text = IsRealVm ? Messages.COPY_VM : Messages.COPY_TEMPLATE;
|
|
|
|
|
|
|
|
|
|
bool allow_copy = !vm.is_a_template || vm.allowed_operations.Contains(vm_operations.copy);
|
2017-09-03 04:33:29 +02:00
|
|
|
|
bool anyDiskFastCloneable = vm.AnyDiskFastClonable();
|
|
|
|
|
bool hasAtLeastOneDisk = vm.HasAtLeastOneDisk();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
CopyRadioButton.Enabled = allow_copy && hasAtLeastOneDisk;
|
|
|
|
|
FastClonePanel.Enabled = !allow_copy || anyDiskFastCloneable || !hasAtLeastOneDisk;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (!FastClonePanel.Enabled)
|
|
|
|
|
{
|
|
|
|
|
CloneRadioButton.Checked = false;
|
|
|
|
|
}
|
|
|
|
|
toolTipContainer1.SetToolTip(Messages.FAST_CLONE_UNAVAILABLE);
|
2017-09-03 04:33:29 +02:00
|
|
|
|
if (vm.is_a_template && !(anyDiskFastCloneable || allow_copy))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
CloneRadioButton.Text = Messages.COPY_VM_CLONE_TEMPLATE_SLOW;
|
|
|
|
|
FastCloneDescription.Text = Messages.COPY_VM_SLOW_CLONE_DESCRIPTION;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FastCloneDescription.Text = !vm.is_a_template ? Messages.COPY_VM_FAST_CLONE_DESCRIPTION : Messages.COPY_TEMPLATE_FAST_CLONE_DESCRIPTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!CloneRadioButton.Enabled)
|
|
|
|
|
CopyRadioButton.Checked = true;
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
if (vm.DescriptionType() != VM.VmDescriptionType.None)
|
|
|
|
|
DescriptionTextBox.Text = vm.Description();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2018-10-09 12:03:36 +02:00
|
|
|
|
srPicker1.Invalidate();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
srPicker1.selectDefaultSROrAny();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 01:05:33 +02:00
|
|
|
|
private void srPicker1_SelectedIndexChanged(object sender, EventArgs e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
EnableMoveButton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void NameTextBox_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
EnableMoveButton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnableMoveButton()
|
|
|
|
|
{
|
|
|
|
|
MoveButton.Enabled = NameTextBox.Text.Trim().Length > 0 && srPicker1.SR != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CloseButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MoveButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
if (!tableLayoutPanelSrPicker.Enabled || CloneRadioButton.Checked)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
new VMCloneAction(TheVM,NameTextBox.Text, DescriptionTextBox.Text).RunAsync();
|
|
|
|
|
Close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (srPicker1.SR == null)
|
|
|
|
|
return;
|
|
|
|
|
//TODO this
|
|
|
|
|
//bool sameSr = true;
|
|
|
|
|
//XenObject<SR> vm_sr = null;
|
|
|
|
|
//foreach (XenObject<VBD> vbd in TheVM.Connection.ResolveAll(TheVM.VBDs))
|
|
|
|
|
//{
|
|
|
|
|
// if (vbd.type != vbd_type.Disk)
|
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
|
|
// XenObject<VDI> vdi = TheVM.Connection.Resolve(vbd.VDI);
|
|
|
|
|
// if (vdi == null)
|
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
|
|
// XenObject<SR> sr = TheVM.Connection.Resolve(vdi.SR);
|
|
|
|
|
// if(sr == null)
|
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
|
|
// if (vm_sr == null)
|
|
|
|
|
// {
|
|
|
|
|
// vm_sr = sr;
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (vm_sr.opaque_ref != sr.opaque_ref)
|
|
|
|
|
// {
|
|
|
|
|
// sameSr = false;
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//if (sameSr && vm_sr != null && srPicker1.SR.opaque_ref == vm_sr.opaque_ref) // if we dont change the sr use clone
|
|
|
|
|
//{
|
|
|
|
|
// action = new VmAction(TheVM.Connection, TheVM, TheVM.GetStorageHost(TheVM.Connection), NameTextBox.Text);
|
|
|
|
|
//}
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
var action = new VMCopyAction(TheVM, TheVM.GetStorageHost(false), srPicker1.SR, NameTextBox.Text, DescriptionTextBox.Text);
|
|
|
|
|
//}
|
|
|
|
|
action.RunAsync();
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string GetDefaultCopyName(VM vmToCopy)
|
|
|
|
|
{
|
|
|
|
|
List<string> takenNames = new List<string>();
|
|
|
|
|
foreach (VM vm in vmToCopy.Connection.Cache.VMs)
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
takenNames.Add(vm.Name());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2017-09-03 04:33:29 +02:00
|
|
|
|
return Helpers.MakeUniqueName(string.Format(Messages.ACTION_TEMPLATE_CLONE_NEW_NAME, vmToCopy.Name()), takenNames);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void radioButton2_CheckedChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
tableLayoutPanelSrPicker.Enabled = CopyRadioButton.Checked;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
// Since the radiobuttons aren't in the same panel, we have to do manual mutual exclusion
|
|
|
|
|
CloneRadioButton.Checked = !CopyRadioButton.Checked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CloneRadioButton_CheckedChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Since the radiobuttons aren't in the same panel, we have to do manual mutual exclusion
|
|
|
|
|
CopyRadioButton.Checked = !CloneRadioButton.Checked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CopyVMDialog_Shown(object sender, EventArgs e)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
tableLayoutPanelSrPicker.Enabled = CopyRadioButton.Enabled && CopyRadioButton.Checked;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|