#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;
using System.Collections.Generic;
using System.ComponentModel;
using System.Management.Automation;
using SolidFire.Core.Validation;
using System.Management.Automation.Runspaces;
using SolidFire.Core;
using SolidFire.Element.Api;
using ElementVolume = SolidFire.Element.Api.Volume;
#endregion
namespace SolidFire.Volume.New
{
///
/// CreateSnapshot is used to create a point-in-time copy of a volume. A snapshot can be created from any volume or from an existing
/// snapshot. If a SnapshotID is not provided with this API method, a snapshot is created from the volume's active branch.
///
[Cmdlet(VerbsCommon.New, "SFSnapshot", ConfirmImpact = ConfirmImpact.Medium, DefaultParameterSetName = "VolumeId")]
public class NewSFSnapshot : SFCmdlet
{
#region Private Data
private bool? _enableRemoteReplication;
private string _retention;
private List processedIDs = new List();
private Pipeline pipeline;
#endregion
#region Parameters
[Parameter(Position = 0, ParameterSetName = "VolumeId", Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Unique ID of the volume image from which to copy")]
[ValidatePattern(SolidFireValidations.Numeric)]
public Int64 VolumeID { get; set; }
[Parameter(Position = 0, ParameterSetName = "Volume", Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Volume objects from which to copy")]
public ElementVolume Volume { get; set; }
[Parameter(Position = 1, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Unique ID of a snapshot from which the new snapshot is made")]
[ValidatePattern(SolidFireValidations.Numeric)]
public Int64 SnapshotID { get; set; }
[Parameter(Position = 2, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = false, HelpMessage = "Name for the snapshot")]
[ValidatePattern(SolidFireValidations.UpperAndLowerCaseAndNumeric)]
public string Name { get; set; }
[Parameter(Position = 3, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = false, HelpMessage = "Should the snapshot be replicated to remote storage?")]
public SwitchParameter EnableRemoteReplication
{
get { return _enableRemoteReplication.HasValue ? _enableRemoteReplication.Value : false; }
set { _enableRemoteReplication = value.IsPresent; }
}
[Parameter(Position = 4, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The amount of time the snapshot should be retained. Enter in HHHHHHHH:mm:ss. Maximum retention time is 10000000:00:00")]
[SFValidatePattern(SolidFireValidations.HoursMinutesSeconds, "The argument is not in the format of HHHHHHHH:mm:ss.")]
public string Retention
{
get
{
return _retention;
}
set
{
SolidFireValidations.ValidateHoursMinutesSecondsForRetention(value);
_retention = value;
}
}
[Parameter(Position = 5, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Attributes to set")]
public Hashtable Attributes { get; set; }
#endregion
#region Cmdlet Overrides
protected override void BeginProcessing()
{
base.BeginProcessing();
CheckConnection();
pipeline = Runspace.DefaultRunspace.CreateNestedPipeline();
}
protected override void EndProcessing()
{
var command = new Command("Get-SFSnapshot");
command.Parameters.Add("SnapshotID", processedIDs);
if (SFConnection != null)
{
command.Parameters.Add("SFConnection", SFConnection);
}
else if (Target != null)
{
command.Parameters.Add("Target", Target);
}
pipeline.Commands.Add(command);
WriteObject(pipeline.Invoke(), true);
base.EndProcessing();
processedIDs.Clear();
pipeline = null;
}
protected override void ProcessRecord()
{
base.ProcessRecord();
var request = new CreateSnapshotRequest();
request.Attributes = Attributes == null ? null : Attributes;
request.Name = Name;
request.SnapshotID = SnapshotID;
request.EnableRemoteReplication = _enableRemoteReplication.HasValue ? _enableRemoteReplication : null;
request.Retention = _retention;
switch (ParameterSetName)
{
case "VolumeId":
request.VolumeID = VolumeID;
break;
case "Volume":
request.VolumeID = Volume.VolumeID;
break;
};
var objsFromAPI = SendRequest("CreateSnapshot", request);
foreach (var obj in objsFromAPI)
{
processedIDs.Add(obj.Result.SnapshotID);
}
}
#endregion
}
}