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:
2026-01-19 17:25:18 +03:00
parent 694822f0d6
commit f56784f2aa
44 changed files with 1601 additions and 1905 deletions

View File

@@ -1,20 +1,24 @@
namespace PS.IPAM.Cmdlets;
using System;
using System.Collections.Generic;
using System.Management.Automation;
using PS.IPAM;
using PS.IPAM.Helpers;
/// <summary>
/// Updates an existing IP address entry in phpIPAM.
/// </summary>
[Cmdlet(VerbsCommon.Set, "Address", DefaultParameterSetName = "ById")]
[OutputType(typeof(Address))]
public class SetAddressCmdlet : PSCmdlet
public class SetAddressCmdlet : BaseCmdlet
{
[Parameter(
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
Position = 0,
ParameterSetName = "ById")]
ParameterSetName = "ById",
HelpMessage = "The address ID to update.")]
[ValidateNotNullOrEmpty]
public int? Id { get; set; }
@@ -23,142 +27,72 @@ public class SetAddressCmdlet : PSCmdlet
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
Position = 0,
ParameterSetName = "ByAddressObject")]
ParameterSetName = "ByAddressObject",
HelpMessage = "The address object to update.")]
[ValidateNotNullOrEmpty]
public Address? AddressObject { get; set; }
[Parameter(
Position = 1,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
ParameterSetName = "ById")]
[Parameter(Position = 1, HelpMessage = "Set as gateway address.")]
public bool? Gateway { get; set; }
[Parameter(
Position = 2,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
ParameterSetName = "ById")]
[Parameter(Position = 2, HelpMessage = "Description for the address.")]
public string? Description { get; set; }
[Parameter(
Mandatory = false,
Position = 3)]
[Parameter(Position = 3, HelpMessage = "Hostname for the address.")]
public string? Hostname { get; set; }
[Parameter(
Mandatory = false,
Position = 4)]
[Parameter(Position = 4, HelpMessage = "MAC address.")]
public string? MAC { get; set; }
[Parameter(
Mandatory = false,
Position = 5)]
[Parameter(Position = 5, HelpMessage = "Owner of the address.")]
public string? Owner { get; set; }
[Parameter(
Mandatory = false,
Position = 6)]
[Parameter(Position = 6, HelpMessage = "Tag ID for the address.")]
public int? TagId { get; set; }
[Parameter(
Mandatory = false,
Position = 7)]
[Parameter(Position = 7, HelpMessage = "Ignore PTR record generation.")]
public bool? PTRIgnore { get; set; }
[Parameter(
Mandatory = false,
Position = 8)]
[Parameter(Position = 8, HelpMessage = "PTR record ID.")]
public int? PTRId { get; set; }
[Parameter(
Mandatory = false,
Position = 9)]
[Parameter(Position = 9, HelpMessage = "Note for the address.")]
public string? Note { get; set; }
[Parameter(
Mandatory = false,
Position = 10)]
[Parameter(Position = 10, HelpMessage = "Exclude from ping scanning.")]
public bool? ExcludePing { get; set; }
[Parameter(
Mandatory = false,
Position = 11)]
[Parameter(Position = 11, HelpMessage = "Associated device ID.")]
public int? DeviceId { get; set; }
[Parameter(
Mandatory = false,
Position = 12)]
[Parameter(Position = 12, HelpMessage = "Port information.")]
public string? Port { get; set; }
[Parameter(
Mandatory = false)]
[Parameter(HelpMessage = "Custom fields as a hashtable or PSObject.")]
public object? CustomFields { get; set; }
protected override void ProcessRecord()
{
try
{
int addressId;
if (ParameterSetName == "ById")
{
addressId = Id!.Value;
}
else
{
addressId = AddressObject!.Id;
}
var identifiers = new List<string> { addressId.ToString() };
var body = new Dictionary<string, object>();
if (Gateway.HasValue)
body["is_gateway"] = Gateway.Value;
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 (TagId.HasValue)
body["tag"] = TagId.Value;
if (PTRIgnore.HasValue)
body["PTRignore"] = PTRIgnore.Value;
if (PTRId.HasValue)
body["PTR"] = PTRId.Value;
if (!string.IsNullOrEmpty(Note))
body["note"] = Note;
if (ExcludePing.HasValue)
body["excludePing"] = ExcludePing.Value;
if (DeviceId.HasValue)
body["deviceId"] = DeviceId.Value;
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 addressId = ParameterSetName == "ById" ? Id!.Value : AddressObject!.Id;
var identifiers = new[] { addressId.ToString() };
var body = BuildRequestBody();
try
{
RequestHelper.InvokeRequest("PATCH", controllers.addresses, null, null, body, identifiers.ToArray())
.GetAwaiter().GetResult();
RequestHelper.InvokeRequest(
"PATCH", ApiController.Addresses, null, null, body, identifiers
).GetAwaiter().GetResult();
}
finally
{
var address = RequestHelper.InvokeRequest("GET", controllers.addresses, types.Address, null, null, identifiers.ToArray())
.GetAwaiter().GetResult();
if (address != null)
{
WriteObject(address);
}
// Always return the updated address
var address = RequestHelper.InvokeRequest(
"GET", ApiController.Addresses, ModelType.Address, null, null, identifiers
).GetAwaiter().GetResult();
WriteResult(address);
}
}
catch (Exception ex)
@@ -167,20 +101,28 @@ public class SetAddressCmdlet : 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>();
if (Gateway.HasValue) body["is_gateway"] = Gateway.Value;
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 (TagId.HasValue) body["tag"] = TagId.Value;
if (PTRIgnore.HasValue) body["PTRignore"] = PTRIgnore.Value;
if (PTRId.HasValue) body["PTR"] = PTRId.Value;
if (!string.IsNullOrEmpty(Note)) body["note"] = Note;
if (ExcludePing.HasValue) body["excludePing"] = ExcludePing.Value;
if (DeviceId.HasValue) body["deviceId"] = DeviceId.Value;
if (!string.IsNullOrEmpty(Port)) body["port"] = Port;
foreach (var kvp in ConvertCustomFields(CustomFields))
{
foreach (var prop in psobj.Properties)
{
dict[prop.Name] = prop.Value ?? new object();
}
body[kvp.Key] = kvp.Value;
}
else if (customFields is Dictionary<string, object> dictObj)
{
return dictObj;
}
return dict;
return body;
}
}