#region Copyright /* * Copyright © 2014-2016 NetApp, Inc. All Rights Reserved. * * CONFIDENTIALITY NOTICE: THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION OF * NETAPP, INC. USE, DISCLOSURE OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR * EXPRESS WRITTEN PERMISSION OF NETAPP, INC. */ #endregion #region Using Directives using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Management.Automation; using SolidFire.Core.Helpers; using SolidFire.Core.Objects; using SolidFire.Core.Validation; using SolidFire.Core; using SolidFire.Element.Api; using ElementAccount = SolidFire.Element.Api.Account; #endregion namespace SolidFire.Account.Get { /// /// ListAccounts returns the entire list of accounts, with optional paging support. /// [Cmdlet(VerbsCommon.Get, "SFAccount", DefaultParameterSetName = "AccountId")] public class GetSFAccount : SFCmdlet { #region Private Data /// /// /// private Int64[] accountId; private string[] username; #endregion #region Parameters [Parameter(Position = 0, ParameterSetName = "AccountId", Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false, HelpMessage = "Please enter an account ID or list of account IDs")] [ValidateNotNullOrEmpty] [ValidatePattern(SolidFireValidations.Numeric)] public Int64[] AccountID { get { return accountId; } set { accountId = value; } } [Parameter(Position = 0, ParameterSetName = "Username", Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false, HelpMessage = "Please enter a username or list of usernames")] [ValidateNotNullOrEmpty] [ValidatePattern(SolidFireValidations.UpperAndLowerCaseNumericAndDots)] public String[] Username { get { return username; } set { username = value; } } #endregion #region Cmdlet Overrides protected override void BeginProcessing() { base.BeginProcessing(); CheckConnection(); } /// /// /// protected override void ProcessRecord() { base.ProcessRecord(); var request = new ListAccountsRequest(); var objsFromAPI = SendRequest("ListAccounts", request); foreach (var obj in objsFromAPI) { switch (ParameterSetName) { case "AccountId": ProcessByAccountId(obj); break; case "Username": ProcessByUserName(obj); break; default: throw new ArgumentException("Invalid parameter set."); } } } private void ProcessByUserName(SFWrapper objFromApi) { const WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.Compiled; var matchedAccounts = new List(); foreach (var name in username) { var wildcard = new WildcardPattern(name, options); var foundAccounts = objFromApi.Result.Accounts.Where(r => r.Status != "removed" && (wildcard.IsMatch(r.Username))).ToList(); if (foundAccounts.Count > 0) { matchedAccounts.AddRange(foundAccounts); } } if (matchedAccounts.Count > 0) { WriteObject(matchedAccounts.Distinct(), true); } } private void ProcessByAccountId(SFWrapper objFromApi) { // If there were no parameter arguments sent in for the Name parameter then we just want to list all volumes. if (accountId == null) { WriteObject(objFromApi.Result.Accounts, true); return; } foreach (var id in accountId) { var foundAccount = objFromApi.Result.Accounts.FirstOrDefault(r => r.Status != "removed" && r.AccountID == id); if (foundAccount != null) { WriteObject(foundAccount); } } } #endregion } }