Fixes, added session class

This commit is contained in:
2022-12-12 13:08:04 +03:00
parent 7d0d1b53ae
commit 5a34f03779
22 changed files with 193 additions and 225 deletions

View File

@@ -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
}