protected bool CanExecute(VDI vdi) { if (vdi == null) { return(false); } foreach (VBD vbd in vdi.Connection.ResolveAll <VBD>(vdi.VBDs)) { if (targetVM == null || vbd.VM.opaque_ref == targetVM.opaque_ref) { if (!vbd.currently_attached) { continue; } DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow, vbd); if (!cmd.CanExecute()) { return(false); } } } return(true); }
protected override string GetCantExecuteReasonCore(SelectedItem item) { VDI vdi = item.XenObject as VDI; if (vdi == null) { return(base.GetCantExecuteReasonCore(item)); } foreach (VBD vbd in vdi.Connection.ResolveAll <VBD>(vdi.VBDs)) { if (targetVM == null || vbd.VM.opaque_ref == targetVM.opaque_ref) { if (!vbd.currently_attached) { continue; } DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow, vbd); if (!cmd.CanExecute()) { var reasons = cmd.GetCantExecuteReasons(); return(reasons.Count > 0 ? reasons.ElementAt(0).Value : Messages.UNKNOWN); } } } return(base.GetCantExecuteReasonCore(item)); }
protected override string GetCantExecuteReasonCore(SelectedItem item) { VDI vdi = item.XenObject as VDI; if (vdi == null) { return(base.GetCantExecuteReasonCore(item)); } foreach (VBD vbd in vdi.Connection.ResolveAll <VBD>(vdi.VBDs)) { if (targetVM == null || vbd.VM.opaque_ref == targetVM.opaque_ref) { if (!vbd.currently_attached) { continue; } DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow, vbd); if (!cmd.CanExecute()) { return(cmd.ToolTipText); } } } return(base.GetCantExecuteReasonCore(item)); }
protected override string GetCantExecuteReasonCore(IXenObject item) { VDI vdi = item as VDI; if (vdi == null) { return(base.GetCantExecuteReasonCore(item)); } SR sr = vdi.Connection.Resolve <SR>(vdi.SR); if (sr == null) { return(Messages.SR_COULD_NOT_BE_CONTACTED); } VDI.FriendlyType vdiType = vdi.VDIType(); if (vdi.Locked) { return(vdiType == VDI.FriendlyType.SNAPSHOT ? Messages.CANNOT_DELETE_SNAPSHOT_IN_USE : vdiType == VDI.FriendlyType.ISO ? Messages.CANNOT_DELETE_ISO_IN_USE : Messages.CANNOT_DELETE_VD_IN_USE); } if (sr.Physical()) { return(FriendlyErrorNames.VDI_IS_A_PHYSICAL_DEVICE); } if (sr.IsToolsSR()) { return(Messages.CANNOT_DELETE_TOOLS_SR); } if (vdi.IsUsedByHA()) { return(Messages.CANNOT_DELETE_HA_VD); } if (vdi.IsMetadataForDR()) { return(Messages.CANNOT_DELETE_DR_VD); } List <VBD> vbds = vdi.Connection.ResolveAll <VBD>(vdi.VBDs); if (vbds.Count > 1 && !AllowMultipleVBDDelete) { return(Messages.CANNOT_DELETE_VDI_MULTIPLE_VBDS); } foreach (VBD vbd in vbds) { VM vm = vdi.Connection.Resolve <VM>(vbd.VM); if (vdiType == VDI.FriendlyType.SYSTEM_DISK) { if (vm.power_state == vm_power_state.Running) { return(string.Format( Messages.CANNOT_DELETE_IN_USE_SYS_VD, Helpers.GetName(vm).Ellipsise(20))); } } if (vbd.Locked) { return(vdiType == VDI.FriendlyType.SNAPSHOT ? Messages.CANNOT_DELETE_SNAPSHOT_IN_USE : vdiType == VDI.FriendlyType.ISO ? Messages.CANNOT_DELETE_ISO_IN_USE : Messages.CANNOT_DELETE_VD_IN_USE); } if (vbd.currently_attached) { if (!AllowRunningVMDelete) { return(string.Format(Messages.CANNOT_DELETE_VDI_ACTIVE_ON, Helpers.GetName(vm).Ellipsise(20))); } DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow, vbd); if (!cmd.CanExecute()) { var reasons = cmd.GetCantExecuteReasons(); return(reasons.Count > 0 ? string.Format(Messages.CANNOT_DELETE_CANNOT_DEACTIVATE_REASON, Helpers.GetName(vm).Ellipsise(20), reasons.ElementAt(0).Value) : Messages.UNKNOWN); } } } // This is a necessary final check, there are other blocking reasons non covered in this method // Known examples: // - someone else is calling a delete on this vdi already, altering the allowed ops // - the storage manager cannot perform a delete on the SR due to drivers if (!vdi.allowed_operations.Contains(vdi_operations.destroy)) { return(vdiType == VDI.FriendlyType.SNAPSHOT ? Messages.CANNOT_DELETE_SNAPSHOT_GENERIC : vdiType == VDI.FriendlyType.ISO ? Messages.CANNOT_DELETE_ISO_GENERIC : Messages.CANNOT_DELETE_VD_GENERIC); } return(base.GetCantExecuteReasonCore(item)); }
protected bool CanExecute(VDI vdi) { if (vdi == null) { return(false); } SR sr = vdi.Connection.Resolve <SR>(vdi.SR); if (sr == null) { return(false); } if (vdi.Locked) { return(false); } if (sr.Physical()) { return(false); } if (sr.IsToolsSR()) { return(false); } if (vdi.IsUsedByHA()) { return(false); } List <VBD> vbds = vdi.Connection.ResolveAll <VBD>(vdi.VBDs); if (vbds.Count > 1 && !AllowMultipleVBDDelete) { return(false); } foreach (VBD vbd in vbds) { VM vm = vdi.Connection.Resolve <VM>(vbd.VM); if (vdi.type == vdi_type.system) { if (vm.power_state == vm_power_state.Running) { return(false); } } if (vbd.Locked) { return(false); } if (vbd.currently_attached) { //Check if we can unplug DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow, vbd); if (!AllowRunningVMDelete || !cmd.CanExecute()) { return(false); } } } if (sr.HBALunPerVDI()) { return(true); } if (!vdi.allowed_operations.Contains(vdi_operations.destroy)) { if (AllowRunningVMDelete) { // We deliberately DONT call allowed operations because we assume we know better :) // Xapi will think we can't delete because VBDs are plugged. We are going to unplug them. // Known risks of this method that will make us fail because we are disrespecting xapi: // - someone else is calling a delete on this vdi already, altering the allowed ops // - the storage manager cannot perform a delete on the SR due to drivers return(true); } return(false); } return(true); }
protected override string GetCantExecuteReasonCore(SelectedItem item) { VDI vdi = item.XenObject as VDI; if (vdi == null) return base.GetCantExecuteReasonCore(item); SR sr = vdi.Connection.Resolve<SR>(vdi.SR); if (sr == null) return Messages.SR_COULD_NOT_BE_CONTACTED; VDI.FriendlyType vdiType = vdi.VDIType; if (vdi.Locked) return vdiType == VDI.FriendlyType.SNAPSHOT ? Messages.CANNOT_DELETE_SNAPSHOT_IN_USE : vdiType == VDI.FriendlyType.ISO ? Messages.CANNOT_DELETE_ISO_IN_USE : Messages.CANNOT_DELETE_VD_IN_USE; if (sr.Physical) return FriendlyErrorNames.VDI_IS_A_PHYSICAL_DEVICE; if (sr.IsToolsSR) return Messages.CANNOT_DELETE_TOOLS_SR; if (vdi.IsUsedByHA) return Messages.CANNOT_DELETE_HA_VD; if (vdi.IsMetadataForDR) return Messages.CANNOT_DELETE_DR_VD; List<VBD> vbds = vdi.Connection.ResolveAll<VBD>(vdi.VBDs); if (vbds.Count > 1 && !AllowMultipleVBDDelete) return Messages.CANNOT_DELETE_VDI_MULTIPLE_VBDS; foreach (VBD vbd in vbds) { VM vm = vdi.Connection.Resolve<VM>(vbd.VM); if (vdiType == VDI.FriendlyType.SYSTEM_DISK) { if (vm.power_state == vm_power_state.Running) return string.Format( Messages.CANNOT_DELETE_IN_USE_SYS_VD, Helpers.GetName(vm).Ellipsise(20)); } if (vbd.Locked) return vdiType == VDI.FriendlyType.SNAPSHOT ? Messages.CANNOT_DELETE_SNAPSHOT_IN_USE : vdiType == VDI.FriendlyType.ISO ? Messages.CANNOT_DELETE_ISO_IN_USE : Messages.CANNOT_DELETE_VD_IN_USE; if (vbd.currently_attached) { if (!AllowRunningVMDelete) { return string.Format(Messages.CANNOT_DELETE_VDI_ACTIVE_ON, Helpers.GetName(vm).Ellipsise(20)); } DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow.CommandInterface, vbd); if (!cmd.CanExecute()) { return string.Format(Messages.CANNOT_DELETE_CANNOT_DEACTIVATE_REASON, Helpers.GetName(vm).Ellipsise(20), cmd.ToolTipText); } } } // This is a necessary final check, there are other blocking reasons non covered in this method // Known examples: // - someone else is calling a delete on this vdi already, altering the allowed ops // - the storage manager cannot perform a delete on the SR due to drivers if (!vdi.allowed_operations.Contains(vdi_operations.destroy)) return vdiType == VDI.FriendlyType.SNAPSHOT ? Messages.CANNOT_DELETE_SNAPSHOT_GENERIC : vdiType == VDI.FriendlyType.ISO ? Messages.CANNOT_DELETE_ISO_GENERIC : Messages.CANNOT_DELETE_VD_GENERIC; return base.GetCantExecuteReasonCore(item); }
protected bool CanExecute(VDI vdi) { SR sr = vdi.Connection.Resolve<SR>(vdi.SR); if (vdi == null || sr == null) return false; if (vdi.Locked) return false; if (sr.Physical) return false; if (sr.IsToolsSR) return false; if (vdi.IsUsedByHA) { return false; } List<VBD> vbds = vdi.Connection.ResolveAll<VBD>(vdi.VBDs); if (vbds.Count > 1 && !AllowMultipleVBDDelete) return false; foreach (VBD vbd in vbds) { VM vm = vdi.Connection.Resolve<VM>(vbd.VM); if (vdi.type == vdi_type.system) { if (vm.power_state == vm_power_state.Running) return false; } if (vbd.Locked) return false; if (vbd.currently_attached) { //Check if we can unplug DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow.CommandInterface, vbd); if (!AllowRunningVMDelete || !cmd.CanExecute()) return false; } } if (sr.HBALunPerVDI) return true; if (!vdi.allowed_operations.Contains(vdi_operations.destroy)) { if (AllowRunningVMDelete) { // We deliberately DONT call allowed operations because we assume we know better :) // Xapi will think we can't delete because VBDs are plugged. We are going to unplug them. // Known risks of this method that will make us fail because we are disrespecting xapi: // - someone else is calling a delete on this vdi already, altering the allowed ops // - the storage manager cannot perform a delete on the SR due to drivers return true; } return false; } return true; }
private void DeactivateButton_Click(object sender, EventArgs e) { List<VBDRow> rows = SelectedVBDRows; if (rows == null) return; List<SelectedItem> l = new List<SelectedItem>(); foreach (VBDRow r in rows) l.Add(new SelectedItem(r.VBD)); SelectedItemCollection col = new SelectedItemCollection(l); Command cmd = null; if (col.AsXenObjects<VBD>().Find(vbd => !vbd.currently_attached) == null) cmd = new DeactivateVBDCommand(Program.MainWindow, l); else cmd = new ActivateVBDCommand(Program.MainWindow, l); if (cmd.CanExecute()) cmd.Execute(); }
private void UpdateButtons() { AttachVirtualDiskCommand attachCmd = new AttachVirtualDiskCommand(Program.MainWindow, vm); AttachButton.Enabled = attachCmd.CanExecute(); AddButton.Enabled = attachCmd.CanExecute(); List<VBDRow> vbdRows = SelectedVBDRows; if (dataGridViewStorage.Rows.Count == 0 || vbdRows == null || vm == null) { DeactivateButton.Enabled = false; DetachDriveButton.Enabled = false; DeleteDriveButton.Enabled = false; EditButton.Enabled = false; MoveButton.Enabled = false; return; } EditButton.Enabled = vbdRows.Count == 1 && !vbdRows[0].VBD.Locked && !vbdRows[0].VDI.Locked; List<SelectedItem> selectedVDIs = new List<SelectedItem>(); List<SelectedItem> selectedVBDs = new List<SelectedItem>(); foreach (VBDRow r in vbdRows) { selectedVDIs.Add(new SelectedItem(r.VDI)); selectedVBDs.Add(new SelectedItem(r.VBD)); } DeleteVirtualDiskCommand deleteCmd = new DeleteVirtualDiskCommand(Program.MainWindow, selectedVDIs); // User has visibility that this disk in use by this VM. Allow unplug + delete in single step (non default behaviour), // but only if we are the only VBD (default behaviour) deleteCmd.AllowRunningVMDelete = true; if (deleteCmd.CanExecute()) { DeleteButtonContainer.RemoveAll(); DeleteDriveButton.Enabled = true; } else { DeleteButtonContainer.SetToolTip(deleteCmd.ToolTipText); DeleteDriveButton.Enabled = false; } Command activationCmd = null; SelectedItemCollection vbdCol = new SelectedItemCollection(selectedVBDs); if (vbdCol.AsXenObjects<VBD>().Find(delegate(VBD vbd) { return !vbd.currently_attached; }) == null) { // no VBDs are attached so we are deactivating DeactivateButton.Text = Messages.DEACTIVATE; activationCmd = new DeactivateVBDCommand(Program.MainWindow, selectedVBDs); } else { // this is the default cause in the mixed attached/detatched scenario. We try to activate all the selection // The command error reports afterwards about the ones which are already attached DeactivateButton.Text = Messages.ACTIVATE; activationCmd = new ActivateVBDCommand(Program.MainWindow, selectedVBDs); } if (activationCmd.CanExecute()) { DeactivateButtonContainer.RemoveAll(); DeactivateButton.Enabled = true; } else { DeactivateButtonContainer.SetToolTip(activationCmd.ToolTipText); DeactivateButton.Enabled = false; } DetachVirtualDiskCommand detachCmd = new DetachVirtualDiskCommand(Program.MainWindow, selectedVDIs, vm); if (detachCmd.CanExecute()) { DetachButtonContainer.RemoveAll(); DetachDriveButton.Enabled = true; } else { DetachButtonContainer.SetToolTip(detachCmd.ToolTipText); DetachDriveButton.Enabled = false; } // Move button Command moveCmd = MoveMigrateCommand(selectedVDIs); if (moveCmd.CanExecute()) { MoveButton.Enabled = true; MoveButtonContainer.RemoveAll(); } else { MoveButton.Enabled = false; MoveButtonContainer.SetToolTip(moveCmd.ToolTipText); } }
protected override string GetCantExecuteReasonCore(SelectedItem item) { VDI vdi = item.XenObject as VDI; if (vdi == null) return base.GetCantExecuteReasonCore(item); foreach (VBD vbd in vdi.Connection.ResolveAll<VBD>(vdi.VBDs)) { if (targetVM == null || vbd.VM.opaque_ref == targetVM.opaque_ref) { if (!vbd.currently_attached) continue; DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow, vbd); if (!cmd.CanExecute()) { var reasons = cmd.GetCantExecuteReasons(); return reasons.Count > 0 ? reasons.ElementAt(0).Value : Messages.UNKNOWN; } } } return base.GetCantExecuteReasonCore(item); }
protected bool CanExecute(VDI vdi) { if (vdi == null) return false; foreach (VBD vbd in vdi.Connection.ResolveAll<VBD>(vdi.VBDs)) { if (targetVM == null || vbd.VM.opaque_ref == targetVM.opaque_ref) { if (!vbd.currently_attached) continue; DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow, vbd); if (!cmd.CanExecute()) return false; } } return true; }
protected override string GetCantExecuteReasonCore(SelectedItem item) { VDI vdi = item.XenObject as VDI; if (vdi == null) return base.GetCantExecuteReasonCore(item); foreach (VBD vbd in vdi.Connection.ResolveAll<VBD>(vdi.VBDs)) { if (targetVM == null || vbd.VM.opaque_ref == targetVM.opaque_ref) { if (!vbd.currently_attached) continue; DeactivateVBDCommand cmd = new DeactivateVBDCommand(Program.MainWindow.CommandInterface, vbd); if (!cmd.CanExecute()) return cmd.ToolTipText; } } return base.GetCantExecuteReasonCore(item); }
private void ActivateDeactivateVdi() { List<VBDRow> rows = SelectedVBDRows; if (rows == null) return; var selection = from VBDRow r in rows select new SelectedItem(r.VBD); SelectedItemCollection col = new SelectedItemCollection(selection); Command cmd = null; if (col.AsXenObjects<VBD>().Find(vbd => !vbd.currently_attached) == null) cmd = new DeactivateVBDCommand(Program.MainWindow, col); else cmd = new ActivateVBDCommand(Program.MainWindow, col); if (cmd.CanExecute()) cmd.Execute(); }