private void InitializeDataContext()
        {
            var registeredImages = RegisteredVirtualMachineService.GetRegisteredVMImagePaths();

            foreach (var image in registeredImages)
            {
                int startIndex = image.IndexOf("] ") + "] ".Length;
                int length = image.IndexOf('/', startIndex) - startIndex;
                string projectName = image.Substring(startIndex, length);
                var service = new RegisteredVirtualMachineService(image);

                if (!dataDB.Projects.Select(p => p.ProjectName).Contains(projectName))
                {
                    var project = new Project(projectName);
                    dataDB.Projects.Add(project);
                    dataDB.SaveChanges();
                }

                startIndex = image.LastIndexOf('/') + 1;
                length = image.LastIndexOf('.') - startIndex;
                string machineName = image.Substring(startIndex, length);

                VirtualMachine vm;

                try
                {
                    vm = dataDB.VirtualMachines.Single(v => v.MachineName == machineName);
                }
                catch (Exception)
                {
                    vm = new VirtualMachine();
                    dataDB.VirtualMachines.Add(vm);
                }

                //PowerOn(vm, service);
                vm.MachineName = machineName;
                vm.ImagePathName = image;
                vm.Status = service.GetStatus();

                //TODO reset
                //vm.Hostname = "test-hostname";
                vm.Hostname = service.GetHostname();
                vm.IP = service.GetIP();
                vm.Project = dataDB.Projects.Single(p => p.ProjectName == projectName);

                dataDB.SaveChanges();
            }
        }
        public ICollection<Project> GetAllProjects()
        {
            foreach (var vm in dataDB.VirtualMachines.Where(v => v.Status != VirtualMachine.ARCHIVED &&
                v.Status != VirtualMachine.PENDING))
            {
                RegisteredVirtualMachineService service = null;

                try
                {
                    service = new RegisteredVirtualMachineService(vm.ImagePathName);
                }
                catch (Vestris.VMWareLib.VMWareException)
                {
                    dataDB.VirtualMachines.Remove(vm);
                    continue;
                }

                vm.Status = service.GetStatus();

                if (vm.IP == null && vm.Status == VirtualMachine.RUNNING)
                    vm.IP = service.GetIP();

                if (vm.Hostname == null && vm.Status == VirtualMachine.RUNNING)
                    vm.Hostname = service.GetHostname();
            }

            dataDB.SaveChanges();
            return dataDB.Projects.ToList();
        }
        public void ReinitializeAllRegisteredVirtualMachines()
        {
            var imagePathNames = RegisteredVirtualMachineService.GetRegisteredVMImagePaths();

            foreach (var image in imagePathNames)
            {
                int startIndex = image.IndexOf("] ") + "] ".Length;
                int length = image.IndexOf('/', startIndex) - startIndex;
                string projectName = image.Substring(startIndex, length);
                var service = new RegisteredVirtualMachineService(image);

                if (!dataDB.Projects.Select(p => p.ProjectName).Contains(projectName))
                {
                    var project = new Project(projectName);
                    dataDB.Projects.Add(project);
                    dataDB.SaveChanges();
                }

                startIndex = image.LastIndexOf('/') + 1;
                length = image.LastIndexOf('.') - startIndex;
                string machineName = image.Substring(startIndex, length);

                if (!dataDB.VirtualMachines.Select(v => v.MachineName).Contains(machineName))
                {
                    VirtualMachine vm = new VirtualMachine();

                    vm.MachineName = machineName;
                    vm.ImagePathName = image;
                    vm.Status = service.GetStatus();
                    vm.Hostname = service.GetHostname();
                    vm.IP = service.GetIP();
                    vm.Project = dataDB.Projects.Single(p => p.ProjectName == projectName);

                    dataDB.VirtualMachines.Add(vm);
                    dataDB.SaveChanges();
                }
            }
        }
 public void SetIPTest()
 {
     VirtualMachineRepository vmr = new VirtualMachineRepository();
     VirtualMachine vm = vmr.GetAllRegisteredVirtualMachines().GetEnumerator().Current; // TODO: Initialize to an appropriate value
     RegisteredVirtualMachineService target = new RegisteredVirtualMachineService(vm); // TODO: Initialize to an appropriate value
     string oldIP = target.GetIP();
     //set the new IP to the same first several sets of characters, but change the last few
     string value = oldIP.Substring(0, oldIP.LastIndexOf('.')) + ".200";
     target.SetIP(value);
     string newIP = target.GetIP();
     target.SetIP(oldIP);
     Assert.Equals(newIP, value);
 }