mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-15 18:55:46 +00:00
Updated BuildNewURI to be compatible with new version of netbox
The way the parameters are done for the API has changed. `Get-NetboxIPAMAddress -Status 'dhcp' -Query 'dns_name__ic=example.org'` Used to return `/api/ipam/ip-addresses/?q=dns_name__ic=example.org&status=dhcp` Now it returns `/api/ipam/ip-addresses/?dns_name__ic=bel082224&status=dhcp`
This commit is contained in:
parent
7275591ac8
commit
00cdedbbab
1 changed files with 56 additions and 47 deletions
|
|
@ -271,80 +271,89 @@ function Add-NetboxVirtualMachineInterface {
|
||||||
|
|
||||||
|
|
||||||
function BuildNewURI {
|
function BuildNewURI {
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Create a new URI for Netbox
|
Create a new URI for Netbox
|
||||||
|
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
Internal function used to build a URIBuilder object.
|
Internal function used to build a URIBuilder object.
|
||||||
|
|
||||||
.PARAMETER Hostname
|
.PARAMETER Hostname
|
||||||
Hostname of the Netbox API
|
Hostname of the Netbox API
|
||||||
|
|
||||||
.PARAMETER Segments
|
.PARAMETER Segments
|
||||||
Array of strings for each segment in the URL path
|
Array of strings for each segment in the URL path
|
||||||
|
|
||||||
.PARAMETER Parameters
|
.PARAMETER Parameters
|
||||||
Hashtable of query parameters to include
|
Hashtable of query parameters to include
|
||||||
|
|
||||||
.PARAMETER HTTPS
|
.PARAMETER HTTPS
|
||||||
Whether to use HTTPS or HTTP
|
Whether to use HTTPS or HTTP
|
||||||
|
|
||||||
.PARAMETER Port
|
.PARAMETER Port
|
||||||
A description of the Port parameter.
|
A description of the Port parameter.
|
||||||
|
|
||||||
.PARAMETER APIInfo
|
.PARAMETER APIInfo
|
||||||
A description of the APIInfo parameter.
|
A description of the APIInfo parameter.
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS C:\> BuildNewURI
|
PS C:\> BuildNewURI
|
||||||
|
|
||||||
.NOTES
|
.NOTES
|
||||||
Additional information about the function.
|
Additional information about the function.
|
||||||
#>
|
#>
|
||||||
|
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
[OutputType([System.UriBuilder])]
|
[OutputType([System.UriBuilder])]
|
||||||
param
|
param
|
||||||
(
|
(
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[string[]]$Segments,
|
[string[]]$Segments,
|
||||||
|
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[hashtable]$Parameters,
|
[hashtable]$Parameters,
|
||||||
|
|
||||||
[switch]$SkipConnectedCheck
|
[switch]$SkipConnectedCheck
|
||||||
)
|
)
|
||||||
|
|
||||||
Write-Verbose "Building URI"
|
Write-Verbose "Building URI"
|
||||||
|
|
||||||
if (-not $SkipConnectedCheck) {
|
if (-not $SkipConnectedCheck) {
|
||||||
# There is no point in continuing if we have not successfully connected to an API
|
# There is no point in continuing if we have not successfully connected to an API
|
||||||
$null = CheckNetboxIsConnected
|
$null = CheckNetboxIsConnected
|
||||||
}
|
}
|
||||||
|
|
||||||
# Begin a URI builder with HTTP/HTTPS and the provided hostname
|
# Begin a URI builder with HTTP/HTTPS and the provided hostname
|
||||||
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
|
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
|
||||||
|
|
||||||
# Generate the path by trimming excess slashes and whitespace from the $segments[] and joining together
|
# Generate the path by trimming excess slashes and whitespace from the $segments[] and joining together
|
||||||
$uriBuilder.Path = "api/{0}/" -f ($Segments.ForEach({
|
$uriBuilder.Path = "api/{0}/" -f ($Segments.ForEach({
|
||||||
$_.trim('/').trim()
|
$_.trim('/').trim()
|
||||||
}) -join '/')
|
}) -join '/')
|
||||||
|
|
||||||
Write-Verbose " URIPath: $($uriBuilder.Path)"
|
Write-Verbose " URIPath: $($uriBuilder.Path)"
|
||||||
|
|
||||||
if ($parameters) {
|
if ($Parameters) {
|
||||||
# Loop through the parameters and use the HttpUtility to create a Query string
|
# Loop through the parameters and use the HttpUtility to create a Query string
|
||||||
[System.Collections.Specialized.NameValueCollection]$URIParams = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
|
[System.Collections.Specialized.NameValueCollection]$URIParams = [System.Web.HttpUtility]::ParseQueryString($uriBuilder.Query)
|
||||||
|
|
||||||
foreach ($param in $Parameters.GetEnumerator()) {
|
foreach ($param in $Parameters.GetEnumerator()) {
|
||||||
Write-Verbose " Adding URI parameter $($param.Key):$($param.Value)"
|
Write-Verbose " Adding URI parameter $($param.Key):$($param.Value)"
|
||||||
$URIParams[$param.Key] = $param.Value
|
if ($param.Key -eq 'q') {
|
||||||
|
# Append the query string directly
|
||||||
|
$uriBuilder.Query = $uriBuilder.Query.TrimStart('?') + '&' + $param.Value
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$URIParams[$param.Key] = $param.Value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$uriBuilder.Query = $URIParams.ToString()
|
# Combine the existing query with new parameters
|
||||||
|
$existingQuery = $uriBuilder.Query.TrimStart('?')
|
||||||
|
$newQuery = $URIParams.ToString()
|
||||||
|
$uriBuilder.Query = ($existingQuery + '&' + $newQuery).Trim('&')
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Verbose " Completed building URIBuilder"
|
Write-Verbose " Completed building URIBuilder"
|
||||||
# Return the entire UriBuilder object
|
# Return the entire UriBuilder object
|
||||||
$uriBuilder
|
$uriBuilder
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue