mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-13 18:02:29 +00:00
Connect(Setup): Fix indent (using Visual code Formatter)
This commit is contained in:
parent
1f89363cbd
commit
b164ffe0b4
1 changed files with 32 additions and 29 deletions
|
|
@ -1,75 +1,76 @@
|
||||||
function Connect-NetboxAPI {
|
function Connect-NetboxAPI {
|
||||||
<#
|
<#
|
||||||
.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"
|
||||||
|
|
||||||
.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
|
||||||
)
|
)
|
||||||
|
|
||||||
if (-not $Credential) {
|
if (-not $Credential) {
|
||||||
try {
|
try {
|
||||||
$Credential = Get-NetboxCredential -ErrorAction Stop
|
$Credential = Get-NetboxCredential -ErrorAction Stop
|
||||||
} catch {
|
}
|
||||||
|
catch {
|
||||||
# Credentials are not set... Try to obtain from the user
|
# Credentials are not set... Try to obtain from the user
|
||||||
if (-not ($Credential = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) {
|
if (-not ($Credential = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) {
|
||||||
throw "Token is necessary to connect to a Netbox API."
|
throw "Token is necessary to connect to a Netbox API."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$null = Set-NetboxCredential -Credential $Credential
|
$null = Set-NetboxCredential -Credential $Credential
|
||||||
|
|
||||||
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)) {
|
||||||
|
|
@ -77,35 +78,37 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$null = Set-NetboxHostName -Hostname $uriBuilder.Host
|
$null = Set-NetboxHostName -Hostname $uriBuilder.Host
|
||||||
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
|
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
|
||||||
$null = Set-NetboxHostPort -Port $uriBuilder.Port
|
$null = Set-NetboxHostPort -Port $uriBuilder.Port
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Write-Verbose "Verifying API connectivity..."
|
Write-Verbose "Verifying API connectivity..."
|
||||||
$null = VerifyAPIConnectivity
|
$null = VerifyAPIConnectivity
|
||||||
} catch {
|
}
|
||||||
|
catch {
|
||||||
Write-Verbose "Failed to connect. Generating error"
|
Write-Verbose "Failed to connect. Generating error"
|
||||||
Write-Verbose $_.Exception.Message
|
Write-Verbose $_.Exception.Message
|
||||||
if (($_.Exception.Response) -and ($_.Exception.Response.StatusCode -eq 403)) {
|
if (($_.Exception.Response) -and ($_.Exception.Response.StatusCode -eq 403)) {
|
||||||
throw "Invalid token"
|
throw "Invalid token"
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
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
|
||||||
|
|
@ -114,6 +117,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"
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue