#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using HeuristicLab.Clients.Hive.CloudManager.Azure;
using HeuristicLab.Clients.Hive.CloudManager.Model;
using HeuristicLab.Clients.Hive.CloudManager.Properties;
using HeuristicLab.Common;
using HeuristicLab.Core;
namespace HeuristicLab.Clients.Hive.CloudManager {
[Item("CloudManagerClient", "Hive Cloud Manager Client.")]
public sealed class CloudManagerClient : IContent {
private static CloudManagerClient instance;
public static CloudManagerClient Instance {
get {
if (instance == null) instance = new CloudManagerClient();
return instance;
}
}
private CloudManagerClient() {
subscriptions = new ItemList();
hostedServices = new ItemList();
azureProvider = new AzureProvider();
}
#region Properties
private IItemList subscriptions;
public IItemList Subscriptions {
get { return subscriptions; }
set {
if (value != subscriptions) {
subscriptions = value;
//fire event OnSubscriptionsChagned
}
}
}
private IItemList hostedServices;
public IItemList HostedServices {
get { return hostedServices; }
set {
if (value != hostedServices) {
hostedServices = value;
}
}
}
private IAzureProvider azureProvider;
public IAzureProvider AzureProvider {
get { return azureProvider; }
set { azureProvider = value; }
}
#endregion
#region Events
public event EventHandler Refreshing;
private void OnRefreshing() {
EventHandler handler = Refreshing;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Refreshed;
private void OnRefreshed() {
var handler = Refreshed;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
#region Refresh
public void Refresh() {
OnRefreshing();
try {
IItemList subs = new ItemList(Subscriptions);
foreach (Subscription subscription in subs) {
if (subscription.DiscoverServices) {
List servs = AzureProvider.DiscoverSlaveService(subscription);
foreach (HostedService s in servs) {
Add(s);
}
List listToDelete = new List();
foreach (HostedService s in HostedServices) {
if (!servs.Contains(s)) {
//Remove(s);
listToDelete.Add(s);
}
}
foreach (HostedService s in listToDelete) {
Remove(s);
}
}
}
}
catch {
throw;
}
finally {
OnRefreshed();
}
}
#endregion
public void Add(Subscription subscription) {
if (subscription == null) {
throw new ArgumentNullException("subscription", "Subscription must not be null.");
}
if (Subscriptions.Contains(subscription)) {
Subscriptions.Remove(subscription);
}
Subscriptions.Add(subscription);
}
public void Remove(Subscription subscription) {
if (subscription == null) {
throw new ArgumentNullException("subscription", "Subscription must not be null.");
}
if (Subscriptions.Contains(subscription)) {
Subscriptions.Remove(subscription);
}
}
public void Remove(HostedService hostedService) {
if (hostedService == null) {
throw new ArgumentNullException("hostedService", "HostedService must not be null.");
}
if (HostedServices.Contains(hostedService)) {
HostedServices.Remove(hostedService);
}
}
public void Add(HostedService hostedService) {
if (hostedService == null) {
throw new ArgumentNullException("subscription", "Subscription must not be null.");
}
if (HostedServices.Contains(hostedService)) {
//HostedServices.Remove(hostedService);
HostedService hs = HostedServices[HostedServices.IndexOf(hostedService)];
hs.Merge(hostedService);
} else {
HostedServices.Add(hostedService);
}
}
public void ChangeIntances(Deployment deployment, HostedService hostedService) {
if (deployment == null) {
throw new ArgumentNullException("Deployment", "Deployment must not be null.");
}
if (hostedService == null) {
throw new ArgumentNullException("HostedService", "HostedService must not be null.");
}
if (deployment.Modified) {
AzureProvider.ChangeInstanceCount(deployment.Subscription,
hostedService.ServiceName,
deployment.DeploymentSlot,
HeuristicLab.Clients.Hive.CloudManager.Azure.Constants.DeploymentRoleName,
deployment.NewInstanceCount);
deployment.Modified = false;
}
}
public void Delete(Deployment deployment, HostedService hostedService) {
if (deployment == null) {
throw new ArgumentNullException("Deployment", "Deployment must not be null.");
}
if (hostedService == null) {
throw new ArgumentNullException("HostedService", "HostedService must not be null.");
}
//AzureProvider.DeleteDeployment();
}
public void Delete(HostedService hostedService) {
if (hostedService == null) {
throw new ArgumentNullException("HostedService", "HostedService must not be null.");
}
AzureProvider.DeleteHostedService(hostedService.Subscription, hostedService.ServiceName);
}
public void Delete(Subscription subscription) {
if (subscription == null) {
throw new ArgumentNullException("subscription", "Subscription must not be null.");
}
if (Subscriptions.Contains(subscription)) {
Subscriptions.Remove(subscription);
}
}
public void PersistSubscriptionsToUserConfig() {
Settings.Default.Upgrade();
StringCollection strCol = Settings.Default.AzureSubscriptions;
strCol.Clear();
foreach (Subscription sub in Subscriptions) {
if (sub.SaveToConfig) {
string setting = sub.GetSettingString();
setting = CryptoService.EncryptString(CryptoService.ToSecureString(setting));
strCol.Add(setting);
}
}
Settings.Default.Save();
}
public List RestoreSubscriptionsFromUserConfig() {
List result = new List();
StringCollection strCol = Settings.Default.AzureSubscriptions;
foreach (string azureSub in strCol) {
string setting = CryptoService.ToInsecureString(CryptoService.DecryptString(azureSub));
Subscription tmp = Subscription.ParseSettingString(setting);
Subscription s = CloudManagerClient.Instance.AzureProvider.GetSubscriptionInfo(tmp.SubscriptionID, tmp.CertificateThumbprint);
s.SaveToConfig = true;
s.DiscoverServices = true;
result.Add(s);
}
return result;
}
}
}