///* ============================================================================ //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.Text; //using System.Management.Automation; //using OpenStack.Objects.DataAccess; //using System.IO; //using OpenStack.Common.Properties; //using System.Xml; //using System.Xml.Serialization; //using OpenStack.Client.Powershell.Providers.Storage; //using OpenStack.Objects.Domain; //using OpenStack.Objects.Utility; //using OpenStack.Client.Powershell.Providers.Common; //using System.Linq; //using System.Collections.ObjectModel; //using System.Management.Automation.Host; //namespace OpenStack.Client.Powershell.Cmdlets.Common //{ // public class BasePSCmdlet : PSCmdlet // { // private OpenStack.Objects.DataAccess.ResponseFormat _responseFormat = ResponseFormat.none; // private BaseRepositoryFactory _repositoryFactory; // #region Properties ////========================================================================================= ///// <summary> ///// ///// </summary> ////========================================================================================= // protected BaseUIContainer CurrentContainer // { // get // { // CommonDriveInfo tempDrive = this.Drive as CommonDriveInfo; // if (tempDrive != null) // { // return tempDrive.CurrentContainer as BaseUIContainer; // } // else return null; // } // } ////========================================================================================= ///// <summary> ///// Exposes the currently mapped Drive. Belongs in base class??? ///// </summary> ////========================================================================================= // protected PSDriveInfo Drive // { // get // { // return this.SessionState.Drive.Current; // } // } ////========================================================================================= ///// <summary> ///// ///// </summary> ///// <param name="message"></param> ////========================================================================================= // protected void WriteHeaderSection(string headerText) // { // WriteObject(" "); // Console.ForegroundColor = ConsoleColor.DarkGray; // WriteObject("=============================================================================================="); // Console.ForegroundColor = ConsoleColor.Yellow; // WriteObject(headerText); // Console.ForegroundColor = ConsoleColor.DarkGray; // WriteObject("=============================================================================================="); // Console.ForegroundColor = ConsoleColor.Green; // } ////================================================================================================== ///// <summary> ///// ///// </summary> ////================================================================================================== // protected Context Context // { // get // { // return (Context)this.SessionState.PSVariable.GetValue("Context", null); // } // set // { // this.SessionState.PSVariable.Set(new PSVariable("Context", value)); // } // } ////========================================================================================= ///// <summary> ///// ///// </summary> ////========================================================================================= // protected BaseRepositoryFactory RepositoryFactory // { // get // { // if (_repositoryFactory == null) // { // try // { // _repositoryFactory = (BaseRepositoryFactory)this.SessionState.PSVariable.Get("BaseRepositoryFactory").Value; // } // catch (NullReferenceException ex) // { // throw new PSSecurityException("The Authentication process has failed for this session. Please ensure that proper credentials have been supplied before using any of these cmdlets."); // } // return _repositoryFactory; // } // else // { // return _repositoryFactory; // } // } // } ////========================================================================================= ///// <summary> ///// ///// </summary> ///// <returns></returns> ////========================================================================================= // protected string ConfigFilePath // { // get // { // try // { // return (string)this.SessionState.PSVariable.Get("ConfigPath").Value; // } // catch (Exception) // { // return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\" + @"OS\OpenStack.config"; // } // } // } ////========================================================================================= ///// <summary> ///// ///// </summary> ///// <returns></returns> ////========================================================================================= // protected Settings Settings // { // set // { // this.Context.Settings = value; // } // get // { // return this.Context.Settings; // } // } // #endregion // #region Methods ////================================================================================================== ///// <summary> ///// ///// </summary> ///// <param name="path"></param> ///// <returns></returns> ////================================================================================================== // protected string TranslateQuickPickNumber(string path) // { // CommonDriveInfo drive = this.Drive as CommonDriveInfo; // if (drive != null) // { // BaseUIContainer result = null; // int number = 0; // if (Int32.TryParse(Path.GetFileName(path), out number)) // { // if (path == "\\" + this.Drive.CurrentLocation) // { // return path.Replace(Path.GetFileName(path), drive.CurrentContainer.Entity.Id); // } // //else if (path.Length < this.Drive.CurrentLocation.Length) // //{ // // result = drive.CurrentContainer.Parent; // //} // else // { // result = drive.CurrentContainer.Containers.Where(q => q.Entity.QuickPickNumber == number).FirstOrDefault<BaseUIContainer>(); // } // } // else // { // return path; // } // if (result != null) // return path.Replace(Path.GetFileName(path), result.Id); // else return null; // } // else return null; // } ////================================================================================================== ///// <summary> ///// ///// </summary> ////================================================================================================== // protected override void BeginProcessing() // { // if (this.Drive.Name != "OpenStack" && this.Drive.Provider.Name != "OS-Storage") // { // ErrorRecord err = new ErrorRecord(new InvalidOperationException("You must be attached to an ObjectStorage Container or the OpenStack drive to execute an OpenStack Cloud cmdlet."), "0" , ErrorCategory.InvalidOperation, this); // this.ThrowTerminatingError(err); // } // bool isAuthorized = false; // Type type = this.GetType(); // object[] metadata = type.GetCustomAttributes(false); // bool foundattribute = false; // foreach (object attribute in metadata) // { // RequiredServiceIdentifierAttribute identifier = attribute as RequiredServiceIdentifierAttribute; // if (identifier != null) // { // if (this.Context.ServiceCatalog.GetService(identifier.Services) != null) // isAuthorized = true; // } // } // if (isAuthorized == false && foundattribute == false) return; // if (!isAuthorized) // this.ThrowTerminatingError(new ErrorRecord(new InvalidOperationException("You're not current authorized to use this service. Please go to https://www.OpenStack.com/ for more information on signing up for this service."), "aa", ErrorCategory.InvalidOperation, this)); // } ////================================================================================================== ///// <summary> ///// Writes out the files represented as StorageObjects for the supplied path. ///// </summary> ////================================================================================================== // protected void WriteXML<T>(T graph, string path) // { // XmlTextWriter xtw = null; // MemoryStream stream = new MemoryStream(); // StringBuilder builder = new StringBuilder(); // XmlDocument document = new XmlDocument(); // StringWriter writer = null; // XmlSerializer serializer = new XmlSerializer(typeof(T)); // try // { // serializer.Serialize(stream, graph); // stream.Position = 0; // document.Load(stream); // writer = new StringWriter(builder); // xtw = new XmlTextWriter(writer); // xtw.Formatting = Formatting.Indented; // document.WriteTo(xtw); // } // finally // { // xtw.Close(); // } // WriteObject(builder.ToString()); // WriteObject(""); // } // #endregion // #region Methods ////========================================================================================= ///// <summary> ///// ///// </summary> ///// <param name="entity"></param> ///// <returns></returns> ////========================================================================================= // protected bool UserConfirmsDeleteAction(string entity) // { // Collection<ChoiceDescription> choices = new Collection<ChoiceDescription>(); // choices.Add(new ChoiceDescription("Y", "Yes")); // choices.Add(new ChoiceDescription("N", "No")); // if (this.Host.UI.PromptForChoice("Confirm Action", "You are about to delete all " + entity + " in the current container. Are you sure about this?", choices, 0) == 0) // { // return true; // } // else // { // return false; // } // } ////========================================================================================= ///// <summary> ///// ///// </summary> ////========================================================================================= // protected void UpdateCache(Context context) // { // CommonDriveInfo tempDrive = this.Drive as CommonDriveInfo; // if (tempDrive != null) // { // BaseUIContainer container = tempDrive.CurrentContainer as BaseUIContainer; // container.Context = context; // if (container != null) // { // try // { // container.Load(); // } // catch (InvalidOperationException) { } // if (container.Parent != null) // container.Parent.Load(); // } // } // } ////========================================================================================= ///// <summary> ///// ///// </summary> ////========================================================================================= // protected void UpdateCache() // { // CommonDriveInfo tempDrive = this.Drive as CommonDriveInfo; // if (tempDrive != null) // { // BaseUIContainer container = tempDrive.CurrentContainer as BaseUIContainer; // if (container != null) // { // try // { // container.Load(); // } // catch (InvalidOperationException) { } // if (container.Parent != null) // container.Parent.Load(); // } // } // } ////========================================================================================= ///// <summary> ///// Updates the cache if the current UIContainer manages the supplied type. ///// </summary> ////========================================================================================= // protected void UpdateCache<T>() where T : BaseUIContainer // { // T container = ((CommonDriveInfo)this.Drive).CurrentContainer as T; // if (container != null) // { // container.Load(); // } // else // { // T parentContainer = ((CommonDriveInfo)this.Drive).CurrentContainer.Parent as T; // if (parentContainer != null) // { // parentContainer.Load(); // } // } // } ////========================================================================================= ///// <summary> ///// ///// </summary> ///// <param name="path"></param> ////========================================================================================= // protected StoragePath CreateStoragePath(string path) // { // return ((OSDriveInfo)this.Drive).CreateStoragePath(path); // } // #endregion // } //}