1
/* ============================================================================
2
Copyright 2014 Hewlett Packard
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
============================================================================ */
17
using System.Collections.Generic;
18
using System.Collections.ObjectModel;
19
using System.Management.Automation;
20
using OpenStack.Client.Powershell.Providers.Storage;
21
using OpenStack.Identity;
23
namespace OpenStack.Client.Powershell.Utility
25
public class AccountCapabilities
27
private SessionState _session;
28
private Context _context;
29
private IOpenStackClient _coreClient;
30
private Cmdlet _cmdlet;
36
get { return _cmdlet; }
37
set { _cmdlet = value; }
40
public IOpenStackClient CoreClient
42
get { return _coreClient; }
43
set { _coreClient = value; }
46
private Context Context
48
get { return _context; }
49
set { _context = value; }
51
private SessionState SessionState
53
get { return _session; }
54
set { _session = value; }
58
//=======================================================================================================
62
/// <param name="session"></param>
63
/// <param name="context"></param>
64
/// <param name="coreClient"></param>
65
/// <param name="cmd"></param>
66
//=======================================================================================================
67
public AccountCapabilities(SessionState session, Context context, IOpenStackClient coreClient, Cmdlet cmd)
71
_coreClient = coreClient;
74
//=========================================================================================
78
//=========================================================================================
79
private void WriteHeader(string message)
81
// Write out the commands header information first..
84
Console.ForegroundColor = ConsoleColor.DarkGray;
85
WriteObject("===================================================================");
86
Console.ForegroundColor = ConsoleColor.Yellow;
88
Console.ForegroundColor = ConsoleColor.DarkGray;
89
WriteObject("===================================================================");
90
Console.ForegroundColor = ConsoleColor.Green;
93
//=========================================================================================
97
//=========================================================================================
98
public void WriteContainers()
100
var invalidDriveNames = new List<string>();
101
var parameters = new ObjectStorageDriveParameters();
102
var provider = new ObjectStorageNavigationProvider();
103
var converter = new ObjectStorageDriveConverter(this.Context, _session.Drive.Current.Provider, this.CoreClient);
104
var drives = converter.ConvertContainers();
108
this.WriteHeader("Storage Containers available in this AZ include");
110
// Remove the old Users drives first..
112
Collection<PSDriveInfo> deadDrives = this.SessionState.Drive.GetAllForProvider("Object Storage");
113
foreach (PSDriveInfo deadDrive in deadDrives)
115
this.SessionState.Drive.Remove(deadDrive.Name, true, "local");
118
foreach (PSDriveInfo drive in drives)
120
if (drive.Name != string.Empty)
122
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), this.Context.Forecolor);
123
WriteObject("Storage Container : [" + drive.Name + "] now available.");
128
this.SessionState.Drive.New(drive, "local");
130
catch (PSArgumentException ex)
132
if (drive.Name != string.Empty)
133
invalidDriveNames.Add(drive.Name);
139
// No storageContainers exist for the new credentials so make some up...
141
//PSDriveInfo driveInfo = new PSDriveInfo("OS-Init", this.SessionState.Drive.Current.Provider, "/", "Root folder for your storageContainer", null);
142
//this.SessionState.Drive.New(new ObjectStoragePSDriveInfo(driveInfo, parameters, this.Context, this.Context.ServiceCatalog.GetPublicEndpoint("object-store", this.Context.CurrentRegion)), "local");
145
if (invalidDriveNames.Count > 0) {
146
ShowNameConflictError(invalidDriveNames);
149
//=======================================================================================================
153
/// <param name="invalidDriveNames"></param>
154
//=======================================================================================================
155
private void ShowNameConflictError(List<string> invalidDriveNames)
158
Console.ForegroundColor = ConsoleColor.DarkGray;
159
WriteObject("=================================================================");
160
Console.ForegroundColor = ConsoleColor.Red;
161
WriteObject("Error : A subset of your Containers could not be bound to this");
162
WriteObject("session due to naming conflicts with the naming standards required");
163
WriteObject("for Powershell drives. These containers are listed below.");
164
Console.ForegroundColor = ConsoleColor.DarkGray;
165
WriteObject("=================================================================");
166
Console.ForegroundColor = ConsoleColor.Green;
169
foreach (string name in invalidDriveNames)
175
//=======================================================================================================
179
/// <param name="text"></param>
180
//=======================================================================================================
181
private void WriteObject(string text)
183
this.Cmdlet.WriteObject(text);
185
//=======================================================================================================
189
//=======================================================================================================
190
public void WriteServices()
193
Console.ForegroundColor = ConsoleColor.DarkGray;
194
WriteObject("=================================================================");
195
Console.ForegroundColor = ConsoleColor.Yellow;
196
WriteObject("Binding to new Account. New service catalog is as follows.");
197
Console.ForegroundColor = ConsoleColor.DarkGray;
198
WriteObject("=================================================================");
199
Console.ForegroundColor = ConsoleColor.Green;
202
foreach (OpenStackServiceDefinition service in this.Context.ServiceCatalog.GetServicesInAvailabilityZone(this.Context.CurrentRegion))
204
this.Cmdlet.WriteObject(service);
b'\\ No newline at end of file'