From 7d0d1b53ae3875658b3501d39ed71f3824f5a14e Mon Sep 17 00:00:00 2001 From: Arnike Date: Tue, 6 Dec 2022 17:20:45 +0300 Subject: [PATCH] first working types --- classlib/class/subnet.cs | 15 +++++++++--- classlib/class/tag.cs | 1 + classlib/class/vlan.cs | 15 ++++++++---- classlib/enum/methods.cs | 1 + classlib/enum/types.cs | 15 ++++++------ functions/private/Invoke-Request.ps1 | 30 ++++++++++++++++------- functions/public/Get-Address.ps1 | 18 +++++++------- functions/public/Get-FirstFreeIP.ps1 | 14 +++++++---- functions/public/Get-L2Domain.ps1 | 25 +++++++++++--------- functions/public/Get-Subnet.ps1 | 12 ++++------ functions/public/Get-Tag.ps1 | 22 +++++++++++------ functions/public/Get-Vlan.ps1 | 34 +++++++++++++++++---------- ps.ipam.psd1 | Bin 3336 -> 3352 bytes ps.ipam.psm1 | 2 -- 14 files changed, 129 insertions(+), 75 deletions(-) diff --git a/classlib/class/subnet.cs b/classlib/class/subnet.cs index 809d00f..45cdf2a 100644 --- a/classlib/class/subnet.cs +++ b/classlib/class/subnet.cs @@ -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() { diff --git a/classlib/class/tag.cs b/classlib/class/tag.cs index 9030726..ddc94e5 100644 --- a/classlib/class/tag.cs +++ b/classlib/class/tag.cs @@ -1,4 +1,5 @@ namespace PS.IPAM; +using System; [Serializable] public class Tag { diff --git a/classlib/class/vlan.cs b/classlib/class/vlan.cs index 2e7cff1..77ddb50 100644 --- a/classlib/class/vlan.cs +++ b/classlib/class/vlan.cs @@ -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() diff --git a/classlib/enum/methods.cs b/classlib/enum/methods.cs index 4cdee6b..49c8f68 100644 --- a/classlib/enum/methods.cs +++ b/classlib/enum/methods.cs @@ -1,6 +1,7 @@ namespace PS.IPAM; using System; +[Serializable] public enum methods { GET, POST, diff --git a/classlib/enum/types.cs b/classlib/enum/types.cs index b438a03..1dfaba1 100644 --- a/classlib/enum/types.cs +++ b/classlib/enum/types.cs @@ -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 } \ No newline at end of file diff --git a/functions/private/Invoke-Request.ps1 b/functions/private/Invoke-Request.ps1 index 1a7dff9..926ad8a 100644 --- a/functions/private/Invoke-Request.ps1 +++ b/functions/private/Invoke-Request.ps1 @@ -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)") diff --git a/functions/public/Get-Address.ps1 b/functions/public/Get-Address.ps1 index 6c4610c..69f9bbe 100644 --- a/functions/public/Get-Address.ps1 +++ b/functions/public/Get-Address.ps1 @@ -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) diff --git a/functions/public/Get-FirstFreeIP.ps1 b/functions/public/Get-FirstFreeIP.ps1 index c5702d8..c661ae8 100644 --- a/functions/public/Get-FirstFreeIP.ps1 +++ b/functions/public/Get-FirstFreeIP.ps1 @@ -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) diff --git a/functions/public/Get-L2Domain.ps1 b/functions/public/Get-L2Domain.ps1 index d61bf48..45bfc3b 100644 --- a/functions/public/Get-L2Domain.ps1 +++ b/functions/public/Get-L2Domain.ps1 @@ -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 } - $_identifiers = @($Id) + 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 \ No newline at end of file diff --git a/functions/public/Get-Subnet.ps1 b/functions/public/Get-Subnet.ps1 index 205a749..db4f155 100644 --- a/functions/public/Get-Subnet.ps1 +++ b/functions/public/Get-Subnet.ps1 @@ -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" { diff --git a/functions/public/Get-Tag.ps1 b/functions/public/Get-Tag.ps1 index 393bc4e..9936ce7 100644 --- a/functions/public/Get-Tag.ps1 +++ b/functions/public/Get-Tag.ps1 @@ -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 } diff --git a/functions/public/Get-Vlan.ps1 b/functions/public/Get-Vlan.ps1 index 10565a0..55e8d07 100644 --- a/functions/public/Get-Vlan.ps1 +++ b/functions/public/Get-Vlan.ps1 @@ -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 \ No newline at end of file diff --git a/ps.ipam.psd1 b/ps.ipam.psd1 index a08729afadeaf464337b2063dc92f40edfd0d691..456cb9fd0f96d623b4003ac0b0bc7e0ff3498115 100644 GIT binary patch delta 26 hcmeB>njy7e9y4zNLn%WNLk>eGLoq|f=Gn}-oB(L(2XO!Z delta 12 TcmbOs)giTE9`ojP%vqcOAB6;c diff --git a/ps.ipam.psm1 b/ps.ipam.psm1 index 1b25c40..89aad14 100644 --- a/ps.ipam.psm1 +++ b/ps.ipam.psm1 @@ -1,5 +1,3 @@ -using namespace ps.ipam - Get-ChildItem "$(Split-Path $script:MyInvocation.MyCommand.Path)\functions" -Filter "*.ps1" -Recurse | ForEach-Object { . $_.FullName }