#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.Management.Automation;
using SolidFire.Core.Validation;
using SolidFire.Core;
using SolidFire.Element.Api;
using System.Management.Automation.Runspaces;
using System.Collections;
using System.Linq;
#endregion
namespace SolidFire.Volume.Invoke
{
///
/// RollbackToSnapshotis used to make an existing snapshot the "active" volume image. This method creates a new snapsho
/// from an existing snapshot. The new snapshot becomes "active" and the existing snapshot is preserved until it is manually
/// deleted. The previously "active" snapshot is deleted unless the parameter saveCurrentState is set with a value of "true."
///
[Cmdlet(VerbsLifecycle.Invoke, "SFRollbackToSnapshot", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
public class InvokeSFRollbackToSnapshot : SFCmdlet
{
#region Private Data
private Pipeline pipeline;
private List processedIDs = new List();
#endregion
#region Parameters
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false, HelpMessage = "Please enter a VolumeID for the volume to restore.")]
[ValidatePattern(SolidFireValidations.Numeric)]
public Int64 VolumeID { get; set; }
[Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false, HelpMessage = "Please enter the SnapshotID of a previously created snapshot on the given volume.")]
[ValidatePattern(SolidFireValidations.Numeric)]
public Int64 SnapshotID { get; set; }
[Parameter(Position = 2, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false, HelpMessage = "Name for the snapshot. If no name is given then the name of the snapshot being rolled back to is used with '- copy' appended to the end of the name.")]
public String Name { get; set; }
[Parameter(Position = 3, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = false, HelpMessage = "true: The previous active volume image is kept. false: (default) The previous active volume image is deleted.")]
public bool SaveCurrentState { get; set; }
[Parameter(Position = 4, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = false, 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 ProcessRecord()
{
base.ProcessRecord();
var request = new RollbackToSnapshotRequest();
request.VolumeID = VolumeID;
request.SnapshotID = SnapshotID;
request.Name = Name;
request.SaveCurrentState = SaveCurrentState;
request.Attributes = Attributes;
var objsFromAPI = SendRequest("RollbackToSnapshot", request);
foreach (var obj in objsFromAPI.Where(r => r.Result.SnapshotID.HasValue))
{
processedIDs.Add(obj.Result.SnapshotID.Value);
}
}
protected override void EndProcessing()
{
if (processedIDs.Count > 0)
{
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;
}
#endregion
}
}