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

@ -1,6 +1,6 @@

function BuildNewURI {
<#
<#
.SYNOPSIS
Create a new URI for Netbox
@ -52,11 +52,17 @@ function BuildNewURI {
$null = CheckNetboxIsConnected
}
# Begin a URI builder with HTTP/HTTPS and the provided hostname
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
# 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

@ -1,5 +1,5 @@
function Connect-NetboxAPI {
<#
<#
.SYNOPSIS
Connects to the Netbox API and ensures Credential work properly
@ -40,7 +40,7 @@
param
(
[Parameter(ParameterSetName = 'Manual',
Mandatory = $true)]
Mandatory = $true)]
[string]$Hostname,
[Parameter(Mandatory = $false)]
@ -54,7 +54,7 @@
[uint16]$Port = 443,
[Parameter(ParameterSetName = 'URI',
Mandatory = $true)]
Mandatory = $true)]
[string]$URI,
[Parameter(Mandatory = $false)]
@ -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
@ -128,12 +129,12 @@
}
}
# Write-Verbose "Caching API definition"
# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition
#
# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) {
# $Script:NetboxConfig.Connected = $false
# throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)"
# Write-Verbose "Caching API definition"
# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition
#
# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) {
# $Script:NetboxConfig.Connected = $false
# throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)"
# }
Write-Verbose "Checking Netbox version compatibility"

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

@ -271,7 +271,7 @@ function Add-NetboxVirtualMachineInterface {
function BuildNewURI {
<#
<#
.SYNOPSIS
Create a new URI for Netbox
@ -323,11 +323,17 @@ function BuildNewURI {
$null = CheckNetboxIsConnected
}
# Begin a URI builder with HTTP/HTTPS and the provided hostname
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
# 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 '/')
@ -466,7 +472,7 @@ function Clear-NetboxCredential {
#region File Connect-NetboxAPI.ps1
function Connect-NetboxAPI {
<#
<#
.SYNOPSIS
Connects to the Netbox API and ensures Credential work properly
@ -507,7 +513,7 @@ function Connect-NetboxAPI {
param
(
[Parameter(ParameterSetName = 'Manual',
Mandatory = $true)]
Mandatory = $true)]
[string]$Hostname,
[Parameter(Mandatory = $false)]
@ -521,7 +527,7 @@ function Connect-NetboxAPI {
[uint16]$Port = 443,
[Parameter(ParameterSetName = 'URI',
Mandatory = $true)]
Mandatory = $true)]
[string]$URI,
[Parameter(Mandatory = $false)]
@ -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
@ -595,12 +602,12 @@ function Connect-NetboxAPI {
}
}
# Write-Verbose "Caching API definition"
# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition
#
# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) {
# $Script:NetboxConfig.Connected = $false
# throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)"
# Write-Verbose "Caching API definition"
# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition
#
# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) {
# $Script:NetboxConfig.Connected = $false
# throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)"
# }
Write-Verbose "Checking Netbox version compatibility"
@ -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