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,24 @@
|
||||
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 permission information from phpIPAM.
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "Permissions")]
|
||||
public class GetPermissionsCmdlet : PSCmdlet
|
||||
public class GetPermissionsCmdlet : BaseCmdlet
|
||||
{
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByID")]
|
||||
ParameterSetName = "ByID",
|
||||
HelpMessage = "The address ID.")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? Id { get; set; }
|
||||
|
||||
@@ -23,7 +27,8 @@ public class GetPermissionsCmdlet : PSCmdlet
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByIP")]
|
||||
ParameterSetName = "ByIP",
|
||||
HelpMessage = "The IP address to search for.")]
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
@@ -38,7 +43,8 @@ public class GetPermissionsCmdlet : PSCmdlet
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByHostName")]
|
||||
ParameterSetName = "ByHostName",
|
||||
HelpMessage = "The hostname to search for.")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? HostName { get; set; }
|
||||
|
||||
@@ -47,7 +53,8 @@ public class GetPermissionsCmdlet : PSCmdlet
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByTag")]
|
||||
ParameterSetName = "ByTag",
|
||||
HelpMessage = "The tag ID.")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? TagId { get; set; }
|
||||
|
||||
@@ -56,7 +63,8 @@ public class GetPermissionsCmdlet : PSCmdlet
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "BySubnetId")]
|
||||
ParameterSetName = "BySubnetId",
|
||||
HelpMessage = "The subnet ID.")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? SubnetId { get; set; }
|
||||
|
||||
@@ -65,7 +73,8 @@ public class GetPermissionsCmdlet : PSCmdlet
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "BySubnetCIDR")]
|
||||
ParameterSetName = "BySubnetCIDR",
|
||||
HelpMessage = "The subnet in CIDR notation.")]
|
||||
[ValidatePattern(@"^\d+\.\d+\.\d+\.\d+/\d{1,2}$")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? SubnetCIDR { get; set; }
|
||||
@@ -74,7 +83,7 @@ public class GetPermissionsCmdlet : PSCmdlet
|
||||
{
|
||||
try
|
||||
{
|
||||
var controller = controllers.addresses;
|
||||
var controller = ApiController.Addresses;
|
||||
var identifiers = new List<string>();
|
||||
|
||||
switch (ParameterSetName)
|
||||
@@ -82,19 +91,23 @@ public class GetPermissionsCmdlet : PSCmdlet
|
||||
case "ByID":
|
||||
identifiers.Add(Id!);
|
||||
break;
|
||||
|
||||
case "ByIP":
|
||||
identifiers.Add("search");
|
||||
identifiers.Add(IP!.ToString());
|
||||
break;
|
||||
|
||||
case "ByHostName":
|
||||
identifiers.Add("search_hostname");
|
||||
identifiers.Add(HostName!);
|
||||
break;
|
||||
|
||||
case "ByTag":
|
||||
identifiers.Add("tags");
|
||||
identifiers.Add(TagId!);
|
||||
identifiers.Add("addresses");
|
||||
break;
|
||||
|
||||
case "BySubnetId":
|
||||
if (IP != null)
|
||||
{
|
||||
@@ -103,42 +116,34 @@ public class GetPermissionsCmdlet : PSCmdlet
|
||||
}
|
||||
else
|
||||
{
|
||||
controller = controllers.subnets;
|
||||
controller = ApiController.Subnets;
|
||||
identifiers.Add(SubnetId!);
|
||||
identifiers.Add("addresses");
|
||||
}
|
||||
break;
|
||||
|
||||
case "BySubnetCIDR":
|
||||
controller = controllers.subnets;
|
||||
var subnet = RequestHelper.InvokeRequest("GET", controllers.subnets, types.Subnetwork, null, null, new[] { "cidr", SubnetCIDR! })
|
||||
.GetAwaiter().GetResult();
|
||||
controller = ApiController.Subnets;
|
||||
var subnet = RequestHelper.InvokeRequest(
|
||||
"GET", ApiController.Subnets, ModelType.Subnetwork, null, null,
|
||||
new[] { "cidr", SubnetCIDR! }
|
||||
).GetAwaiter().GetResult() as Subnetwork;
|
||||
|
||||
if (subnet == null)
|
||||
{
|
||||
throw new Exception("Cannot find subnet!");
|
||||
throw new ItemNotFoundException($"Subnet '{SubnetCIDR}' not found.");
|
||||
}
|
||||
var subnetObj = subnet as Subnetwork;
|
||||
identifiers.Add(subnetObj!.Id.ToString());
|
||||
|
||||
identifiers.Add(subnet.Id.ToString());
|
||||
identifiers.Add("addresses");
|
||||
break;
|
||||
}
|
||||
|
||||
var result = RequestHelper.InvokeRequest("GET", controller, null, null, null, identifiers.ToArray())
|
||||
.GetAwaiter().GetResult();
|
||||
var result = RequestHelper.InvokeRequest(
|
||||
"GET", controller, null, null, null, identifiers.ToArray()
|
||||
).GetAwaiter().GetResult();
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
if (result is System.Collections.IEnumerable enumerable && !(result is string))
|
||||
{
|
||||
foreach (var item in enumerable)
|
||||
{
|
||||
WriteObject(item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteObject(result);
|
||||
}
|
||||
}
|
||||
WriteResult(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user