93 lines
2.8 KiB
C#
93 lines
2.8 KiB
C#
namespace PS.IPAM.Cmdlets;
|
|
|
|
using System;
|
|
using System.Management.Automation;
|
|
using PS.IPAM.Helpers;
|
|
|
|
/// <summary>
|
|
/// Creates a new phpIPAM API session.
|
|
/// </summary>
|
|
[Cmdlet(VerbsCommon.New, "Session", DefaultParameterSetName = "Credentials")]
|
|
[OutputType(typeof(Session))]
|
|
public class NewSessionCmdlet : BaseCmdlet
|
|
{
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 0,
|
|
HelpMessage = "The phpIPAM server URL (must start with http:// or https://).")]
|
|
[ValidateNotNullOrEmpty]
|
|
[ValidatePattern("^https?://")]
|
|
public string URL { get; set; } = string.Empty;
|
|
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 1,
|
|
HelpMessage = "The API application ID configured in phpIPAM.")]
|
|
[ValidateNotNullOrEmpty]
|
|
public string AppID { get; set; } = string.Empty;
|
|
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 2,
|
|
ParameterSetName = "Credentials",
|
|
HelpMessage = "The credentials (username and password) for authentication.")]
|
|
[ValidateNotNullOrEmpty]
|
|
public PSCredential? Credentials { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 2,
|
|
ParameterSetName = "Token",
|
|
HelpMessage = "The static API token for authentication.")]
|
|
[ValidateNotNullOrEmpty]
|
|
public string? Token { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 3,
|
|
HelpMessage = "If specified, SSL certificate errors will be ignored.")]
|
|
public SwitchParameter IgnoreSSL { get; set; }
|
|
|
|
protected override void ProcessRecord()
|
|
{
|
|
try
|
|
{
|
|
Session session;
|
|
|
|
if (ParameterSetName == "Credentials" && Credentials != null)
|
|
{
|
|
session = SessionManager.CreateSessionWithCredentialsAsync(
|
|
URL,
|
|
AppID,
|
|
Credentials,
|
|
IgnoreSSL.IsPresent
|
|
).GetAwaiter().GetResult();
|
|
}
|
|
else if (ParameterSetName == "Token" && Token != null)
|
|
{
|
|
session = SessionManager.CreateSessionWithToken(URL, AppID, Token);
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException("Invalid parameter set. Provide either Credentials or Token.");
|
|
}
|
|
|
|
WriteObject(session);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteError(new ErrorRecord(ex, "NewSessionError", ErrorCategory.AuthenticationError, null));
|
|
}
|
|
}
|
|
}
|