Refactor IPAM model classes to use records for Address, Subnetwork, Vlan, Vrf, Section, Tag, Domain, Nameserver, and Session; enhance documentation and implement value equality for records.
This commit is contained in:
@@ -1,168 +1,85 @@
|
||||
namespace PS.IPAM.Cmdlets;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Automation;
|
||||
using PS.IPAM;
|
||||
using PS.IPAM.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new address using the first available IP in a subnet.
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.New, "FirstFreeIP")]
|
||||
[OutputType(typeof(Address))]
|
||||
public class NewFirstFreeIPCmdlet : PSCmdlet
|
||||
public class NewFirstFreeIPCmdlet : BaseCmdlet
|
||||
{
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0)]
|
||||
Position = 0,
|
||||
HelpMessage = "The subnet ID to create the address in.")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string SubnetId { get; set; } = string.Empty;
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 2)]
|
||||
[Parameter(Mandatory = false, Position = 1, HelpMessage = "Mark this address as a gateway.")]
|
||||
public SwitchParameter Gateway { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 3)]
|
||||
[Parameter(Mandatory = false, Position = 2, HelpMessage = "Description for the address.")]
|
||||
public string? Description { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 4)]
|
||||
[Parameter(Mandatory = false, Position = 3, HelpMessage = "Hostname for the address.")]
|
||||
public string? Hostname { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 5)]
|
||||
[Parameter(Mandatory = false, Position = 4, HelpMessage = "MAC address.")]
|
||||
public string? MAC { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 6)]
|
||||
[Parameter(Mandatory = false, Position = 5, HelpMessage = "Owner of the address.")]
|
||||
public string? Owner { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 7)]
|
||||
[Parameter(Mandatory = false, Position = 6, HelpMessage = "Tag ID for the address.")]
|
||||
public string? TagId { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 8)]
|
||||
[Parameter(Mandatory = false, Position = 7, HelpMessage = "Ignore PTR record generation.")]
|
||||
public SwitchParameter PTRIgnore { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 9)]
|
||||
[Parameter(Mandatory = false, Position = 8, HelpMessage = "PTR record ID.")]
|
||||
public string? PTRId { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 10)]
|
||||
[Parameter(Mandatory = false, Position = 9, HelpMessage = "Note for the address.")]
|
||||
public string? Note { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 11)]
|
||||
[Parameter(Mandatory = false, Position = 10, HelpMessage = "Exclude from ping scanning.")]
|
||||
public SwitchParameter ExcludePing { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 12)]
|
||||
[Parameter(Mandatory = false, Position = 11, HelpMessage = "Associated device ID.")]
|
||||
public string? DeviceId { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 13)]
|
||||
[Parameter(Mandatory = false, Position = 12, HelpMessage = "Port information.")]
|
||||
public string? Port { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true)]
|
||||
[Parameter(Mandatory = false, HelpMessage = "Custom fields as a hashtable or PSObject.")]
|
||||
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 }
|
||||
};
|
||||
var identifiers = new[] { "first_free" };
|
||||
var body = BuildRequestBody();
|
||||
|
||||
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();
|
||||
var result = RequestHelper.InvokeRequest(
|
||||
"POST", ApiController.Addresses, null, null, body, identifiers
|
||||
).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);
|
||||
}
|
||||
var address = RequestHelper.InvokeRequest(
|
||||
"GET", ApiController.Addresses, ModelType.Address, null, null,
|
||||
new[] { "search", ip! }
|
||||
).GetAwaiter().GetResult();
|
||||
|
||||
WriteResult(address);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -171,20 +88,31 @@ public class NewFirstFreeIPCmdlet : PSCmdlet
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, object> ConvertCustomFields(object customFields)
|
||||
private Dictionary<string, object> BuildRequestBody()
|
||||
{
|
||||
var dict = new Dictionary<string, object>();
|
||||
if (customFields is PSObject psobj)
|
||||
var body = new Dictionary<string, object>
|
||||
{
|
||||
foreach (var prop in psobj.Properties)
|
||||
{
|
||||
dict[prop.Name] = prop.Value ?? new object();
|
||||
}
|
||||
}
|
||||
else if (customFields is Dictionary<string, object> dictObj)
|
||||
["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;
|
||||
|
||||
foreach (var kvp in ConvertCustomFields(CustomFields))
|
||||
{
|
||||
return dictObj;
|
||||
body[kvp.Key] = kvp.Value;
|
||||
}
|
||||
return dict;
|
||||
|
||||
return body;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user