#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;
#endregion
namespace SolidFire.Volume.Get
{
///
/// Cmdlet implementation to return information about the volume access groups for a cluster.
///
[Cmdlet(VerbsCommon.Get, "SFVolumeAccessGroup", DefaultParameterSetName = "VolumeAccessGroupId")]
public class GetSFVolumeAccessGroup : SFCmdlet
{
#region Private Data
///
/// Member to hold the list of volume access group id's for the cmdlet assigned before processing.
///
private Int64[] volumeAccessGroupId;
///
/// Member to hold the list of volume access group name's for the cmdlet assigned before processing.
/// ///
private string[] name;
#endregion
#region Parameters
///
/// List of id's used to find specific access groups by id.
///
[Parameter(Position = 0, ParameterSetName = "VolumeAccessGroupId", Mandatory = false, ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false,
HelpMessage = "Please enter a volume access group ID or list of volume access group IDs")]
[ValidateNotNullOrEmpty]
[ValidatePattern(SolidFireValidations.Numeric)]
public Int64[] VolumeAccessGroupID
{
get { return volumeAccessGroupId; }
set { volumeAccessGroupId = value; }
}
///
/// List of strings used to find specific access groups by name.
///
[Parameter(Position = 0, ParameterSetName = "VolumeAccessGroupName", 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 name; }
set { name = value; }
}
#endregion
#region Cmdlet Overrides
///
/// Implementation of CmdLet BeginProcessing to check that a connection has been made before allowing
/// the cmdlet processing to continue.
///
protected override void BeginProcessing()
{
base.BeginProcessing();
CheckConnection();
}
///
/// Implementation of CmdLet ProcessRecord to find volume access groups for the different parameter sets.
///
protected override void ProcessRecord()
{
base.ProcessRecord();
var request = new ListVolumeAccessGroupsRequest();
var objsFromAPI = SendRequest("ListVolumeAccessGroups", request);
foreach (var obj in objsFromAPI)
{
switch (ParameterSetName)
{
case "VolumeAccessGroupId":
ProcessByVolumeAccessGroupId(obj);
break;
case "VolumeAccessGroupName":
ProcessByName(obj);
break;
default:
throw new ArgumentException("Invalid parameter set.");
}
}
}
///
/// Finds all or specific volume access groups by volume access group name, including wildcard support.
///
/// List of all volume access groups
private void ProcessByName(SFWrapper objFromApi)
{
const WildcardOptions options = WildcardOptions.IgnoreCase |
WildcardOptions.Compiled;
var vagInfoObjects = new List();
foreach (var grpName in name)
{
var wildcard = new WildcardPattern(grpName, options);
var foundNodes =
objFromApi.Result.VolumeAccessGroups.Where(r => (wildcard.IsMatch(r.Name)))
.ToList();
vagInfoObjects.AddRange(foundNodes);
}
if (vagInfoObjects.Count > 0)
{
WriteObject(vagInfoObjects.Distinct(), true);
}
}
///
/// Finds all or specific volume access groups by volume access group id.
///
/// List of all volume access groups
private void ProcessByVolumeAccessGroupId(SFWrapper objFromApi)
{
// If there were no parameter arguments sent in for the Name parameter then we just want to list all volumes.
if (volumeAccessGroupId == null)
{
WriteObject(objFromApi.Result.VolumeAccessGroups, true);
return;
}
foreach (var id in volumeAccessGroupId)
{
var sfVolumeAccessGroupInfo =
objFromApi.Result.VolumeAccessGroups.FirstOrDefault(r => r.VolumeAccessGroupID == id);
WriteObject(sfVolumeAccessGroupInfo);
}
}
#endregion
}
}