private void EnableAppropriateHostsNoWlb(Session session)
        {
            SelectedItemCollection selection  = Command.GetSelection();
            IXenConnection         connection = selection[0].Connection;

            VMOperationCommand cmdHome = new VMOperationHomeServerCommand(Command.MainWindowCommandInterface, selection, _operation, session);

            Host affinityHost             = connection.Resolve(((VM)Command.GetSelection()[0].XenObject).affinity);
            VMOperationCommand cpmCmdHome = new CrossPoolMigrateToHomeCommand(Command.MainWindowCommandInterface, selection, affinityHost);

            Program.Invoke(Program.MainWindow, delegate
            {
                var firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];

                bool oldMigrateToHomeCmdCanRun = cmdHome.CanExecute();
                if (affinityHost == null || _operation == vm_operations.start_on || !oldMigrateToHomeCmdCanRun && !cpmCmdHome.CanExecute())
                {
                    firstItem.Command = cmdHome;
                }
                else
                {
                    firstItem.Command = oldMigrateToHomeCmdCanRun ? cmdHome : cpmCmdHome;
                }
            });

            List <VMOperationToolStripMenuSubItem> dropDownItems = DropDownItems.Cast <VMOperationToolStripMenuSubItem>().ToList();

            // Adds the migrate wizard button, do this before the enable checks on the other items
            Program.Invoke(Program.MainWindow, () => AddAdditionalMenuItems(selection));

            foreach (VMOperationToolStripMenuSubItem item in dropDownItems)
            {
                if (_isDropDownClosed)
                {
                    // Stop making requests to assert can start on each host after dropdown is closed
                    break;
                }

                Host host = item.Tag as Host;
                if (host != null)
                {
                    VMOperationCommand      cmd    = new VMOperationHostCommand(Command.MainWindowCommandInterface, selection, delegate { return(host); }, host.Name().EscapeAmpersands(), _operation, session);
                    CrossPoolMigrateCommand cpmCmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection, host, _resumeAfter);

                    VMOperationToolStripMenuSubItem tempItem = item;
                    Program.Invoke(Program.MainWindow, delegate
                    {
                        bool oldMigrateCmdCanRun = cmd.CanExecute();
                        if (_operation == vm_operations.start_on || (!oldMigrateCmdCanRun && !cpmCmd.CanExecute() && string.IsNullOrEmpty(cpmCmd.CantExecuteReason)))
                        {
                            tempItem.Command = cmd;
                        }
                        else
                        {
                            tempItem.Command = oldMigrateCmdCanRun ? cmd : cpmCmd;
                        }
                    });
                }
            }
        }
        private void EnableAppropriateHostsNoWlb(Session session)
        {
            SelectedItemCollection selection  = Command.GetSelection();
            IXenConnection         connection = selection[0].Connection;

            VMOperationCommand cmdHome = new VMOperationHomeServerCommand(Command.MainWindowCommandInterface, selection, _operation, session);

            Host affinityHost             = connection.Resolve(((VM)Command.GetSelection()[0].XenObject).affinity);
            VMOperationCommand cpmCmdHome = new CrossPoolMigrateToHomeCommand(Command.MainWindowCommandInterface, selection, affinityHost);

            Program.Invoke(Program.MainWindow, delegate
            {
                var firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];

                bool oldMigrateToHomeCmdCanRun = cmdHome.CanExecute();
                if (affinityHost == null || _operation == vm_operations.start_on || !oldMigrateToHomeCmdCanRun && !cpmCmdHome.CanExecute())
                {
                    firstItem.Command = cmdHome;
                }
                else
                {
                    firstItem.Command = oldMigrateToHomeCmdCanRun ? cmdHome : cpmCmdHome;
                }
            });

            List <VMOperationToolStripMenuSubItem> dropDownItems = DropDownItems.Cast <VMOperationToolStripMenuSubItem>().ToList();

            foreach (VMOperationToolStripMenuSubItem item in dropDownItems)
            {
                if (item.Tag is Host)
                {
                    Host host = (Host)item.Tag;

                    string hostNameText = host.Name.EscapeAmpersands();

                    VMOperationCommand cmd    = new VMOperationHostCommand(Command.MainWindowCommandInterface, selection, delegate { return(host); }, hostNameText, _operation, session);
                    VMOperationCommand cpmCmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection, host)
                    {
                        MenuText = hostNameText
                    };

                    VMOperationToolStripMenuSubItem tempItem = item;
                    Program.Invoke(Program.MainWindow, delegate
                    {
                        bool oldMigrateCmdCanRun = cmd.CanExecute();
                        if (_operation == vm_operations.start_on || !oldMigrateCmdCanRun && !cpmCmd.CanExecute())
                        {
                            tempItem.Command = cmd;
                        }
                        else
                        {
                            tempItem.Command = oldMigrateCmdCanRun ? cmd : cpmCmd;
                        }
                    });
                }
            }

            Program.Invoke(Program.MainWindow, () => AddAdditionalMenuItems(selection));
        }
        public new static bool CanExecute(VM vm, Host preSelectedHost)
        {
            if (vm == null || !vm.is_a_template || vm.DefaultTemplate() || vm.Locked)
            {
                return(false);
            }

            return(CrossPoolMigrateCommand.CanExecute(vm, preSelectedHost));
        }
        public static bool CanExecute(VM vm, Host preSelectedHost)
        {
            if (vm == null || vm.is_a_template || vm.Locked || vm.power_state != vm_power_state.Halted)
            {
                return(false);
            }

            return(CrossPoolMigrateCommand.CanExecute(vm, preSelectedHost));
        }
