public ActionResult CreatePost(MachineLoginCredential machinelogincredential, int?[] MachinesList)
        {
            MachinesManagement machineManagment = new MachinesManagement();

            if (ModelState.IsValid)
            {
                db.MachineLoginCredentials.Add(machinelogincredential);
                db.SaveChanges();

                if (MachinesList != null)
                {
                    machinelogincredential.Machines = new List<Machine>();
                    foreach (int machineID in MachinesList)
                    {
                        var machine = db.Machines.Find(machineID);
                        //ignore modifying the machine if the daemon is running
                        if (machine.StatusEnum != global::Aneka.PAL.Management.Model.DaemonProbeStatus.ServiceStarted)
                        {
                            machineManagment.UpdateMachineStatus((int)machineID, machinelogincredential);
                            machinelogincredential.Machines.Add(machine);
                        }

                    }
                    db.SaveChanges();
                }

                return RedirectToAction("Dashboard", "Home");
            }

            return View("~/Views/Home/Dashboard.cshtml", machinelogincredential);
        }
        public ActionResult CreatePost(CloudWebPortal.Areas.Aneka.Models.Machine machine, Daemon daemon, bool InstallDaemon, int? ResourcePool, int? LoginCredential, int? PlatformID, int? TypeID, int?[] SoftwareAppliancesList)
        {
            MachinesManagement machineManagment = new MachinesManagement();
            DaemonsManagement daemonManagement = new DaemonsManagement();

            if (!ResourcePool.HasValue)
                ModelState.AddModelError(String.Empty, "Please Select A Resource Pool");

            if (!PlatformID.HasValue)
                ModelState.AddModelError(String.Empty, "Machine Platform Is Requared");
            else
                machine.Platform = db.MachinePlatforms.Find(PlatformID);

            if (!TypeID.HasValue)
                ModelState.AddModelError(String.Empty, "Machine Type Is Requared");
            else
                machine.Type = db.MachineTypes.Find(TypeID);

            if (ModelState.IsValid)
            {
                machine.StatusEnum = DaemonProbeStatus.Unknown;

                //Create Machine and Daemon
                machine.Daemon = daemon;
                db.Machines.Add(machine);
                db.SaveChanges();

                //Add To A Resource Pool
                if (ResourcePool.HasValue)
                {
                    db.ResourcePools.Find(ResourcePool).Machines.Add(machine);
                    db.SaveChanges();
                }

                //Link Login Credential
                MachineLoginCredential login = null;
                if (LoginCredential.HasValue)
                {
                    login = db.MachineLoginCredentials.Find(LoginCredential);
                    login.Machines.Add(machine);
                    db.SaveChanges();
                }

                //Add Software Appliances
                if (SoftwareAppliancesList != null)
                {
                    machine.SoftwareAppliances = new List<SoftwareAppliance>();
                    foreach (int appID in SoftwareAppliancesList)
                    {
                        var app = db.SoftwareAppliances.Find(appID);
                        machine.SoftwareAppliances.Add(app);
                    }
                    db.SaveChanges();
                }

                machine.StatusEnum = machineManagment.UpdateMachineStatus(machine.MachineId, login).Status;

                if (InstallDaemon && machine.StatusEnum == DaemonProbeStatus.ServiceNotInstalled && login != null)
                {
                    UpdateInProgressMessage(machine.MachineId, "Installing Daemon");

                    AsynchronousCallback installingDaemonThread = new AsynchronousCallback(daemonManagement.InstallDaemon);
                    installingDaemonThread.BeginInvoke(machine.MachineId, System.Web.HttpContext.Current.Server.MapPath("~/"), null, null);
                }
                else
                {
                    if (machine.StatusEnum == DaemonProbeStatus.ServiceStarted && login != null)
                    {
                        FetchExistCloud(machine.MachineId);
                    }
                }

                return RedirectToAction("ManageMachinesandResourcePools", "Infrastructure");
            }

            return View("~/Views/Infrastructure/ManageMachinesandResourcePools.cshtml", machine);
        }
        public ActionResult RefreshMachine(int id)
        {
            MachinesManagement machineManagment = new MachinesManagement();

            CloudWebPortal.Areas.Aneka.Models.Machine machine = db.Machines.Find(id);
            machineManagment.UpdateMachineStatus(machine.MachineId, machineManagment.getLogin(machine));
            return RedirectToAction("ManageMachinesandResourcePools", "Infrastructure");
        }
        public ActionResult RefreshAllMachineInResourcePool(int id)
        {
            MachinesManagement machineManagment = new MachinesManagement();

            List<CloudWebPortal.Areas.Aneka.Models.Machine> machineList = db.ResourcePools.Find(id).Machines.ToList();
            foreach (var machine in machineList)
                machineManagment.UpdateMachineStatus(machine.MachineId, machineManagment.getLogin(machine));
            return RedirectToAction("ManageMachinesandResourcePools", "Infrastructure");
        }
        public ActionResult EditPost(CloudWebPortal.Areas.Aneka.Models.Machine machine, Daemon daemon, int? ResourcePool, int? LoginCredential, int? PlatformID, int? TypeID, int?[] SoftwareAppliancesList)
        {
            MachinesManagement machineManagment = new MachinesManagement();

            if(machine.StatusEnum == DaemonProbeStatus.ServiceStarted)
                ModelState.AddModelError(String.Empty, "Editing the machine while the daemon is running is not supported yet, please stop the daemon first");

            if (!ResourcePool.HasValue)
                ModelState.AddModelError(String.Empty, "Please Select A Resource Pool");
            if (!PlatformID.HasValue)
                ModelState.AddModelError(String.Empty, "Machine Platform Is Requared");
            if (!TypeID.HasValue)
                ModelState.AddModelError(String.Empty, "Machine Type Is Requared");

            if (ModelState.IsValid)
            {
                db.Entry(machine).State = EntityState.Modified;
                db.SaveChanges();

                machine.Daemon = daemon;
                db.SaveChanges();

                var machine2 = db.Machines.Include(x => x.Platform).Include(x => x.Type).Where(x => x.MachineId == machine.MachineId).First();
                if (machine2.Platform == null)
                    machine2.Platform = new MachinePlatform();
                if (machine2.Type == null)
                    machine2.Type = new MachineType();
                MachinePlatform platform = db.MachinePlatforms.Find(PlatformID);
                machine.Platform = platform;
                MachineType type = db.MachineTypes.Find(TypeID);
                machine.Type = type;
                db.SaveChanges();
                machine2 = null;

                //Resource Pool List
                var ResourcePoolsList = db.ResourcePools.ToList();
                for(int ResourcePoolIndex = 0; ResourcePoolIndex < ResourcePoolsList.Count(); ResourcePoolIndex++)
                    if (ResourcePoolsList[ResourcePoolIndex].Machines.Where(x => x.MachineId == machine.MachineId).Count() > 0)
                        db.ResourcePools.ToList()[ResourcePoolIndex].Machines.Remove(machine);
                db.SaveChanges();
                if (ResourcePool.HasValue)
                {
                    db.ResourcePools.Find(ResourcePool).Machines.Add(machine);
                    db.SaveChanges();
                }

                //Login List
                var MachineLoginCredentialList = db.MachineLoginCredentials.ToList();
                for(int MachineLoginCredentialIndex = 0; MachineLoginCredentialIndex < MachineLoginCredentialList.Count(); MachineLoginCredentialIndex++)
                    if (MachineLoginCredentialList[MachineLoginCredentialIndex].Machines.Where(x => x.MachineId == machine.MachineId).Count() > 0)
                        db.MachineLoginCredentials.ToList()[MachineLoginCredentialIndex].Machines.Remove(machine);
                db.SaveChanges();
                MachineLoginCredential login = null;
                if (LoginCredential.HasValue)
                {
                    login = db.MachineLoginCredentials.Find(LoginCredential);
                    login.Machines.Add(machine);
                    db.SaveChanges();
                }

                //SoftwareAppliances List
                machine2 = db.Machines.Include(x => x.SoftwareAppliances).Where(x => x.MachineId == machine.MachineId).First();

                if(machine2.SoftwareAppliances == null)
                    machine2.SoftwareAppliances = new List<SoftwareAppliance>();

                foreach (SoftwareAppliance app in db.SoftwareAppliances.ToList())
                    machine2.SoftwareAppliances.Remove(app);
                db.SaveChanges();

                if (SoftwareAppliancesList != null)
                {
                    foreach (int appID in SoftwareAppliancesList)
                    {
                        var app = db.SoftwareAppliances.Find(appID);
                        machine2.SoftwareAppliances.Add(app);
                    }
                    db.SaveChanges();
                }

                machineManagment.UpdateMachineStatus(machine.MachineId, login);

                return RedirectToAction("ManageMachinesandResourcePools", "Infrastructure");
            }
            return View("~/Views/Infrastructure/ManageMachinesandResourcePools.cshtml", machine);
        }
        public ActionResult EditPost(MachineLoginCredential machinelogincredential, int?[] MachinesList)
        {
            MachinesManagement machineManagment = new MachinesManagement();

            if (ModelState.IsValid)
            {

                db.Entry(machinelogincredential).State = EntityState.Modified;
                db.SaveChanges();

                //Machines List
                var machinelogincredential2 = db.MachineLoginCredentials.Include(x => x.Machines).Where(x => x.MachineLoginCredentialId == machinelogincredential.MachineLoginCredentialId).First();

                if (machinelogincredential2.Machines == null)
                    machinelogincredential2.Machines = new List<Machine>();

                foreach (Machine m in db.Machines.ToList())
                    machinelogincredential2.Machines.Remove(m);
                db.SaveChanges();

                if (MachinesList != null)
                {
                    foreach (int machineID in MachinesList)
                    {
                        var machine = db.Machines.Find(machineID);
                        //ignore modifying the machine if the daemon is running
                        if (machine.StatusEnum != global::Aneka.PAL.Management.Model.DaemonProbeStatus.ServiceStarted)
                        {
                            machineManagment.UpdateMachineStatus((int)machineID, machinelogincredential);
                            machinelogincredential.Machines.Add(machine);
                        }
                    }
                    db.SaveChanges();
                }

                return RedirectToAction("Dashboard", "Home");
            }
            return View("~/Views/Home/Dashboard.cshtml",machinelogincredential);
        }