From 5a34f037796522ed3a469817a8a4ca35cdf26577 Mon Sep 17 00:00:00 2001 From: Arnike Date: Mon, 12 Dec 2022 13:08:04 +0300 Subject: [PATCH] Fixes, added session class --- classlib/class/address.cs | 5 +- classlib/class/section.cs | 3 + classlib/class/session.cs | 32 +++++++ classlib/class/vrf.cs | 5 +- classlib/enum/authType.cs | 8 ++ classlib/enum/methods.cs | 10 --- functions/private/Invoke-Request.ps1 | 38 +++++--- functions/private/Test-Session.ps1 | 6 +- functions/private/Update-Session.ps1 | 6 +- functions/public/Get-Address.ps1 | 14 ++- functions/public/Get-FirstFreeIP.ps1 | 5 +- functions/public/Get-L2Domain.ps1 | 12 +-- functions/public/Get-Nameserver.ps1 | 3 +- functions/public/Get-Section.ps1 | 15 ++-- functions/public/Get-Subnet.ps1 | 22 ++--- functions/public/Get-Tag.ps1 | 5 +- functions/public/Get-Vlan.ps1 | 20 +++-- functions/public/Get-Vrf.ps1 | 15 ++-- functions/public/New-Address.ps1 | 14 ++- functions/public/New-Session.ps1 | 33 ++++--- functions/public/Remove-Address.ps1 | 21 ++--- functions/public/Set-Address.ps1 | 126 +++++++-------------------- 22 files changed, 193 insertions(+), 225 deletions(-) create mode 100644 classlib/class/session.cs create mode 100644 classlib/enum/authType.cs delete mode 100644 classlib/enum/methods.cs diff --git a/classlib/class/address.cs b/classlib/class/address.cs index 2546695..1d15998 100644 --- a/classlib/class/address.cs +++ b/classlib/class/address.cs @@ -23,6 +23,7 @@ public class Address { public string FirewallAddressObject { get; } public DateTime? EditDate { get; } public int CustomerId { get; } + public Object? ExtendedData { get; } public Address( int id, @@ -44,7 +45,8 @@ public class Address { int PTR, string firewallAddressObject, DateTime? editDate, - int customer_id + int customer_id, + Object? extendedData ) { this.Id = id; this.SubnetId = subnetId; @@ -66,6 +68,7 @@ public class Address { this.FirewallAddressObject = firewallAddressObject; this.EditDate = editDate; this.CustomerId = customer_id; + this.ExtendedData = extendedData; } public override string ToString() { diff --git a/classlib/class/section.cs b/classlib/class/section.cs index 8f33f8b..c8589a7 100644 --- a/classlib/class/section.cs +++ b/classlib/class/section.cs @@ -12,6 +12,7 @@ public class Section { public string SubnetOrdering { get; } public int Order { get; } public DateTime? EditDate { get; } + public bool ShowSubnet { get; } public bool ShowVlan { get; } public bool ShowVRF { get; } public bool ShowSupernetOnly { get; } @@ -27,6 +28,7 @@ public class Section { string subnetOrdering, int order, DateTime? editDate, + bool showSubnet, bool showVlan, bool showVRF, bool showSupernetOnly, @@ -41,6 +43,7 @@ public class Section { this.SubnetOrdering = subnetOrdering; this.Order = order; this.EditDate = editDate; + this.ShowSubnet = showSubnet; this.ShowVlan = showVlan; this.ShowVRF = showVRF; this.ShowSupernetOnly = showSupernetOnly; diff --git a/classlib/class/session.cs b/classlib/class/session.cs new file mode 100644 index 0000000..774d011 --- /dev/null +++ b/classlib/class/session.cs @@ -0,0 +1,32 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public class Session { + public AuthType AuthType { get; } + public string Token { get; } + public string AppID { get; } + public string URL { get; } + public DateTime? Expires { get; } + public Object? Credentials { get; } + public Session( + AuthType authType, + string token, + string appId, + string url, + DateTime? expires, + Object? credentials + ) { + AuthType = authType; + Token = token; + AppID = appId; + URL = url; + Expires = expires; + Credentials = credentials; + } + + public override string ToString() + { + return base.ToString(); + } +} \ No newline at end of file diff --git a/classlib/class/vrf.cs b/classlib/class/vrf.cs index 39654c3..92c2af9 100644 --- a/classlib/class/vrf.cs +++ b/classlib/class/vrf.cs @@ -9,6 +9,7 @@ public class Vrf { public string Description { get; } public string Sections { get; } public DateTime? EditDate { get; } + public Object? ExtendedData { get; } public Vrf( int id, @@ -16,7 +17,8 @@ public class Vrf { string rd, string description, string sections, - DateTime? editDate + DateTime? editDate, + Object? custom_fields ) { this.Id = id; this.Name = name; @@ -24,6 +26,7 @@ public class Vrf { this.Description = description; this.Sections = sections; this.EditDate = editDate; + this.ExtendedData = custom_fields; } public override string ToString() { diff --git a/classlib/enum/authType.cs b/classlib/enum/authType.cs new file mode 100644 index 0000000..bb513c6 --- /dev/null +++ b/classlib/enum/authType.cs @@ -0,0 +1,8 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public enum AuthType { + credentials, + token +} \ No newline at end of file diff --git a/classlib/enum/methods.cs b/classlib/enum/methods.cs deleted file mode 100644 index 49c8f68..0000000 --- a/classlib/enum/methods.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace PS.IPAM; -using System; - -[Serializable] -public enum methods { - GET, - POST, - PATCH, - DELETE -} \ No newline at end of file diff --git a/functions/private/Invoke-Request.ps1 b/functions/private/Invoke-Request.ps1 index 926ad8a..2a81e66 100644 --- a/functions/private/Invoke-Request.ps1 +++ b/functions/private/Invoke-Request.ps1 @@ -2,7 +2,8 @@ function Invoke-Request { [CmdletBinding()] param ( [parameter(Mandatory=$true)] - [PS.IPAM.methods] + [ValidateSet("GET","POST","PATCH","DELETE")] + [string] $Method, [parameter(Mandatory=$true)] [PS.IPAM.controllers] @@ -27,7 +28,7 @@ function Invoke-Request { $Controller = $Controller - $_uri = "$($script:ipamURL)/api/$($script:ipamAppID)/$Controller" + $_uri = "$($script:psipamSession.URL)/api/$($script:psipamSession.AppID)/$Controller" if ($SubController) { $_uri += "/$SubController" } if ($Identifiers) { $_uri += "/$($Identifiers -join '/')/" } @@ -35,9 +36,9 @@ function Invoke-Request { "Accept" = "application/json" "Content-Type" = "application/json" } - switch ($script:ipamAuthType) { - "Credentials" { $_headers.Add("token", $script:ipamToken) } - "Token" { $_headers.Add("phpipam-token", $script:ipamToken) } + switch ($script:psipamSession.AuthType) { + "Credentials" { $_headers.Add("token", $script:psipamSession.Token) } + "Token" { $_headers.Add("phpipam-token", $script:psipamSession.Token) } } $_arguments = @{ @@ -46,7 +47,7 @@ function Invoke-Request { Headers = $_headers } - if ($Method -eq [PS.IPAM.methods]::POST -or $Method -eq [PS.IPAM.methods]::PATCH) { + if ($Method -eq "POST" -or $Method -eq "PATCH") { if ($Params -is [PSCustomObject]) { $_params = @{}; $Params | Get-Member -MemberType *Property | Where-Object { @@ -57,23 +58,40 @@ function Invoke-Request { $_arguments.Add("Body",($_params | ConvertTo-Json)) } - $_response = Invoke-RestMethod @_arguments + Write-Verbose -Message "Invoking web request to $($_uri), with method $($_arguments.Method), headers: $($_arguments.Headers)" + $_response = Invoke-RestMethod @_arguments + if ($_response.code -match "20\d") { if ($Type -is [PS.IPAM.types]) { switch ($Type) { - "vlan" { + "address" { + $_paramList = @("id","subnetId","ip","is_gateway","description","hostname","mac","owner","tag","deviceId","location","port","note","lastSeen","excludePing","PTRignore","PTR","firewallAddressObject","editDate","customer_id") + $_response.data | ForEach-Object { - New-Object -TypeName ([PS.IPAM.Vlan]) -ArgumentList (@($_.psobject.properties.value[0..6]) + ($_ | Select-Object -Property custom_* -ExcludeProperty custom_*)) + New-Object -TypeName ([PS.IPAM.Address]) -ArgumentList (@(($_ | Select-Object $_paramList).psobject.properties.value) + ($_ | Select-Object -Property custom_* -ExcludeProperty 'custom_\*')) + }; break + } + "vlan" { + $_paramList = @("vlanId","domainId","name","number","description","editDate","customer_id","custom_fields") + + $_response.data | ForEach-Object { + New-Object -TypeName ([PS.IPAM.Vlan]) -ArgumentList (@(($_ | Select-Object $_paramList).psobject.properties.value) + ($_ | 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_*)) + New-Object -TypeName ([PS.IPAM.Subnetwork]) -ArgumentList (@($_.psobject.properties.value[0..30]) + ($_ | Select-Object -Property custom_* -ExcludeProperty 'custom_\*')) + }; break + } + "vrf" { + $_response.data | ForEach-Object { + New-Object -TypeName ([PS.IPAM.Vrf]) -ArgumentList (@($_.psobject.properties.value[0..5]) + ($_ | Select-Object -Property custom_* -ExcludeProperty 'custom_\*')) }; break } Default { $_response.data | ForEach-Object { New-Object -TypeName ("PS.IPAM.$Type") -ArgumentList $_.psobject.properties.value } } } + } else { return $_response.data } diff --git a/functions/private/Test-Session.ps1 b/functions/private/Test-Session.ps1 index a1ab7c8..1558a55 100644 --- a/functions/private/Test-Session.ps1 +++ b/functions/private/Test-Session.ps1 @@ -3,11 +3,11 @@ function Test-Session { param ( ) - if ($script:ipamToken) { - if ($script:ipamExpires -eq "Never") { + if ($script:psipamSession) { + if ($null -eq $script:psipamSession.Expires) { return "Valid" } else { - if ($script:ipamExpires -lt (Get-Date)) { + if ($script:psipamSession.Expires -lt (Get-Date)) { return "Expired" } else { return "Valid" diff --git a/functions/private/Update-Session.ps1 b/functions/private/Update-Session.ps1 index e583084..245e35b 100644 --- a/functions/private/Update-Session.ps1 +++ b/functions/private/Update-Session.ps1 @@ -9,10 +9,10 @@ function Update-Session { throw "No session available!" } if ($_tokenStatus -eq "Valid") { - return (Invoke-Request -Method PATCH -Controller user).expires + return (Invoke-Request -Method [ps.ipam.methods]::PATCH -Controller [ps.ipam.controllers]::user).expires } if ($_tokenStatus -eq "Expired" -or $Force) { - New-Session -URL $script:ipamURL -AppID $script:ipamAppID -Credentials $script:ipamCredentials - return $script:ipamExpires + New-Session -URL $script:psipamSession.URL -AppID $script:psipamSession.AppID -Credentials $script:psipamSession.Credentials + return } } \ No newline at end of file diff --git a/functions/public/Get-Address.ps1 b/functions/public/Get-Address.ps1 index 69f9bbe..57c32cf 100644 --- a/functions/public/Get-Address.ps1 +++ b/functions/public/Get-Address.ps1 @@ -3,29 +3,25 @@ function Get-Address { [OutputType([PS.IPAM.address])] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByIP")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1,ParameterSetName="BySubnetId")] - [ValidateScript({[ipaddress] $_})] [ValidateNotNullOrEmpty()] - [string] + [ipaddress] $IP, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByHostName")] [ValidateNotNullOrEmpty()] [string] $HostName, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByTag")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $TagId, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetId")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $SubnetId, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetCIDR")] [ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})] @@ -36,7 +32,7 @@ function Get-Address { process { $_params = @{ Controller = [PS.IPAM.controllers]::addresses - Method = [PS.IPAM.methods]::GET + Method = "GET" Type = [PS.IPAM.types]::address } switch ($PSCmdlet.ParameterSetName) { diff --git a/functions/public/Get-FirstFreeIP.ps1 b/functions/public/Get-FirstFreeIP.ps1 index c661ae8..daba78f 100644 --- a/functions/public/Get-FirstFreeIP.ps1 +++ b/functions/public/Get-FirstFreeIP.ps1 @@ -7,9 +7,8 @@ function Get-FirstFreeIP { [string] $CIDR, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id, [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")] [ValidateNotNullOrEmpty()] @@ -19,7 +18,7 @@ function Get-FirstFreeIP { process { $_params = @{ Controller = [PS.IPAM.controllers]::subnets - Method = [PS.IPAM.methods]::GET + Method = "GET" } switch ($PSCmdlet.ParameterSetName) { "ByID" { $_subnetId = $Id; break } diff --git a/functions/public/Get-L2Domain.ps1 b/functions/public/Get-L2Domain.ps1 index 45bfc3b..d97694b 100644 --- a/functions/public/Get-L2Domain.ps1 +++ b/functions/public/Get-L2Domain.ps1 @@ -1,9 +1,8 @@ function Get-L2Domain { - [CmdletBinding(DefaultParameterSetName="NoParams")] + [CmdletBinding(DefaultParameterSetName="ByID")] [OutputType([PS.IPAM.Domain])] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] [int] $Id @@ -11,16 +10,11 @@ function Get-L2Domain { process { $_params = @{ Controller = [PS.IPAM.controllers]::l2domains - Method = [PS.IPAM.methods]::GET + Method = "GET" Type = [PS.IPAM.types]::Domain } - switch ($PSCmdlet.ParameterSetName) { - "ByID" { - $_identifiers = @($Id) - } - } - + $_identifiers = @($Id) $_params.Add("Identifiers",$_identifiers) Invoke-Request @_params diff --git a/functions/public/Get-Nameserver.ps1 b/functions/public/Get-Nameserver.ps1 index f487108..6e798fa 100644 --- a/functions/public/Get-Nameserver.ps1 +++ b/functions/public/Get-Nameserver.ps1 @@ -2,9 +2,8 @@ function Get-Nameserver { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id ) process { diff --git a/functions/public/Get-Section.ps1 b/functions/public/Get-Section.ps1 index 30caca2..f866064 100644 --- a/functions/public/Get-Section.ps1 +++ b/functions/public/Get-Section.ps1 @@ -1,10 +1,10 @@ function Get-Section { - [CmdletBinding(DefaultParameterSetName="ByID")] + [CmdletBinding(DefaultParameterSetName="NoParams")] + [OutputType([PS.IPAM.Section])] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id, [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,ParameterSetName="ByName")] [ValidateNotNullOrEmpty()] @@ -12,12 +12,10 @@ function Get-Section { $Name ) process { - [string[]]$visiblePropertiesList = @('Id','Name','Description') - $visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList) - $_params = @{ - Controller = "sections" + Controller = [PS.IPAM.controllers]::sections Method = "GET" + Type = [PS.IPAM.types]::Section } switch ($PSCmdlet.ParameterSetName) { "ByID" { $_identifiers = @($Id) } @@ -25,8 +23,7 @@ function Get-Section { } $_params.Add("Identifiers",$_identifiers) - Invoke-Request @_params | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru + Invoke-Request @_params } } Export-ModuleMember Get-Section \ No newline at end of file diff --git a/functions/public/Get-Subnet.ps1 b/functions/public/Get-Subnet.ps1 index db4f155..71cbdb8 100644 --- a/functions/public/Get-Subnet.ps1 +++ b/functions/public/Get-Subnet.ps1 @@ -8,16 +8,14 @@ function Get-Subnet { [string] $CIDR, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySectionId")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="ByVlanNumber")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1,ParameterSetName="ByVlanId")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $SectionId, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySectionName")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=3,ParameterSetName="ByVlanNumber")] @@ -26,25 +24,21 @@ function Get-Subnet { [string] $SectionName, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByVrfId")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $VrfId, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByVlanId")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $VlanId, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByVlanNumber")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $VlanNumber, [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1,ParameterSetName="ByVlanNumber")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] - $VlanDomain, + [int] + $VlanDomainId, [parameter(Mandatory=$false,ParameterSetName="ByID")] [switch] $Slaves, @@ -55,7 +49,7 @@ function Get-Subnet { process { $_params = @{ Controller = [PS.IPAM.controllers]::subnets - Method = [PS.IPAM.methods]::GET + Method = "GET" Type = [PS.IPAM.types]::Subnetwork } switch ($PSCmdlet.ParameterSetName) { diff --git a/functions/public/Get-Tag.ps1 b/functions/public/Get-Tag.ps1 index 9936ce7..606131c 100644 --- a/functions/public/Get-Tag.ps1 +++ b/functions/public/Get-Tag.ps1 @@ -3,9 +3,8 @@ function Get-Tag { [OutputType([PS.IPAM.Tag])] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id, [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")] [ValidateNotNullOrEmpty()] @@ -19,7 +18,7 @@ function Get-Tag { process { $_params = @{ Controller = [PS.IPAM.controllers]::addresses - Method = [PS.IPAM.methods]::GET + Method = "GET" Type = [PS.IPAM.types]::tag } $_identifiers = @("tags") diff --git a/functions/public/Get-Vlan.ps1 b/functions/public/Get-Vlan.ps1 index 55e8d07..a6aa22f 100644 --- a/functions/public/Get-Vlan.ps1 +++ b/functions/public/Get-Vlan.ps1 @@ -3,29 +3,30 @@ function Get-Vlan { [OutputType([PS.IPAM.Vlan])] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByNumber")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Number, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByL2Domain")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $L2DomainId, [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")] [ValidateNotNullOrEmpty()] [PS.IPAM.Subnetwork] - $SubnetObject + $SubnetObject, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByDomainObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Domain] + $DomainObject ) process { $_params = @{ Controller = [PS.IPAM.controllers]::vlan - Method = [PS.IPAM.methods]::GET + Method = "GET" Type = [PS.IPAM.types]::Vlan } switch ($PSCmdlet.ParameterSetName) { @@ -46,6 +47,9 @@ function Get-Vlan { return $null } } + "DomainObject" { + Get-Vlan -L2DomainId $DomainObject.Id + } } $_params.Add("Identifiers",$_identifiers) diff --git a/functions/public/Get-Vrf.ps1 b/functions/public/Get-Vrf.ps1 index 7ab658b..9ff2c78 100644 --- a/functions/public/Get-Vrf.ps1 +++ b/functions/public/Get-Vrf.ps1 @@ -1,26 +1,23 @@ function Get-Vrf { [CmdletBinding(DefaultParameterSetName="ByID")] + [OutputType([PS.IPAM.Vrf])] param ( [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 = "vrf" + Controller = [PS.IPAM.controllers]::vrf Method = "GET" + Type = [PS.IPAM.types]::vrf } if ($Id) { $_identifiers = @($Id) } - $_params.Add("Identifiers",$_identifiers) + $_params.Add("Identifiers",$_identifiers) - Invoke-Request @_params | Select-Object @{n="id";e={$_.vrfId}},* | Select-Object -ExcludeProperty vrfId | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru + Invoke-Request @_params } } Export-ModuleMember -Function Get-Vrf \ No newline at end of file diff --git a/functions/public/New-Address.ps1 b/functions/public/New-Address.ps1 index b805b86..36069af 100644 --- a/functions/public/New-Address.ps1 +++ b/functions/public/New-Address.ps1 @@ -7,9 +7,8 @@ function New-Address { ValueFromPipelineByPropertyName=$true, HelpMessage="Id of subnet address belongs to", Position=0)] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $SubnetId, [parameter( Mandatory=$true, @@ -72,9 +71,8 @@ function New-Address { ValueFromPipelineByPropertyName=$true, HelpMessage="Id of subnet address belongs to", Position=7)] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $TagId, [parameter( Mandatory=$false, @@ -90,9 +88,8 @@ function New-Address { ValueFromPipelineByPropertyName=$true, HelpMessage="Id of PowerDNS PTR record", Position=7)] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $PTRId, [parameter( Mandatory=$false, @@ -117,9 +114,8 @@ function New-Address { ValueFromPipelineByPropertyName=$true, HelpMessage="Id of device address belongs to", Position=12)] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $DeviceId, [parameter( Mandatory=$false, @@ -137,7 +133,7 @@ function New-Address { ) process { $_params = @{ - Controller = "addresses" + Controller = [PS.IPAM.controllers]::addresses Method = "POST" } diff --git a/functions/public/New-Session.ps1 b/functions/public/New-Session.ps1 index 1757310..73fb82b 100644 --- a/functions/public/New-Session.ps1 +++ b/functions/public/New-Session.ps1 @@ -29,25 +29,30 @@ function New-Session { "Authorization" = "Basic $_auth" } - $_response = Invoke-RestMethod -Method Post -Uri $_uri -Headers $_headers -ErrorAction SilentlyContinue + $_response = Invoke-RestMethod -Method POST -Uri $_uri -Headers $_headers if ($_response.success -ne $true) { return $_response.error } - $script:ipamAuthType = "Credentials" - $script:ipamAuth = $true - $script:ipamToken = $_response.data.token - $script:ipamAppID = $AppID - $script:ipamURL = $URL - $script:ipamCredentials = $Credentials - $script:ipamExpires = Get-Date $_response.data.expires + $script:psipamSession = [PS.IPAM.Session]::new( + [ps.ipam.authType]::credentials, + $_response.data.token, + $AppID, + $URL, + (Get-Date $_response.data.expires), + $Credentials + ) + break } "Token" { - $script:ipamAuthType = "Token" - $script:ipamAuth = $true - $script:ipamToken = $Token - $script:ipamAppID = $AppID - $script:ipamURL = $URL - $script:ipamExpires = "Never" + $script:psipamSession = [PS.IPAM.Session]::new( + [ps.ipam.authType]::token, + $Token, + $AppID, + $URL, + $null, + $null + ) + break } } } diff --git a/functions/public/Remove-Address.ps1 b/functions/public/Remove-Address.ps1 index fba0892..536d14d 100644 --- a/functions/public/Remove-Address.ps1 +++ b/functions/public/Remove-Address.ps1 @@ -2,30 +2,23 @@ function Remove-Address { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id, - [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,ParameterSetName="ByIP")] - [ValidateScript({[ipaddress] $_ })] + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")] [ValidateNotNullOrEmpty()] - [string] - $IP, - [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=1,ParameterSetName="ByIP")] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $SubnetId + [PS.IPAM.Address] + $AddressObject ) process { $_params = @{ - Controller = "addresses" + Controller = [PS.IPAM.controllers]::addresses Method = "DELETE" } switch ($PSCmdlet.ParameterSetName) { - "ByID" { $_identifiers = @($Id) } - "ByIP" { $_identifiers = @($IP,$SubnetId) } + "ByID" { $_identifiers = @($Id);break } + "ByAddressObject" { $_identifiers = @($AddressObject.Id);break } } $_params.Add("Identifiers",$_identifiers) diff --git a/functions/public/Set-Address.ps1 b/functions/public/Set-Address.ps1 index a9c0693..934de3d 100644 --- a/functions/public/Set-Address.ps1 +++ b/functions/public/Set-Address.ps1 @@ -1,127 +1,61 @@ function Set-Address { - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName="ById")] param ( - [parameter( - Mandatory=$true, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Id of subnet address belongs to", - Position=0)] - [ValidateScript({ $_ -match "^\d+$" })] + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,HelpMessage="Id of subnet address belongs to",Position=0,ParameterSetName="ById")] [ValidateNotNullOrEmpty()] - [string] + [int] $Id, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Defines if address is presented as gateway", - Position=1)] + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Address] + $AddressObject, + [parameter(Mandatory=$false,HelpMessage="Defines if address is presented as gateway",Position=1)] [bool] $Gateway, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Address description", - Position=2)] + [parameter(Mandatory=$false,HelpMessage="Address description",Position=2)] [ValidateNotNullOrEmpty()] [string] $Description, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Address hostname", - Position=3)] + [parameter(Mandatory=$false,HelpMessage="Address hostname",Position=3)] [ValidateNotNullOrEmpty()] [string] $Hostname, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Mac address", - Position=4)] + [parameter(Mandatory=$false,HelpMessage="Mac address",Position=4)] [ValidateScript({ $_.Replace(":","") -match "^$('([A-F0-9]{2})' * 6)$" })] [ValidateNotNullOrEmpty()] [string] $MAC, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Address owner", - Position=5)] + [parameter(Mandatory=$false,HelpMessage="Address owner",Position=5)] [ValidateNotNullOrEmpty()] [string] $Owner, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Id of subnet address belongs to", - Position=6)] - [ValidateScript({ $_ -match "^\d+$" })] + [parameter(Mandatory=$false,HelpMessage="Id of subnet address belongs to",Position=6)] [ValidateNotNullOrEmpty()] - [string] + [int] $TagId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Controls if PTR should not be created", - Position=7)] + [parameter(Mandatory=$false,HelpMessage="Controls if PTR should not be created",Position=7)] [bool] $PTRIgnore, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Id of PowerDNS PTR record", - Position=8)] - [ValidateScript({ $_ -match "^\d+$" })] + [parameter(Mandatory=$false,HelpMessage="Id of PowerDNS PTR record",Position=8)] [ValidateNotNullOrEmpty()] - [string] + [int] $PTRId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Note", - Position=9)] + [parameter(Mandatory=$false,HelpMessage="Note",Position=9)] [ValidateNotNullOrEmpty()] [string] $Note, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Exclude this address from status update scans (ping)", - Position=10)] + [parameter(Mandatory=$false,HelpMessage="Exclude this address from status update scans (ping)",Position=10)] [bool] $ExcludePing, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Id of device address belongs to", - Position=11)] - [ValidateScript({ $_ -match "^\d+$" })] + [parameter(Mandatory=$false,HelpMessage="Id of device address belongs to",Position=11)] [ValidateNotNullOrEmpty()] - [string] + [int] $DeviceId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Port", - Position=12)] + [parameter(Mandatory=$false,HelpMessage="Port",Position=12)] [ValidateNotNullOrEmpty()] [string] $Port, - - [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] + [parameter(Mandatory=$false)] [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] $CustomFields ) @@ -130,21 +64,25 @@ function Set-Address { Controller = "addresses" Method = "PATCH" } - $_identifiers = @($Id) + switch ($PSCmdlet.ParameterSetName) { + "ByID" { $_id = $Id;break } + "ByAddressObject" { $_id = $AddressObject.id;break } + } + $_identifiers = @($_id) $_params.Add("Identifiers",$_identifiers) $_body = @{ } - if ($Gateway) { $_body.Add("is_gateway", [int]$Gateway) } + if ($Gateway) { $_body.Add("is_gateway", $Gateway) } if ($Description) { $_body.Add("description", $Description) } if ($Hostname) { $_body.Add("hostname", $Hostname) } if ($MAC) { $_body.Add("mac", $MAC) } if ($Owner) { $_body.Add("owner", $Owner) } if ($TagId) { $_body.Add("tag", $TagId) } - if ($PTRIgnore) { $_body.Add("PTRignore", [int]$PTRIgnore) } + if ($PTRIgnore) { $_body.Add("PTRignore", $PTRIgnore) } if ($PTRId) { $_body.add("PTR", $PTRId)} if ($Note) { $_body.Add("note", $Note) } - if ($ExcludePing) { $_body.Add("excludePing", [int]$ExcludePing) } + if ($ExcludePing) { $_body.Add("excludePing", $ExcludePing) } if ($DeviceId) { $_body.Add("deviceId", $DeviceId) } if ($Port) { $_body.Add("port", $Port) } @@ -165,7 +103,7 @@ function Set-Address { Invoke-Request @_params } finally { - Get-Address -Id $Id + Get-Address -Id $_id } } }