Example #5
0
        public static bool CanExecute(VM vm, Host preSelectedHost, bool force)
        {
            if (vm == null || vm.is_a_template || vm.Locked || vm.power_state == vm_power_state.Running)
            {
                return(false);
            }

            return(CrossPoolMigrateCommand.CanExecute(vm, preSelectedHost, force));
        }
 protected override void AddAdditionalMenuItems(SelectedItemCollection selection)
 {
     if (selection.ToList().All(item => Helpers.TampaOrGreater(item.Connection) && !Helpers.CrossPoolMigrationRestrictedWithWlb(item.Connection)))
     {
         VMOperationCommand cmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection);
         DropDownItems.Add(new ToolStripSeparator());
         VMOperationToolStripMenuSubItem lastItem = new VMOperationToolStripMenuSubItem(cmd);
         DropDownItems.Add(lastItem);
     }
 }
 protected override void AddAdditionalMenuItems(SelectedItemCollection selection)
 {
     if (selection.ToList().All(item => !Helpers.CrossPoolMigrationRestrictedWithWlb(item.Connection)))
     {
         VMOperationCommand cmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection);
         DropDownItems.Add(new ToolStripSeparator());
         VMOperationToolStripMenuSubItem lastItem = new VMOperationToolStripMenuSubItem(cmd);
         DropDownItems.Add(lastItem);
     }
 }
        public bool VerifyLunPerVdiBehaviour(bool IsLunPerVdi)
        {
            Mock<VM> vm = ObjectFactory.BuiltObject<VM>(ObjectBuilderType.VmWithHomeServerHost, id);
            Mock<SR> sr = ObjectManager.NewXenObject<SR>(id);
            Mock<Host> targetHost = ObjectManager.NewXenObject<Host>(id);
            vm.Setup(v => v.SRs).Returns(new List<SR> {sr.Object});
            vm.Setup(v => v.allowed_operations).Returns(new List<vm_operations> {vm_operations.migrate_send});
            sr.Setup(s => s.HBALunPerVDI).Returns(IsLunPerVdi);

            IMainWindow mw = new MockMainWindow();

            CrossPoolMigrateCommand cmd = new CrossPoolMigrateCommand(mw, new List<SelectedItem> { new SelectedItem(vm.Object)}, targetHost.Object);
            return cmd.CanExecute();
        }
        private void EnableAppropriateHostsNoWlb(Session session)
        {
            SelectedItemCollection selection  = Command.GetSelection();
            IXenConnection         connection = selection[0].Connection;
            VMOperationCommand     commandForFirstMenuItem = new VMOperationHomeServerCommand(Command.MainWindowCommandInterface, selection, _operation, session);

            Program.Invoke(Program.MainWindow, delegate
            {
                VMOperationToolStripMenuSubItem firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];
                firstItem.Command = commandForFirstMenuItem;
            });

            List <VMOperationToolStripMenuSubItem> dropDownItems = DropDownItems.Cast <VMOperationToolStripMenuSubItem>().ToList();

            foreach (VMOperationToolStripMenuSubItem item in dropDownItems)
            {
                if (item.Tag is Host)
                {
                    Host host = (Host)item.Tag;

                    VMOperationCommand cmd    = new VMOperationHostCommand(Command.MainWindowCommandInterface, selection, delegate { return(host); }, host.Name.EscapeAmpersands(), _operation, session);
                    VMOperationCommand cpmCmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection, host)
                    {
                        MenuText = host.Name.EscapeAmpersands()
                    };

                    VMOperationToolStripMenuSubItem tempItem = item;
                    Program.Invoke(Program.MainWindow, delegate
                    {
                        bool oldMigrateCmdCanRun = cmd.CanExecute();
                        if (!oldMigrateCmdCanRun && !cpmCmd.CanExecute())
                        {
                            tempItem.Command = cmd;
                        }
                        else
                        {
                            tempItem.Command = oldMigrateCmdCanRun ? cmd : cpmCmd;
                        }
                    });
                }
            }

            Program.Invoke(Program.MainWindow, () => AddAdditionalMenuItems(selection));
        }
        private void EnableAppropriateHostsNoWlb(Session session)
        {
            SelectedItemCollection selection  = Command.GetSelection();
            IXenConnection         connection = selection[0].Connection;

            VMOperationCommand cmdHome = new VMOperationHomeServerCommand(Command.MainWindowCommandInterface, selection, _operation, session);
            Host affinityHost          = connection.Resolve(((VM)Command.GetSelection()[0].XenObject).affinity);

            Program.Invoke(Program.MainWindow, delegate
            {
                var firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];

                bool oldMigrateToHomeCmdCanRun = cmdHome.CanExecute();
                if (affinityHost == null || _operation == vm_operations.start_on || oldMigrateToHomeCmdCanRun)
                {
                    firstItem.Command = cmdHome;
                    firstItem.Enabled = oldMigrateToHomeCmdCanRun;
                }
                else
                {
                    VMOperationCommand cpmCmdHome = new CrossPoolMigrateToHomeCommand(Command.MainWindowCommandInterface, selection, affinityHost);

                    if (cpmCmdHome.CanExecute())
                    {
                        firstItem.Command = cpmCmdHome;
                        firstItem.Enabled = true;
                    }
                    else
                    {
                        firstItem.Command = cmdHome;
                        firstItem.Enabled = false;
                    }
                }
            });

            List <VMOperationToolStripMenuSubItem> dropDownItems = DropDownItems.Cast <VMOperationToolStripMenuSubItem>().ToList();

            // Adds the migrate wizard button, do this before the enable checks on the other items
            Program.Invoke(Program.MainWindow, () => AddAdditionalMenuItems(selection));

            foreach (VMOperationToolStripMenuSubItem item in dropDownItems)
            {
                if (_isDropDownClosed)
                {
                    // Stop making requests to assert can start on each host after dropdown is closed
                    break;
                }

                Host host = item.Tag as Host;
                if (host != null)
                {
                    // API calls could happen in CanExecute(), which take time to wait.
                    // So a Producer-Consumer-Queue with size 25 is used here to :
                    //   1. Make API calls for different menu items happen in parallel;
                    //   2. Limit the count of concurrent threads (now it's 25).
                    workerQueueWithouWlb.EnqueueItem(() =>
                    {
                        if (_isDropDownClosed)
                        {
                            return;
                        }
                        VMOperationCommand cmd         = new VMOperationHostCommand(Command.MainWindowCommandInterface, selection, delegate { return(host); }, host.Name().EscapeAmpersands(), _operation, session);
                        CrossPoolMigrateCommand cpmCmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection, host, _resumeAfter);

                        VMOperationToolStripMenuSubItem tempItem = item;
                        bool oldMigrateCmdCanRun = cmd.CanExecute();
                        if ((_operation == vm_operations.start_on) || oldMigrateCmdCanRun)
                        {
                            Program.Invoke(Program.MainWindow, delegate
                            {
                                tempItem.Command = cmd;
                                tempItem.Enabled = oldMigrateCmdCanRun;
                            });
                        }
                        else
                        {
                            bool crossPoolMigrateCmdCanRun = cpmCmd.CanExecute();
                            if (crossPoolMigrateCmdCanRun || !string.IsNullOrEmpty(cpmCmd.CantExecuteReason))
                            {
                                Program.Invoke(Program.MainWindow, delegate
                                {
                                    tempItem.Command = cpmCmd;
                                    tempItem.Enabled = crossPoolMigrateCmdCanRun;
                                });
                            }
                            else
                            {
                                Program.Invoke(Program.MainWindow, delegate
                                {
                                    tempItem.Command = cmd;
                                    tempItem.Enabled = oldMigrateCmdCanRun;
                                });
                            }
                        }
                    });
                }
            }
        }
 public bool IntrapoolWlbEnabledBehaviour(bool WlbEnabled)
 {
     Mock<VM> vm = ObjectFactory.BuiltObject<VM>(ObjectBuilderType.VmWithHomeServerHost, id);
     Mock<Host> targetHost = ObjectFactory.BuiltObject<Host>(ObjectBuilderType.TampaHost, id);
     Mock<Host> preselectedHost = ObjectFactory.BuiltObject<Host>(ObjectBuilderType.TampaHost, id);
     ObjectManager.MockConnectionFor(id).Setup(c => c.Resolve(It.IsAny<XenRef<Host>>())).Returns(targetHost.Object);
     vm.Setup(v => v.allowed_operations).Returns(new List<vm_operations> { vm_operations.migrate_send });
     Mock<Pool> pool = ObjectManager.NewXenObject<Pool>(id);
     pool.Setup(p => p.wlb_enabled).Returns(WlbEnabled);
     pool.Setup(p => p.wlb_url).Returns("wlburl"); //Configured == true
     
     IMainWindow mw = new MockMainWindow();
     CrossPoolMigrateCommand cmd = new CrossPoolMigrateCommand(mw, new List<SelectedItem> { new SelectedItem(vm.Object) }, preselectedHost.Object);
     bool canExecute = cmd.CanExecute();
     pool.Verify(p=>p.wlb_enabled, Times.AtLeastOnce());
     return canExecute;
 }
        public bool CrossPoolWlbEnabledBehaviour(bool WlbEnabledVM, bool WlbEnabledTarget)
        {
            //First connection
            Mock<VM> vm = ObjectFactory.BuiltObject<VM>(ObjectBuilderType.VmWithHomeServerHost, id);
            Mock<Host> master = ObjectFactory.BuiltObject<Host>(ObjectBuilderType.TampaHost, id);
            ObjectManager.MockConnectionFor(id).Setup(c => c.Resolve(It.IsAny<XenRef<Host>>())).Returns(master.Object);
            vm.Setup(v => v.allowed_operations).Returns(new List<vm_operations> { vm_operations.migrate_send });
            Mock<Pool> pool = ObjectManager.NewXenObject<Pool>(id);
            pool.Setup(p => p.wlb_enabled).Returns(WlbEnabledVM);
            pool.Setup(p => p.wlb_url).Returns("wlburl"); //Configured == true

            //Second connection
            Mock<Host> targetHost = ObjectFactory.BuiltObject<Host>(ObjectBuilderType.TampaHost, id2);
            Mock<Pool> targetPool = ObjectManager.NewXenObject<Pool>(id2);
            ObjectManager.MockConnectionFor(id2).Setup(c => c.Resolve(It.IsAny<XenRef<Host>>())).Returns(targetHost.Object);
            targetPool.Setup(p => p.wlb_enabled).Returns(WlbEnabledTarget);
            targetPool.Setup(p => p.wlb_url).Returns("wlburl"); //Configured == true

            //Command
            IMainWindow mw = new MockMainWindow();
            CrossPoolMigrateCommand cmd = new CrossPoolMigrateCommand(mw, new List<SelectedItem> { new SelectedItem(vm.Object) }, targetHost.Object);
            bool canExecute = cmd.CanExecute();

            //As the command is launching the wizard it should only need to 
            //check the VMs are on Wlb as the target can be changed in the wizard anyhow
            pool.Verify(p => p.wlb_enabled, Times.AtLeastOnce());
            targetPool.Verify(tp => tp.wlb_enabled, Times.Never());
            return canExecute;
        }