NetboxPS/Functions/Helpers/InvokeNetboxRequest.ps1
Ben Claussen cfb53cf933
Version 1.3.0 (#3)
* Add `OPTIONS` method validation

* Remove parameter validation as workaround for CHOICES API endpoint until further testing

* Add help block for Get-NetboxTenant

* SkipConnectedCheck for Get-NetboxAPIDefinition

* Correct help block for New-NetboxIPAMAddress

* Add parameter position 0 for 'Address' in Get-NetboxIPAMAddress

* Allow pipeline input for Address parameter in New-NetboxIPAMAddress

* Update parameter types

* Add parameter sets and logic for ID/Query searches

* Add Get-NetboxDCIMSite

* Update psproj

* Update deploy.ps1

* Move Get-NetboxCircuit

* Add Circuit cmdlets
- New-NetboxCircuit
- Get-NetboxCircuitProvider
- Get-NetboxCircuitTermination
- Get-NetboxCircuitType

* Update deploy script output path

* Update Set-NetboxIPAMAddress
- Replace Interface parameter with Assigned_Object_Type and Assigned_Object_Id
- Add validation logic for Assigned_Object_ parameters
- Change Status parameter to string

* Add Get-ModelDefinition function

* Update psproj

* Update deploy.ps1 variables

* Update exported files

* Remove references to `_choices` API calls

* Add Postman collection

* Add Postman collection

* Update deploy.ps1

* Add Set-NetboxIPAMPrefix function

* Increment version to 1.3.0

Co-authored-by: Ben Claussen <claussen@neonet.org>
2021-03-25 16:52:08 -04:00

105 lines
No EOL
3.1 KiB
PowerShell

<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.172
Created on: 3/26/2020 14:24
Created by: Claussen
Organization: NEOnet
Filename: InvokeNetboxRequest.ps1
===========================================================================
.DESCRIPTION
A description of the file.
#>
function InvokeNetboxRequest {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.UriBuilder]$URI,
[Hashtable]$Headers = @{
},
[pscustomobject]$Body = $null,
[ValidateRange(0, 60)]
[uint16]$Timeout = 5,
[ValidateSet('GET', 'PATCH', 'PUT', 'POST', 'DELETE', 'OPTIONS', IgnoreCase = $true)]
[string]$Method = 'GET',
[switch]$Raw
)
$creds = Get-NetboxCredential
$Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password
$splat = @{
'Method' = $Method
'Uri' = $URI.Uri.AbsoluteUri # This property auto generates the scheme, hostname, path, and query
'Headers' = $Headers
'TimeoutSec' = $Timeout
'ContentType' = 'application/json'
'ErrorAction' = 'Stop'
'Verbose' = $VerbosePreference
}
if ($Body) {
Write-Verbose "BODY: $($Body | ConvertTo-Json -Compress)"
$null = $splat.Add('Body', ($Body | ConvertTo-Json -Compress))
}
$result = Invoke-RestMethod @splat
#region TODO: Handle errors a little more gracefully...
<#
try {
Write-Verbose "Sending request..."
$result = Invoke-RestMethod @splat
Write-Verbose $result
} catch {
Write-Verbose "Caught exception"
if ($_.Exception.psobject.properties.Name.contains('Response')) {
Write-Verbose "Exception contains a response property"
if ($Raw) {
Write-Verbose "RAW provided...throwing raw exception"
throw $_
}
Write-Verbose "Converting response to object"
$myError = GetNetboxAPIErrorBody -Response $_.Exception.Response | ConvertFrom-Json
} else {
Write-Verbose "No response property found"
$myError = $_
}
}
Write-Verbose "MyError is $($myError.GetType().FullName)"
if ($myError -is [Exception]) {
throw $_
} elseif ($myError -is [pscustomobject]) {
throw $myError.detail
}
#>
#endregion TODO: Handle errors a little more gracefully...
# If the user wants the raw value from the API... otherwise return only the actual result
if ($Raw) {
Write-Verbose "Returning raw result by choice"
return $result
} else {
if ($result.psobject.Properties.Name.Contains('results')) {
Write-Verbose "Found Results property on data, returning results directly"
return $result.Results
} else {
Write-Verbose "Did NOT find results property on data, returning raw result"
return $result
}
}
}