Initial commit
This commit is contained in:
64
functions/private/Invoke-PSIPAMRequest.ps1
Normal file
64
functions/private/Invoke-PSIPAMRequest.ps1
Normal file
@@ -0,0 +1,64 @@
|
||||
function Invoke-PSIPAMRequest {
|
||||
[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("nameservers")]
|
||||
[string]
|
||||
$SubController,
|
||||
[Parameter(Mandatory=$false)]
|
||||
[ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })]
|
||||
$Params,
|
||||
[Parameter(Mandatory=$false)]
|
||||
[array]
|
||||
$Identifiers
|
||||
)
|
||||
$_tokenStatus = Test-PSIPAMSession
|
||||
|
||||
if ($_tokenStatus -eq "NoToken") { throw "No session available!" }
|
||||
if ($_tokenStatus -eq "Expired") { Update-PSIPAMSession }
|
||||
|
||||
$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"
|
||||
"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") {
|
||||
return $_response.data
|
||||
} else {
|
||||
throw ("Error - $($_response.code)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user