mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-13 18:02:29 +00:00
Fix ScriptAnalyzer issues
- Get/Set-NetboxCredential singular noun - Set-NetboxCredential token use securestring - Set-NetboxCredential/Hostname to use shouldProcess - Update tests
This commit is contained in:
parent
7ba549828d
commit
4df43e6d6d
6 changed files with 37 additions and 36 deletions
|
|
@ -286,7 +286,7 @@ function InvokeNetboxRequest {
|
||||||
[switch]$Raw
|
[switch]$Raw
|
||||||
)
|
)
|
||||||
|
|
||||||
$creds = Get-NetboxCredentials
|
$creds = Get-NetboxCredential
|
||||||
|
|
||||||
$Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password
|
$Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ function GetNetboxConfigVariable {
|
||||||
}
|
}
|
||||||
|
|
||||||
function Set-NetboxHostName {
|
function Set-NetboxHostName {
|
||||||
[CmdletBinding(ConfirmImpact = 'Medium',
|
[CmdletBinding(ConfirmImpact = 'Low',
|
||||||
SupportsShouldProcess = $true)]
|
SupportsShouldProcess = $true)]
|
||||||
[OutputType([string])]
|
[OutputType([string])]
|
||||||
param
|
param
|
||||||
|
|
@ -45,8 +45,10 @@ function Set-NetboxHostName {
|
||||||
[string]$Hostname
|
[string]$Hostname
|
||||||
)
|
)
|
||||||
|
|
||||||
$script:NetboxConfig.Hostname = $Hostname.Trim()
|
if ($PSCmdlet.ShouldProcess('Netbox Hostname', 'Set')) {
|
||||||
$script:NetboxConfig.Hostname
|
$script:NetboxConfig.Hostname = $Hostname.Trim()
|
||||||
|
$script:NetboxConfig.Hostname
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Get-NetboxHostname {
|
function Get-NetboxHostname {
|
||||||
|
|
@ -61,50 +63,49 @@ function Get-NetboxHostname {
|
||||||
$script:NetboxConfig.Hostname
|
$script:NetboxConfig.Hostname
|
||||||
}
|
}
|
||||||
|
|
||||||
function Set-NetboxCredentials {
|
function Set-NetboxCredential {
|
||||||
[CmdletBinding(DefaultParameterSetName = 'CredsObject',
|
[CmdletBinding(DefaultParameterSetName = 'CredsObject',
|
||||||
ConfirmImpact = 'Medium',
|
ConfirmImpact = 'Low',
|
||||||
SupportsShouldProcess = $true)]
|
SupportsShouldProcess = $true)]
|
||||||
[OutputType([pscredential], ParameterSetName = 'CredsObject')]
|
|
||||||
[OutputType([pscredential], ParameterSetName = 'UserPass')]
|
|
||||||
[OutputType([pscredential])]
|
[OutputType([pscredential])]
|
||||||
param
|
param
|
||||||
(
|
(
|
||||||
[Parameter(ParameterSetName = 'CredsObject',
|
[Parameter(ParameterSetName = 'CredsObject',
|
||||||
Mandatory = $true)]
|
Mandatory = $true)]
|
||||||
[pscredential]$Credentials,
|
[pscredential]$Credential,
|
||||||
|
|
||||||
[Parameter(ParameterSetName = 'UserPass',
|
[Parameter(ParameterSetName = 'UserPass',
|
||||||
Mandatory = $true)]
|
Mandatory = $true)]
|
||||||
[string]$Token
|
[securestring]$Token
|
||||||
)
|
)
|
||||||
|
|
||||||
switch ($PsCmdlet.ParameterSetName) {
|
if ($PSCmdlet.ShouldProcess('Netbox Credentials', 'Set')) {
|
||||||
'CredsObject' {
|
switch ($PsCmdlet.ParameterSetName) {
|
||||||
$script:NetboxConfig.Credentials = $Credentials
|
'CredsObject' {
|
||||||
break
|
$script:NetboxConfig.Credential = $Credential
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
'UserPass' {
|
||||||
|
$script:NetboxConfig.Credential = [System.Management.Automation.PSCredential]::new('notapplicable', $Token)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
'UserPass' {
|
$script:NetboxConfig.Credential
|
||||||
$securePW = ConvertTo-SecureString $Token -AsPlainText -Force
|
|
||||||
$script:NetboxConfig.Credentials = [System.Management.Automation.PSCredential]::new('notapplicable', $securePW)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$script:NetboxConfig.Credentials
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Get-NetboxCredentials {
|
function Get-NetboxCredential {
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
[OutputType([pscredential])]
|
[OutputType([pscredential])]
|
||||||
param ()
|
param ()
|
||||||
|
|
||||||
if (-not $script:NetboxConfig.Credentials) {
|
if (-not $script:NetboxConfig.Credential) {
|
||||||
throw "Netbox Credentials not set! You may set with Set-NetboxCredentials"
|
throw "Netbox Credentials not set! You may set with Set-NetboxCredential"
|
||||||
}
|
}
|
||||||
|
|
||||||
$script:NetboxConfig.Credentials
|
$script:NetboxConfig.Credential
|
||||||
}
|
}
|
||||||
|
|
||||||
function VerifyAPIConnectivity {
|
function VerifyAPIConnectivity {
|
||||||
|
|
@ -153,7 +154,7 @@ function Connect-NetboxAPI {
|
||||||
|
|
||||||
if (-not $Credentials) {
|
if (-not $Credentials) {
|
||||||
try {
|
try {
|
||||||
$Credentials = Get-NetboxCredentials -ErrorAction Stop
|
$Credentials = 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 ($Credentials = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) {
|
if (-not ($Credentials = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) {
|
||||||
|
|
@ -163,7 +164,7 @@ function Connect-NetboxAPI {
|
||||||
}
|
}
|
||||||
|
|
||||||
$null = Set-NetboxHostName -Hostname $Hostname
|
$null = Set-NetboxHostName -Hostname $Hostname
|
||||||
$null = Set-NetboxCredentials -Credentials $Credentials
|
$null = Set-NetboxCredential -Credential $Credentials
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Write-Verbose "Verifying API connectivity..."
|
Write-Verbose "Verifying API connectivity..."
|
||||||
|
|
|
||||||
|
|
@ -162,7 +162,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
||||||
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
||||||
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,17 @@ Describe "Setup tests" -Tag 'Core', 'Setup' -Fixture {
|
||||||
}
|
}
|
||||||
|
|
||||||
It "Throws an error for empty credentials" {
|
It "Throws an error for empty credentials" {
|
||||||
{ Get-NetboxCredentials } | Should -Throw
|
{ Get-NetboxCredential } | Should -Throw
|
||||||
}
|
}
|
||||||
|
|
||||||
Context "Plain text credentials" {
|
Context "Plain text credentials" {
|
||||||
It "Sets the credentials using plain text" {
|
It "Sets the credentials using plain text" {
|
||||||
$Creds = Set-NetboxCredentials -Token "faketoken" | Should -BeOfType [pscredential]
|
Set-NetboxCredential -Token (ConvertTo-SecureString -String "faketoken" -Force -AsPlainText) | Should -BeOfType [pscredential]
|
||||||
}
|
}
|
||||||
|
|
||||||
It "Checks the set credentials" {
|
It "Checks the set credentials" {
|
||||||
$Creds = Set-NetboxCredentials -Token "faketoken"
|
$Creds = Set-NetboxCredential -Token (ConvertTo-SecureString -String "faketoken" -Force -AsPlainText)
|
||||||
(Get-NetboxCredentials).GetNetworkCredential().Password | Should -BeExactly "faketoken"
|
(Get-NetboxCredential).GetNetworkCredential().Password | Should -BeExactly "faketoken"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,11 +52,11 @@ Describe "Setup tests" -Tag 'Core', 'Setup' -Fixture {
|
||||||
$Creds = [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
$Creds = [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
||||||
|
|
||||||
It "Sets the credentials using [pscredential]" {
|
It "Sets the credentials using [pscredential]" {
|
||||||
Set-NetboxCredentials -Credentials $Creds | Should -BeOfType [pscredential]
|
Set-NetboxCredential -Credential $Creds | Should -BeOfType [pscredential]
|
||||||
}
|
}
|
||||||
|
|
||||||
It "Checks the set credentials" {
|
It "Checks the set credentials" {
|
||||||
(Get-NetboxCredentials).GetNetworkCredential().Password | Should -BeExactly 'faketoken'
|
(Get-NetboxCredential).GetNetworkCredential().Password | Should -BeExactly 'faketoken'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
||||||
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue