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

« back to all changes in this revision

Viewing changes to Openstack.Client.Powershell/Utility/AccountCapabilities.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
 
/* ============================================================================
2
 
Copyright 2014 Hewlett Packard
3
 
 
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
7
 
 
8
 
    http://www.apache.org/licenses/LICENSE-2.0
9
 
 
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
 
============================================================================ */
16
 
using System;
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;
22
 
 
23
 
namespace OpenStack.Client.Powershell.Utility
24
 
{
25
 
    public class AccountCapabilities
26
 
    {
27
 
        private SessionState _session;
28
 
        private Context _context;
29
 
        private IOpenStackClient _coreClient;
30
 
        private Cmdlet _cmdlet;
31
 
        
32
 
        #region Properties
33
 
 
34
 
        public Cmdlet Cmdlet
35
 
        {
36
 
            get { return _cmdlet; }
37
 
            set { _cmdlet = value; }
38
 
        }
39
 
 
40
 
        public IOpenStackClient CoreClient
41
 
        {
42
 
            get { return _coreClient; }
43
 
            set { _coreClient = value; }
44
 
        }
45
 
    
46
 
        private Context Context
47
 
        {
48
 
            get { return _context; }
49
 
            set { _context = value; }
50
 
        }
51
 
        private SessionState SessionState
52
 
        {
53
 
            get { return _session; }
54
 
            set { _session = value; }
55
 
        } 
56
 
        #endregion
57
 
        #region Methods
58
 
//=======================================================================================================
59
 
/// <summary>
60
 
/// 
61
 
/// </summary>
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)
68
 
        {
69
 
            _session    = session;
70
 
            _context    = context;
71
 
            _coreClient = coreClient;
72
 
            _cmdlet     = cmd;
73
 
        }
74
 
//=========================================================================================
75
 
/// <summary>
76
 
/// 
77
 
/// </summary>
78
 
//=========================================================================================
79
 
        private void WriteHeader(string message)
80
 
        {
81
 
            // Write out the commands header information first..
82
 
 
83
 
            WriteObject("");
84
 
            Console.ForegroundColor = ConsoleColor.DarkGray;
85
 
            WriteObject("===================================================================");
86
 
            Console.ForegroundColor = ConsoleColor.Yellow;
87
 
            WriteObject(message);
88
 
            Console.ForegroundColor = ConsoleColor.DarkGray;
89
 
            WriteObject("===================================================================");
90
 
            Console.ForegroundColor = ConsoleColor.Green;
91
 
            WriteObject(" ");
92
 
        }
93
 
//=========================================================================================
94
 
/// <summary>
95
 
/// 
96
 
/// </summary>
97
 
//=========================================================================================
98
 
        public void WriteContainers()
99
 
        {
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();
105
 
         
106
 
            if (drives != null)
107
 
            {
108
 
                this.WriteHeader("Storage Containers available in this AZ include");
109
 
 
110
 
                // Remove the old Users drives first..
111
 
 
112
 
                Collection<PSDriveInfo> deadDrives = this.SessionState.Drive.GetAllForProvider("Object Storage");
113
 
                foreach (PSDriveInfo deadDrive in deadDrives)
114
 
                {
115
 
                    this.SessionState.Drive.Remove(deadDrive.Name, true, "local");
116
 
                }
117
 
 
118
 
                foreach (PSDriveInfo drive in drives)
119
 
                {
120
 
                    if (drive.Name != string.Empty)
121
 
                    {
122
 
                        Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), this.Context.Forecolor);
123
 
                        WriteObject("Storage Container : [" + drive.Name + "] now available.");
124
 
                    }
125
 
 
126
 
                    try
127
 
                    {
128
 
                        this.SessionState.Drive.New(drive, "local");
129
 
                    }
130
 
                    catch (PSArgumentException ex)
131
 
                    {
132
 
                        if (drive.Name != string.Empty)
133
 
                            invalidDriveNames.Add(drive.Name);
134
 
                    }
135
 
                }          
136
 
            }
137
 
            else
138
 
            {
139
 
                // No storageContainers exist for the new credentials so make some up...
140
 
 
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");
143
 
            }
144
 
 
145
 
            if (invalidDriveNames.Count > 0) {
146
 
                ShowNameConflictError(invalidDriveNames);
147
 
            }
148
 
        }
149
 
//=======================================================================================================
150
 
/// <summary>
151
 
/// 
152
 
/// </summary>
153
 
/// <param name="invalidDriveNames"></param>
154
 
//=======================================================================================================
155
 
        private void ShowNameConflictError(List<string> invalidDriveNames)
156
 
        {
157
 
            WriteObject("");
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;
167
 
            WriteObject(" ");
168
 
 
169
 
            foreach (string name in invalidDriveNames)
170
 
            {
171
 
                WriteObject(name);
172
 
                WriteObject(" ");
173
 
            }
174
 
        }
175
 
//=======================================================================================================
176
 
/// <summary>
177
 
/// 
178
 
/// </summary>
179
 
/// <param name="text"></param>
180
 
//=======================================================================================================
181
 
        private void WriteObject(string text)
182
 
        {
183
 
            this.Cmdlet.WriteObject(text);
184
 
        }
185
 
//=======================================================================================================
186
 
/// <summary>
187
 
/// 
188
 
/// </summary>
189
 
//=======================================================================================================
190
 
        public void WriteServices()
191
 
        {
192
 
            WriteObject("");
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;
200
 
            WriteObject(" ");
201
 
 
202
 
            foreach (OpenStackServiceDefinition service in this.Context.ServiceCatalog.GetServicesInAvailabilityZone(this.Context.CurrentRegion))
203
 
            {
204
 
                this.Cmdlet.WriteObject(service);
205
 
            }
206
 
            WriteObject("");
207
 
        }
208
 
        #endregion
209
 
    }
210
 
}
211
 
         
 
 
b'\\ No newline at end of file'