#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;
#endregion
namespace SolidFire.Volume.New
{
///
/// NewSFGroupSnapshot is used to create a point-in-time copy of a group of volumes. The snapshot created can then be used later as a
/// backup or rollback to ensure the data on the group of volumes is consistent for the point in time in which the snapshot was
/// created.
///
[Cmdlet(VerbsCommon.New, "SFGroupSnapshot", ConfirmImpact = ConfirmImpact.Medium, DefaultParameterSetName = "VolumeId")]
public class NewSFGroupSnapshot : SFCmdlet
{
#region Private Data
private bool? _enableRemoteReplication;
private string _retention;
///
/// Member to hold the volume ids from the calls to ProcessRecord and
/// used to submit to the API call.
///
private List _volumeIdsToGroupSnapshot = new List();
private Pipeline pipeline;
#endregion
#region Parameters
///
/// Unique IDs of the volumes to include in the group snapshot.
///
[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; }
///
/// Name entered for the group snapshot. If no name is entered, the date and time the group snapshot was taken is used.
///
[Parameter(Position = 1, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = false, HelpMessage = "Name for the snapshot group")]
[ValidatePattern(SolidFireValidations.UpperAndLowerCaseAndNumeric)]
public string Name { get; set; }
[Parameter(Position = 2, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = false, HelpMessage = "Should the snapsnot be replicated to remote storage?")]
public SwitchParameter EnableRemoteReplication
{
get { return _enableRemoteReplication.HasValue ? _enableRemoteReplication.Value : false; }
set { _enableRemoteReplication = value.IsPresent; }
}
[Parameter(Position = 3, 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;
}
}
///
/// Attributes to save with the group snapshot.
///
[Parameter(Position = 4, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = false, HelpMessage = "Attributes to set")]
public Hashtable Attributes { get; set; }
#endregion
#region Cmdlet Overrides
///
/// Implementation for validation the status of the connection before
/// allowing the cmdlet to continue processing.
///
protected override void BeginProcessing()
{
base.BeginProcessing();
CheckConnection(minVersionNumber: 7.0f);
pipeline = Runspace.DefaultRunspace.CreateNestedPipeline();
}
///
/// Implementation for collecting the volume IDs.
/// Note: This method is called multiple times per execution of the
/// CmdLet for pipelined objects.
///
protected override void ProcessRecord()
{
base.ProcessRecord();
foreach (var id in this.VolumeID)
{
// Queue up the volume ID for a group snapshot to be taken in the EndProcessing method.
WriteVerbose(string.Format("VolumeID {0} queued up for new group snapshot", id));
_volumeIdsToGroupSnapshot.Add(id);
}
}
///
/// Implementation for executing the input against the API.
///
protected override void EndProcessing()
{
base.EndProcessing();
// Assemble the required parameters for the API call.
// volumes is the only required attribute.
var request = new CreateGroupSnapshotRequest();
request.Volumes = _volumeIdsToGroupSnapshot.ToArray();
request.Name = Name;
request.EnableRemoteReplication = _enableRemoteReplication.HasValue ? _enableRemoteReplication : null;
request.Retention = _retention;
request.Attributes = Attributes == null ? null : Attributes;
// Call the CreateGroupSnapshot API method with parameters.
var objsFromAPI = SendRequest("CreateGroupSnapshot", request);
foreach (var obj in objsFromAPI)
{
var command = new Command("Get-SFGroupSnapshot");
command.Parameters.Add("GroupSnapshotID", obj.Result.GroupSnapshotID);
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);
pipeline = null;
}
#endregion
}
}