Trim whitespaces

This commit is contained in:
Ben Claussen 2021-07-22 11:39:39 -04:00
parent 22607fde48
commit ea9f6c41bc
4 changed files with 51 additions and 51 deletions

View file

@ -2,69 +2,69 @@
<# <#
.SYNOPSIS .SYNOPSIS
Connects to the Netbox API and ensures Credential work properly Connects to the Netbox API and ensures Credential work properly
.DESCRIPTION .DESCRIPTION
Connects to the Netbox API and ensures Credential work properly Connects to the Netbox API and ensures Credential work properly
.PARAMETER Hostname .PARAMETER Hostname
The hostname for the resource such as netbox.domain.com The hostname for the resource such as netbox.domain.com
.PARAMETER Credential .PARAMETER Credential
Credential object containing the API key in the password. Username is not applicable Credential object containing the API key in the password. Username is not applicable
.PARAMETER Scheme .PARAMETER Scheme
Scheme for the URI such as HTTP or HTTPS. Defaults to HTTPS Scheme for the URI such as HTTP or HTTPS. Defaults to HTTPS
.PARAMETER Port .PARAMETER Port
Port for the resource. Value between 1-65535 Port for the resource. Value between 1-65535
.PARAMETER URI .PARAMETER URI
The full URI for the resource such as "https://netbox.domain.com:8443" The full URI for the resource such as "https://netbox.domain.com:8443"
.PARAMETER SkipCertificateCheck .PARAMETER SkipCertificateCheck
A description of the SkipCertificateCheck parameter. A description of the SkipCertificateCheck parameter.
.PARAMETER TimeoutSeconds .PARAMETER TimeoutSeconds
The number of seconds before the HTTP call times out. Defaults to 30 seconds The number of seconds before the HTTP call times out. Defaults to 30 seconds
.EXAMPLE .EXAMPLE
PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com" PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com"
This will prompt for Credential, then proceed to attempt a connection to Netbox This will prompt for Credential, then proceed to attempt a connection to Netbox
.NOTES .NOTES
Additional information about the function. Additional information about the function.
#> #>
[CmdletBinding(DefaultParameterSetName = 'Manual')] [CmdletBinding(DefaultParameterSetName = 'Manual')]
param param
( (
[Parameter(ParameterSetName = 'Manual', [Parameter(ParameterSetName = 'Manual',
Mandatory = $true)] Mandatory = $true)]
[string]$Hostname, [string]$Hostname,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[pscredential]$Credential, [pscredential]$Credential,
[Parameter(ParameterSetName = 'Manual')] [Parameter(ParameterSetName = 'Manual')]
[ValidateSet('https', 'http', IgnoreCase = $true)] [ValidateSet('https', 'http', IgnoreCase = $true)]
[string]$Scheme = 'https', [string]$Scheme = 'https',
[Parameter(ParameterSetName = 'Manual')] [Parameter(ParameterSetName = 'Manual')]
[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)]
[switch]$SkipCertificateCheck = $false, [switch]$SkipCertificateCheck = $false,
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[ValidateRange(1, 65535)] [ValidateRange(1, 65535)]
[uint16]$TimeoutSeconds = 30 [uint16]$TimeoutSeconds = 30
) )
if (-not $Credential) { if (-not $Credential) {
try { try {
$Credential = Get-NetboxCredential -ErrorAction Stop $Credential = Get-NetboxCredential -ErrorAction Stop
@ -75,14 +75,14 @@
} }
} }
} }
$invokeParams = @{ SkipCertificateCheck = $SkipCertificateCheck; } $invokeParams = @{ SkipCertificateCheck = $SkipCertificateCheck; }
if ("Desktop" -eq $PSVersionTable.PsEdition) { if ("Desktop" -eq $PSVersionTable.PsEdition) {
#Remove -SkipCertificateCheck from Invoke Parameter (not supported <= PS 5) #Remove -SkipCertificateCheck from Invoke Parameter (not supported <= PS 5)
$invokeParams.remove("SkipCertificateCheck") $invokeParams.remove("SkipCertificateCheck")
} }
#for PowerShell (<=) 5 (Desktop), Enable TLS 1.1, 1.2 and Disable SSL chain trust #for PowerShell (<=) 5 (Desktop), Enable TLS 1.1, 1.2 and Disable SSL chain trust
if ("Desktop" -eq $PSVersionTable.PsEdition) { if ("Desktop" -eq $PSVersionTable.PsEdition) {
#Enable TLS 1.1 and 1.2 #Enable TLS 1.1 and 1.2
@ -92,12 +92,12 @@
Set-NetboxuntrustedSSL Set-NetboxuntrustedSSL
} }
} }
switch ($PSCmdlet.ParameterSetName) { switch ($PSCmdlet.ParameterSetName) {
'Manual' { 'Manual' {
$uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port) $uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port)
} }
'URI' { 'URI' {
$uriBuilder = [System.UriBuilder]::new($URI) $uriBuilder = [System.UriBuilder]::new($URI)
if ([string]::IsNullOrWhiteSpace($uriBuilder.Host)) { if ([string]::IsNullOrWhiteSpace($uriBuilder.Host)) {
@ -105,14 +105,14 @@
} }
} }
} }
$null = Set-NetboxHostName -Hostname $uriBuilder.Host $null = Set-NetboxHostName -Hostname $uriBuilder.Host
$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-NetboxInvokeParams -invokeParams $invokeParams $null = Set-NetboxInvokeParams -invokeParams $invokeParams
$null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds $null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds
try { try {
Write-Verbose "Verifying API connectivity..." Write-Verbose "Verifying API connectivity..."
$null = VerifyAPIConnectivity $null = VerifyAPIConnectivity
@ -125,18 +125,18 @@
throw $_ throw $_
} }
} }
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)"
} }
$script:NetboxConfig.Connected = $true $script:NetboxConfig.Connected = $true
Write-Verbose "Successfully connected!" Write-Verbose "Successfully connected!"
#Write-Verbose "Caching static choices" #Write-Verbose "Caching static choices"
#$script:NetboxConfig.Choices.Circuits = Get-NetboxCircuitsChoices #$script:NetboxConfig.Choices.Circuits = Get-NetboxCircuitsChoices
#$script:NetboxConfig.Choices.DCIM = Get-NetboxDCIMChoices # Not completed yet #$script:NetboxConfig.Choices.DCIM = Get-NetboxDCIMChoices # Not completed yet
@ -145,6 +145,6 @@
##$script:NetboxConfig.Choices.Secrets = Get-NetboxSecretsChoices # Not completed yet ##$script:NetboxConfig.Choices.Secrets = Get-NetboxSecretsChoices # Not completed yet
##$script:NetboxConfig.Choices.Tenancy = Get-NetboxTenancyChoices ##$script:NetboxConfig.Choices.Tenancy = Get-NetboxTenancyChoices
#$script:NetboxConfig.Choices.Virtualization = Get-NetboxVirtualizationChoices #$script:NetboxConfig.Choices.Virtualization = Get-NetboxVirtualizationChoices
Write-Verbose "Connection process completed" Write-Verbose "Connection process completed"
} }

