#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.ComponentModel;
using System.Management.Automation;
using SolidFire.Core;
using SolidFire.Element.Api;
using System.Management.Automation.Runspaces;
using System.Collections.Generic;
using System.Linq;
using SolidFire.Exceptions;
using SolidFire.Core.Helpers;
#endregion
namespace SolidFire.Volume.Remove
{
///
/// Used to delete one or more schedules, sorta. Actually it calls ModifySchedule and sets ToBeDeleted to true. The API doesn't support the immediate
/// removal of a schedule but our CmdLet pattern requires us to supply a Remove* CmdLet.
///
[Cmdlet(VerbsCommon.Remove, "SFSchedule", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
public class RemoveSFSchedule : SFCmdlet
{
#region Private Data
private Pipeline _pipeline;
private List _processedIDs;
#endregion
#region Parameters
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Schedule ID(s) to remove")]
public long[] ScheduleID { get; set; }
#endregion
#region Cmdlet Overrides
protected override void BeginProcessing()
{
base.BeginProcessing();
CheckConnection(minVersionNumber: 8.0f);
_processedIDs = new List();
_pipeline = Runspace.DefaultRunspace.CreateNestedPipeline();
}
protected override void ProcessRecord()
{
base.ProcessRecord();
foreach (var id in ScheduleID)
{
var scheduleBeforeModify = getSchedule(id);
scheduleBeforeModify.ToBeDeleted = true;
var request = new ModifyScheduleRequest();
request.Schedule = scheduleBeforeModify;
var objsFromApi = SendRequest("ModifySchedule", request, throwTerminatingError: false);
_processedIDs.Add(id);
}
}
protected override void EndProcessing()
{
base.EndProcessing();
if (_processedIDs.Count > 0)
{
var command = new Command("Get-SFSchedule");
command.Parameters.Add("ScheduleID", _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);
_processedIDs.Clear();
}
_pipeline = null;
}
#endregion
private Schedule getSchedule(long scheduleID)
{
var volPipeline = Runspace.DefaultRunspace.CreateNestedPipeline();
var command = new Command("Get-SFSchedule");
command.Parameters.Add("ScheduleID", scheduleID);
if (SFConnection != null)
{
command.Parameters.Add("SFConnection", SFConnection);
}
else if (Target != null)
{
command.Parameters.Add("Target", Target);
}
volPipeline.Commands.Add(command);
var psObj = volPipeline.Invoke().FirstOrDefault();
volPipeline = null;
if (psObj == null)
{
var message = string.Format("Unable to find Schedule with ID {0}. Cannot continue with Schedule modification.", scheduleID);
var errRecord = new ErrorRecord(new RequestException(message), message, ErrorCategory.InvalidData, this);
SolidFireUtilities.HandleError(errRecord, this, true);
}
return (Schedule)psObj.BaseObject;
}
}
}