Fixed subcontrollers

This commit is contained in:
2022-12-14 15:26:43 +03:00
parent 9912656752
commit 6fb2e81142
13 changed files with 191 additions and 111 deletions

View File

@@ -26,11 +26,9 @@ function Invoke-Request {
if ($_tokenStatus -eq "NoToken") { throw "No session available!" }
if ($_tokenStatus -eq "Expired") { Update-Session }
$Controller = $Controller
$_uri = "$($script:psipamSession.URL)/api/$($script:psipamSession.AppID)/$Controller"
if ($SubController) { $_uri += "/$SubController" }
if ($Identifiers) { $_uri += "/$($Identifiers -join '/')/" }
if ($SubController -ne $null) { $SubController;$_uri += "/$SubController" }
if ($Identifiers -ne $null) { $_uri += "/$($Identifiers -join '/')/" }
$_headers = @{
"Accept" = "application/json"
@@ -60,42 +58,48 @@ function Invoke-Request {
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) {
"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.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_\*'))
}; 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
try {
$_response = Invoke-RestMethod @_arguments
}
catch [System.Net.WebException] {
switch ($_.Exception.Message) {
"The remote server returned an error: (404) Not Found." { Write-Verbose -Message "Not found." }
"The remote server returned an error: (400) Bad Request." { Write-Warning -Message "Bad Request." }
Default { Write-Verbose -Message "Error occured while requesting api" }
}
}
if (!$_response) { return $null }
if ($Type -is [PS.IPAM.types]) {
switch ($Type) {
"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.Address]) -ArgumentList (@(($_ | Select-Object $_paramList).psobject.properties.value) + ($_ | Select-Object -Property custom_* -ExcludeProperty 'custom_\*'))
}
}
"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_\*'))
}
}
"subnetwork" {
$_response.data | ForEach-Object {
New-Object -TypeName ([PS.IPAM.Subnetwork]) -ArgumentList (@($_.psobject.properties.value[0..30]) + ($_ | Select-Object -Property custom_* -ExcludeProperty 'custom_\*'))
}
}
"vrf" {
$_response.data | ForEach-Object {
New-Object -TypeName ([PS.IPAM.Vrf]) -ArgumentList (@($_.psobject.properties.value[0..5]) + ($_ | Select-Object -Property custom_* -ExcludeProperty 'custom_\*'))
}
}
Default { $_response.data | ForEach-Object { New-Object -TypeName ("PS.IPAM.$Type") -ArgumentList $_.psobject.properties.value } }
}
} else {
throw ("Error - $($_response.code)")
$_response.data
}
}