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,20 +1,23 @@
|
||||
namespace PS.IPAM.Cmdlets;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Automation;
|
||||
using System.Net;
|
||||
using PS.IPAM;
|
||||
using PS.IPAM.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves subnet usage statistics from phpIPAM.
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "SubnetUsage", DefaultParameterSetName = "ByID")]
|
||||
public class GetSubnetUsageCmdlet : PSCmdlet
|
||||
public class GetSubnetUsageCmdlet : BaseCmdlet
|
||||
{
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByCIDR")]
|
||||
ParameterSetName = "ByCIDR",
|
||||
HelpMessage = "The subnet in CIDR notation.")]
|
||||
[ValidatePattern(@"^\d+\.\d+\.\d+\.\d+/\d{1,2}$")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? CIDR { get; set; }
|
||||
@@ -24,43 +27,60 @@ public class GetSubnetUsageCmdlet : PSCmdlet
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByID")]
|
||||
ParameterSetName = "ByID",
|
||||
HelpMessage = "The subnet ID.")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public int? Id { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "BySubnetObject",
|
||||
HelpMessage = "The subnet object.")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Subnetwork? SubnetObject { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
try
|
||||
{
|
||||
int subnetId;
|
||||
if (ParameterSetName == "ByCIDR")
|
||||
{
|
||||
var subnet = RequestHelper.InvokeRequest("GET", controllers.subnets, types.Subnetwork, null, null, new[] { "cidr", CIDR! })
|
||||
.GetAwaiter().GetResult();
|
||||
if (subnet == null)
|
||||
{
|
||||
throw new Exception("Cannot find subnet!");
|
||||
}
|
||||
var subnetObj = subnet as Subnetwork;
|
||||
subnetId = subnetObj!.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
subnetId = Id!.Value;
|
||||
}
|
||||
|
||||
var subnetId = GetSubnetId();
|
||||
var identifiers = new List<string> { subnetId.ToString(), "usage" };
|
||||
var result = RequestHelper.InvokeRequest("GET", controllers.subnets, null, null, null, identifiers.ToArray())
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
WriteObject(result);
|
||||
}
|
||||
var result = RequestHelper.InvokeRequest(
|
||||
"GET", ApiController.Subnets, null, null, null, identifiers.ToArray()
|
||||
).GetAwaiter().GetResult();
|
||||
|
||||
WriteResult(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteError(new ErrorRecord(ex, "GetSubnetUsageError", ErrorCategory.InvalidOperation, null));
|
||||
}
|
||||
}
|
||||
|
||||
private int GetSubnetId()
|
||||
{
|
||||
switch (ParameterSetName)
|
||||
{
|
||||
case "ByCIDR":
|
||||
var subnet = RequestHelper.InvokeRequest(
|
||||
"GET", ApiController.Subnets, ModelType.Subnetwork, null, null,
|
||||
new[] { "cidr", CIDR! }
|
||||
).GetAwaiter().GetResult() as Subnetwork;
|
||||
|
||||
if (subnet == null)
|
||||
{
|
||||
throw new ItemNotFoundException($"Subnet '{CIDR}' not found.");
|
||||
}
|
||||
return subnet.Id;
|
||||
|
||||
case "BySubnetObject":
|
||||
return SubnetObject!.Id;
|
||||
|
||||
default:
|
||||
return Id!.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user