#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 ElementVolume = SolidFire.Element.Api.Volume;
#endregion
namespace SolidFire.Volume.Get
{
///
/// ListSnapshots is used to return the attributes of each snapshot taken on the volume.
///
[Cmdlet(VerbsCommon.Get, "SFGroupSnapshot", DefaultParameterSetName = "GroupSnapshotId")]
public class GetSFGroupSnapshot : SFCmdlet
{
#region Private Data
private ElementVolume[] volume;
private Int64[] volumeId;
private Int64[] groupSnapshotId;
private string[] groupSnapshotName;
private List matchedGroupSnapshots;
#endregion
#region Parameters
[Parameter(Position = 0, ParameterSetName = "GroupSnapshotId", Mandatory = false, ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false,
HelpMessage = "Please enter a group snapshot ID or list of group snapshot IDs")]
[ValidateNotNullOrEmpty]
[ValidatePattern(SolidFireValidations.Numeric)]
public Int64[] GroupSnapshotID
{
get { return groupSnapshotId; }
set { groupSnapshotId = value; }
}
[Parameter(Position = 0, ParameterSetName = "GroupSnapshotName", Mandatory = false, ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false,
HelpMessage = "Please enter a snapshot name or list of snapshot names")]
[ValidateNotNullOrEmpty]
[ValidatePattern(SolidFireValidations.UpperAndLowerCaseAndNumeric)]
public String[] GroupSnapshotName
{
get { return groupSnapshotName; }
set { groupSnapshotName = value; }
}
[Parameter(Position = 0, ParameterSetName = "VolumeId", Mandatory = false, ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false,
HelpMessage = "An array of unique volume IDs to query.")]
[ValidateNotNullOrEmpty]
[ValidatePattern(SolidFireValidations.Numeric)]
public Int64[] VolumeID
{
get { return volumeId; }
set { volumeId = value; }
}
[Parameter(Position = 0, ParameterSetName = "Volume", Mandatory = false, ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false,
HelpMessage = "Please specify volume object(s)")]
[ValidateNotNullOrEmpty]
public ElementVolume[] Volume
{
get { return volume; }
set { volume = value; }
}
#endregion
#region Cmdlet Overrides
protected override void BeginProcessing()
{
base.BeginProcessing();
matchedGroupSnapshots = new List();
CheckConnection(minVersionNumber: 7.0f);
}
///
///
///
///
protected override void ProcessRecord()
{
base.ProcessRecord();
var request = new ListGroupSnapshotsRequest();
var objsFromAPI = SendRequest("ListGroupSnapshots", request);
foreach (var obj in objsFromAPI)
{
switch (ParameterSetName)
{
case "GroupSnapshotId":
ProcessByGroupSnapshotId(obj);
break;
case "GroupSnapshotName":
ProcessByGroupSnapshotName(obj);
break;
case "VolumeId":
ProcessByVolumeId(obj);
break;
case "Volume":
ProcessByVolume(obj);
break;
default:
throw new ArgumentException("Invalid parameter set.");
}
WriteObject(matchedGroupSnapshots.Distinct(), true);
}
}
#endregion
#region Methods
private void ProcessByVolumeId(SFWrapper objFromApi)
{
// If there were no parameter arguments sent in for the VolumeID parameter then we just want to list all Snapshots.
if (volumeId == null)
{
matchedGroupSnapshots.AddRange(objFromApi.Result.GroupSnapshots);
return;
}
foreach (var volId in volumeId)
{
var foundGroups =
objFromApi.Result.GroupSnapshots.Where(
g => g.Members.FirstOrDefault(m => m.VolumeID == volId) != null);
matchedGroupSnapshots.AddRange(foundGroups);
}
}
private void ProcessByGroupSnapshotId(SFWrapper objFromApi)
{
// If there were no parameter arguments sent in for the VolumeID parameter then we just want to list all Snapshots.
if (groupSnapshotId == null)
{
WriteObject(objFromApi.Result.GroupSnapshots, true);
return;
}
foreach (var gsId in groupSnapshotId)
{
var foundGroups = objFromApi.Result.GroupSnapshots.Where(g => g.GroupSnapshotID == gsId);
matchedGroupSnapshots.AddRange(foundGroups);
}
}
private void ProcessByGroupSnapshotName(SFWrapper objFromApi)
{
const WildcardOptions options = WildcardOptions.IgnoreCase |
WildcardOptions.Compiled;
foreach (var name in groupSnapshotName)
{
var wildcard = new WildcardPattern(name, options);
var foundGroups =
objFromApi.Result.GroupSnapshots.Where(r => (wildcard.IsMatch(r.Name))).ToList();
matchedGroupSnapshots.AddRange(foundGroups);
}
}
private void ProcessByVolume(SFWrapper objFromApi)
{
// If there were no parameter arguments sent in for the VolumeID parameter then we just want to list all Snapshots.
if (volume == null)
{
WriteObject(objFromApi.Result.GroupSnapshots, true);
return;
}
foreach (var vol in this.volume)
{
var foundGroups =
objFromApi.Result.GroupSnapshots.Where(
g => g.Members.FirstOrDefault(m => m.VolumeID == vol.VolumeID) != null);
matchedGroupSnapshots.AddRange(foundGroups);
}
}
#endregion
}
}