2
using System.Collections.Generic;
3
using System.Collections.ObjectModel;
5
using System.Management.Automation;
7
using System.Threading.Tasks;
8
using OpenStack.Client.Powershell.Utility;
10
using OpenStack.Client.Powershell.Providers.Storage;
11
using OpenStack.Client.Powershell.Utility;
12
using OpenStack.Storage;
14
namespace OpenStack.Client.Powershell.Providers.Storage
17
/// This class is responsible for Converting the Users current set of Storage Containers into PSDrives..
19
public class ObjectStorageDriveConverter
21
private Context _context;
22
private ProviderInfo _providerInfo;
23
private IOpenStackClient _client;
24
private SessionState _sessionState;
27
private SessionState SessionState
29
get { return _sessionState; }
30
set { _sessionState = value; }
32
private IOpenStackClient CoreClient
34
get { return _client; }
35
set { _client = value; }
38
private Context Context
40
get { return _context; }
43
private ProviderInfo ProviderInfo
45
get { return _providerInfo; }
46
set { _providerInfo = value; }
50
//==================================================================================================
54
/// <param name="context"></param>
55
/// <param name="providerInfo"></param>
56
/// <param name="client"></param>
57
//==================================================================================================
58
public ObjectStorageDriveConverter(Context context, ProviderInfo providerInfo, IOpenStackClient client)
61
_providerInfo = providerInfo;
65
//==================================================================================================
69
/// <typeparam name="T"></typeparam>
70
/// <param name="service"></param>
71
/// <returns></returns>
72
//==================================================================================================
73
protected T CreateServiceClient<T>(CoreServices service) where T : IOpenStackServiceClient
75
ServiceProvider provider = this.Context.CurrentServiceProvider;
76
return this.CoreClient.CreateServiceClientByName<T>(provider.ServiceMaps.TranslateServiceName(service));
78
//==================================================================================================
82
/// <returns></returns>
83
//==================================================================================================
84
public System.Collections.ObjectModel.Collection<PSDriveInfo> ConvertContainers()
86
IEnumerable<StorageContainer> storageContainers = null;
87
var parameters = new ObjectStorageDriveParameters();
89
if (this.Context != null && this.Context.Settings != null) {
90
parameters.Settings = this.Context.Settings;
93
throw new NullReferenceException("Context and/or Settings");
98
this.CoreClient.SetRegion(this.Context.CurrentRegion);
99
Task<StorageAccount> getAccountTask = this.CreateServiceClient<IStorageServiceClient>(CoreServices.ObjectStorage).GetStorageAccount();
100
getAccountTask.Wait();
101
storageContainers = getAccountTask.Result.Containers;
105
Console.WriteLine(ex);
108
Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>();
110
// For every storageContainer that the User has access to, create a Drive that he can mount within Powershell..
114
string serviceName = this.Context.CurrentServiceProvider.ServiceMaps.TranslateServiceName(CoreServices.ObjectStorage);
115
string publicStoreUrl = this.Context.ServiceCatalog.GetPublicEndpoint(serviceName, this.Context.CurrentRegion).ToString();
117
if (storageContainers.Count() > 0)
119
foreach (StorageContainer storageContainer in storageContainers)
121
PSDriveInfo driveInfo = new PSDriveInfo(storageContainer.Name, this.ProviderInfo, "/", "Root folder for your storageContainer", null);
122
ObjectStoragePSDriveInfo kvsDriveInfo = new ObjectStoragePSDriveInfo(driveInfo, parameters, this.Context, publicStoreUrl);
125
drives.Add(kvsDriveInfo);
127
catch (Exception) { }
132
PSDriveInfo driveInfo = new PSDriveInfo("OS-Init", this.SessionState.Drive.Current.Provider, "/", "Root folder for your storageContainer", null);
133
return new Collection<PSDriveInfo>
135
new ObjectStoragePSDriveInfo(driveInfo, parameters, this.Context, publicStoreUrl)
139
catch (Exception ex) {}
143
//=======================================================================================================
145
/// Removes all currently registered drives..
147
//=======================================================================================================
148
private void RemoveDrives()
150
// Remove the old Users drives first..
152
Collection<PSDriveInfo> deadDrives = this.SessionState.Drive.GetAllForProvider("Object Storage");
153
foreach (PSDriveInfo deadDrive in deadDrives)
155
this.SessionState.Drive.Remove(deadDrive.Name, true, "local");