Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Views/AddAzureServiceDialog.cs @ 7387

Last change on this file since 7387 was 7387, checked in by spimming, 12 years ago

#1680:

  • Collection for hosted services added to CloudManagerClient
  • Disable controls while loading subscription
  • Stop and dispose timer
  • Rebuild treeview content on OnContentChanged event
File size: 12.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Windows.Forms;
5using HeuristicLab.Clients.Hive.CloudManager.Azure;
6using HeuristicLab.Clients.Hive.CloudManager.Model;
7using HeuristicLab.Core;
8
9namespace HeuristicLab.Clients.Hive.CloudManager.Views {
10  public partial class AddAzureServiceDialog : Form {
11    private BindingList<Subscription> subscriptions;
12    private BindingList<HostedService> hostedServices;
13    private BindingList<string> locations;
14    private BindingList<AffinityGroup> affinityGroups;
15
16    private BackgroundWorker workerUpdate;
17    private BackgroundWorker workerCreate;
18
19    private bool isInitializing;
20    private bool bwCompleted;
21    private bool closePending;
22
23    private string certificateFile;
24    private string certificatePassword;
25
26    public AddAzureServiceDialog(IItemList<Subscription> subs) {
27      InitializeComponent();
28      isInitializing = true;
29      bwCompleted = true;
30
31      this.subscriptions = new BindingList<Subscription>(subs);
32      this.hostedServices = new BindingList<HostedService>();
33      this.locations = new BindingList<string>();
34      this.affinityGroups = new BindingList<AffinityGroup>();
35
36      SetLookupBinding(cmbLocation, locations, "", "");
37      SetLookupBinding(cmbAffinityGroup, affinityGroups, "Label", "Name");
38      SetLookupBinding(cmbChooseHostedService, hostedServices, "ServiceName", "ServiceName");
39      SetLookupBinding(cmbChooseSubscription, subscriptions, "SubscriptionName", "SubscriptionID");
40
41      if (!cbNewHostedService.Checked) {
42        SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
43      }
44
45      certificateFile = string.Empty;
46      certificatePassword = string.Empty;
47
48      workerUpdate = new BackgroundWorker();
49      workerUpdate.DoWork += new DoWorkEventHandler(UpdateTask);
50      workerUpdate.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
51
52      workerCreate = new BackgroundWorker();
53      workerCreate.DoWork += new DoWorkEventHandler(CreateDoploymentTask);
54      workerCreate.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CreateDeploymentCompleted);
55
56      isInitializing = false;
57      cmbChooseSubscription_SelectedValueChanged(this, EventArgs.Empty);
58    }
59
60    protected override void OnFormClosing(FormClosingEventArgs e) {
61      if (!bwCompleted) {
62        SetAllControlsEnabled(this, false);
63        e.Cancel = true;
64        closePending = true;
65      }
66    }
67
68    private void SetLookupBinding(ListControl toBind, object dataSource, string displayMember, string valueMember) {
69      toBind.DisplayMember = displayMember;
70      toBind.ValueMember = valueMember;
71      toBind.DataSource = dataSource;
72    }
73
74    private void SetGroupBoxControlsEnabled(GroupBox gb, bool isEnabled) {
75      foreach (Control ctrl in gb.Controls) {
76        ctrl.Enabled = isEnabled;
77      }
78    }
79
80    private void SetAllControlsEnabled(Form frm, bool isEnabled) {
81      foreach (Control ctrl in frm.Controls) {
82        if (!(ctrl is ProgressBar)) {
83          ctrl.Enabled = isEnabled;
84        }
85      }
86    }
87
88    private void SetBindingList<T>(ref BindingList<T> bindinglist, List<T> list) {
89      if (list != null) {
90        BindingList<T> newList = new BindingList<T>(list);
91        bindinglist = newList;
92      }
93    }
94
95    private void UpdateBindingList<T>(BindingList<T> bindinglist, List<T> list) {
96      if (list != null) {
97        bindinglist.Clear();
98        foreach (T item in list)
99          bindinglist.Add(item);
100      }
101    }
102
103    private void UpdateSubscriptionItem(BindingList<Subscription> bindinglist, Subscription item) {
104      if (item != null) {
105        foreach (Subscription blItem in bindinglist)
106          if (blItem.Equals(item)) {
107            blItem.Merge(item);
108          }
109      }
110    }
111
112    private bool ValidateInput() {
113      bool valid = true;
114      // TODO: input validation
115      // see http://stackoverflow.com/questions/769184/winform-ui-validation
116      return valid;
117    }
118
119    private void cbNewHostedService_CheckedChanged(object sender, EventArgs e) {
120      if (cbNewHostedService.Checked) {
121        Subscription subscription = (Subscription)cmbChooseSubscription.SelectedItem;
122        if (subscription.CurrentHostedServices == subscription.MaxHostedServices) {
123          MessageBox.Show("You have already created the maximum number of hosted services", "Limit Reached", MessageBoxButtons.OK, MessageBoxIcon.Error);
124          cbNewHostedService.Checked = false;
125        } else {
126          SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
127          cmbChooseHostedService.Enabled = false;
128        }
129      } else {
130        // checked == false
131        SetGroupBoxControlsEnabled(gbNewHostedService, false);
132        if (hostedServices.Count == 0) {
133          cbNewHostedService.Checked = true;
134        }
135      }
136    }
137
138    private void cmbChooseSubscription_SelectedValueChanged(object sender, EventArgs e) {
139      if (!isInitializing) {
140        Subscription item = (Subscription)cmbChooseSubscription.SelectedItem;
141        if (item != null) {
142          bwCompleted = false;
143          progressBar.Visible = true;
144          SetAllControlsEnabled(this, false);
145          workerUpdate.RunWorkerAsync(item);
146        }
147      }
148    }
149
150    private void btnAddCertificate_Click(object sender, EventArgs e) {
151      using (var form = new AddCertificate()) {
152        form.ShowDialog();
153        if (!form.ErrorOccured) {
154          certificateFile = form.CertificateFile;
155          certificatePassword = form.CertificatePassword;
156          lblCertificateFile.Text = certificateFile;
157        }
158      }
159    }
160
161    private void btnCancel_Click(object sender, EventArgs e) {
162      this.Close();
163    }
164
165    private void btnOk_Click(object sender, EventArgs e) {
166      if (cmbChooseSubscription.SelectedItem != null) {
167        Subscription sub = (Subscription)cmbChooseSubscription.SelectedItem;
168        if (ValidateInput()) {
169          bool newHostedServiceChecked = cbNewHostedService.Checked;
170          bool regionChecked = rbRegion.Checked;
171          string certFile = certificateFile;
172          string certPw = certificatePassword;
173          int instanceCount = Convert.ToInt32(tbInstanceCount.Text);
174          HostedService hostedService = new HostedService();
175          hostedService.ServiceName = tbServiceName.Text;
176          HostedServiceProperties properties = new HostedServiceProperties();
177          if (regionChecked) {
178            properties.AffinityGroup = string.Empty;
179            properties.Location = (string)cmbLocation.SelectedItem;
180          } else {
181            properties.AffinityGroup = ((AffinityGroup)cmbAffinityGroup.SelectedItem).Name;
182            properties.Location = string.Empty;
183          }
184
185          properties.AffinityGroup = ((AffinityGroup)cmbAffinityGroup.SelectedItem).Name;
186          properties.Label = tbLabel.Text;
187          properties.Location = (string)cmbLocation.SelectedItem;
188          hostedService.HostedServiceProperties = properties;
189
190          var parameters = Tuple.Create<Subscription, bool, bool, string, string, HostedService, int>(sub, newHostedServiceChecked, regionChecked, certificateFile, certificatePassword, hostedService, instanceCount);
191
192          bwCompleted = false;
193          progressBar.Visible = true;
194          SetAllControlsEnabled(this, false);
195          workerCreate.RunWorkerAsync(parameters);
196        }
197      }
198    }
199
200    private void UpdateTask(object sender, DoWorkEventArgs e) {
201      try {
202        Subscription item = (Subscription)e.Argument;
203        item = CloudManagerClient.Instance.AzureProvider.GetSubscriptionInfo(item.SubscriptionID, item.CertificateThumbprint);
204        this.Invoke((MethodInvoker)delegate { lblCores.Text = item.CurrentCoreCount + " / " + item.MaxCoreCount; });
205
206        List<HostedService> services = CloudManagerClient.Instance.AzureProvider.ListHostedServices(item);
207        List<string> locs = CloudManagerClient.Instance.AzureProvider.ListLocations(item);
208        List<AffinityGroup> groups = CloudManagerClient.Instance.AzureProvider.ListAffinityGroups(item);
209
210        this.Invoke((MethodInvoker)delegate { UpdateSubscriptionItem(subscriptions, item); });
211        this.Invoke((MethodInvoker)delegate { UpdateBindingList<HostedService>(hostedServices, services); });
212        this.Invoke((MethodInvoker)delegate { UpdateBindingList<string>(locations, locs); });
213        this.Invoke((MethodInvoker)delegate { UpdateBindingList<AffinityGroup>(affinityGroups, groups); });
214      }
215      catch (Exception ex) {
216        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
217      }
218    }
219
220    private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
221      SetAllControlsEnabled(this, true);
222      progressBar.Visible = false;
223      bwCompleted = true;
224      if (closePending) {
225        this.Close();
226      }
227    }
228
229    private void CreateDoploymentTask(object sender, DoWorkEventArgs e) {
230      bool errorOccured = false;
231      Tuple<Subscription, bool, bool, string, string, HostedService, int> parameters = (Tuple<Subscription, bool, bool, string, string, HostedService, int>)e.Argument;
232
233      Subscription sub = parameters.Item1;
234      bool newHostedServiceChecked = parameters.Item2;
235      bool regionChecked = parameters.Item3;
236      string certFile = parameters.Item4;
237      string certPw = parameters.Item5;
238      HostedService hostedService = parameters.Item6;
239      int instanceCount = parameters.Item7;
240
241      // STEP 1 - Create Hosted Service
242      try {
243        if (newHostedServiceChecked) {
244          if (regionChecked) {
245            CloudManagerClient.Instance.AzureProvider.CreateHostedService(sub, hostedService.ServiceName, hostedService.HostedServiceProperties.Label, hostedService.HostedServiceProperties.Description, hostedService.HostedServiceProperties.Location);
246          } else {
247            CloudManagerClient.Instance.AzureProvider.CreateHostedService(sub, hostedService.ServiceName, hostedService.HostedServiceProperties.Label, hostedService.HostedServiceProperties.Description, new AffinityGroup() { Name = hostedService.HostedServiceProperties.AffinityGroup });
248          }
249        }
250      }
251      catch (Exception ex) {
252        errorOccured = true;
253        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
254      }
255
256      // STEP 2 - Add Certificate
257      try {
258        if ((!errorOccured) && (certFile != string.Empty)) {
259          CloudManagerClient.Instance.AzureProvider.AddCertificate(sub, hostedService, certFile, certPw);
260        }
261      }
262      catch (Exception ex) {
263        errorOccured = true;
264        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
265        if (newHostedServiceChecked) {
266          CloudManagerClient.Instance.AzureProvider.DeleteHostedService(sub, hostedService.ServiceName);
267        }
268
269      }
270
271      // STEP 3 - Create Deployment
272      try {
273        if (!errorOccured) {
274          CloudManagerClient.Instance.AzureProvider.CreateDeployment(sub, hostedService.ServiceName, Guid.NewGuid().ToString(), Constants.DeploymentSlotStaging, Constants.DeploymentPackageUrl, Constants.DeploymentConfigurationUrl, Constants.DeploymentLabel, instanceCount);
275        }
276      }
277      catch (Exception ex) {
278        errorOccured = true;
279        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
280        if (newHostedServiceChecked) {
281          CloudManagerClient.Instance.AzureProvider.DeleteHostedService(sub, hostedService.ServiceName);
282        }
283      }
284
285      e.Result = errorOccured;
286    }
287
288    private void CreateDeploymentCompleted(object sender, RunWorkerCompletedEventArgs e) {
289      SetAllControlsEnabled(this, true);
290      progressBar.Visible = false;
291      bwCompleted = true;
292      bool errorOccured = (bool)e.Result;
293      if (closePending) {
294        this.Close();
295      } else if (!errorOccured) {
296        this.Close();
297      } else {
298        this.Show();
299      }
300    }
301  }
302}
Note: See TracBrowser for help on using the repository browser.