~travis-plummer/openstack-cli-powershell/master

« back to all changes in this revision

Viewing changes to Openstack.Client.Powershell/Providers/ObjectStorage/ObjectStorageDriveConverter.cs

  • Committer: Monty Taylor
  • Date: 2015-10-17 20:03:56 UTC
  • Revision ID: git-v1:803406e7e09f6f4199f7a92f11f2904ad1d6cc15
Retire stackforge/openstack-cli-powershell

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using System;
2
 
using System.Collections.Generic;
3
 
using System.Collections.ObjectModel;
4
 
using System.Linq;
5
 
using System.Management.Automation;
6
 
using System.Text;
7
 
using System.Threading.Tasks;
8
 
using OpenStack.Client.Powershell.Utility;
9
 
using OpenStack;
10
 
using OpenStack.Client.Powershell.Providers.Storage;
11
 
using OpenStack.Client.Powershell.Utility;
12
 
using OpenStack.Storage;
13
 
 
14
 
namespace OpenStack.Client.Powershell.Providers.Storage
15
 
{
16
 
    /// <summary>
17
 
    /// This class is responsible for Converting the Users current set of Storage Containers into PSDrives..
18
 
    /// </summary>
19
 
    public class ObjectStorageDriveConverter
20
 
    {
21
 
        private Context _context;
22
 
        private ProviderInfo _providerInfo;
23
 
        private IOpenStackClient _client;
24
 
        private SessionState _sessionState;
25
 
        
26
 
        #region Properties
27
 
        private SessionState SessionState
28
 
        {
29
 
            get { return _sessionState; }
30
 
            set { _sessionState = value; }
31
 
        }
32
 
        private IOpenStackClient CoreClient
33
 
        {
34
 
          get { return _client; }
35
 
          set { _client = value; }
36
 
        }
37
 
 
38
 
        private Context Context
39
 
        {
40
 
            get { return _context; }
41
 
        }
42
 
 
43
 
        private ProviderInfo ProviderInfo
44
 
        {
45
 
          get { return _providerInfo; }
46
 
          set { _providerInfo = value; }
47
 
        }
48
 
        #endregion
49
 
        #region Ctors
50
 
//==================================================================================================
51
 
/// <summary>
52
 
/// 
53
 
/// </summary>
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)
59
 
        {
60
 
            _context      = context;
61
 
            _providerInfo = providerInfo;
62
 
            _client       = client;
63
 
        }
64
 
        #endregion
65
 
//==================================================================================================
66
 
/// <summary>
67
 
/// 
68
 
/// </summary>
69
 
/// <typeparam name="T"></typeparam>
70
 
/// <param name="service"></param>
71
 
/// <returns></returns>
72
 
//==================================================================================================
73
 
        protected T CreateServiceClient<T>(CoreServices service) where T : IOpenStackServiceClient
74
 
        {
75
 
            ServiceProvider provider = this.Context.CurrentServiceProvider;
76
 
            return this.CoreClient.CreateServiceClientByName<T>(provider.ServiceMaps.TranslateServiceName(service));
77
 
        }
78
 
//==================================================================================================
79
 
/// <summary>
80
 
/// 
81
 
/// </summary>
82
 
/// <returns></returns>
83
 
//==================================================================================================
84
 
        public System.Collections.ObjectModel.Collection<PSDriveInfo> ConvertContainers()
85
 
        { 
86
 
            IEnumerable<StorageContainer> storageContainers = null;
87
 
            var parameters = new ObjectStorageDriveParameters();
88
 
 
89
 
            if (this.Context != null && this.Context.Settings != null) {
90
 
                parameters.Settings = this.Context.Settings;
91
 
            }           
92
 
            else {
93
 
                throw new NullReferenceException("Context and/or Settings");
94
 
            }
95
 
 
96
 
            try
97
 
            {
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;
102
 
            }
103
 
            catch (Exception ex)
104
 
            {
105
 
                Console.WriteLine(ex);
106
 
            }
107
 
 
108
 
            Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>();
109
 
 
110
 
            // For every storageContainer that the User has access to, create a Drive that he can mount within Powershell..
111
 
 
112
 
            try
113
 
            {
114
 
                string serviceName    = this.Context.CurrentServiceProvider.ServiceMaps.TranslateServiceName(CoreServices.ObjectStorage);
115
 
                string publicStoreUrl = this.Context.ServiceCatalog.GetPublicEndpoint(serviceName, this.Context.CurrentRegion).ToString();
116
 
 
117
 
                if (storageContainers.Count() > 0)
118
 
                {
119
 
                    foreach (StorageContainer storageContainer in storageContainers)
120
 
                    {
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);
123
 
                        try
124
 
                        {
125
 
                            drives.Add(kvsDriveInfo);
126
 
                        }
127
 
                        catch (Exception) { }
128
 
                    }
129
 
                }
130
 
                else
131
 
                {
132
 
                    PSDriveInfo driveInfo = new PSDriveInfo("OS-Init", this.SessionState.Drive.Current.Provider, "/", "Root folder for your storageContainer", null);
133
 
                    return new Collection<PSDriveInfo>   
134
 
                        {   
135
 
                        new ObjectStoragePSDriveInfo(driveInfo, parameters, this.Context, publicStoreUrl)   
136
 
                        };
137
 
                }
138
 
            }
139
 
            catch (Exception ex) {}
140
 
 
141
 
            return drives;
142
 
        }
143
 
//=======================================================================================================
144
 
/// <summary>
145
 
/// Removes all currently registered drives..
146
 
/// </summary>
147
 
//=======================================================================================================
148
 
        private void RemoveDrives()
149
 
        {
150
 
            // Remove the old Users drives first..
151
 
 
152
 
            Collection<PSDriveInfo> deadDrives = this.SessionState.Drive.GetAllForProvider("Object Storage");
153
 
            foreach (PSDriveInfo deadDrive in deadDrives)
154
 
            {
155
 
                this.SessionState.Drive.Remove(deadDrive.Name, true, "local");
156
 
            }
157
 
        }
158
 
    }
159
 
}