NetboxPS/Functions/Setup/Connect-NetboxAPI.ps1

150 lines
5.1 KiB
PowerShell
Raw Normal View History

2020-04-09 09:57:20 -04:00
function Connect-NetboxAPI {
<#
2020-04-09 09:57:20 -04:00
.SYNOPSIS
Connects to the Netbox API and ensures Credential work properly
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
.DESCRIPTION
2021-03-31 10:23:45 -04:00
Connects to the Netbox API and ensures Credential work properly
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
.PARAMETER Hostname
2021-03-31 10:23:45 -04:00
The hostname for the resource such as netbox.domain.com
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
.PARAMETER Credential
2021-03-31 10:23:45 -04:00
Credential object containing the API key in the password. Username is not applicable
2021-07-22 11:39:39 -04:00
.PARAMETER Scheme
2021-03-31 10:23:45 -04:00
Scheme for the URI such as HTTP or HTTPS. Defaults to HTTPS
2021-07-22 11:39:39 -04:00
.PARAMETER Port
2021-03-31 10:23:45 -04:00
Port for the resource. Value between 1-65535
2021-07-22 11:39:39 -04:00
.PARAMETER URI
2021-03-31 10:23:45 -04:00
The full URI for the resource such as "https://netbox.domain.com:8443"
2021-07-22 11:39:39 -04:00
.PARAMETER SkipCertificateCheck
A description of the SkipCertificateCheck parameter.
2021-07-22 11:39:39 -04:00
.PARAMETER TimeoutSeconds
The number of seconds before the HTTP call times out. Defaults to 30 seconds
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
.EXAMPLE
PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com"
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
This will prompt for Credential, then proceed to attempt a connection to Netbox
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
.NOTES
Additional information about the function.
#>
2021-07-22 11:39:39 -04:00
[CmdletBinding(DefaultParameterSetName = 'Manual')]
2020-04-09 09:57:20 -04:00
param
(
[Parameter(ParameterSetName = 'Manual',
Mandatory = $true)]
2020-04-09 09:57:20 -04:00
[string]$Hostname,
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
[Parameter(Mandatory = $false)]
[pscredential]$Credential,
2021-07-22 11:39:39 -04:00
[Parameter(ParameterSetName = 'Manual')]
[ValidateSet('https', 'http', IgnoreCase = $true)]
[string]$Scheme = 'https',
2021-07-22 11:39:39 -04:00
[Parameter(ParameterSetName = 'Manual')]
[uint16]$Port = 443,
2021-07-22 11:39:39 -04:00
[Parameter(ParameterSetName = 'URI',
Mandatory = $true)]
[string]$URI,
2021-07-22 11:39:39 -04:00
[Parameter(Mandatory = $false)]
[switch]$SkipCertificateCheck = $false,
2021-07-22 11:39:39 -04:00
[ValidateNotNullOrEmpty()]
[ValidateRange(1, 65535)]
[uint16]$TimeoutSeconds = 30
2020-04-09 09:57:20 -04:00
)
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
if (-not $Credential) {
try {
$Credential = Get-NetboxCredential -ErrorAction Stop
2021-07-22 10:28:22 -04:00
} catch {
2020-04-09 09:57:20 -04:00
# Credentials are not set... Try to obtain from the user
if (-not ($Credential = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) {
throw "Token is necessary to connect to a Netbox API."
}
}
}
2021-07-22 11:39:39 -04:00
$invokeParams = @{ SkipCertificateCheck = $SkipCertificateCheck; }
2021-07-22 11:39:39 -04:00
if ("Desktop" -eq $PSVersionTable.PsEdition) {
#Remove -SkipCertificateCheck from Invoke Parameter (not supported <= PS 5)
$invokeParams.remove("SkipCertificateCheck")
}
2021-07-22 11:39:39 -04:00
#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
Set-NetboxCipherSSL
if ($SkipCertificateCheck) {
#Disable SSL chain trust...
Set-NetboxuntrustedSSL
}
}
2021-07-22 11:39:39 -04:00
switch ($PSCmdlet.ParameterSetName) {
'Manual' {
$uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port)
}
2021-07-22 11:39:39 -04:00
'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]"
}
}
}
2021-07-22 11:39:39 -04:00
$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
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
try {
Write-Verbose "Verifying API connectivity..."
$null = VerifyAPIConnectivity
2021-07-22 10:28:22 -04:00
} catch {
2020-04-09 09:57:20 -04:00
Write-Verbose "Failed to connect. Generating error"
Write-Verbose $_.Exception.Message
if (($_.Exception.Response) -and ($_.Exception.Response.StatusCode -eq 403)) {
throw "Invalid token"
2021-07-22 10:28:22 -04:00
} else {
2020-04-09 09:57:20 -04:00
throw $_
}
}
2021-07-22 11:39:39 -04:00
Write-Verbose "Caching API definition"
$script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition
2021-07-22 11:39:39 -04:00
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)"
}
2021-07-22 11:39:39 -04:00
$script:NetboxConfig.Connected = $true
Write-Verbose "Successfully connected!"
2021-07-22 11:39:39 -04:00
#Write-Verbose "Caching static choices"
#$script:NetboxConfig.Choices.Circuits = Get-NetboxCircuitsChoices
#$script:NetboxConfig.Choices.DCIM = Get-NetboxDCIMChoices # Not completed yet
#$script:NetboxConfig.Choices.Extras = Get-NetboxExtrasChoices
#$script:NetboxConfig.Choices.IPAM = Get-NetboxIPAMChoices
##$script:NetboxConfig.Choices.Secrets = Get-NetboxSecretsChoices # Not completed yet
##$script:NetboxConfig.Choices.Tenancy = Get-NetboxTenancyChoices
#$script:NetboxConfig.Choices.Virtualization = Get-NetboxVirtualizationChoices
2021-07-22 11:39:39 -04:00
2020-04-09 09:57:20 -04:00
Write-Verbose "Connection process completed"
}