Base
This commit is contained in:
76
functions/private/Invoke-Request.ps1
Normal file
76
functions/private/Invoke-Request.ps1
Normal file
@@ -0,0 +1,76 @@
|
||||
function Invoke-Request {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[parameter(Mandatory=$true)]
|
||||
[ValidateSet("POST","GET","PATCH","DELETE")]
|
||||
[string]
|
||||
$Method,
|
||||
[parameter(Mandatory=$true)]
|
||||
[ValidateSet("user","vlan","subnets","addresses","sections","vrf","l2domains","tools")]
|
||||
[string]
|
||||
$Controller,
|
||||
[parameter(Mandatory=$false)]
|
||||
[ValidateSet("address","tag","subnet")]
|
||||
[string]
|
||||
$Type,
|
||||
[parameter(Mandatory=$false)]
|
||||
[ValidateSet("nameservers")]
|
||||
[string]
|
||||
$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.ToLower()
|
||||
|
||||
$_uri = "$($script:ipamURL)/api/$($script:ipamAppID)/$Controller"
|
||||
if ($SubController) { $_uri += "/$SubController" }
|
||||
if ($Identifiers) { $_uri += "/$($Identifiers -join '/')/" }
|
||||
|
||||
$_headers = @{
|
||||
"Accept" = "application/json"
|
||||
"Content-Type" = "application/json"
|
||||
}
|
||||
switch ($script:ipamAuthType) {
|
||||
"Credentials" { $_headers.Add("token", $script:ipamToken) }
|
||||
"Token" { $_headers.Add("phpipam-token", $script:ipamToken) }
|
||||
}
|
||||
|
||||
$_arguments = @{
|
||||
Method = $Method
|
||||
Uri = $_uri
|
||||
Headers = $_headers
|
||||
}
|
||||
|
||||
if ($Method -match "POST|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))
|
||||
}
|
||||
|
||||
$_response = Invoke-RestMethod @_arguments
|
||||
|
||||
if ($_response.code -match "20\d") {
|
||||
switch ($Type) {
|
||||
"address" { $_response.data | ForEach-Object { New-Object -TypeName "PS.IPAM.Address" -ArgumentList $_.psobject.properties.value } }
|
||||
"subnet" { $_response.data | ForEach-Object { New-Object -TypeName "PS.IPAM.Subnetwork" -ArgumentList $_.psobject.properties.value } }
|
||||
"tag" { $_response.data | ForEach-Object { New-Object -TypeName "PS.IPAM.Tag" -ArgumentList $_.psobject.properties.value } }
|
||||
Default { $_response.data }
|
||||
}
|
||||
} else {
|
||||
throw ("Error - $($_response.code)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user