From 22607fde48cf21c969647d5f64fb94eee45545f5 Mon Sep 17 00:00:00 2001 From: Ben Claussen Date: Thu, 22 Jul 2021 11:06:13 -0400 Subject: [PATCH] Add TimeoutSeconds parameter and logic to Connect-NetboxAPI - Updated `InvokeNetboxRequest` to use `NetboxConfig.Timeout` - Updated `Get-NetboxAPIDefinition` to use `NetboxConfig.Timeout` --- Functions/Helpers/InvokeNetboxRequest.ps1 | 4 +- Functions/Setup/Connect-NetboxAPI.ps1 | 71 +++++++++++-------- .../Setup/Support/Get-NetboxAPIDefinition.ps1 | 2 +- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/Functions/Helpers/InvokeNetboxRequest.ps1 b/Functions/Helpers/InvokeNetboxRequest.ps1 index 5750730..9696518 100644 --- a/Functions/Helpers/InvokeNetboxRequest.ps1 +++ b/Functions/Helpers/InvokeNetboxRequest.ps1 @@ -24,8 +24,8 @@ function InvokeNetboxRequest { [pscustomobject]$Body = $null, - [ValidateRange(0, 60)] - [uint16]$Timeout = 5, + [ValidateRange(1, 65535)] + [uint16]$Timeout = (Get-NetboxTimeout), [ValidateSet('GET', 'PATCH', 'PUT', 'POST', 'DELETE', 'OPTIONS', IgnoreCase = $true)] [string]$Method = 'GET', diff --git a/Functions/Setup/Connect-NetboxAPI.ps1 b/Functions/Setup/Connect-NetboxAPI.ps1 index e732f16..70c6457 100644 --- a/Functions/Setup/Connect-NetboxAPI.ps1 +++ b/Functions/Setup/Connect-NetboxAPI.ps1 @@ -1,60 +1,70 @@ function Connect-NetboxAPI { - <# +<# .SYNOPSIS Connects to the Netbox API and ensures Credential work properly - + .DESCRIPTION Connects to the Netbox API and ensures Credential work properly - + .PARAMETER Hostname The hostname for the resource such as netbox.domain.com - + .PARAMETER Credential Credential object containing the API key in the password. Username is not applicable - + .PARAMETER Scheme Scheme for the URI such as HTTP or HTTPS. Defaults to HTTPS - + .PARAMETER Port Port for the resource. Value between 1-65535 - + .PARAMETER URI The full URI for the resource such as "https://netbox.domain.com:8443" - + + .PARAMETER SkipCertificateCheck + A description of the SkipCertificateCheck parameter. + + .PARAMETER TimeoutSeconds + The number of seconds before the HTTP call times out. Defaults to 30 seconds + .EXAMPLE PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com" - + This will prompt for Credential, then proceed to attempt a connection to Netbox - + .NOTES Additional information about the function. #> - + [CmdletBinding(DefaultParameterSetName = 'Manual')] param ( [Parameter(ParameterSetName = 'Manual', Mandatory = $true)] [string]$Hostname, - + [Parameter(Mandatory = $false)] [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, - + [Parameter(Mandatory = $false)] - [switch]$SkipCertificateCheck = $false + [switch]$SkipCertificateCheck = $false, + + [ValidateNotNullOrEmpty()] + [ValidateRange(1, 65535)] + [uint16]$TimeoutSeconds = 30 ) - + if (-not $Credential) { try { $Credential = Get-NetboxCredential -ErrorAction Stop @@ -65,14 +75,14 @@ } } } - + $invokeParams = @{ SkipCertificateCheck = $SkipCertificateCheck; } - + if ("Desktop" -eq $PSVersionTable.PsEdition) { #Remove -SkipCertificateCheck from Invoke Parameter (not supported <= PS 5) $invokeParams.remove("SkipCertificateCheck") } - + #for PowerShell (<=) 5 (Desktop), Enable TLS 1.1, 1.2 and Disable SSL chain trust if ("Desktop" -eq $PSVersionTable.PsEdition) { #Enable TLS 1.1 and 1.2 @@ -82,12 +92,12 @@ Set-NetboxuntrustedSSL } } - + switch ($PSCmdlet.ParameterSetName) { 'Manual' { $uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port) } - + 'URI' { $uriBuilder = [System.UriBuilder]::new($URI) if ([string]::IsNullOrWhiteSpace($uriBuilder.Host)) { @@ -95,13 +105,14 @@ } } } - + $null = Set-NetboxHostName -Hostname $uriBuilder.Host $null = Set-NetboxCredential -Credential $Credential $null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme $null = Set-NetboxHostPort -Port $uriBuilder.Port $null = Set-NetboxInvokeParams -invokeParams $invokeParams - + $null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds + try { Write-Verbose "Verifying API connectivity..." $null = VerifyAPIConnectivity @@ -114,18 +125,18 @@ throw $_ } } - + 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)" } - + $script:NetboxConfig.Connected = $true Write-Verbose "Successfully connected!" - + #Write-Verbose "Caching static choices" #$script:NetboxConfig.Choices.Circuits = Get-NetboxCircuitsChoices #$script:NetboxConfig.Choices.DCIM = Get-NetboxDCIMChoices # Not completed yet @@ -134,6 +145,6 @@ ##$script:NetboxConfig.Choices.Secrets = Get-NetboxSecretsChoices # Not completed yet ##$script:NetboxConfig.Choices.Tenancy = Get-NetboxTenancyChoices #$script:NetboxConfig.Choices.Virtualization = Get-NetboxVirtualizationChoices - + Write-Verbose "Connection process completed" } \ No newline at end of file diff --git a/Functions/Setup/Support/Get-NetboxAPIDefinition.ps1 b/Functions/Setup/Support/Get-NetboxAPIDefinition.ps1 index 21315c8..59ca911 100644 --- a/Functions/Setup/Support/Get-NetboxAPIDefinition.ps1 +++ b/Functions/Setup/Support/Get-NetboxAPIDefinition.ps1 @@ -25,5 +25,5 @@ function Get-NetboxAPIDefinition { $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters -SkipConnectedCheck - InvokeNetboxRequest -URI $URI -Timeout 10 + InvokeNetboxRequest -URI $URI }