#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, "SFSnapshot", DefaultParameterSetName = "SnapshotId")]
public class GetSFSnapshot : SFCmdlet
{
#region Private Data
private ElementVolume[] volume;
private Int64[] volumeId;
private Int64[] snapshotId;
private string[] snapshotName;
#endregion
#region Parameters
[Parameter(Position = 0, ParameterSetName = "SnapshotId", Mandatory = false, ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false,
HelpMessage = "Please enter a snapshot ID or list of snapshot IDs")]
[ValidateNotNullOrEmpty]
[ValidatePattern(SolidFireValidations.Numeric)]
public Int64[] SnapshotID
{
get { return snapshotId; }
set { snapshotId = value; }
}
[Parameter(Position = 0, ParameterSetName = "SnapshotName", 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[] SnapshotName
{
get { return snapshotName; }
set { snapshotName = value; }
}
[Parameter(Position = 0, ParameterSetName = "VolumeId", Mandatory = false, ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false,
HelpMessage = "Please enter a volume ID or list of volume IDs")]
[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();
CheckConnection();
}
protected override void ProcessRecord()
{
base.ProcessRecord();
var request = new ListSnapshotsRequest();
var objsFromAPI = SendRequest("ListSnapshots", request);
foreach (var obj in objsFromAPI)
{
switch (ParameterSetName)
{
case "Volume":
ProcessByVolume(obj);
break;
case "VolumeId":
ProcessByVolumeId(obj);
break;
case "SnapshotName":
ProcessBySnapshotName(obj);
break;
case "SnapshotId":
ProcessBySnapshotId(obj);
break;
default:
throw new ArgumentException("Invalid parameter set.");
}
}
}
#endregion
#region Methods
private void ProcessBySnapshotId(SFWrapper objFromApi)
{
// If there were no parameter arguments sent in for the SnapshotID parameter then we just want to list all Snapshots.
if (snapshotId == null)
{
WriteObject(objFromApi.Result.Snapshots, true);
return;
}
foreach (var snapid in snapshotId)
{
foreach (var snapShot in objFromApi.Result.Snapshots)
{
if (snapShot.SnapshotID != snapid) continue;
WriteObject(snapShot);
}
}
}
private void ProcessByVolume(SFWrapper objFromApi)
{
foreach (var sfVolumeInfo in volume)
{
foreach (var snapShot in objFromApi.Result.Snapshots)
{
if (snapShot.VolumeID != sfVolumeInfo.VolumeID)
continue;
WriteObject(snapShot);
}
}
}
private void ProcessBySnapshotName(SFWrapper objFromApi)
{
const WildcardOptions options = WildcardOptions.IgnoreCase |
WildcardOptions.Compiled;
var snapshotObjects = new List();
foreach (var name in snapshotName)
{
var wildcard = new WildcardPattern(name, options);
var foundSnapshots =
objFromApi.Result.Snapshots.Where(r => (wildcard.IsMatch(r.Name))).ToList();
snapshotObjects.AddRange(foundSnapshots);
}
if (snapshotObjects.Count > 0)
{
WriteObject(snapshotObjects.Distinct(), true);
}
}
private void ProcessByVolumeId(SFWrapper objFromApi)
{
foreach (var volId in volumeId)
{
foreach (var snapShot in objFromApi.Result.Snapshots)
{
if (snapShot.VolumeID != volId) continue;
WriteObject(snapShot);
}
}
}
#endregion
}
}