feature: Add ability to define a custom URLPath

This commit is contained in:
Chris Lawson 2023-03-22 17:20:22 +08:00
parent c736e0a1f7
commit 2e131d45d2
7 changed files with 114 additions and 38 deletions

View file

@ -52,11 +52,17 @@ function BuildNewURI {
$null = CheckNetboxIsConnected
}
# Begin a URI builder with HTTP/HTTPS and the provided hostname
# Begin a URI builder with HTTP/HTTPS and the provided hostname, and url path if required
if (-not $script:NetboxConfig.URLPath) {
throw "Netbox Credentials not set! You may set with Set-NetboxCredential"
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
} else {
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort, "/$($script:NetboxConfig.URLPath.trim('/'))")
}
# 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()
}) -join '/')

View file

@ -112,6 +112,7 @@
$null = Set-NetboxCredential -Credential $Credential
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
$null = Set-NetboxHostPort -Port $uriBuilder.Port
$null = Set-NetboxURLPath -Path $uriBuilder.Path
$null = Set-NetboxInvokeParams -invokeParams $invokeParams
$null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds

View file

@ -0,0 +1,11 @@
function Get-NetboxURLPath {
[CmdletBinding()]
param ()
Write-Verbose "Getting Netbox URL Path"
if ($null -eq $script:NetboxConfig.URLPath) {
throw "Netbox URL Path is not set! You may set it with Set-NetboxURLPath -Path 'netbox/'"
}
$script:NetboxConfig.URLPath
}

View file

@ -0,0 +1,15 @@
function Set-NetboxURLPath {
[CmdletBinding(ConfirmImpact = 'Low',
SupportsShouldProcess = $true)]
[OutputType([string])]
param
(
[Parameter(Mandatory = $true)]
[string]$Path
)
if ($PSCmdlet.ShouldProcess('Netbox URL Path', 'Set')) {
$script:NetboxConfig.URLPath = $Path.Trim()
$script:NetboxConfig.URLPath
}
}

View file

@ -91,8 +91,8 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface',
'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate',
'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix',
'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag',
'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxVersion',
'Get-NetboxVirtualizationCluster',
'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxURLPath',
'Get-NetboxVersion', 'Get-NetboxVirtualizationCluster',
'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine',
'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest',
'New-NetboxCircuit', 'New-NetboxContact',
@ -112,10 +112,10 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface',
'Set-NetboxHostName', 'Set-NetboxHostPort', 'Set-NetboxHostScheme',
'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress',
'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout',
'Set-NetboxUnstrustedSSL', 'Set-NetboxVirtualMachine',
'Set-NetboxVirtualMachineInterface', 'SetupNetboxConfigVariable',
'Test-NetboxAPIConnected', 'ThrowNetboxRESTError',
'VerifyAPIConnectivity'
'Set-NetboxUnstrustedSSL', 'Set-NetboxURLPath',
'Set-NetboxVirtualMachine', 'Set-NetboxVirtualMachineInterface',
'SetupNetboxConfigVariable', 'Test-NetboxAPIConnected',
'ThrowNetboxRESTError', 'VerifyAPIConnectivity'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()

View file

@ -91,8 +91,8 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface',
'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate',
'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix',
'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag',
'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxVersion',
'Get-NetboxVirtualizationCluster',
'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxURLPath',
'Get-NetboxVersion', 'Get-NetboxVirtualizationCluster',
'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine',
'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest',
'New-NetboxCircuit', 'New-NetboxContact',
@ -112,10 +112,10 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface',
'Set-NetboxHostName', 'Set-NetboxHostPort', 'Set-NetboxHostScheme',
'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress',
'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout',
'Set-NetboxUnstrustedSSL', 'Set-NetboxVirtualMachine',
'Set-NetboxVirtualMachineInterface', 'SetupNetboxConfigVariable',
'Test-NetboxAPIConnected', 'ThrowNetboxRESTError',
'VerifyAPIConnectivity'
'Set-NetboxUnstrustedSSL', 'Set-NetboxURLPath',
'Set-NetboxVirtualMachine', 'Set-NetboxVirtualMachineInterface',
'SetupNetboxConfigVariable', 'Test-NetboxAPIConnected',
'ThrowNetboxRESTError', 'VerifyAPIConnectivity'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()

View file

@ -323,11 +323,17 @@ function BuildNewURI {
$null = CheckNetboxIsConnected
}
# Begin a URI builder with HTTP/HTTPS and the provided hostname
# Begin a URI builder with HTTP/HTTPS and the provided hostname, and url path if required
if (-not $script:NetboxConfig.URLPath) {
throw "Netbox Credentials not set! You may set with Set-NetboxCredential"
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
} else {
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort, "/$($script:NetboxConfig.URLPath.trim('/'))")
}
# 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()
}) -join '/')
@ -579,6 +585,7 @@ function Connect-NetboxAPI {
$null = Set-NetboxCredential -Credential $Credential
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
$null = Set-NetboxHostPort -Port $uriBuilder.Port
$null = Set-NetboxURLPath -Path $uriBuilder.Path
$null = Set-NetboxInvokeParams -invokeParams $invokeParams
$null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds
@ -3069,6 +3076,22 @@ function Get-NetboxTimeout {
#endregion
#region File Get-NetboxURLPath.ps1
function Get-NetboxURLPath {
[CmdletBinding()]
param ()
Write-Verbose "Getting Netbox URL Path"
if ($null -eq $script:NetboxConfig.URLPath) {
throw "Netbox URL Path is not set! You may set it with Set-NetboxURLPath -Path 'netbox/'"
}
$script:NetboxConfig.URLPath
}
#endregion
#region File Get-NetboxVersion.ps1
@ -5885,6 +5908,26 @@ Function Set-NetboxUntrustedSSL {
#endregion
#region File Set-NetboxURLPath.ps1
function Set-NetboxURLPath {
[CmdletBinding(ConfirmImpact = 'Low',
SupportsShouldProcess = $true)]
[OutputType([string])]
param
(
[Parameter(Mandatory = $true)]
[string]$Path
)
if ($PSCmdlet.ShouldProcess('Netbox URL Path', 'Set')) {
$script:NetboxConfig.URLPath = $Path.Trim()
$script:NetboxConfig.URLPath
}
}
#endregion
#region File Set-NetboxVirtualMachine.ps1