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.ComponentModel;
|
|
|
|
|
using System.Windows.Forms;
|
2018-02-05 11:53:46 +01:00
|
|
|
|
using XenAdmin.Actions;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Controls
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
public partial class SrPicker : CustomTreeView
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// Migrate is the live VDI move operation
|
|
|
|
|
public enum SRPickerType { VM, InstallFromTemplate, MoveOrCopy, Migrate, LunPerVDI };
|
|
|
|
|
private SRPickerType usage = SRPickerType.VM;
|
2016-09-27 22:14:07 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
//Used in the MovingVDI usage
|
|
|
|
|
private VDI[] existingVDIs;
|
2016-09-27 22:14:07 +02:00
|
|
|
|
public void SetExistingVDIs(VDI[] vdis)
|
|
|
|
|
{
|
|
|
|
|
existingVDIs = vdis;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private IXenConnection connection;
|
|
|
|
|
private Host affinity;
|
|
|
|
|
private SrPickerItem LastSelectedItem;
|
2018-10-08 16:35:11 +02:00
|
|
|
|
|
|
|
|
|
public event Action<object> SrSelectionChanged;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public long DiskSize = 0;
|
|
|
|
|
|
|
|
|
|
private readonly CollectionChangeEventHandler SR_CollectionChangedWithInvoke;
|
|
|
|
|
|
|
|
|
|
public SrPicker()
|
|
|
|
|
{
|
2017-01-25 13:15:59 +01:00
|
|
|
|
SR_CollectionChangedWithInvoke = Program.ProgramInvokeHandler(SR_CollectionChanged);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 12:03:36 +02:00
|
|
|
|
public override bool ShowCheckboxes
|
|
|
|
|
{
|
|
|
|
|
get { return false; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool ShowDescription
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool ShowImages
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int NodeIndent
|
|
|
|
|
{
|
|
|
|
|
get { return 3; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public SRPickerType Usage
|
|
|
|
|
{
|
|
|
|
|
set { usage = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// For new disk dialog only
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IXenConnection Connection
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
return;
|
|
|
|
|
connection = value;
|
|
|
|
|
|
|
|
|
|
Pool pool = Helpers.GetPoolOfOne(connection);
|
|
|
|
|
if (pool != null)
|
|
|
|
|
{
|
|
|
|
|
pool.PropertyChanged -= Server_PropertyChanged;
|
|
|
|
|
pool.PropertyChanged += Server_PropertyChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection.Cache.RegisterCollectionChanged<SR>(SR_CollectionChangedWithInvoke);
|
|
|
|
|
|
|
|
|
|
refresh();
|
2018-10-09 12:03:36 +02:00
|
|
|
|
foreach (SrPickerItem srItem in Items)
|
2018-02-05 11:53:46 +01:00
|
|
|
|
{
|
|
|
|
|
SrRefreshAction a = new SrRefreshAction(srItem.TheSR, true);
|
|
|
|
|
a.RunAsync();
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 12:03:36 +02:00
|
|
|
|
protected override void OnSelectedIndexChanged(EventArgs e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
base.OnSelectedIndexChanged(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
onItemSelect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onItemSelect()
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SrPickerItem item = SelectedItem as SrPickerItem;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (item == null || !item.Enabled)
|
|
|
|
|
{
|
2018-10-08 16:35:11 +02:00
|
|
|
|
if (SrSelectionChanged != null)
|
|
|
|
|
SrSelectionChanged(null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-27 11:20:23 +02:00
|
|
|
|
|
2018-10-08 16:35:11 +02:00
|
|
|
|
if (SrSelectionChanged != null)
|
|
|
|
|
SrSelectionChanged(item);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (!item.Enabled && LastSelectedItem != null && LastSelectedItem.TheSR.opaque_ref != item.TheSR.opaque_ref)
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SelectedItem = LastSelectedItem;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
else if (!item.Enabled && LastSelectedItem != null && LastSelectedItem.TheSR.opaque_ref == item.TheSR.opaque_ref)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SrPickerItem first = Items[0] as SrPickerItem;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (first != null && first.Enabled)
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SelectedItem = first;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
else
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SelectedItem = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
LastSelectedItem = item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SR SR
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
return SelectedItem is SrPickerItem && (SelectedItem as SrPickerItem).Enabled ? (SelectedItem as SrPickerItem).TheSR : null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SR DefaultSR = null;
|
|
|
|
|
|
|
|
|
|
public void SetAffinity(Host host)
|
|
|
|
|
{
|
|
|
|
|
affinity = host;
|
|
|
|
|
refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly SrPickerItemFactory itemFactory = new SrPickerItemFactory();
|
|
|
|
|
|
|
|
|
|
public void refresh()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
SR selectedSr = SR;
|
|
|
|
|
bool selected = false;
|
2018-10-09 12:03:36 +02:00
|
|
|
|
BeginUpdate();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
ClearAllNodes();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
foreach (SR sr in connection.Cache.SRs)
|
|
|
|
|
{
|
2018-10-08 16:28:46 +02:00
|
|
|
|
SrPickerItem item = itemFactory.PickerItem(sr, usage, affinity, DiskSize, existingVDIs);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (item.Show)
|
2018-10-09 12:03:36 +02:00
|
|
|
|
AddNode(item);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
foreach (PBD pbd in sr.Connection.ResolveAll(sr.PBDs))
|
|
|
|
|
{
|
|
|
|
|
if (pbd != null)
|
|
|
|
|
{
|
|
|
|
|
pbd.PropertyChanged -= Server_PropertyChanged;
|
|
|
|
|
pbd.PropertyChanged += Server_PropertyChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sr.PropertyChanged -= Server_PropertyChanged;
|
|
|
|
|
sr.PropertyChanged += Server_PropertyChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
EndUpdate();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selectedSr != null)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
foreach (SrPickerItem node in Items)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (node.TheSR != null && node.TheSR.uuid == selectedSr.uuid)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SelectedItem = node;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
onItemSelect();
|
|
|
|
|
selected = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 12:03:36 +02:00
|
|
|
|
if (!selected && Items.Count > 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{// If no selection made, select default SR
|
|
|
|
|
if (!selectDefaultSR())
|
|
|
|
|
{
|
|
|
|
|
// If no default SR, select first entry in list
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SelectedIndex = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
onItemSelect();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-18 17:33:25 +02:00
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2015-09-18 17:33:25 +02:00
|
|
|
|
public void UpdateDiskSize()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
foreach (SrPickerItem node in Items)
|
2015-09-18 17:33:25 +02:00
|
|
|
|
{
|
2018-10-08 16:28:46 +02:00
|
|
|
|
node.UpdateDiskSize(DiskSize);
|
2015-09-18 17:33:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
Refresh();
|
2015-09-18 17:33:25 +02:00
|
|
|
|
onItemSelect();
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Server_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.PropertyName == "name_label" || e.PropertyName == "PBDs" || e.PropertyName == "physical_utilisation" || e.PropertyName == "currently_attached" || e.PropertyName == "default_SR")
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, refresh);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SR_CollectionChanged(object sender, CollectionChangeEventArgs e)
|
|
|
|
|
{
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, refresh);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-25 13:15:59 +01:00
|
|
|
|
private void UnregisterHandlers()
|
|
|
|
|
{
|
|
|
|
|
if (connection == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var pool = Helpers.GetPoolOfOne(connection);
|
|
|
|
|
if (pool != null)
|
|
|
|
|
pool.PropertyChanged -= Server_PropertyChanged;
|
|
|
|
|
|
|
|
|
|
foreach (var sr in connection.Cache.SRs)
|
|
|
|
|
{
|
|
|
|
|
foreach (var pbd in sr.Connection.ResolveAll(sr.PBDs))
|
|
|
|
|
{
|
|
|
|
|
if (pbd != null)
|
|
|
|
|
pbd.PropertyChanged -= Server_PropertyChanged;
|
|
|
|
|
}
|
|
|
|
|
sr.PropertyChanged -= Server_PropertyChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection.Cache.DeregisterCollectionChanged<SR>(SR_CollectionChangedWithInvoke);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Selects the default SR, if it exists.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>true if the default SR was selected, otherwise false.</returns>
|
|
|
|
|
public bool selectDefaultSR()
|
|
|
|
|
{
|
|
|
|
|
if (DefaultSR == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
2018-10-09 12:03:36 +02:00
|
|
|
|
foreach (SrPickerItem node in Items)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (node.TheSR != null && node.TheSR == DefaultSR && node.Enabled)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SelectedItem = node;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void selectSRorNone(SR TheSR)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
foreach (SrPickerItem node in Items)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (node.TheSR != null && node.TheSR.opaque_ref == TheSR.opaque_ref)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SelectedItem = node;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 16:35:11 +02:00
|
|
|
|
if (SrSelectionChanged != null)
|
|
|
|
|
SrSelectionChanged(null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void selectDefaultSROrAny()
|
|
|
|
|
{
|
|
|
|
|
if (selectDefaultSR())
|
|
|
|
|
return;
|
2018-10-09 12:03:36 +02:00
|
|
|
|
foreach (SrPickerItem item in Items)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (item.Enabled)
|
|
|
|
|
{
|
|
|
|
|
selectSRorNone(item.TheSR);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-08 16:35:11 +02:00
|
|
|
|
if (SrSelectionChanged != null)
|
|
|
|
|
SrSelectionChanged(null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void selectSRorDefaultorAny(SR sr)
|
|
|
|
|
{
|
|
|
|
|
if (sr != null)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
foreach (SrPickerItem node in Items)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (node.TheSR != null && node.TheSR.opaque_ref == sr.opaque_ref)
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
SelectedItem = node;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
selectDefaultSROrAny();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ValidSelectionExists
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-10-09 12:03:36 +02:00
|
|
|
|
foreach (SrPickerItem item in Items)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (item.Enabled)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-09 12:03:36 +02:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
UnregisterHandlers();
|
|
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|