public ProviderResponse<Cloud> Connect(Cloud cloud)
        {
            ProviderResponse<Cloud> response = new ProviderResponse<Cloud>();
            Cloud local = cloud.DeepCopy();
            IVcapClient client = new VcapClient(local);

            try
            {
                VcapClientResult result = client.Login();
                if (!result.Success)
                    throw new Exception(result.Message);
                local.AccessToken = client.CurrentToken;
                var applications = client.GetApplications();
                var provisionedServices = client.GetProvisionedServices();
                var availableServices = client.GetSystemServices();
                local.Applications.Synchronize(new SafeObservableCollection<Application>(applications), new ApplicationEqualityComparer());
                local.Services.Synchronize(new SafeObservableCollection<ProvisionedService>(provisionedServices), new ProvisionedServiceEqualityComparer());
                local.AvailableServices.Synchronize(new SafeObservableCollection<SystemService>(availableServices), new SystemServiceEqualityComparer());
                foreach (Application app in local.Applications)
                {
                    var instances = GetInstances(local, app);
                    if (instances.Response != null)
                        app.InstanceCollection.Synchronize(new SafeObservableCollection<Instance>(instances.Response), new InstanceEqualityComparer());
                }
                response.Response = local;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }
            return response;
        }
 public ProviderResponse<bool> ChangePassword(Cloud cloud, string newPassword)
 {
     ProviderResponse<bool> response = new ProviderResponse<bool>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         var vcapResult = client.ChangePassword(newPassword);
         if (!vcapResult.Success)
             throw new Exception(vcapResult.Message);
         response.Response = true;
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<bool> ValidateAccount(Cloud cloud)
 {
     ProviderResponse<bool> response = new ProviderResponse<bool>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         var vcapResponse = client.Login();
         if (vcapResponse != null &&
             !vcapResponse.Success &&
             !String.IsNullOrEmpty(vcapResponse.Message))
             throw new Exception(vcapResponse.Message);
         response.Response = true;
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<bool> Update(Cloud cloud, Application app, string directoryToPushFrom)
 {
     ProviderResponse<bool> response = new ProviderResponse<bool>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         var result = client.Update(app.Name, new System.IO.DirectoryInfo(directoryToPushFrom));
         if (!result.Success)
             throw new Exception(result.Message);
         response.Response = true;
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<bool> UpdateApplication(Application app, Cloud cloud)
 {
     ProviderResponse<bool> response = new ProviderResponse<bool>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         var vcapResponse = client.UpdateApplication(app);
         if (vcapResponse != null && !String.IsNullOrEmpty(vcapResponse.Description))
             throw new Exception(vcapResponse.Description);
         response.Response = true;
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<bool> RegisterAccount(Cloud cloud,string email, string password)
 {
     ProviderResponse<bool> response = new ProviderResponse<bool>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         var vcapResult = client.AddUser(email,password);
         if (!vcapResult.Success)
             throw new Exception(vcapResult.Message);
         response.Response = true;
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<bool> Stop(Application app, Cloud cloud)
 {
     ProviderResponse<bool> response = new ProviderResponse<bool>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         client.Stop(app);
         response.Response = true;
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<SafeObservableCollection<StatInfo>> GetStats(Cloud cloud, Application application)
 {
     ProviderResponse<SafeObservableCollection<StatInfo>> response = new ProviderResponse<SafeObservableCollection<StatInfo>>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         response.Response = new SafeObservableCollection<StatInfo>(client.GetStats(application));
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<bool> Push(Cloud cloud, string name, string url, ushort instances, string directoryToPushFrom, uint memory, string[] services)
 {
     ProviderResponse<bool> response = new ProviderResponse<bool>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         var result = client.Push(name, url, instances, new System.IO.DirectoryInfo(directoryToPushFrom), memory, services);
         if (!result.Success)
             throw new Exception(result.Message);
         response.Response = true;
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<SafeObservableCollection<ProvisionedService>> GetProvisionedServices(Cloud cloud)
 {
     ProviderResponse<SafeObservableCollection<ProvisionedService>> response = new ProviderResponse<SafeObservableCollection<ProvisionedService>>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         response.Response = new SafeObservableCollection<ProvisionedService>(client.GetProvisionedServices());
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<IEnumerable<StatInfo>> GetStats(Application app, Cloud cloud)
 {
     var response = new ProviderResponse<IEnumerable<StatInfo>>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         response.Response = client.GetStats(app);
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
        public ProviderResponse<IEnumerable<Instance>> GetInstances(Cloud cloud, Application app)
        {
            var response = new ProviderResponse<IEnumerable<Instance>>();
            try
            {
                IVcapClient client = new VcapClient(cloud);
                var stats = client.GetStats(app);
                var instances = new SafeObservableCollection<Instance>();
                if (stats != null)
                {

                    foreach (var stat in stats)
                    {
                        var instance = new Instance()
                                       {
                                           ID = stat.ID,
                                           State = stat.State
                                       };
                        if (stat.Stats != null)
                        {
                            instance.Cores = stat.Stats.Cores;
                            instance.MemoryQuota = stat.Stats.MemQuota/1048576;
                            instance.DiskQuota = stat.Stats.DiskQuota/1048576;
                            instance.Host = stat.Stats.Host;
                            instance.Parent = app;
                            instance.Uptime = TimeSpan.FromSeconds(Convert.ToInt32(stat.Stats.Uptime));

                            if (stat.Stats.Usage != null)
                            {
                                instance.Cpu = stat.Stats.Usage.CpuTime/100;
                                instance.Memory = Convert.ToInt32(stat.Stats.Usage.MemoryUsage)/1024;
                                instance.Disk = Convert.ToInt32(stat.Stats.Usage.DiskUsage)/1048576;
                            }
                        }
                        instances.Add(instance);
                    }
                }
                response.Response = instances;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }
            return response;
        }
 public ProviderResponse<VcapFilesResult> GetFiles(Cloud cloud, Application application, string path, ushort instanceId)
 {
     ProviderResponse<VcapFilesResult> response = new ProviderResponse<VcapFilesResult>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         var result = client.Files(application.Name, path, instanceId);
         response.Response = result;
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<Application> GetApplication(Application app, Cloud cloud)
 {
     var response = new ProviderResponse<Application>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         response.Response = client.GetApplication(app.Name);
         var instancesResponse = this.GetInstances(cloud, app);
         if (instancesResponse.Response != null)
             response.Response.InstanceCollection.Synchronize(new SafeObservableCollection<Instance>(instancesResponse.Response),new InstanceEqualityComparer());
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
 public ProviderResponse<bool> CreateService(Cloud cloud, string serviceName, string provisionedServiceName)
 {
     ProviderResponse<bool> response = new ProviderResponse<bool>();
     try
     {
         IVcapClient client = new VcapClient(cloud);
         var vcapResult = client.CreateService(serviceName, provisionedServiceName);
         if (!vcapResult.Success)
             throw new Exception(vcapResult.Message);
         response.Response = true;
     }
     catch (Exception ex)
     {
         response.Message = ex.Message;
     }
     return response;
 }
        private void PerformAction(string action, Project project, Cloud cloud, ProjectDirectories dir,
            Func<IVcapClient, DirectoryInfo, VcapClientResult> function)
        {
            var worker = new BackgroundWorker();

            Messenger.Default.Register<NotificationMessageAction<string>>(this,
                message =>
                {
                    if (message.Notification.Equals(Messages.SetProgressData))
                        message.Execute(action);
                });

            var window = new ProgressDialog();
            var dispatcher = window.Dispatcher;
            var helper = new WindowInteropHelper(window);
            helper.Owner = (IntPtr)(dte.MainWindow.HWnd);
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += (s, args) =>
            {
                if (worker.CancellationPending) { args.Cancel = true; return; }
                Messenger.Default.Send(new ProgressMessage(0, "Starting " + action));
                var site = project.Object as VsWebSite.VSWebSite;

                if (worker.CancellationPending) { args.Cancel = true; return; }
                if (!Directory.Exists(dir.StagingPath))
                {
                    dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressMessage(10, "Creating Staging Path"))));
                    Directory.CreateDirectory(dir.StagingPath);
                }

                if (worker.CancellationPending) { args.Cancel = true; return; }
                if (Directory.Exists(dir.DeployFromPath))
                {
                    dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressMessage(10, "Creating Precompiled Site Path"))));
                    Directory.Delete(dir.DeployFromPath, true);
                }

                if (worker.CancellationPending) { args.Cancel = true; return; }

                dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressMessage(30, "Preparing Compiler"))));

                if (site != null)
                    site.PreCompileWeb(dir.DeployFromPath, true);
                else
                {
                    var frameworkPath = (site == null) ? project.GetFrameworkPath() : string.Empty;

                    string objDir = Path.Combine(dir.ProjectDirectory, "obj");
                    if (Directory.Exists(objDir))
                    {
                        Directory.Delete(objDir, true); // NB: this can cause precompile errors
                    }
                    var process = new System.Diagnostics.Process()
                    {
                        StartInfo = new ProcessStartInfo()
                        {

                            FileName = frameworkPath + "\\aspnet_compiler.exe",
                            Arguments = String.Format("-nologo -v / -p \"{0}\" -f -u -c \"{1}\"", dir.ProjectDirectory, dir.DeployFromPath),
                            CreateNoWindow = true,
                            ErrorDialog = false,
                            UseShellExecute = false,
                            RedirectStandardOutput = true
                        }
                    };

                    dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressMessage(40, "Precompiling Site"))));
                    process.Start();
                    var output = process.StandardOutput.ReadToEnd();
                    process.WaitForExit();
                    if (false == String.IsNullOrEmpty(output))
                    {
                        dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressError("Asp Compile Error: " + output))));
                        return;
                    }
                }

                dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressMessage(50, "Logging in to Cloud Foundry"))));
                if (worker.CancellationPending) { args.Cancel = true; return; }

                var client = new VcapClient(cloud);
                var result = client.Login();

                if (result.Success == false)
                {
                    dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressError("Vcap Login Failure: " + result.Message))));
                    return;
                }
                dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressMessage(75, "Sending to " + cloud.Url))));
                if (worker.CancellationPending) { args.Cancel = true; return; }

                var response = function(client, new DirectoryInfo(dir.DeployFromPath));
                if (result.Success == false)
                {
                    dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressError("Vcap Login Failure: " + result.Message))));
                    return;
                }
                dispatcher.BeginInvoke((Action)(() => Messenger.Default.Send(new ProgressMessage(100, action + " complete."))));
            };

            worker.RunWorkerAsync();
            if (!window.ShowDialog().GetValueOrDefault())
                worker.CancelAsync();
        }