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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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