View file

@ -3,11 +3,11 @@ function Get-NetboxTimeout {
[CmdletBinding()] [CmdletBinding()]
[OutputType([uint16])] [OutputType([uint16])]
param () param ()
Write-Verbose "Getting Netbox Timeout" Write-Verbose "Getting Netbox Timeout"
if ($null -eq $script:NetboxConfig.Timeout) { if ($null -eq $script:NetboxConfig.Timeout) {
throw "Netbox Timeout is not set! You may set it with Set-NetboxTimeout -TimeoutSeconds [uint16]" throw "Netbox Timeout is not set! You may set it with Set-NetboxTimeout -TimeoutSeconds [uint16]"
} }
$script:NetboxConfig.Timeout $script:NetboxConfig.Timeout
} }

View file

@ -9,7 +9,7 @@ function Set-NetboxTimeout {
[ValidateRange(1, 65535)] [ValidateRange(1, 65535)]
[uint16]$TimeoutSeconds = 30 [uint16]$TimeoutSeconds = 30
) )
if ($PSCmdlet.ShouldProcess('Netbox Timeout', 'Set')) { if ($PSCmdlet.ShouldProcess('Netbox Timeout', 'Set')) {
$script:NetboxConfig.Timeout = $TimeoutSeconds $script:NetboxConfig.Timeout = $TimeoutSeconds
$script:NetboxConfig.Timeout $script:NetboxConfig.Timeout

View file

@ -1,29 +1,29 @@
<# <#
.NOTES .NOTES
=========================================================================== ===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.174 Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.174
Created on: 4/28/2020 11:57 Created on: 4/28/2020 11:57
Created by: Claussen Created by: Claussen
Organization: NEOnet Organization: NEOnet
Filename: Get-NetboxAPIDefinition.ps1 Filename: Get-NetboxAPIDefinition.ps1
=========================================================================== ===========================================================================
.DESCRIPTION .DESCRIPTION
A description of the file. A description of the file.
#> #>
function Get-NetboxAPIDefinition { function Get-NetboxAPIDefinition {
[CmdletBinding()] [CmdletBinding()]
param () param ()
#$URI = "https://netbox.neonet.org/api/docs/?format=openapi" #$URI = "https://netbox.neonet.org/api/docs/?format=openapi"
$Segments = [System.Collections.ArrayList]::new(@('docs')) $Segments = [System.Collections.ArrayList]::new(@('docs'))
$URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary @{'format' = 'openapi' } $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary @{'format' = 'openapi' }
$URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters -SkipConnectedCheck $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters -SkipConnectedCheck
InvokeNetboxRequest -URI $URI InvokeNetboxRequest -URI $URI
} }