Fixes, added session class
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user