2020-04-09 09:57:20 -04:00
|
|
|
|
function Connect-NetboxAPI {
|
2021-07-22 11:06:13 -04:00
|
|
|
|
<#
|
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
|
|
|
|
|
2021-03-31 10:06:41 -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
|
|
|
|
|
2021-03-31 10:06:41 -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
|
|
|
|
|
2021-03-31 10:06:41 -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
|
|
|
|
|
2021-07-22 11:06:13 -04:00
|
|
|
|
.PARAMETER SkipCertificateCheck
|
|
|
|
|
|
A description of the SkipCertificateCheck parameter.
|
2021-07-22 11:39:39 -04:00
|
|
|
|
|
2021-07-22 11:06:13 -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
|
|
|
|
|
2021-03-31 10:06:41 -04:00
|
|
|
|
[CmdletBinding(DefaultParameterSetName = 'Manual')]
|
2020-04-09 09:57:20 -04:00
|
|
|
|
param
|
|
|
|
|
|
(
|
2021-03-31 10:06:41 -04:00
|
|
|
|
[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)]
|
2021-03-31 10:06:41 -04:00
|
|
|
|
[pscredential]$Credential,
|
2021-07-22 11:39:39 -04:00
|
|
|
|
|
2021-03-31 10:06:41 -04:00
|
|
|
|
[Parameter(ParameterSetName = 'Manual')]
|
|
|
|
|
|
[ValidateSet('https', 'http', IgnoreCase = $true)]
|
|
|
|
|
|
[string]$Scheme = 'https',
|
2021-07-22 11:39:39 -04:00
|
|
|
|
|
2021-03-31 10:06:41 -04:00
|
|
|
|
[Parameter(ParameterSetName = 'Manual')]
|
|
|
|
|
|
[uint16]$Port = 443,
|
2021-07-22 11:39:39 -04:00
|
|
|
|
|
2021-03-31 10:06:41 -04:00
|
|
|
|
[Parameter(ParameterSetName = 'URI',
|
|
|
|
|
|
Mandatory = $true)]
|
2021-07-21 23:02:18 +02:00
|
|
|
|
[string]$URI,
|
2021-07-22 11:39:39 -04:00
|
|
|
|
|
2021-07-21 23:02:18 +02:00
|
|
|
|
[Parameter(Mandatory = $false)]
|
2021-07-22 11:06:13 -04:00
|
|
|
|
[switch]$SkipCertificateCheck = $false,
|
2021-07-22 11:39:39 -04:00
|
|
|
|
|
2021-07-22 11:06:13 -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
|
|
|
|
|
2021-07-21 23:02:18 +02:00
|
|
|
|
$invokeParams = @{ SkipCertificateCheck = $SkipCertificateCheck; }
|
2021-07-22 11:39:39 -04:00
|
|
|
|
|
2021-07-21 23:02:18 +02: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
|
|
|
|
|
2021-07-21 23:02:18 +02:00
|
|
|
|
#for PowerShell (<=) 5 (Desktop), Enable TLS 1.1, 1.2 and Disable SSL chain trust
|
|
|
|
|
|
if ("Desktop" -eq $PSVersionTable.PsEdition) {
|
2021-09-10 17:31:15 +02:00
|
|
|
|
#Add System.web (Need for ParseQueryString)
|
|
|
|
|
|
Add-Type -AssemblyName System.Web
|
2021-07-21 23:02:18 +02:00
|
|
|
|
#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
|
|
|
|
|
2021-03-31 10:06:41 -04:00
|
|
|
|
switch ($PSCmdlet.ParameterSetName) {
|
|
|
|
|
|
'Manual' {
|
|
|
|
|
|
$uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port)
|
|
|
|
|
|
}
|
2021-07-22 11:39:39 -04:00
|
|
|
|
|
2021-03-31 10:06:41 -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
|
|
|
|
|
2021-03-31 10:06:41 -04:00
|
|
|
|
$null = Set-NetboxHostName -Hostname $uriBuilder.Host
|
2021-07-21 23:02:18 +02:00
|
|
|
|
$null = Set-NetboxCredential -Credential $Credential
|
2021-03-31 10:06:41 -04:00
|
|
|
|
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
|
|
|
|
|
|
$null = Set-NetboxHostPort -Port $uriBuilder.Port
|
2021-07-21 23:02:18 +02:00
|
|
|
|
$null = Set-NetboxInvokeParams -invokeParams $invokeParams
|
2021-07-22 11:06:13 -04:00
|
|
|
|
$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
|
|
|
|
|
2021-07-22 11:52:57 -04:00
|
|
|
|
# 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"
|
|
|
|
|
|
$script:NetboxConfig.NetboxVersion = Get-NetboxVersion
|
|
|
|
|
|
if ([version]$script:NetboxConfig.NetboxVersion.'netbox-version' -lt 2.8) {
|
2020-05-04 12:36:46 -04:00
|
|
|
|
$Script:NetboxConfig.Connected = $false
|
2021-07-22 11:52:57 -04:00
|
|
|
|
throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.NetboxVersion.'netbox-version')"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Write-Verbose "Found compatible version [$($script:NetboxConfig.NetboxVersion.'netbox-version')]!"
|
2020-05-04 12:36:46 -04:00
|
|
|
|
}
|
2023-02-16 09:45:51 -05:00
|
|
|
|
|
|
|
|
|
|
$script:NetboxConfig.ContentTypes = Get-NetboxContentType -Limit 500
|
|
|
|
|
|
|
2020-05-04 12:36:46 -04:00
|
|
|
|
$script:NetboxConfig.Connected = $true
|
|
|
|
|
|
Write-Verbose "Successfully connected!"
|
2021-07-22 11:39:39 -04:00
|
|
|
|
|
2020-05-04 12:36:46 -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"
|
|
|
|
|
|
}
|