1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Windows.Forms;
|
---|
5 | using HeuristicLab.Clients.Hive.CloudManager.Model;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Clients.Hive.CloudManager.Views {
|
---|
9 | public partial class AddAzureServiceDialog : Form {
|
---|
10 | private BindingList<Subscription> subscriptions;
|
---|
11 | private BindingList<HostedService> hostedServices;
|
---|
12 | private BindingList<string> locations;
|
---|
13 | private BindingList<AffinityGroup> affinityGroups;
|
---|
14 |
|
---|
15 | private BackgroundWorker worker;
|
---|
16 |
|
---|
17 | private bool isInitializing;
|
---|
18 | private bool bwCompleted;
|
---|
19 | private bool closePending;
|
---|
20 |
|
---|
21 | private string certificateFile;
|
---|
22 | private string certificatePassword;
|
---|
23 |
|
---|
24 | public AddAzureServiceDialog(IItemList<Subscription> subs) {
|
---|
25 | InitializeComponent();
|
---|
26 | isInitializing = true;
|
---|
27 | bwCompleted = true;
|
---|
28 |
|
---|
29 | this.subscriptions = new BindingList<Subscription>(subs);
|
---|
30 | this.hostedServices = new BindingList<HostedService>();
|
---|
31 | this.locations = new BindingList<string>();
|
---|
32 | this.affinityGroups = new BindingList<AffinityGroup>();
|
---|
33 |
|
---|
34 | SetLookupBinding(cmbLocation, locations, "", "");
|
---|
35 | SetLookupBinding(cmbAffinityGroup, affinityGroups, "Label", "Name");
|
---|
36 | SetLookupBinding(cmbChooseHostedService, hostedServices, "ServiceName", "ServiceName");
|
---|
37 | SetLookupBinding(cmbChooseSubscription, subscriptions, "SubscriptionName", "SubscriptionID");
|
---|
38 |
|
---|
39 | if (!cbNewHostedService.Checked) {
|
---|
40 | SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
|
---|
41 | }
|
---|
42 |
|
---|
43 | worker = new BackgroundWorker();
|
---|
44 | worker.DoWork += new DoWorkEventHandler(UpdateTask);
|
---|
45 | worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
|
---|
46 |
|
---|
47 |
|
---|
48 | isInitializing = false;
|
---|
49 | cmbChooseSubscription_SelectedValueChanged(this, EventArgs.Empty);
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected override void OnFormClosing(FormClosingEventArgs e) {
|
---|
53 | if (!bwCompleted) {
|
---|
54 | SetAllControlsEnabled(this, false);
|
---|
55 | e.Cancel = true;
|
---|
56 | closePending = true;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void SetLookupBinding(ListControl toBind, object dataSource, string displayMember, string valueMember) {
|
---|
61 | toBind.DisplayMember = displayMember;
|
---|
62 | toBind.ValueMember = valueMember;
|
---|
63 | toBind.DataSource = dataSource;
|
---|
64 | }
|
---|
65 |
|
---|
66 | private void SetGroupBoxControlsEnabled(GroupBox gb, bool isEnabled) {
|
---|
67 | foreach (Control ctrl in gb.Controls) {
|
---|
68 | ctrl.Enabled = isEnabled;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void SetAllControlsEnabled(Form frm, bool isEnabled) {
|
---|
73 | foreach (Control ctrl in frm.Controls) {
|
---|
74 | if (!(ctrl is ProgressBar)) {
|
---|
75 | ctrl.Enabled = isEnabled;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void SetBindingList<T>(ref BindingList<T> bindinglist, List<T> list) {
|
---|
81 | if (list != null) {
|
---|
82 | BindingList<T> newList = new BindingList<T>(list);
|
---|
83 | bindinglist = newList;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void UpdateBindingList<T>(BindingList<T> bindinglist, List<T> list) {
|
---|
88 | if (list != null) {
|
---|
89 | bindinglist.Clear();
|
---|
90 | foreach (T item in list)
|
---|
91 | bindinglist.Add(item);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void cbNewHostedService_CheckedChanged(object sender, EventArgs e) {
|
---|
96 | if (cbNewHostedService.Checked) {
|
---|
97 | Subscription subscription = (Subscription)cmbChooseSubscription.SelectedItem;
|
---|
98 | if (subscription.CurrentHostedServices == subscription.MaxHostedServices) {
|
---|
99 | MessageBox.Show("You have already created the maximum number of hosted services", "Limit Reached", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
100 | cbNewHostedService.Checked = false;
|
---|
101 | } else {
|
---|
102 | SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
|
---|
103 | cmbChooseHostedService.Enabled = false;
|
---|
104 | }
|
---|
105 | } else {
|
---|
106 | // checked == false
|
---|
107 | SetGroupBoxControlsEnabled(gbNewHostedService, false);
|
---|
108 | if (hostedServices.Count == 0) {
|
---|
109 | cbNewHostedService.Checked = true;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void cmbChooseSubscription_SelectedValueChanged(object sender, EventArgs e) {
|
---|
115 | if (!isInitializing) {
|
---|
116 | Subscription item = (Subscription)cmbChooseSubscription.SelectedItem;
|
---|
117 | if (item != null) {
|
---|
118 | bwCompleted = false;
|
---|
119 | progressBar.Visible = true;
|
---|
120 | SetAllControlsEnabled(this, false);
|
---|
121 | worker.RunWorkerAsync(item);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void btnAddCertificate_Click(object sender, EventArgs e) {
|
---|
127 | using (var form = new AddCertificate()) {
|
---|
128 | form.ShowDialog();
|
---|
129 | if (!form.ErrorOccured) {
|
---|
130 | certificateFile = form.CertificateFile;
|
---|
131 | certificatePassword = form.CertificatePassword;
|
---|
132 | lblCertificateFile.Text = certificateFile;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | private void btnCancel_Click(object sender, EventArgs e) {
|
---|
138 | this.Close();
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void btnOk_Click(object sender, EventArgs e) {
|
---|
142 | if (cmbChooseSubscription.SelectedItem != null) {
|
---|
143 | if (cbNewHostedService.Checked) {
|
---|
144 | if (rbRegion.Checked) {
|
---|
145 | string serviceName = tbServiceName.Text;
|
---|
146 | string label = tbLabel.Text;
|
---|
147 | string location = (string)cmbLocation.SelectedItem;
|
---|
148 | Subscription sub = (Subscription)cmbChooseSubscription.SelectedItem;
|
---|
149 | CloudManagerClient.Instance.AzureProvider.CreateHostedService(sub, serviceName, label, "Hosted service for hive slave", location);
|
---|
150 | } else {
|
---|
151 | // rbAffinitGroup.Checked
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void UpdateTask(object sender, DoWorkEventArgs e) {
|
---|
158 | try {
|
---|
159 | Subscription item = (Subscription)e.Argument;
|
---|
160 | item = CloudManagerClient.Instance.AzureProvider.GetSubscriptionInfo(item.SubscriptionID, item.CertificateThumbprint);
|
---|
161 | this.Invoke((MethodInvoker)delegate { lblCores.Text = item.CurrentCoreCount + " / " + item.MaxCoreCount; });
|
---|
162 |
|
---|
163 | List<HostedService> services = CloudManagerClient.Instance.AzureProvider.ListHostedServices(item);
|
---|
164 | List<string> locs = CloudManagerClient.Instance.AzureProvider.ListLocations(item);
|
---|
165 | List<AffinityGroup> groups = CloudManagerClient.Instance.AzureProvider.ListAffinityGroups(item);
|
---|
166 |
|
---|
167 | this.Invoke((MethodInvoker)delegate { UpdateBindingList<HostedService>(hostedServices, services); });
|
---|
168 | this.Invoke((MethodInvoker)delegate { UpdateBindingList<string>(locations, locs); });
|
---|
169 | this.Invoke((MethodInvoker)delegate { UpdateBindingList<AffinityGroup>(affinityGroups, groups); });
|
---|
170 | }
|
---|
171 | catch (Exception ex) {
|
---|
172 | MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
177 | SetAllControlsEnabled(this, true);
|
---|
178 | progressBar.Visible = false;
|
---|
179 | bwCompleted = true;
|
---|
180 | if (closePending) {
|
---|
181 | this.Close();
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|