#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.Validation;
using SolidFire.Core;
using SolidFire.Element.Api;
using ElementVolume = SolidFire.Element.Api.Volume;
using ElementAccount = SolidFire.Element.Api.Account;
#endregion
namespace SolidFire.Volume.Get
{
///
/// ListActiveVolumes is used to return the list of active volumes currently in the system.
/// The list of volumes is returned sorted in VolumeID order and can be returned in multiple parts (pages).
///
[Cmdlet(VerbsCommon.Get, "SFVolume", DefaultParameterSetName = "VolumeId")]
public class GetSFVolume : SFCmdlet
{
#region Private Data
///
///
///
private Int64[] volumeId;
private string[] volumeName;
private Int64[] _accountId;
private ElementAccount[] _account;
#endregion
#region Parameters
[Parameter(Position = 0, ParameterSetName = "VolumeId", Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false, HelpMessage = "Please enter a VolumeID or list of VolumeIDs")]
[ValidateNotNullOrEmpty]
[ValidatePattern(SolidFireValidations.Numeric)]
public Int64[] VolumeID
{
get
{
return volumeId;
}
set
{
volumeId = value;
}
}
[Parameter(Position = 0, ParameterSetName = "VolumeName", Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false, HelpMessage = "Please enter a volume name or list of volume names")]
[ValidateNotNullOrEmpty]
[ValidatePattern(SolidFireValidations.UpperAndLowerCaseAndNumeric)]
public String[] Name
{
get
{
return volumeName;
}
set
{
volumeName = value;
}
}
[Parameter(Position = 0, ParameterSetName = "AccountId", Mandatory = true, 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 = "Account", Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false, HelpMessage = "Please specify account(s)")]
[ValidateNotNullOrEmpty]
public ElementAccount[] Account
{
get
{
return _account;
}
set
{
_account = value;
}
}
[Parameter(Position = 1, HelpMessage = "Use -ExcludeVVOLs to exclude volumes that are Virtual Volumes from the returned volumes.")]
public SwitchParameter ExcludeVVOLs { get; set; }
///
/// Switch to specify if the resultset should be active volumes only (default) or also include deleted volumes.
///
[Parameter(Mandatory = false, Position = 2, HelpMessage = "Use -IncludeDeleted to specify if the resultset should be active volumes only (default) or also include deleted volumes.")]
public SwitchParameter IncludeDeleted { get; set; }
#endregion
#region Cmdlet Overrides
protected override void BeginProcessing()
{
base.BeginProcessing();
CheckConnection();
}
protected override void ProcessRecord()
{
base.ProcessRecord();
List volumes = new List();
if (IncludeDeleted)
{
var deleted = SendRequest("ListDeletedVolumes", new ListDeletedVolumesRequest());
volumes.AddRange(deleted.SelectMany(r => r.Result.Volumes).ToList()); // aka flatmap
}
var activeRequest = new ListActiveVolumesRequest();
var active = SendRequest("ListActiveVolumes", activeRequest);
volumes.AddRange(active.SelectMany(r => r.Result.Volumes).ToList()); // aka flatmap
if (ExcludeVVOLs)
{
volumes.RemoveAll(v => v.VirtualVolumeID.HasValue);
}
var returnVols = new List();
switch (ParameterSetName)
{
case "VolumeId":
returnVols = FilterByVolumeId(volumes);
break;
case "VolumeName":
returnVols = FilterByName(volumes);
break;
case "Account":
returnVols = FilterByAccountId(_account.Select(a => a.AccountID).ToArray(), volumes);
break;
case "AccountId":
returnVols = FilterByAccountId(_accountId, volumes);
break;
default:
throw new ArgumentException("Invalid parameter set.");
}
WriteObject(returnVols, true);
}
private List FilterByName(List volumes)
{
const WildcardOptions options = WildcardOptions.IgnoreCase |
WildcardOptions.Compiled;
var returnObjects = new List();
foreach (var name in volumeName)
{
var wildcard = new WildcardPattern(name, options);
returnObjects.AddRange(volumes.Where(r => (wildcard.IsMatch(r.Name))).ToList());
}
return returnObjects.Distinct().ToList();
}
private List FilterByVolumeId(List volumes)
{
if (volumeId == null)
{
return volumes;
}
return volumes.Where(v => volumeId.ToList().Contains(v.VolumeID)).ToList();
}
private List FilterByAccountId(Int64[] accountids, List volumes)
{
return volumes.Where(v => accountids.ToList().Contains(v.AccountID)).ToList();
}
#endregion
}
}