public ActionResult CreatePost(Cloud cloud, Master master, int? masterMachineID, int?[] selectedServicesIDs)
        {
            if (!masterMachineID.HasValue)
                ModelState.AddModelError(String.Empty, "You must select a master");
            if (selectedServicesIDs == null)
                ModelState.AddModelError(String.Empty, "You must select at least one service");

            if (ModelState.IsValid)
            {
                master.isInstalled = false;
                master.Services = new List<Service>();
                foreach (var sericeId in selectedServicesIDs)
                {
                    Service service = db.Services.Find(sericeId);
                    master.Services.Add(service);
                }
                db.SaveChanges();

                Machine machine = db.Machines.Find(masterMachineID);
                if (machine.Masters == null)
                    machine.Masters = new List<Master>();
                machine.Masters.Add(master);
                db.SaveChanges();

                cloud.Master = master;
                db.SaveChanges();

                db.Clouds.Add(cloud);
                db.SaveChanges();

                CreateContainer(cloud.CloudId);

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

            return View("~/Views/Home/Dashboard.cshtml",cloud);
        }
        public ActionResult Details(int id)
        {
            MasterContainersManagement masterContainersManagement = new MasterContainersManagement();

            //Get the cloud entity from the database
            Cloud cloud = db.Clouds.Find(id);

            if (cloud == null)
            {
                cloud = new Cloud();
                cloud.CloudName = "Cloud could not be found";
                return PartialView();
            }

            //Pass the master machine entity to the view
            ViewBag.MasterMachine = masterContainersManagement.machineLookupFromMasterId(cloud.Master.MasterId);

            //Create and pass a Dictionary to the view, the key is the worker ID, and the value id the machine entity.
            Dictionary<int, Machine> WorkersMachines = new Dictionary<int, Machine>();
            List<Machine> AllMachines = db.Machines.ToList();
            foreach (Worker worker in cloud.Workers)
            {
                foreach (Machine machine in AllMachines)
                {
                    if(machine.Workers.Contains(worker))
                        WorkersMachines.Add(worker.WorkerId,machine);
                }
            }
            ViewBag.WorkersMachines = WorkersMachines;

            //Pass the cloud entity to the view
            return PartialView(cloud);
        }
        public ActionResult EditPost(Cloud cloud, Master master, int? masterMachineID, int?[] selectedServicesIDs)
        {
            if (!masterMachineID.HasValue)
                ModelState.AddModelError(String.Empty, "You must select a master");
            if (selectedServicesIDs == null)
                ModelState.AddModelError(String.Empty, "You must select at least one service");

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

                Cloud cloudFromDB = db.Clouds.Find(cloud.CloudId);
                cloudFromDB.CloudName = cloud.CloudName;
                cloudFromDB.DBConnectionString = cloud.DBConnectionString;
                db.SaveChanges();

                Master masterFromDB = db.Masters.Find(master.MasterId);

                masterFromDB.Cost = master.Cost;
                masterFromDB.DisplayName = master.DisplayName;
                masterFromDB.MasterFailoverBackupURI = master.MasterFailoverBackupURI;
                masterFromDB.Port = master.Port;

                if (masterFromDB.Services != null)
                {
                    List<Service> services = masterFromDB.Services.ToList();
                    foreach (var service in services)
                        masterFromDB.Services.Remove(service);
                }
                else
                    masterFromDB.Services = new List<Service>();

                foreach (var sericeId in selectedServicesIDs)
                {
                    Service service = db.Services.Find(sericeId);
                    masterFromDB.Services.Add(service);
                }
                db.SaveChanges();

                foreach (Machine machineFromDB in db.Machines.ToList())
                    if (machineFromDB.Masters.Where(x => x.MasterId == masterFromDB.MasterId).ToList().Count > 0)
                    {
                        machineFromDB.Masters.Remove(masterFromDB);
                        db.SaveChanges();
                        break;
                    }

                Machine machine = db.Machines.Find(masterMachineID);
                if (machine.Masters == null)
                    machine.Masters = new List<Master>();
                machine.Masters.Add(masterFromDB);
                db.SaveChanges();

                //////TO DO
                //Edit Container in the cloud

                return RedirectToAction("Dashboard", "Home");
            }
            return View("~/Views/Home/Dashboard.cshtml",cloud);
        }
        /// <summary>
        /// Build Cloud from already Exist master container
        /// </summary>
        /// <param name="configure"></param>
        /// <param name="property"></param>
        public void buildExistMaster(global::Aneka.Runtime.Configuration configure, ContainerProperty property)
        {
            try
            {
                Uri uri = new Uri(configure.IndexServerUri);
                CloudWebPortal.Areas.Aneka.Models.Machine machine = db.Machines.Where(x => x.IP == uri.Host).First();
                Cloud cloud = new Cloud();
                Master master = new Master();
                if (property.Status == ContainerStatus.Started)
                {
                    IContainerManagerProxy containerProxy = ProxyCreator.CreateContainerManagerProxy();
                    NodeInfo nodeInfo = containerProxy.GetNodeInfo(uri.ToString());
                    if (nodeInfo != null)
                    {
                        master.Services = new List<Service>();
                        foreach (String serviceName in nodeInfo.GetServices())
                        {
                            foreach (Service serviceFromDB in db.Services.ToList())
                            {
                                if (serviceFromDB.SpringSegmentXML.Contains(serviceName))
                                    master.Services.Add(serviceFromDB);
                            }
                        }
                    }
                }

                master.DisplayName = configure.IndexServerUri;
                master.isInstalled = true;
                master.Port = configure.Port;
                master.AnekaContainerID = property.Id;
                master.Cost = (int)configure.Cost;
                cloud.CloudName = configure.Name;

                db.Masters.Add(master);
                machine.Masters.Add(master);
                db.SaveChanges();

                db.Clouds.Add(cloud);
                cloud.Master = master;
                db.SaveChanges();

                try
                {
                    //Get already exist users
                    Uri masterUri = new Uri(ContainersManagement.GetContainerUri(machine.IP, master.Port));
                    global::Aneka.Security.UserCredentials adminLogin = new global::Aneka.Security.UserCredentials("Administrator", String.Empty);
                    AnekaUsersManagement anekaUsersManagement = new AnekaUsersManagement(masterUri, adminLogin);
                    List<CloudUserAccount> cloudUserAccounts = anekaUsersManagement.getAllUsersFromTheCloud();
                    foreach (CloudUserAccount account in cloudUserAccounts)
                    {
                        if (account.Password == "")
                            account.Password = "******";
                        account.Clouds = new List<Cloud>();
                        account.Clouds.Add(cloud);
                        db.CloudUserAccounts.Add(account);
                        db.SaveChanges();
                    }
                }
                catch (Exception)
                {

                }
                finally
                {
                    FinishedMasterActivity(cloud.CloudId);
                }

            }
            catch (Exception)
            {
            }
        }