NetboxPS/Functions/Setup.ps1

170 lines
4.8 KiB
PowerShell
Raw Normal View History

2018-05-11 15:54:43 -04:00
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.148
Created on: 2/28/2018 3:33 PM
Created by: Ben Claussen
Organization: NEOnet
Filename: Setup.ps1
===========================================================================
.DESCRIPTION
These are the function used to setup the environment for connecting
to a Netbox API
#>
function Set-NetboxHostName {
[CmdletBinding(ConfirmImpact = 'Low',
2018-05-18 12:14:27 -04:00
SupportsShouldProcess = $true)]
[OutputType([string])]
param
(
[Parameter(Mandatory = $true)]
[string]$Hostname
)
if ($PSCmdlet.ShouldProcess('Netbox Hostname', 'Set')) {
$script:NetboxConfig.Hostname = $Hostname.Trim()
$script:NetboxConfig.Hostname
}
2018-05-11 15:54:43 -04:00
}
function Get-NetboxHostname {
[CmdletBinding()]
param ()
Write-Verbose "Getting Netbox hostname"
2018-05-18 12:14:27 -04:00
if ($null -eq $script:NetboxConfig.Hostname) {
2018-05-11 15:54:43 -04:00
throw "Netbox Hostname is not set! You may set it with Set-NetboxHostname -Hostname 'hostname.domain.tld'"
}
$script:NetboxConfig.Hostname
}
function Set-NetboxCredential {
2018-05-18 12:14:27 -04:00
[CmdletBinding(DefaultParameterSetName = 'CredsObject',
ConfirmImpact = 'Low',
2018-05-18 12:14:27 -04:00
SupportsShouldProcess = $true)]
[OutputType([pscredential])]
param
(
[Parameter(ParameterSetName = 'CredsObject',
Mandatory = $true)]
[pscredential]$Credential,
2018-05-18 12:14:27 -04:00
[Parameter(ParameterSetName = 'UserPass',
Mandatory = $true)]
[securestring]$Token
2018-05-18 12:14:27 -04:00
)
if ($PSCmdlet.ShouldProcess('Netbox Credentials', 'Set')) {
switch ($PsCmdlet.ParameterSetName) {
'CredsObject' {
$script:NetboxConfig.Credential = $Credential
break
}
'UserPass' {
$script:NetboxConfig.Credential = [System.Management.Automation.PSCredential]::new('notapplicable', $Token)
break
}
2018-05-18 12:14:27 -04:00
}
$script:NetboxConfig.Credential
2018-05-18 12:14:27 -04:00
}
2018-05-11 15:54:43 -04:00
}
function Get-NetboxCredential {
2018-05-11 15:54:43 -04:00
[CmdletBinding()]
[OutputType([pscredential])]
param ()
if (-not $script:NetboxConfig.Credential) {
throw "Netbox Credentials not set! You may set with Set-NetboxCredential"
2018-05-11 15:54:43 -04:00
}
$script:NetboxConfig.Credential
2018-05-11 15:54:43 -04:00
}
function Connect-NetboxAPI {
<#
.SYNOPSIS
Connects to the Netbox API and ensures Credential work properly
2018-05-11 15:54:43 -04:00
.DESCRIPTION
A detailed description of the Connect-NetboxAPI function.
.PARAMETER Hostname
A description of the Hostname parameter.
.PARAMETER Credential
A description of the Credential parameter.
2018-05-11 15:54:43 -04:00
.EXAMPLE
PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com"
This will prompt for Credential, then proceed to attempt a connection to Netbox
2018-05-11 15:54:43 -04:00
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$Hostname,
[Parameter(Mandatory = $false)]
[pscredential]$Credential
2018-05-11 15:54:43 -04:00
)
if (-not $Credential) {
2018-05-11 15:54:43 -04:00
try {
$Credential = Get-NetboxCredential -ErrorAction Stop
2018-05-11 15:54:43 -04:00
} catch {
# Credentials are not set... Try to obtain from the user
if (-not ($Credential = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) {
2018-05-11 15:54:43 -04:00
throw "Token is necessary to connect to a Netbox API."
}
}
}
$null = Set-NetboxHostName -Hostname $Hostname
$null = Set-NetboxCredential -Credential $Credential
2018-05-11 15:54:43 -04:00
try {
Write-Verbose "Verifying API connectivity..."
2018-05-18 12:14:27 -04:00
$null = VerifyAPIConnectivity
2018-05-11 15:54:43 -04:00
$script:NetboxConfig.Connected = $true
Write-Verbose "Successfully connected!"
} catch {
Write-Verbose "Failed to connect. Generating error"
Write-Verbose $_.Exception.Message
if (($_.Exception.Response) -and ($_.Exception.Response.StatusCode -eq 403)) {
throw "Invalid token"
} else {
throw $_
}
}
2018-05-14 16:46:12 -04:00
Write-Verbose "Caching static choices"
2018-05-11 15:54:43 -04:00
$script:NetboxConfig.Choices.Circuits = Get-NetboxCircuitsChoices
2018-05-23 11:10:40 -04:00
$script:NetboxConfig.Choices.DCIM = Get-NetboxDCIMChoices # Not completed yet
2018-05-11 15:54:43 -04:00
$script:NetboxConfig.Choices.Extras = Get-NetboxExtrasChoices
$script:NetboxConfig.Choices.IPAM = Get-NetboxIPAMChoices
2018-05-14 16:46:12 -04:00
#$script:NetboxConfig.Choices.Secrets = Get-NetboxSecretsChoices # Not completed yet
#$script:NetboxConfig.Choices.Tenancy = Get-NetboxTenancyChoices
2018-05-11 15:54:43 -04:00
$script:NetboxConfig.Choices.Virtualization = Get-NetboxVirtualizationChoices
2018-05-14 16:46:12 -04:00
Write-Verbose "Connection process completed"
2018-05-11 15:54:43 -04:00
}