Added assembly name and root namespace to project file; updated session and vlan classes with new properties; modified module manifest to reference DLL and updated exported cmdlets; corrected property name in address class.
This commit is contained in:
190
classlib/Cmdlets/NewFirstFreeIPCmdlet.cs
Normal file
190
classlib/Cmdlets/NewFirstFreeIPCmdlet.cs
Normal file
@@ -0,0 +1,190 @@
|
||||
namespace PS.IPAM.Cmdlets;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Automation;
|
||||
using PS.IPAM;
|
||||
using PS.IPAM.Helpers;
|
||||
|
||||
[Cmdlet(VerbsCommon.New, "FirstFreeIP")]
|
||||
[OutputType(typeof(Address))]
|
||||
public class NewFirstFreeIPCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0)]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string SubnetId { get; set; } = string.Empty;
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 2)]
|
||||
public SwitchParameter Gateway { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 3)]
|
||||
public string? Description { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 4)]
|
||||
public string? Hostname { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 5)]
|
||||
public string? MAC { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 6)]
|
||||
public string? Owner { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 7)]
|
||||
public string? TagId { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 8)]
|
||||
public SwitchParameter PTRIgnore { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 9)]
|
||||
public string? PTRId { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 10)]
|
||||
public string? Note { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 11)]
|
||||
public SwitchParameter ExcludePing { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 12)]
|
||||
public string? DeviceId { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 13)]
|
||||
public string? Port { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true)]
|
||||
public object? CustomFields { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
try
|
||||
{
|
||||
var identifiers = new List<string> { "first_free" };
|
||||
var body = new Dictionary<string, object>
|
||||
{
|
||||
{ "subnetId", SubnetId }
|
||||
};
|
||||
|
||||
if (Gateway.IsPresent)
|
||||
body["is_gateway"] = "1";
|
||||
if (!string.IsNullOrEmpty(Description))
|
||||
body["description"] = Description;
|
||||
if (!string.IsNullOrEmpty(Hostname))
|
||||
body["hostname"] = Hostname;
|
||||
if (!string.IsNullOrEmpty(MAC))
|
||||
body["mac"] = MAC;
|
||||
if (!string.IsNullOrEmpty(Owner))
|
||||
body["owner"] = Owner;
|
||||
if (!string.IsNullOrEmpty(TagId))
|
||||
body["tag"] = TagId;
|
||||
if (PTRIgnore.IsPresent)
|
||||
body["PTRignore"] = "1";
|
||||
if (!string.IsNullOrEmpty(PTRId))
|
||||
body["PTR"] = PTRId;
|
||||
if (!string.IsNullOrEmpty(Note))
|
||||
body["note"] = Note;
|
||||
if (ExcludePing.IsPresent)
|
||||
body["excludePing"] = "1";
|
||||
if (!string.IsNullOrEmpty(DeviceId))
|
||||
body["deviceId"] = DeviceId;
|
||||
if (!string.IsNullOrEmpty(Port))
|
||||
body["port"] = Port;
|
||||
|
||||
if (CustomFields != null)
|
||||
{
|
||||
var customDict = ConvertCustomFields(CustomFields);
|
||||
foreach (var kvp in customDict)
|
||||
{
|
||||
body[kvp.Key] = kvp.Value;
|
||||
}
|
||||
}
|
||||
|
||||
var result = RequestHelper.InvokeRequest("POST", controllers.addresses, null, null, body, identifiers.ToArray())
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
var ip = result.ToString();
|
||||
var address = RequestHelper.InvokeRequest("GET", controllers.addresses, types.Address, null, null, new[] { "search", ip })
|
||||
.GetAwaiter().GetResult();
|
||||
if (address != null)
|
||||
{
|
||||
WriteObject(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteError(new ErrorRecord(ex, "NewFirstFreeIPError", ErrorCategory.InvalidOperation, null));
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, object> ConvertCustomFields(object customFields)
|
||||
{
|
||||
var dict = new Dictionary<string, object>();
|
||||
if (customFields is PSObject psobj)
|
||||
{
|
||||
foreach (var prop in psobj.Properties)
|
||||
{
|
||||
dict[prop.Name] = prop.Value ?? new object();
|
||||
}
|
||||
}
|
||||
else if (customFields is Dictionary<string, object> dictObj)
|
||||
{
|
||||
return dictObj;
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user