CP-42691: Use allowed operations to enable/disable vTPM create/destroy.

Signed-off-by: Konstantina Chremmou <Konstantina.Chremmou@cloud.com>
This commit is contained in:
Konstantina Chremmou 2023-04-11 15:41:31 +01:00
parent b93a61673d
commit 6a06d5b8d9
7 changed files with 91 additions and 13 deletions

View File

@ -175,6 +175,22 @@ namespace XenAdmin.Dialogs
dlg.ShowDialog(this);
}
private bool CanRemoveVtpm(VTPM vtpm, out string cannotReason)
{
cannotReason = null;
if (Helpers.XapiEqualOrGreater_23_10_0(vtpm.Connection))
{
if (vtpm.allowed_operations.Contains(vtpm_operations.destroy))
return true;
cannotReason = Messages.VTPM_OPERATION_DISALLOWED_REMOVE;
return false;
}
return _vm.CanRemoveVtpm(out cannotReason);
}
private void ResizeVerticalTabs()
{
int maxHeight = splitContainer.Panel1.Height - toolTipContainerAdd.Height;
@ -182,12 +198,12 @@ namespace XenAdmin.Dialogs
toolTipContainerAdd.Top = verticalTabs.Top + verticalTabs.Height;
}
private void ShowTooltip(Point location)
private void ShowTooltip(VtpmManagementPage page, Point location)
{
if (!_toolTipVisible)
{
Cursor = Cursors.Hand;
var msg = _vm.CanRemoveVtpm(out var cannotReason) ? Messages.VTPM_REMOVE : cannotReason;
var msg = CanRemoveVtpm(page.Vtpm, out var cannotReason) ? Messages.VTPM_REMOVE : cannotReason;
if (!string.IsNullOrEmpty(cannotReason))
{
@ -231,7 +247,7 @@ namespace XenAdmin.Dialogs
if (!(verticalTabs.Items[pageIndex] is VtpmManagementPage page))
return;
var deleteIcon = _vm.CanRemoveVtpm(out _) ? Images.StaticImages._000_Abort_h32bit_16 : Images.StaticImages._000_Abort_gray_h32bit_16;
var deleteIcon = CanRemoveVtpm(page.Vtpm, out _) ? Images.StaticImages._000_Abort_h32bit_16 : Images.StaticImages._000_Abort_gray_h32bit_16;
page.DeleteIconBounds = new Rectangle(e.Bounds.Right - deleteIcon.Width - (32 - deleteIcon.Width) / 2,
e.Bounds.Y + (32 - deleteIcon.Height) / 2, deleteIcon.Width, deleteIcon.Height);
@ -249,7 +265,7 @@ namespace XenAdmin.Dialogs
return;
if (page.DeleteIconBounds.Contains(e.Location))
ShowTooltip(e.Location);
ShowTooltip(page, e.Location);
else
HideTooltip();
}
@ -263,7 +279,7 @@ namespace XenAdmin.Dialogs
if (!(verticalTabs.Items[pageIndex] is VtpmManagementPage page))
return;
if (page.DeleteIconBounds.Contains(e.Location) && _vm.CanRemoveVtpm(out _))
if (page.DeleteIconBounds.Contains(e.Location) && CanRemoveVtpm(page.Vtpm, out _))
RemoveVtpm(page.Vtpm);
}

View File

@ -81,9 +81,7 @@ namespace XenAdmin.Dialogs
private void UpdateButtons()
{
var vm = Vtpm.Connection.Resolve(Vtpm.VM);
string cannotReason = null;
buttonRemove.Enabled = vm!= null && vm.CanRemoveVtpm(out cannotReason);
buttonRemove.Enabled = CanRemoveVtpm(out string cannotReason);
if (buttonRemove.Enabled)
toolTipContainerRemove.RemoveAll();
@ -91,13 +89,33 @@ namespace XenAdmin.Dialogs
toolTipContainerRemove.SetToolTip(cannotReason);
}
private bool CanRemoveVtpm(out string cannotReason)
{
cannotReason = null;
if (Helpers.XapiEqualOrGreater_23_10_0(Vtpm.Connection))
{
if (Vtpm.allowed_operations.Contains(vtpm_operations.destroy))
return true;
cannotReason = Messages.VTPM_OPERATION_DISALLOWED_REMOVE;
return false;
}
var vm = Vtpm.Connection.Resolve(Vtpm.VM);
return vm != null && vm.CanRemoveVtpm(out cannotReason);
}
private void Vtpm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (!(sender is VTPM))
if (!(sender is VTPM vtpm))
return;
if (e.PropertyName == "is_protected" || e.PropertyName == "is_unique")
Program.Invoke(this, UpdateProperties);
if (e.PropertyName == "allowed_operations" && Helpers.XapiEqualOrGreater_23_10_0(vtpm.Connection))
Program.Invoke(this, UpdateButtons);
}
private void buttonRemove_Click(object sender, EventArgs e)

