#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.Account.New
{
///
/// AddAccount is used to add a new account to the system. New volumes can be created under the new account. The CHAP settings
/// specified for the account applies to all volumes owned by the account.
///
[Cmdlet(VerbsCommon.New, "SFAccount", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
public class NewSFAccount : SFCmdlet
{
#region Private Data
private string username;
private string initiatorSecret;
private string targetSecret;
private List processedIDs = new List();
private Pipeline pipeline;
#endregion
#region Parameters
///
///
///
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Unique username for this account")]
[ValidatePattern(SolidFireValidations.UpperAndLowerCaseAndNumeric)]
[ValidateLength(1, 64)]
public String Username
{
get { return username; }
set { username = value; }
}
///
///
///
[Parameter(Position = 1, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "CHAP secret to use for the initiator")]
[ValidatePattern(SolidFireValidations.UpperAndLowerCaseAndNumeric)]
[ValidateLength(12, 16)]
public string InitiatorSecret
{
get { return initiatorSecret; }
set { initiatorSecret = value; }
}
///
///
///
[Parameter(Position = 2, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "CHAP secret to use for the target")]
[ValidatePattern(SolidFireValidations.UpperAndLowerCaseAndNumeric)]
[ValidateLength(12, 16)]
public string TargetSecret
{
get { return targetSecret; }
set { targetSecret = value; }
}
///
///
///
[Parameter(Position = 3, 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()
{
base.EndProcessing();
var command = new Command("Get-SFAccount");
command.Parameters.Add("AccountID", 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;
}
///
///
///
protected override void ProcessRecord()
{
base.ProcessRecord();
// Assemble the parameter object
var request = new AddAccountRequest();
if (Attributes != null)
{
request.Attributes = Attributes;
}
if (!string.IsNullOrEmpty(initiatorSecret))
{
request.InitiatorSecret = new CHAPSecret(initiatorSecret);
}
if (!string.IsNullOrEmpty(targetSecret))
{
request.TargetSecret = new CHAPSecret(targetSecret);
}
request.Username = username;
// confirm is true, so process
var objsFromAPI = SendRequest("AddAccount", request);
foreach (var obj in objsFromAPI)
{
processedIDs.Add(obj.Result.AccountID);
}
}
#endregion
}
}