101 lines
4.1 KiB
PowerShell
101 lines
4.1 KiB
PowerShell
function Invoke-Request {
|
|
[CmdletBinding()]
|
|
param (
|
|
[parameter(Mandatory=$true)]
|
|
[ValidateSet("GET","POST","PATCH","DELETE")]
|
|
[string]
|
|
$Method,
|
|
[parameter(Mandatory=$true)]
|
|
[PS.IPAM.controllers]
|
|
$Controller,
|
|
[parameter(Mandatory=$false)]
|
|
[PS.IPAM.types]
|
|
$Type,
|
|
[parameter(Mandatory=$false)]
|
|
[PS.IPAM.subcontrollers]
|
|
$SubController,
|
|
[Parameter(Mandatory=$false)]
|
|
[ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })]
|
|
$Params,
|
|
[Parameter(Mandatory=$false)]
|
|
[array]
|
|
$Identifiers
|
|
)
|
|
$_tokenStatus = Test-Session
|
|
|
|
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 '/')/" }
|
|
|
|
$_headers = @{
|
|
"Accept" = "application/json"
|
|
"Content-Type" = "application/json"
|
|
}
|
|
switch ($script:psipamSession.AuthType) {
|
|
"Credentials" { $_headers.Add("token", $script:psipamSession.Token) }
|
|
"Token" { $_headers.Add("phpipam-token", $script:psipamSession.Token) }
|
|
}
|
|
|
|
$_arguments = @{
|
|
Method = $Method
|
|
Uri = $_uri
|
|
Headers = $_headers
|
|
}
|
|
|
|
if ($Method -eq "POST" -or $Method -eq "PATCH") {
|
|
if ($Params -is [PSCustomObject]) {
|
|
$_params = @{};
|
|
$Params | Get-Member -MemberType *Property | Where-Object {
|
|
$_params.($_.name) = $Params.($_.name)
|
|
}
|
|
} else { $_params = $Params }
|
|
|
|
$_arguments.Add("Body",($_params | ConvertTo-Json))
|
|
}
|
|
|
|
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
|
|
}
|
|
} else {
|
|
throw ("Error - $($_response.code)")
|
|
}
|
|
} |