using System; using System.ComponentModel; using System.Threading.Tasks; using System.Windows.Forms; using HeuristicLab.Clients.Hive.CloudManager.Azure; using HeuristicLab.Clients.Hive.CloudManager.Model; namespace HeuristicLab.Clients.Hive.CloudManager.Views { public partial class DeleteDeploymentView : Form { private const string formatDeleteDepoyment = "Are you sure you want to delete \" {0} \""; private const string formatDeleteHostedService = "Also delete Hosted Service \"{0}\""; private Deployment deployment; private HostedService hostedService; private BackgroundWorker worker; public bool ErrorOccured { get; set; } public DeleteDeploymentView(Deployment dep, HostedService hs) { InitializeComponent(); worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(DeleteDeploymentTask); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted); deployment = dep; hostedService = hs; lblDialogText.Text = string.Format(formatDeleteDepoyment, deployment.Label); if (hostedService.Deployments.Count < 2) cbDeleteHostedService.Visible = true; cbDeleteHostedService.Text = string.Format(formatDeleteHostedService, hostedService.HostedServiceProperties.Label); } private void btnNo_Click(object sender, System.EventArgs e) { this.Close(); } private void btnYes_Click(object sender, System.EventArgs e) { SetControlsEnabled(false); progressBar.Visible = true; var parameters = Tuple.Create(deployment, hostedService, cbDeleteHostedService.Checked); worker.RunWorkerAsync(parameters); } private void SetControlsEnabled(bool isEnabled) { btnNo.Enabled = isEnabled; btnYes.Enabled = isEnabled; } private void DeleteDeploymentTask(object sender, DoWorkEventArgs e) { Tuple parameters = e.Argument as Tuple; Deployment dep = parameters.Item1; HostedService hs = parameters.Item2; bool DeleteHostedService = parameters.Item3; if (DeleteHostedService) { Parallel.ForEach(hs.Deployments, d => { string requestId = CloudManagerClient.Instance.AzureProvider.DeleteDeployment(hs.Subscription, hs.ServiceName, d.Name); Constants.OperationResult result = CloudManagerClient.Instance.AzureProvider.PollGetOperationStatus(hs.Subscription, requestId, 4, 30); }); CloudManagerClient.Instance.AzureProvider.DeleteHostedService(hs.Subscription, hs.ServiceName); } else { CloudManagerClient.Instance.AzureProvider.DeleteDeployment(hs.Subscription, hs.ServiceName, dep.Name); } } private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { progressBar.Visible = false; if (e.Error == null) { ErrorOccured = false; this.Close(); } else { PluginInfrastructure.ErrorHandling.ShowErrorDialog(e.Error); ErrorOccured = true; this.Show(); SetControlsEnabled(true); } } } }