From 2e131d45d2bd8049b95cdace407023b5f0c46e31 Mon Sep 17 00:00:00 2001 From: Chris Lawson Date: Wed, 22 Mar 2023 17:20:22 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feature:=20Add=20ability=20to=20define?= =?UTF-8?q?=20a=20custom=20URLPath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Functions/Helpers/BuildNewURI.ps1 | 14 ++++-- Functions/Setup/Connect-NetboxAPI.ps1 | 19 ++++---- Functions/Setup/Get-NetboxURLPath.ps1 | 11 +++++ Functions/Setup/Set-NetboxURLPath.ps1 | 15 ++++++ NetboxPS.psd1 | 12 ++--- NetboxPS/NetboxPS.psd1 | 12 ++--- NetboxPS/NetboxPS.psm1 | 69 ++++++++++++++++++++++----- 7 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 Functions/Setup/Get-NetboxURLPath.ps1 create mode 100644 Functions/Setup/Set-NetboxURLPath.ps1 diff --git a/Functions/Helpers/BuildNewURI.ps1 b/Functions/Helpers/BuildNewURI.ps1 index b7a734b..213a3cf 100644 --- a/Functions/Helpers/BuildNewURI.ps1 +++ b/Functions/Helpers/BuildNewURI.ps1 @@ -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 '/') diff --git a/Functions/Setup/Connect-NetboxAPI.ps1 b/Functions/Setup/Connect-NetboxAPI.ps1 index 89e900c..5675ca9 100644 --- a/Functions/Setup/Connect-NetboxAPI.ps1 +++ b/Functions/Setup/Connect-NetboxAPI.ps1 @@ -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" diff --git a/Functions/Setup/Get-NetboxURLPath.ps1 b/Functions/Setup/Get-NetboxURLPath.ps1 new file mode 100644 index 0000000..aeb9cee --- /dev/null +++ b/Functions/Setup/Get-NetboxURLPath.ps1 @@ -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 +} \ No newline at end of file diff --git a/Functions/Setup/Set-NetboxURLPath.ps1 b/Functions/Setup/Set-NetboxURLPath.ps1 new file mode 100644 index 0000000..42cabb8 --- /dev/null +++ b/Functions/Setup/Set-NetboxURLPath.ps1 @@ -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 + } +} \ No newline at end of file diff --git a/NetboxPS.psd1 b/NetboxPS.psd1 index 69a96c5..3de5e0f 100644 --- a/NetboxPS.psd1 +++ b/NetboxPS.psd1 @@ -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 = @() diff --git a/NetboxPS/NetboxPS.psd1 b/NetboxPS/NetboxPS.psd1 index 69a96c5..3de5e0f 100644 --- a/NetboxPS/NetboxPS.psd1 +++ b/NetboxPS/NetboxPS.psd1 @@ -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 = @() diff --git a/NetboxPS/NetboxPS.psm1 b/NetboxPS/NetboxPS.psm1 index 4b45566..a212c86 100644 --- a/NetboxPS/NetboxPS.psm1 +++ b/NetboxPS/NetboxPS.psm1 @@ -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