mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-16 03:05:47 +00:00
✨feature: Add ability to define a custom URLPath
This commit is contained in:
parent
c736e0a1f7
commit
2e131d45d2
7 changed files with 114 additions and 38 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
function BuildNewURI {
|
function BuildNewURI {
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Create a new URI for Netbox
|
Create a new URI for Netbox
|
||||||
|
|
||||||
|
|
@ -52,11 +52,17 @@ function BuildNewURI {
|
||||||
$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, and url path if required
|
||||||
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
|
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
|
# 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 '/')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
function Connect-NetboxAPI {
|
function Connect-NetboxAPI {
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Connects to the Netbox API and ensures Credential work properly
|
Connects to the Netbox API and ensures Credential work properly
|
||||||
|
|
||||||
|
|
@ -40,7 +40,7 @@
|
||||||
param
|
param
|
||||||
(
|
(
|
||||||
[Parameter(ParameterSetName = 'Manual',
|
[Parameter(ParameterSetName = 'Manual',
|
||||||
Mandatory = $true)]
|
Mandatory = $true)]
|
||||||
[string]$Hostname,
|
[string]$Hostname,
|
||||||
|
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
|
|
@ -54,7 +54,7 @@
|
||||||
[uint16]$Port = 443,
|
[uint16]$Port = 443,
|
||||||
|
|
||||||
[Parameter(ParameterSetName = 'URI',
|
[Parameter(ParameterSetName = 'URI',
|
||||||
Mandatory = $true)]
|
Mandatory = $true)]
|
||||||
[string]$URI,
|
[string]$URI,
|
||||||
|
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
|
|
@ -112,6 +112,7 @@
|
||||||
$null = Set-NetboxCredential -Credential $Credential
|
$null = Set-NetboxCredential -Credential $Credential
|
||||||
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
|
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
|
||||||
$null = Set-NetboxHostPort -Port $uriBuilder.Port
|
$null = Set-NetboxHostPort -Port $uriBuilder.Port
|
||||||
|
$null = Set-NetboxURLPath -Path $uriBuilder.Path
|
||||||
$null = Set-NetboxInvokeParams -invokeParams $invokeParams
|
$null = Set-NetboxInvokeParams -invokeParams $invokeParams
|
||||||
$null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds
|
$null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds
|
||||||
|
|
||||||
|
|
@ -128,12 +129,12 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Write-Verbose "Caching API definition"
|
# Write-Verbose "Caching API definition"
|
||||||
# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition
|
# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition
|
||||||
#
|
#
|
||||||
# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) {
|
# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) {
|
||||||
# $Script:NetboxConfig.Connected = $false
|
# $Script:NetboxConfig.Connected = $false
|
||||||
# throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)"
|
# 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"
|
Write-Verbose "Checking Netbox version compatibility"
|
||||||
|
|
|
||||||
11
Functions/Setup/Get-NetboxURLPath.ps1
Normal file
11
Functions/Setup/Get-NetboxURLPath.ps1
Normal 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
|
||||||
|
}
|
||||||
15
Functions/Setup/Set-NetboxURLPath.ps1
Normal file
15
Functions/Setup/Set-NetboxURLPath.ps1
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -91,8 +91,8 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface',
|
||||||
'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate',
|
'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate',
|
||||||
'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix',
|
'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix',
|
||||||
'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag',
|
'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag',
|
||||||
'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxVersion',
|
'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxURLPath',
|
||||||
'Get-NetboxVirtualizationCluster',
|
'Get-NetboxVersion', 'Get-NetboxVirtualizationCluster',
|
||||||
'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine',
|
'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine',
|
||||||
'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest',
|
'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest',
|
||||||
'New-NetboxCircuit', 'New-NetboxContact',
|
'New-NetboxCircuit', 'New-NetboxContact',
|
||||||
|
|
@ -112,10 +112,10 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface',
|
||||||
'Set-NetboxHostName', 'Set-NetboxHostPort', 'Set-NetboxHostScheme',
|
'Set-NetboxHostName', 'Set-NetboxHostPort', 'Set-NetboxHostScheme',
|
||||||
'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress',
|
'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress',
|
||||||
'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout',
|
'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout',
|
||||||
'Set-NetboxUnstrustedSSL', 'Set-NetboxVirtualMachine',
|
'Set-NetboxUnstrustedSSL', 'Set-NetboxURLPath',
|
||||||
'Set-NetboxVirtualMachineInterface', 'SetupNetboxConfigVariable',
|
'Set-NetboxVirtualMachine', 'Set-NetboxVirtualMachineInterface',
|
||||||
'Test-NetboxAPIConnected', 'ThrowNetboxRESTError',
|
'SetupNetboxConfigVariable', 'Test-NetboxAPIConnected',
|
||||||
'VerifyAPIConnectivity'
|
'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.
|
# 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 = @()
|
CmdletsToExport = @()
|
||||||
|
|
|
||||||
|
|
@ -91,8 +91,8 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface',
|
||||||
'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate',
|
'Get-NetboxIPAMAddress', 'Get-NetboxIPAMAggregate',
|
||||||
'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix',
|
'Get-NetboxIPAMAvailableIP', 'Get-NetboxIPAMPrefix',
|
||||||
'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag',
|
'Get-NetboxIPAMRole', 'Get-NetboxIPAMVLAN', 'Get-NetboxTag',
|
||||||
'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxVersion',
|
'Get-NetboxTenant', 'Get-NetboxTimeout', 'Get-NetboxURLPath',
|
||||||
'Get-NetboxVirtualizationCluster',
|
'Get-NetboxVersion', 'Get-NetboxVirtualizationCluster',
|
||||||
'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine',
|
'Get-NetboxVirtualizationClusterGroup', 'Get-NetboxVirtualMachine',
|
||||||
'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest',
|
'Get-NetboxVirtualMachineInterface', 'InvokeNetboxRequest',
|
||||||
'New-NetboxCircuit', 'New-NetboxContact',
|
'New-NetboxCircuit', 'New-NetboxContact',
|
||||||
|
|
@ -112,10 +112,10 @@ FunctionsToExport = 'Add-NetboxDCIMFrontPort', 'Add-NetboxDCIMInterface',
|
||||||
'Set-NetboxHostName', 'Set-NetboxHostPort', 'Set-NetboxHostScheme',
|
'Set-NetboxHostName', 'Set-NetboxHostPort', 'Set-NetboxHostScheme',
|
||||||
'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress',
|
'Set-NetboxInvokeParams', 'Set-NetboxIPAMAddress',
|
||||||
'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout',
|
'Set-NetboxIPAMPrefix', 'Set-NetboxTimeout',
|
||||||
'Set-NetboxUnstrustedSSL', 'Set-NetboxVirtualMachine',
|
'Set-NetboxUnstrustedSSL', 'Set-NetboxURLPath',
|
||||||
'Set-NetboxVirtualMachineInterface', 'SetupNetboxConfigVariable',
|
'Set-NetboxVirtualMachine', 'Set-NetboxVirtualMachineInterface',
|
||||||
'Test-NetboxAPIConnected', 'ThrowNetboxRESTError',
|
'SetupNetboxConfigVariable', 'Test-NetboxAPIConnected',
|
||||||
'VerifyAPIConnectivity'
|
'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.
|
# 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 = @()
|
CmdletsToExport = @()
|
||||||
|
|
|
||||||
|
|
@ -271,7 +271,7 @@ function Add-NetboxVirtualMachineInterface {
|
||||||
|
|
||||||
|
|
||||||
function BuildNewURI {
|
function BuildNewURI {
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Create a new URI for Netbox
|
Create a new URI for Netbox
|
||||||
|
|
||||||
|
|
@ -323,11 +323,17 @@ function BuildNewURI {
|
||||||
$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, and url path if required
|
||||||
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
|
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
|
# 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 '/')
|
||||||
|
|
||||||
|
|
@ -466,7 +472,7 @@ function Clear-NetboxCredential {
|
||||||
#region File Connect-NetboxAPI.ps1
|
#region File Connect-NetboxAPI.ps1
|
||||||
|
|
||||||
function Connect-NetboxAPI {
|
function Connect-NetboxAPI {
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Connects to the Netbox API and ensures Credential work properly
|
Connects to the Netbox API and ensures Credential work properly
|
||||||
|
|
||||||
|
|
@ -507,7 +513,7 @@ function Connect-NetboxAPI {
|
||||||
param
|
param
|
||||||
(
|
(
|
||||||
[Parameter(ParameterSetName = 'Manual',
|
[Parameter(ParameterSetName = 'Manual',
|
||||||
Mandatory = $true)]
|
Mandatory = $true)]
|
||||||
[string]$Hostname,
|
[string]$Hostname,
|
||||||
|
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
|
|
@ -521,7 +527,7 @@ function Connect-NetboxAPI {
|
||||||
[uint16]$Port = 443,
|
[uint16]$Port = 443,
|
||||||
|
|
||||||
[Parameter(ParameterSetName = 'URI',
|
[Parameter(ParameterSetName = 'URI',
|
||||||
Mandatory = $true)]
|
Mandatory = $true)]
|
||||||
[string]$URI,
|
[string]$URI,
|
||||||
|
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
|
|
@ -579,6 +585,7 @@ function Connect-NetboxAPI {
|
||||||
$null = Set-NetboxCredential -Credential $Credential
|
$null = Set-NetboxCredential -Credential $Credential
|
||||||
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
|
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
|
||||||
$null = Set-NetboxHostPort -Port $uriBuilder.Port
|
$null = Set-NetboxHostPort -Port $uriBuilder.Port
|
||||||
|
$null = Set-NetboxURLPath -Path $uriBuilder.Path
|
||||||
$null = Set-NetboxInvokeParams -invokeParams $invokeParams
|
$null = Set-NetboxInvokeParams -invokeParams $invokeParams
|
||||||
$null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds
|
$null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds
|
||||||
|
|
||||||
|
|
@ -595,12 +602,12 @@ function Connect-NetboxAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Write-Verbose "Caching API definition"
|
# Write-Verbose "Caching API definition"
|
||||||
# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition
|
# $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition
|
||||||
#
|
#
|
||||||
# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) {
|
# if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) {
|
||||||
# $Script:NetboxConfig.Connected = $false
|
# $Script:NetboxConfig.Connected = $false
|
||||||
# throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)"
|
# 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"
|
Write-Verbose "Checking Netbox version compatibility"
|
||||||
|
|
@ -3069,6 +3076,22 @@ function Get-NetboxTimeout {
|
||||||
|
|
||||||
#endregion
|
#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
|
#region File Get-NetboxVersion.ps1
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -5885,6 +5908,26 @@ Function Set-NetboxUntrustedSSL {
|
||||||
|
|
||||||
#endregion
|
#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
|
#region File Set-NetboxVirtualMachine.ps1
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue