Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/CloudManagerClient.cs @ 7441

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

#1680: Implemented Save and Delete in CloudResourcesView and CloudManagerClient

File size: 6.3 KB
RevLine 
[7326]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[7387]23using System.Collections.Generic;
[7299]24using HeuristicLab.Clients.Hive.CloudManager.Azure;
25using HeuristicLab.Clients.Hive.CloudManager.Model;
26using HeuristicLab.Common;
[7278]27using HeuristicLab.Core;
28
29namespace HeuristicLab.Clients.Hive.CloudManager {
30  [Item("CloudManagerClient", "Hive Cloud Manager Client.")]
31  public sealed class CloudManagerClient : IContent {
32    private static CloudManagerClient instance;
33    public static CloudManagerClient Instance {
34      get {
35        if (instance == null) instance = new CloudManagerClient();
36        return instance;
37      }
38    }
39
[7324]40    private CloudManagerClient() {
41      subscriptions = new ItemList<Subscription>();
[7387]42      hostedServices = new ItemList<HostedService>();
[7324]43      azureProvider = new AzureProvider();
44    }
[7278]45
46    #region Properties
[7317]47    private IItemList<Subscription> subscriptions;
48    public IItemList<Subscription> Subscriptions {
[7299]49      get { return subscriptions; }
50      set {
51        if (value != subscriptions) {
52          subscriptions = value;
53          //fire event OnSubscriptionsChagned
54        }
55      }
56    }
57
[7387]58    private IItemList<HostedService> hostedServices;
59    public IItemList<HostedService> HostedServices {
60      get { return hostedServices; }
61      set {
62        if (value != hostedServices) {
63          hostedServices = value;
64        }
65      }
66    }
67
[7299]68    private IAzureProvider azureProvider;
69    public IAzureProvider AzureProvider {
70      get { return azureProvider; }
71      set { azureProvider = value; }
72    }
73
[7278]74    #endregion
75
[7299]76    #region Events
[7278]77
[7299]78    public event EventHandler Refreshing;
79    private void OnRefreshing() {
80      EventHandler handler = Refreshing;
81      if (handler != null) handler(this, EventArgs.Empty);
82    }
83    public event EventHandler Refreshed;
84    private void OnRefreshed() {
85      var handler = Refreshed;
86      if (handler != null) handler(this, EventArgs.Empty);
87    }
88
89    #endregion
90
91    #region Refresh
92
93    public void Refresh() {
94      OnRefreshing();
95
96      try {
[7324]97        IItemList<Subscription> subs = new ItemList<Subscription>(Subscriptions);
[7326]98        foreach (Subscription subscription in subs) {
99          if (subscription.DiscoverServices) {
[7387]100            List<HostedService> servs = AzureProvider.DiscoverSlaveService(subscription);
101            foreach (HostedService s in servs) {
102              Add(s);
103            }
[7326]104          }
105        }
[7299]106      }
107      catch {
108        throw;
109      }
110      finally {
111        OnRefreshed();
112      }
113    }
114
115    #endregion
116
[7324]117    public void Add(Subscription subscription) {
118      if (subscription == null) {
119        throw new ArgumentNullException("subscription", "Subscription must not be null.");
120      }
121      if (Subscriptions.Contains(subscription)) {
122        Subscriptions.Remove(subscription);
123      }
124      Subscriptions.Add(subscription);
125    }
[7299]126
[7324]127    public void Remove(Subscription subscription) {
128      if (subscription == null) {
129        throw new ArgumentNullException("subscription", "Subscription must not be null.");
130      }
131      if (Subscriptions.Contains(subscription)) {
132        Subscriptions.Remove(subscription);
133      }
134    }
135
[7387]136    public void Add(HostedService hostedService) {
137      if (hostedService == null) {
138        throw new ArgumentNullException("subscription", "Subscription must not be null.");
139      }
140      if (HostedServices.Contains(hostedService)) {
[7433]141        //HostedServices.Remove(hostedService);
142        HostedService hs = HostedServices[HostedServices.IndexOf(hostedService)];
143        hs.Merge(hostedService);
144
145      } else {
146        HostedServices.Add(hostedService);
[7387]147      }
148    }
[7324]149
[7441]150    public void ChangeIntances(Deployment deployment, HostedService hostedService) {
151      if (deployment == null) {
152        throw new ArgumentNullException("Deployment", "Deployment must not be null.");
153      }
154      if (hostedService == null) {
155        throw new ArgumentNullException("HostedService", "HostedService must not be null.");
156      }
157      if (deployment.Modified) {
158        AzureProvider.ChangeInstanceCount(deployment.Subscription,
159                                          hostedService.ServiceName,
160                                          deployment.DeploymentSlot,
161                                          HeuristicLab.Clients.Hive.CloudManager.Azure.Constants.DeploymentRoleName,
162                                          deployment.NewInstanceCount);
163        deployment.Modified = false;
164      }
165    }
[7324]166
[7441]167    public void Delete(Deployment deployment, HostedService hostedService) {
168      if (deployment == null) {
169        throw new ArgumentNullException("Deployment", "Deployment must not be null.");
170      }
171      if (hostedService == null) {
172        throw new ArgumentNullException("HostedService", "HostedService must not be null.");
173      }
174      //AzureProvider.DeleteDeployment();
175    }
176
177    public void Delete(HostedService hostedService) {
178      if (hostedService == null) {
179        throw new ArgumentNullException("HostedService", "HostedService must not be null.");
180      }
181      AzureProvider.DeleteHostedService(hostedService.Subscription, hostedService.ServiceName);
182    }
183
184    public void Delete(Subscription subscription) {
185      if (subscription == null) {
186        throw new ArgumentNullException("subscription", "Subscription must not be null.");
187      }
188      if (Subscriptions.Contains(subscription)) {
189        Subscriptions.Remove(subscription);
190      }
191    }
192
193
[7278]194  }
195}
Note: See TracBrowser for help on using the repository browser.