first working types

This commit is contained in:
2022-12-06 17:20:45 +03:00
parent a1b03b81c0
commit 7d0d1b53ae
14 changed files with 129 additions and 75 deletions

View File

@@ -19,19 +19,22 @@ public class Subnetwork {
public string Permissions { get; } public string Permissions { get; }
public bool PingSubnet { get; } public bool PingSubnet { get; }
public bool DiscoverSubnet { get; } public bool DiscoverSubnet { get; }
public bool ResolveDNS { get; }
public bool DNSRecursive { get; } public bool DNSRecursive { get; }
public bool DNSRecords { get; } public bool DNSRecords { get; }
public int NameserverId { get; } public int NameserverId { get; }
public bool ScanAgent { get; } public bool ScanAgent { get; }
public bool IsFolder { get; } public bool IsFolder { get; }
public bool IsFull { get; } public bool IsFull { get; }
public bool IsPool { get; }
public int TagId { get; } public int TagId { get; }
public int Threshold { get; } public int Threshold { get; }
public int LocationId { get; } public int LocationId { get; }
public DateTime? EditDate { get; } public DateTime? EditDate { get; }
public DateTime? LastScan { get; } public DateTime? LastScan { get; }
public DateTime? LastDiscovery { get; } public DateTime? LastDiscovery { get; }
public bool ResolveDNS { get; } public Object Calculation { get; }
public Object? ExtendedData { get; }
public Subnetwork( public Subnetwork(
int id, int id,
string subnet, string subnet,
@@ -49,19 +52,22 @@ public class Subnetwork {
string permissions, string permissions,
bool pingSubnet, bool pingSubnet,
bool discoverSubnet, bool discoverSubnet,
bool resolveDNS,
bool dnsRecursive, bool dnsRecursive,
bool dnsRecords, bool dnsRecords,
int nameserverId, int nameserverId,
bool scanAgent, bool scanAgent,
bool isFolder, bool isFolder,
bool isFull, bool isFull,
bool isPool,
int tagId, int tagId,
int threshold, int threshold,
int locationId, int locationId,
DateTime? editDate, DateTime? editDate,
DateTime? lastScan, DateTime? lastScan,
DateTime? lastDiscovery, DateTime? lastDiscovery,
bool resolveDNS Object calculation,
Object? custom_fields
) { ) {
this.Id = id; this.Id = id;
this.Subnet = subnet; this.Subnet = subnet;
@@ -79,19 +85,22 @@ public class Subnetwork {
this.Permissions = permissions; this.Permissions = permissions;
this.PingSubnet = pingSubnet; this.PingSubnet = pingSubnet;
this.DiscoverSubnet = discoverSubnet; this.DiscoverSubnet = discoverSubnet;
this.ResolveDNS = resolveDNS;
this.DNSRecursive = dnsRecursive; this.DNSRecursive = dnsRecursive;
this.DNSRecords = dnsRecords; this.DNSRecords = dnsRecords;
this.NameserverId = nameserverId; this.NameserverId = nameserverId;
this.ScanAgent = scanAgent; this.ScanAgent = scanAgent;
this.IsFolder = isFolder; this.IsFolder = isFolder;
this.IsFull = isFull; this.IsFull = isFull;
this.IsPool = isPool;
this.TagId = tagId; this.TagId = tagId;
this.Threshold = threshold; this.Threshold = threshold;
this.LocationId = locationId; this.LocationId = locationId;
this.EditDate = editDate; this.EditDate = editDate;
this.LastScan = lastScan; this.LastScan = lastScan;
this.LastDiscovery = lastDiscovery; this.LastDiscovery = lastDiscovery;
this.ResolveDNS = resolveDNS; this.Calculation = calculation;
this.ExtendedData = custom_fields;
} }
public string GetCIDR() { public string GetCIDR() {

View File

@@ -1,4 +1,5 @@
namespace PS.IPAM; namespace PS.IPAM;
using System;
[Serializable] [Serializable]
public class Tag { public class Tag {

View File

@@ -1,28 +1,35 @@
namespace PS.IPAM; namespace PS.IPAM;
using System; using System;
using System.Dynamic;
[Serializable] [Serializable]
public class Vlan { public class Vlan : DynamicObject {
public int Id { get; } public int Id { get; }
public int DomainId { get; } public int DomainId { get; }
public string Name { get; } public string Name { get; }
public int Number { get; } public int Number { get; }
public string Description { get; } public string Description { get; }
public DateTime? EditDate { get; } public DateTime? EditDate { get; }
public int CustomerId { get; }
public Object? ExtendedData { get; }
public Vlan ( public Vlan (
int id, int vlanId,
int domainId, int domainId,
string name, string name,
int number, int number,
string description, string description,
DateTime? editDate DateTime? editDate,
int customer_id,
Object? custom_fields
) { ) {
this.Id = id; this.Id = vlanId;
this.DomainId = domainId; this.DomainId = domainId;
this.Name = name; this.Name = name;
this.Number = number; this.Number = number;
this.Description = description; this.Description = description;
this.EditDate = editDate; this.EditDate = editDate;
this.CustomerId = customer_id;
this.ExtendedData = custom_fields;
} }
public override string ToString() public override string ToString()

View File

@@ -1,6 +1,7 @@
namespace PS.IPAM; namespace PS.IPAM;
using System; using System;
[Serializable]
public enum methods { public enum methods {
GET, GET,
POST, POST,

View File

@@ -1,12 +1,13 @@
namespace PS.IPAM; namespace PS.IPAM;
using System; using System;
[Serializable]
public enum types { public enum types {
address, Address,
domain, Domain,
section, Section,
subnetwork, Subnetwork,
tag, Tag,
vlan, Vlan,
vrf Vrf
} }

View File

@@ -2,16 +2,16 @@ function Invoke-Request {
[CmdletBinding()] [CmdletBinding()]
param ( param (
[parameter(Mandatory=$true)] [parameter(Mandatory=$true)]
[methods] [PS.IPAM.methods]
$Method, $Method,
[parameter(Mandatory=$true)] [parameter(Mandatory=$true)]
[controllers] [PS.IPAM.controllers]
$Controller, $Controller,
[parameter(Mandatory=$false)] [parameter(Mandatory=$false)]
[types] [PS.IPAM.types]
$Type, $Type,
[parameter(Mandatory=$false)] [parameter(Mandatory=$false)]
[subcontrollers] [PS.IPAM.subcontrollers]
$SubController, $SubController,
[Parameter(Mandatory=$false)] [Parameter(Mandatory=$false)]
[ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })]
@@ -25,7 +25,7 @@ function Invoke-Request {
if ($_tokenStatus -eq "NoToken") { throw "No session available!" } if ($_tokenStatus -eq "NoToken") { throw "No session available!" }
if ($_tokenStatus -eq "Expired") { Update-Session } if ($_tokenStatus -eq "Expired") { Update-Session }
$Controller = $Controller.ToLower() $Controller = $Controller
$_uri = "$($script:ipamURL)/api/$($script:ipamAppID)/$Controller" $_uri = "$($script:ipamURL)/api/$($script:ipamAppID)/$Controller"
if ($SubController) { $_uri += "/$SubController" } if ($SubController) { $_uri += "/$SubController" }
@@ -46,7 +46,7 @@ function Invoke-Request {
Headers = $_headers Headers = $_headers
} }
if ($Method -eq [methods]::POST -or $Method -eq [methods]::PATCH) { if ($Method -eq [PS.IPAM.methods]::POST -or $Method -eq [PS.IPAM.methods]::PATCH) {
if ($Params -is [PSCustomObject]) { if ($Params -is [PSCustomObject]) {
$_params = @{}; $_params = @{};
$Params | Get-Member -MemberType *Property | Where-Object { $Params | Get-Member -MemberType *Property | Where-Object {
@@ -60,10 +60,22 @@ function Invoke-Request {
$_response = Invoke-RestMethod @_arguments $_response = Invoke-RestMethod @_arguments
if ($_response.code -match "20\d") { if ($_response.code -match "20\d") {
if ($Type) { if ($Type -is [PS.IPAM.types]) {
{ $_response.data | ForEach-Object { New-Object -TypeName $Type -ArgumentList $_.psobject.properties.value } } switch ($Type) {
"vlan" {
$_response.data | ForEach-Object {
New-Object -TypeName ([PS.IPAM.Vlan]) -ArgumentList (@($_.psobject.properties.value[0..6]) + ($_ | Select-Object -Property custom_* -ExcludeProperty custom_*))
}; break
}
"subnetwork" {
$_response.data | ForEach-Object {
New-Object -TypeName ([PS.IPAM.Subnetwork]) -ArgumentList (@($_.psobject.properties.value[0..30]) + ($_ | Select-Object -Property custom_* -ExcludeProperty custom_*))
}; break
}
Default { $_response.data | ForEach-Object { New-Object -TypeName ("PS.IPAM.$Type") -ArgumentList $_.psobject.properties.value } }
}
} else { } else {
$_response.data return $_response.data
} }
} else { } else {
throw ("Error - $($_response.code)") throw ("Error - $($_response.code)")

View File

@@ -1,6 +1,6 @@
function Get-Address { function Get-Address {
[CmdletBinding(DefaultParameterSetName="ByID")] [CmdletBinding(DefaultParameterSetName="ByID")]
[OutputType({[types]::address})] [OutputType([PS.IPAM.address])]
param ( param (
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
[ValidateScript({ $_ -match "^\d+$" })] [ValidateScript({ $_ -match "^\d+$" })]
@@ -35,29 +35,29 @@ function Get-Address {
) )
process { process {
$_params = @{ $_params = @{
Controller = [controllers]::addresses Controller = [PS.IPAM.controllers]::addresses
Method = [methods]::GET Method = [PS.IPAM.methods]::GET
Type = [types]::address Type = [PS.IPAM.types]::address
} }
switch ($PSCmdlet.ParameterSetName) { switch ($PSCmdlet.ParameterSetName) {
"ByID" { $_identifiers = @($id) } "ByID" { $_identifiers = @($id) }
"ByIP" { $_identifiers = ("search",$IP) } "ByIP" { $_identifiers = ("search",$IP) }
"ByHostName" { $_identifiers = ("search_hostname",$HostName) } "ByHostName" { $_identifiers = ("search_hostname",$HostName) }
"ByTag" { $_identifiers = ("tags",$TagId,[controllers]::addresses) } "ByTag" { $_identifiers = ("tags",$TagId,[PS.IPAM.controllers]::addresses) }
"BySubnetId" { "BySubnetId" {
if ($IP) { if ($IP) {
$_identifiers = ($IP,$SubnetId) $_identifiers = ($IP,$SubnetId)
} else { } else {
$_params.Item("Controller") = [controllers]::subnets $_params.Item("Controller") = [PS.IPAM.controllers]::subnets
$_identifiers = ($SubnetId,[controllers]::addresses) $_identifiers = ($SubnetId,[PS.IPAM.controllers]::addresses)
} }
} }
"BySubnetCIDR" { "BySubnetCIDR" {
$_params.Item("Controller") = [controllers]::subnets $_params.Item("Controller") = [PS.IPAM.controllers]::subnets
$_subnetId = (Get-Subnet -CIDR $SubnetCIDR).id $_subnetId = (Get-Subnet -CIDR $SubnetCIDR).id
if (!$_subnetId) { throw "Cannot find subnet!" } if (!$_subnetId) { throw "Cannot find subnet!" }
$_identifiers = ($_subnetId,[controllers]::addresses) $_identifiers = ($_subnetId,[PS.IPAM.controllers]::addresses)
} }
} }
$_params.Add("Identifiers",$_identifiers) $_params.Add("Identifiers",$_identifiers)

View File

@@ -10,19 +10,25 @@ function Get-FirstFreeIP {
[ValidateScript({ $_ -match "^\d+$" })] [ValidateScript({ $_ -match "^\d+$" })]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string] [string]
$Id $Id,
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")]
[ValidateNotNullOrEmpty()]
[PS.IPAM.Subnetwork]
$SubnetObject
) )
process { process {
$_params = @{ $_params = @{
Controller = "subnets" Controller = [PS.IPAM.controllers]::subnets
Method = "GET" Method = [PS.IPAM.methods]::GET
} }
switch ($PSCmdlet.ParameterSetName) { switch ($PSCmdlet.ParameterSetName) {
"ByID" { $_subnetId = $Id } "ByID" { $_subnetId = $Id; break }
"ByCIDR" { "ByCIDR" {
$_subnetId = (Get-Subnet -CIDR $CIDR).id $_subnetId = (Get-Subnet -CIDR $CIDR).id
if (!$_subnetId) { throw "Cannot find subnet!" } if (!$_subnetId) { throw "Cannot find subnet!" }
break
} }
"BySubnetObject" { $_subnetId = $SubnetObject.Id; break }
} }
$_identifiers = @($_subnetId,"first_free") $_identifiers = @($_subnetId,"first_free")
$_params.Add("Identifiers",$_identifiers) $_params.Add("Identifiers",$_identifiers)

View File

@@ -1,26 +1,29 @@
function Get-L2Domain { function Get-L2Domain {
[CmdletBinding(DefaultParameterSetName="ByID")] [CmdletBinding(DefaultParameterSetName="NoParams")]
[OutputType([PS.IPAM.Domain])]
param ( param (
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
[ValidateScript({ $_ -match "^\d+$" })] [ValidateScript({ $_ -match "^\d+$" })]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string] [int]
$Id $Id
) )
process { process {
[string[]]$visiblePropertiesList = @('Id','Name','Description')
$visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList)
$_params = @{ $_params = @{
Controller = "l2domains" Controller = [PS.IPAM.controllers]::l2domains
Method = "GET" Method = [PS.IPAM.methods]::GET
Type = [PS.IPAM.types]::Domain
} }
switch ($PSCmdlet.ParameterSetName) {
"ByID" {
$_identifiers = @($Id) $_identifiers = @($Id)
}
}
$_params.Add("Identifiers",$_identifiers) $_params.Add("Identifiers",$_identifiers)
Invoke-Request @_params | ` Invoke-Request @_params
Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru
} }
} }
Export-ModuleMember -Function Get-L2Domain Export-ModuleMember -Function Get-L2Domain

View File

@@ -1,5 +1,6 @@
function Get-Subnet { function Get-Subnet {
[CmdletBinding(DefaultParameterSetName="ByID")] [CmdletBinding(DefaultParameterSetName="NoParams")]
[OutputType([PS.IPAM.Subnetwork])]
param ( param (
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByCIDR")] [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByCIDR")]
[ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})] [ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})]
@@ -52,13 +53,10 @@ function Get-Subnet {
$Recurse $Recurse
) )
process { process {
[string[]]$visiblePropertiesList = @('Id','Subnet','Mask','Description')
$visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList)
$_params = @{ $_params = @{
Controller = "subnets" Controller = [PS.IPAM.controllers]::subnets
Method = "GET" Method = [PS.IPAM.methods]::GET
Type = "subnet" Type = [PS.IPAM.types]::Subnetwork
} }
switch ($PSCmdlet.ParameterSetName) { switch ($PSCmdlet.ParameterSetName) {
"ByCIDR" { "ByCIDR" {

View File

@@ -1,6 +1,6 @@
function Get-Tag { function Get-Tag {
[CmdletBinding(DefaultParameterSetName="NoParams")] [CmdletBinding(DefaultParameterSetName="NoParams")]
[OutputType({[types]::tag})] [OutputType([PS.IPAM.Tag])]
param ( param (
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
[ValidateScript({ $_ -match "^\d+$" })] [ValidateScript({ $_ -match "^\d+$" })]
@@ -18,16 +18,24 @@ function Get-Tag {
) )
process { process {
$_params = @{ $_params = @{
Controller = [controllers]::addresses Controller = [PS.IPAM.controllers]::addresses
Method = [methods]::GET Method = [PS.IPAM.methods]::GET
Type = [types]::tag Type = [PS.IPAM.types]::tag
} }
$_identifiers = @("tags") $_identifiers = @("tags")
switch ($PSCmdlet.ParameterSetName) { switch ($PSCmdlet.ParameterSetName) {
"ByID" { $_identifiers += $Id } "ByID" { $_identifiers += $Id; break }
"ByAddressObject" { "ByAddressObject" {
if ($InputObject.TagId) { if ($AddressObject.TagId) {
$_identifiers += $InputObject.TagId $_identifiers += $AddressObject.TagId
} else {
return $null
}
break
}
"BySubnetObject" {
if ($SubnetObject.TagId) {
$_identifiers += $SubnetObject.TagId
} else { } else {
return $null return $null
} }

View File

@@ -1,5 +1,6 @@
function Get-Vlan { function Get-Vlan {
[CmdletBinding(DefaultParameterSetName="ByID")] [CmdletBinding(DefaultParameterSetName="ByID")]
[OutputType([PS.IPAM.Vlan])]
param ( param (
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
[ValidateScript({ $_ -match "^\d+$" })] [ValidateScript({ $_ -match "^\d+$" })]
@@ -15,31 +16,40 @@ function Get-Vlan {
[ValidateScript({ $_ -match "^\d+$" })] [ValidateScript({ $_ -match "^\d+$" })]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string] [string]
$L2DomainId $L2DomainId,
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")]
[ValidateNotNullOrEmpty()]
[PS.IPAM.Subnetwork]
$SubnetObject
) )
process { process {
[string[]]$visiblePropertiesList = @('Id','Name','Description')
$visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList)
$_params = @{ $_params = @{
Controller = "vlan" Controller = [PS.IPAM.controllers]::vlan
Method = "GET" Method = [PS.IPAM.methods]::GET
Type = [PS.IPAM.types]::Vlan
} }
switch ($PSCmdlet.ParameterSetName) { switch ($PSCmdlet.ParameterSetName) {
"ByID" { $_identifiers = @($Id) } "ByID" { $_identifiers = @($Id); break }
"ByNumber" { $_identifiers = @("search",$Number) } "ByNumber" { $_identifiers = @("search",$Number); break }
"ByL2Domain"{ "ByL2Domain"{
$_params.Item("Controller") = "l2domains" $_params.Item("Controller") = [PS.IPAM.controllers]::l2domains
$_l2domainId = $L2DomainId $_l2domainId = $L2DomainId
$_identifiers = @($_l2domainId,"vlans") $_identifiers = @($_l2domainId,[PS.IPAM.subcontrollers]::vlans)
break
}
"BySubnetObject" {
if ($SubnetObject.VlanId) {
$_identifiers = @($SubnetObject.VlanId); break
} else {
return $null
}
} }
} }
$_params.Add("Identifiers",$_identifiers) $_params.Add("Identifiers",$_identifiers)
Invoke-Request @_params | Select-Object @{n="id";e={$_.vlanId}},* | Select-Object -ExcludeProperty vlanId | ` Invoke-Request @_params
Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru
} }
} }
Export-ModuleMember -Function Get-Vlan Export-ModuleMember -Function Get-Vlan

Binary file not shown.

View File

@@ -1,5 +1,3 @@
using namespace ps.ipam
Get-ChildItem "$(Split-Path $script:MyInvocation.MyCommand.Path)\functions" -Filter "*.ps1" -Recurse | ForEach-Object { Get-ChildItem "$(Split-Path $script:MyInvocation.MyCommand.Path)\functions" -Filter "*.ps1" -Recurse | ForEach-Object {
. $_.FullName . $_.FullName
} }