View File

@ -275,8 +275,13 @@ namespace XenAdmin.Actions.VMActions
private void AssignVtpm()
{
if (_assignVtpm)
new NewVtpmAction(Connection, VM).RunSync(Session);
if (!_assignVtpm)
return;
if (Helpers.XapiEqualOrGreater_23_10_0(Connection) && !VM.allowed_operations.Contains(vm_operations.create_vtpm))
return;
new NewVtpmAction(Connection, VM).RunSync(Session);
}
private void AssignVgpu()

View File

@ -40165,6 +40165,24 @@ namespace XenAdmin {
}
}
/// <summary>
/// Looks up a localized string similar to Attaching a new vTPM to this VM is currently not allowed..
/// </summary>
public static string VTPM_OPERATION_DISALLOWED_ADD {
get {
return ResourceManager.GetString("VTPM_OPERATION_DISALLOWED_ADD", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Removing this vTPM from the VM is currently not allowed..
/// </summary>
public static string VTPM_OPERATION_DISALLOWED_REMOVE {
get {
return ResourceManager.GetString("VTPM_OPERATION_DISALLOWED_REMOVE", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to You need to shut down this VM before you can attach a new vTPM to it..
/// </summary>

View File

@ -13873,6 +13873,12 @@ Schedule:
<data name="VTPM_NONE_ATTACHED" xml:space="preserve">
<value>There are no vTPMs attached to this VM.</value>
</data>
<data name="VTPM_OPERATION_DISALLOWED_ADD" xml:space="preserve">
<value>Attaching a new vTPM to this VM is currently not allowed.</value>
</data>
<data name="VTPM_OPERATION_DISALLOWED_REMOVE" xml:space="preserve">
<value>Removing this vTPM from the VM is currently not allowed.</value>
</data>
<data name="VTPM_POWER_STATE_WRONG_ATTACH" xml:space="preserve">
<value>You need to shut down this VM before you can attach a new vTPM to it.</value>
</data>

View File

@ -450,6 +450,12 @@ namespace XenAdmin.Core
return coordinator == null || ProductVersionCompare(coordinator.GetXapiVersion(), "22.33.0") >= 0;
}
public static bool XapiEqualOrGreater_23_10_0(IXenConnection conn)
{
var coordinator = GetCoordinator(conn);
return coordinator == null || ProductVersionCompare(coordinator.GetXapiVersion(), "23.10.0") >= 0;
}
#endregion
}
}

View File

@ -1797,9 +1797,18 @@ namespace XenAPI
{
cannotReason = null;
if (VTPMs.Count >= VM.MAX_ALLOWED_VTPMS)
if (Helpers.XapiEqualOrGreater_23_10_0(Connection))
{
cannotReason = string.Format(Messages.VTPM_MAX_REACHED, VM.MAX_ALLOWED_VTPMS);
if (allowed_operations.Contains(vm_operations.create_vtpm))
return true;
cannotReason = Messages.VTPM_OPERATION_DISALLOWED_ADD;
return false;
}
if (VTPMs.Count >= MAX_ALLOWED_VTPMS)
{
cannotReason = string.Format(Messages.VTPM_MAX_REACHED, MAX_ALLOWED_VTPMS);
return false;
}