Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7421 was 7421, checked in by spimming, 13 years ago

#1680:

  • GetOperationStatus method added
  • Check if affinity group is not null
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          if (cmbAffinityGroup.SelectedItem != null) {
186            properties.AffinityGroup = ((AffinityGroup)cmbAffinityGroup.SelectedItem).Name;
187          }
188          properties.Label = tbLabel.Text;
189          properties.Location = (string)cmbLocation.SelectedItem;
190          hostedService.HostedServiceProperties = properties;
191
192          var parameters = Tuple.Create<Subscription, bool, bool, string, string, HostedService, int>(sub, newHostedServiceChecked, regionChecked, certificateFile, certificatePassword, hostedService, instanceCount);
193
194          bwCompleted = false;
195          progressBar.Visible = true;
196          SetAllControlsEnabled(this, false);
197          workerCreate.RunWorkerAsync(parameters);
198        }
199      }
200    }
201
202    private void UpdateTask(object sender, DoWorkEventArgs e) {
203      try {
204        Subscription item = (Subscription)e.Argument;
205        item = CloudManagerClient.Instance.AzureProvider.GetSubscriptionInfo(item.SubscriptionID, item.CertificateThumbprint);
206        this.Invoke((MethodInvoker)delegate { lblCores.Text = item.CurrentCoreCount + " / " + item.MaxCoreCount; });
207
208        List<HostedService> services = CloudManagerClient.Instance.AzureProvider.ListHostedServices(item);
209        List<string> locs = CloudManagerClient.Instance.AzureProvider.ListLocations(item);
210        List<AffinityGroup> groups = CloudManagerClient.Instance.AzureProvider.ListAffinityGroups(item);
211
212        this.Invoke((MethodInvoker)delegate { UpdateSubscriptionItem(subscriptions, item); });
213        this.Invoke((MethodInvoker)delegate { UpdateBindingList<HostedService>(hostedServices, services); });
214        this.Invoke((MethodInvoker)delegate { UpdateBindingList<string>(locations, locs); });
215        this.Invoke((MethodInvoker)delegate { UpdateBindingList<AffinityGroup>(affinityGroups, groups); });
216      }
217      catch (Exception ex) {
218        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
219      }
220    }
221
222    private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
223      SetAllControlsEnabled(this, true);
224      progressBar.Visible = false;
225      bwCompleted = true;
226      if (closePending) {
227        this.Close();
228      }
229    }
230
231    private void CreateDoploymentTask(object sender, DoWorkEventArgs e) {
232      bool errorOccured = false;
233      Tuple<Subscription, bool, bool, string, string, HostedService, int> parameters = (Tuple<Subscription, bool, bool, string, string, HostedService, int>)e.Argument;
234
235      Subscription sub = parameters.Item1;
236      bool newHostedServiceChecked = parameters.Item2;
237      bool regionChecked = parameters.Item3;
238      string certFile = parameters.Item4;
239      string certPw = parameters.Item5;
240      HostedService hostedService = parameters.Item6;
241      int instanceCount = parameters.Item7;
242
243      // STEP 1 - Create Hosted Service
244      try {
245        if (newHostedServiceChecked) {
246          if (regionChecked) {
247            CloudManagerClient.Instance.AzureProvider.CreateHostedService(sub, hostedService.ServiceName, hostedService.HostedServiceProperties.Label, hostedService.HostedServiceProperties.Description, hostedService.HostedServiceProperties.Location);
248          } else {
249            CloudManagerClient.Instance.AzureProvider.CreateHostedService(sub, hostedService.ServiceName, hostedService.HostedServiceProperties.Label, hostedService.HostedServiceProperties.Description, new AffinityGroup() { Name = hostedService.HostedServiceProperties.AffinityGroup });
250          }
251        }
252      }
253      catch (Exception ex) {
254        errorOccured = true;
255        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
256      }
257
258      // STEP 2 - Add Certificate
259      try {
260        if ((!errorOccured) && (certFile != string.Empty)) {
261          CloudManagerClient.Instance.AzureProvider.AddCertificate(sub, hostedService, certFile, certPw);
262        }
263      }
264      catch (Exception ex) {
265        errorOccured = true;
266        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
267        if (newHostedServiceChecked) {
268          CloudManagerClient.Instance.AzureProvider.DeleteHostedService(sub, hostedService.ServiceName);
269        }
270
271      }
272
273      // STEP 3 - Create Deployment
274      try {
275        if (!errorOccured) {
276          CloudManagerClient.Instance.AzureProvider.CreateDeployment(sub, hostedService.ServiceName, Guid.NewGuid().ToString(), Constants.DeploymentSlotStaging, Constants.DeploymentPackageUrl, Constants.DeploymentConfigurationUrl, Constants.DeploymentLabel, instanceCount);
277        }
278      }
279      catch (Exception ex) {
280        errorOccured = true;
281        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
282        if (newHostedServiceChecked) {
283          CloudManagerClient.Instance.AzureProvider.DeleteHostedService(sub, hostedService.ServiceName);
284        }
285      }
286
287      e.Result = errorOccured;
288    }
289
290    private void CreateDeploymentCompleted(object sender, RunWorkerCompletedEventArgs e) {
291      SetAllControlsEnabled(this, true);
292      progressBar.Visible = false;
293      bwCompleted = true;
294      bool errorOccured = (bool)e.Result;
295      if (closePending) {
296        this.Close();
297      } else if (!errorOccured) {
298        this.Close();
299      } else {
300        this.Show();
301      }
302    }
303  }
304}
Note: See TracBrowser for help on using the repository browser.