/* ============================================================================
Copyright 2014 Hewlett Packard
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============================================================================ */
using System;
using System.Management.Automation;
using System.Management.Automation.Provider;
using System.Xml.Linq;
using System.Xml.XPath;
using OpenStack.Client.Powershell.Utility;
using System.Linq;
using OpenStack.Client.Powershell.Utility;
namespace OpenStack.Client.Powershell.Providers.Common
{
public class BaseNavigationCmdletProvider : NavigationCmdletProvider
{
//=========================================================================================
///
///
///
//=========================================================================================
protected OpenStackClient CoreClient
{
get
{
return (OpenStackClient)this.SessionState.PSVariable.Get("CoreClient").Value;
}
set
{
this.SessionState.PSVariable.Set(new PSVariable("CoreClient", value));
}
}
//=========================================================================================
///
///
///
///
//=========================================================================================
protected Settings Settings
{
get
{
return this.Context.Settings;
}
}
//==================================================================================================
///
///
///
//==================================================================================================
protected Context Context
{
get
{
return (Context)this.SessionState.PSVariable.GetValue("Context", null);
}
}
//==================================================================================================
///
///
///
///
//==================================================================================================
private bool IsContextInitialized()
{
if (this.SessionState.PSVariable.GetValue("Context", null) == null) {
return false;
}
else
{
return true;
}
}
//=========================================================================================
///
///
///
///
//=========================================================================================
protected string ConfigFilePath
{
get
{
try
{
return (string)this.SessionState.PSVariable.Get("ConfigPath").Value;
}
catch (Exception)
{
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\" + @"OS\OpenStack.config";
}
}
}
//==================================================================================================
///
///
///
//==================================================================================================
private void SetZoneColor(ServiceProvider provider)
{
// We don't throw a critical exception if this operation fails..
try
{
AvailabilityZone defZone = provider.AvailabilityZones.Where(z => z.IsDefault == true).DefaultIfEmpty(null).FirstOrDefault();
if (defZone == null) return;
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), defZone.ShellForegroundColor);
this.Host.UI.RawUI.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), defZone.ShellForegroundColor);
this.Context.Forecolor = defZone.ShellForegroundColor;
}
catch (System.Management.Automation.Host.HostException) { }
}
//==================================================================================================
///
///
///
///
///
///
//==================================================================================================
protected T CreateServiceClient(CoreServices service) where T : IOpenStackServiceClient
{
ServiceProvider provider = this.Context.CurrentServiceProvider;
return this.CoreClient.CreateServiceClientByName(provider.ServiceMaps.TranslateServiceName(service));
}
//==================================================================================================
///
///
///
//==================================================================================================
protected void InitializeSession()
{
ConfigurationManager configManager = new ConfigurationManager();
ExtensionManager extensionManager = new ExtensionManager(this.SessionState, this.Context);
configManager.Load();
ServiceProvider provider = configManager.GetDefaultServiceProvider();
if (provider.Name == String.Empty && provider.IsDefault == true)
{
// Technically Core is already loaded (you're in it :) but this signs in for you to the ServiceProvider selected..
// This is just used in the case where it's the Users first time loading the CLI..
extensionManager.LoadCore(provider);
}
else
{
// Load any extensions that were supplied by the ServiceProvider...
extensionManager.LoadExtension(provider);
}
this.SetZoneColor(provider);
}
#region Implementation of DriveCmdletProvider
//==================================================================================================
///
/// Removes an Item from the store..
///
///
//==================================================================================================
protected override void ClearItem(string path)
{
base.ClearItem(path);
}
//==================================================================================================
///
/// Called when the user decides to delete a KVSDrive.
///
///
///
//==================================================================================================
protected override PSDriveInfo RemoveDrive(PSDriveInfo drive)
{
if (drive == null)
{
WriteError(new ErrorRecord(new ArgumentNullException("drive"), "NullDrive", ErrorCategory.InvalidArgument, drive));
return null;
}
return drive;
}
//==================================================================================================
///
///
///
///
///
//==================================================================================================
protected override void GetChildNames(string path, ReturnContainers returnContainers)
{
WriteItemObject(path, path, true);
}
//==================================================================================================
///
///
///
///
///
//==================================================================================================
protected override string GetChildName(string path)
{
return base.GetChildName(path);
}
//==================================================================================================
///
///
///
///
///
//==================================================================================================
protected override bool ItemExists(string path)
{
return true;
}
#endregion
//==================================================================================================
///
/// This test should not verify the existance of the item at the path.
/// It should only perform syntactic and semantic validation of the
/// path. For instance, for the file system provider, that path should
/// be canonicalized, syntactically verified, and ensure that the path
/// does not refer to a device.
///
///
///
//==================================================================================================
protected override bool IsValidPath(string path)
{
return true;
}
}
}