From cd6dad5937a50b139c7e2ed7668019e5805d1828 Mon Sep 17 00:00:00 2001 From: Ben Claussen Date: Wed, 31 Mar 2021 10:06:41 -0400 Subject: [PATCH] Add logic and functions to connect via HTTP instead of HTTPS --- Functions/Helpers/BuildNewURI.ps1 | 31 +--------------- Functions/Setup/Connect-NetboxAPI.ps1 | 45 +++++++++++++++++++++--- Functions/Setup/Get-NetboxHostPort.ps1 | 11 ++++++ Functions/Setup/Get-NetboxHostScheme.ps1 | 11 ++++++ Functions/Setup/Set-NetboxHostPort.ps1 | 15 ++++++++ Functions/Setup/Set-NetboxHostScheme.ps1 | 20 +++++++++++ NetboxPS.psd1 | 4 +-- NetboxPS.psproj | 4 +++ 8 files changed, 105 insertions(+), 36 deletions(-) create mode 100644 Functions/Setup/Get-NetboxHostPort.ps1 create mode 100644 Functions/Setup/Get-NetboxHostScheme.ps1 create mode 100644 Functions/Setup/Set-NetboxHostPort.ps1 create mode 100644 Functions/Setup/Set-NetboxHostScheme.ps1 diff --git a/Functions/Helpers/BuildNewURI.ps1 b/Functions/Helpers/BuildNewURI.ps1 index 9b6c3ff..842b25b 100644 --- a/Functions/Helpers/BuildNewURI.ps1 +++ b/Functions/Helpers/BuildNewURI.ps1 @@ -49,21 +49,12 @@ function BuildNewURI { [OutputType([System.UriBuilder])] param ( - [Parameter(Mandatory = $false)] - [string]$Hostname, - [Parameter(Mandatory = $false)] [string[]]$Segments, [Parameter(Mandatory = $false)] [hashtable]$Parameters, - [Parameter(Mandatory = $false)] - [boolean]$HTTPS = $true, - - [ValidateRange(1, 65535)] - [uint16]$Port = 443, - [switch]$SkipConnectedCheck ) @@ -74,28 +65,8 @@ function BuildNewURI { $null = CheckNetboxIsConnected } - if (-not $Hostname) { - $Hostname = Get-NetboxHostname - } - - if ($HTTPS) { - Write-Verbose " Setting scheme to HTTPS" - $Scheme = 'https' - } else { - Write-Warning " Connecting via non-secure HTTP is not-recommended" - - Write-Verbose " Setting scheme to HTTP" - $Scheme = 'http' - - if (-not $PSBoundParameters.ContainsKey('Port')) { - # Set the port to 80 if the user did not supply it - Write-Verbose " Setting port to 80 as default because it was not supplied by the user" - $Port = 80 - } - } - # Begin a URI builder with HTTP/HTTPS and the provided hostname - $uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port) + $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 $uriBuilder.Path = "api/{0}/" -f ($Segments.ForEach({ diff --git a/Functions/Setup/Connect-NetboxAPI.ps1 b/Functions/Setup/Connect-NetboxAPI.ps1 index 9c372b2..ce92db8 100644 --- a/Functions/Setup/Connect-NetboxAPI.ps1 +++ b/Functions/Setup/Connect-NetboxAPI.ps1 @@ -12,6 +12,15 @@ .PARAMETER Credential A description of the Credential parameter. + .PARAMETER Scheme + A description of the Scheme parameter. + + .PARAMETER Port + A description of the Port parameter. + + .PARAMETER URI + A description of the URI parameter. + .EXAMPLE PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com" @@ -21,14 +30,26 @@ Additional information about the function. #> - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = 'Manual')] param ( - [Parameter(Mandatory = $true)] + [Parameter(ParameterSetName = 'Manual', + Mandatory = $true)] [string]$Hostname, [Parameter(Mandatory = $false)] - [pscredential]$Credential + [pscredential]$Credential, + + [Parameter(ParameterSetName = 'Manual')] + [ValidateSet('https', 'http', IgnoreCase = $true)] + [string]$Scheme = 'https', + + [Parameter(ParameterSetName = 'Manual')] + [uint16]$Port = 443, + + [Parameter(ParameterSetName = 'URI', + Mandatory = $true)] + [string]$URI ) if (-not $Credential) { @@ -42,9 +63,25 @@ } } - $null = Set-NetboxHostName -Hostname $Hostname $null = Set-NetboxCredential -Credential $Credential + switch ($PSCmdlet.ParameterSetName) { + 'Manual' { + $uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port) + } + + 'URI' { + $uriBuilder = [System.UriBuilder]::new($URI) + if ([string]::IsNullOrWhiteSpace($uriBuilder.Host)) { + throw "URI appears to be invalid. Must be in format [host.name], [scheme://host.name], or [scheme://host.name:port]" + } + } + } + + $null = Set-NetboxHostName -Hostname $uriBuilder.Host + $null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme + $null = Set-NetboxHostPort -Port $uriBuilder.Port + try { Write-Verbose "Verifying API connectivity..." $null = VerifyAPIConnectivity diff --git a/Functions/Setup/Get-NetboxHostPort.ps1 b/Functions/Setup/Get-NetboxHostPort.ps1 new file mode 100644 index 0000000..691a7a3 --- /dev/null +++ b/Functions/Setup/Get-NetboxHostPort.ps1 @@ -0,0 +1,11 @@ +function Get-NetboxHostPort { + [CmdletBinding()] + param () + + Write-Verbose "Getting Netbox host port" + if ($null -eq $script:NetboxConfig.HostPort) { + throw "Netbox host port is not set! You may set it with Set-NetboxHostPort -Port 'https'" + } + + $script:NetboxConfig.HostPort +} \ No newline at end of file diff --git a/Functions/Setup/Get-NetboxHostScheme.ps1 b/Functions/Setup/Get-NetboxHostScheme.ps1 new file mode 100644 index 0000000..ac396e0 --- /dev/null +++ b/Functions/Setup/Get-NetboxHostScheme.ps1 @@ -0,0 +1,11 @@ +function Get-NetboxHostScheme { + [CmdletBinding()] + param () + + Write-Verbose "Getting Netbox host scheme" + if ($null -eq $script:NetboxConfig.Hostscheme) { + throw "Netbox host sceme is not set! You may set it with Set-NetboxHostScheme -Scheme 'https'" + } + + $script:NetboxConfig.HostScheme +} \ No newline at end of file diff --git a/Functions/Setup/Set-NetboxHostPort.ps1 b/Functions/Setup/Set-NetboxHostPort.ps1 new file mode 100644 index 0000000..5d2c0d6 --- /dev/null +++ b/Functions/Setup/Set-NetboxHostPort.ps1 @@ -0,0 +1,15 @@ +function Set-NetboxHostPort { + [CmdletBinding(ConfirmImpact = 'Low', + SupportsShouldProcess = $true)] + [OutputType([string])] + param + ( + [Parameter(Mandatory = $true)] + [uint16]$Port + ) + + if ($PSCmdlet.ShouldProcess('Netbox Port', 'Set')) { + $script:NetboxConfig.HostPort = $Port + $script:NetboxConfig.HostPort + } +} \ No newline at end of file diff --git a/Functions/Setup/Set-NetboxHostScheme.ps1 b/Functions/Setup/Set-NetboxHostScheme.ps1 new file mode 100644 index 0000000..f5a59c4 --- /dev/null +++ b/Functions/Setup/Set-NetboxHostScheme.ps1 @@ -0,0 +1,20 @@ +function Set-NetboxHostScheme { + [CmdletBinding(ConfirmImpact = 'Low', + SupportsShouldProcess = $true)] + [OutputType([string])] + param + ( + [Parameter(Mandatory = $false)] + [ValidateSet('https', 'http', IgnoreCase = $true)] + [string]$Scheme = 'https' + ) + + if ($PSCmdlet.ShouldProcess('Netbox Host Scheme', 'Set')) { + if ($Scheme -eq 'http') { + Write-Warning "Connecting via non-secure HTTP is not-recommended" + } + + $script:NetboxConfig.HostScheme = $Scheme + $script:NetboxConfig.HostScheme + } +} \ No newline at end of file diff --git a/NetboxPS.psd1 b/NetboxPS.psd1 index 63ef84e..5601780 100644 --- a/NetboxPS.psd1 +++ b/NetboxPS.psd1 @@ -3,7 +3,7 @@ # # Generated by: Ben Claussen # -# Generated on: 2021-03-30 +# Generated on: 2021-03-31 # @{ @@ -12,7 +12,7 @@ RootModule = 'NetboxPS.psm1' # Version number of this module. -ModuleVersion = '1.3.2' +ModuleVersion = '1.3.3' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/NetboxPS.psproj b/NetboxPS.psproj index 7fc51c9..938d28b 100644 --- a/NetboxPS.psproj +++ b/NetboxPS.psproj @@ -104,6 +104,10 @@ Functions\Circuits\Types\Get-NetboxCircuitType.ps1 Functions\Helpers\Get-ModelDefinition.ps1 Functions\IPAM\Prefix\Set-NetboxIPAMPrefix.ps1 + Functions\Setup\Set-NetboxHostScheme.ps1 + Functions\Setup\Get-NetboxHostScheme.ps1 + Functions\Setup\Set-NetboxHostPort.ps1 + Functions\Setup\Get-NetboxHostPort.ps1 R:\Netbox\NetboxPS\Test-Module.ps1 \ No newline at end of file