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:
148
classlib/Cmdlets/GetPermissionsCmdlet.cs
Normal file
148
classlib/Cmdlets/GetPermissionsCmdlet.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
namespace PS.IPAM.Cmdlets;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Automation;
|
||||
using System.Net;
|
||||
using PS.IPAM;
|
||||
using PS.IPAM.Helpers;
|
||||
|
||||
[Cmdlet(VerbsCommon.Get, "Permissions")]
|
||||
public class GetPermissionsCmdlet : PSCmdlet
|
||||
{
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByID")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? Id { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByIP")]
|
||||
[Parameter(
|
||||
Mandatory = false,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 1,
|
||||
ParameterSetName = "BySubnetId")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public IPAddress? IP { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByHostName")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? HostName { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "ByTag")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? TagId { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "BySubnetId")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? SubnetId { get; set; }
|
||||
|
||||
[Parameter(
|
||||
Mandatory = true,
|
||||
ValueFromPipeline = true,
|
||||
ValueFromPipelineByPropertyName = true,
|
||||
Position = 0,
|
||||
ParameterSetName = "BySubnetCIDR")]
|
||||
[ValidatePattern(@"^\d+\.\d+\.\d+\.\d+/\d{1,2}$")]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public string? SubnetCIDR { get; set; }
|
||||
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
try
|
||||
{
|
||||
var controller = controllers.addresses;
|
||||
var identifiers = new List<string>();
|
||||
|
||||
switch (ParameterSetName)
|
||||
{
|
||||
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)
|
||||
{
|
||||
identifiers.Add(IP.ToString());
|
||||
identifiers.Add(SubnetId!);
|
||||
}
|
||||
else
|
||||
{
|
||||
controller = controllers.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();
|
||||
if (subnet == null)
|
||||
{
|
||||
throw new Exception("Cannot find subnet!");
|
||||
}
|
||||
var subnetObj = subnet as Subnetwork;
|
||||
identifiers.Add(subnetObj!.Id.ToString());
|
||||
identifiers.Add("addresses");
|
||||
break;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteError(new ErrorRecord(ex, "GetPermissionsError", ErrorCategory.InvalidOperation, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user