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
|
||||
)
|
||||
|
||||
$creds = Get-NetboxCredentials
|
||||
$creds = Get-NetboxCredential
|
||||
|
||||
$Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ function GetNetboxConfigVariable {
|
|||
}
|
||||
|
||||
function Set-NetboxHostName {
|
||||
[CmdletBinding(ConfirmImpact = 'Medium',
|
||||
[CmdletBinding(ConfirmImpact = 'Low',
|
||||
SupportsShouldProcess = $true)]
|
||||
[OutputType([string])]
|
||||
param
|
||||
|
|
@ -45,8 +45,10 @@ function Set-NetboxHostName {
|
|||
[string]$Hostname
|
||||
)
|
||||
|
||||
$script:NetboxConfig.Hostname = $Hostname.Trim()
|
||||
$script:NetboxConfig.Hostname
|
||||
if ($PSCmdlet.ShouldProcess('Netbox Hostname', 'Set')) {
|
||||
$script:NetboxConfig.Hostname = $Hostname.Trim()
|
||||
$script:NetboxConfig.Hostname
|
||||
}
|
||||
}
|
||||
|
||||
function Get-NetboxHostname {
|
||||
|
|
@ -61,50 +63,49 @@ function Get-NetboxHostname {
|
|||
$script:NetboxConfig.Hostname
|
||||
}
|
||||
|
||||
function Set-NetboxCredentials {
|
||||
function Set-NetboxCredential {
|
||||
[CmdletBinding(DefaultParameterSetName = 'CredsObject',
|
||||
ConfirmImpact = 'Medium',
|
||||
ConfirmImpact = 'Low',
|
||||
SupportsShouldProcess = $true)]
|
||||
[OutputType([pscredential], ParameterSetName = 'CredsObject')]
|
||||
[OutputType([pscredential], ParameterSetName = 'UserPass')]
|
||||
[OutputType([pscredential])]
|
||||
param
|
||||
(
|
||||
[Parameter(ParameterSetName = 'CredsObject',
|
||||
Mandatory = $true)]
|
||||
[pscredential]$Credentials,
|
||||
[pscredential]$Credential,
|
||||
|
||||
[Parameter(ParameterSetName = 'UserPass',
|
||||
Mandatory = $true)]
|
||||
[string]$Token
|
||||
[securestring]$Token
|
||||
)
|
||||
|
||||
switch ($PsCmdlet.ParameterSetName) {
|
||||
'CredsObject' {
|
||||
$script:NetboxConfig.Credentials = $Credentials
|
||||
break
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
'UserPass' {
|
||||
$securePW = ConvertTo-SecureString $Token -AsPlainText -Force
|
||||
$script:NetboxConfig.Credentials = [System.Management.Automation.PSCredential]::new('notapplicable', $securePW)
|
||||
break
|
||||
}
|
||||
$script:NetboxConfig.Credential
|
||||
}
|
||||
|
||||
$script:NetboxConfig.Credentials
|
||||
}
|
||||
|
||||
function Get-NetboxCredentials {
|
||||
function Get-NetboxCredential {
|
||||
[CmdletBinding()]
|
||||
[OutputType([pscredential])]
|
||||
param ()
|
||||
|
||||
if (-not $script:NetboxConfig.Credentials) {
|
||||
throw "Netbox Credentials not set! You may set with Set-NetboxCredentials"
|
||||
if (-not $script:NetboxConfig.Credential) {
|
||||
throw "Netbox Credentials not set! You may set with Set-NetboxCredential"
|
||||
}
|
||||
|
||||
$script:NetboxConfig.Credentials
|
||||
$script:NetboxConfig.Credential
|
||||
}
|
||||
|
||||
function VerifyAPIConnectivity {
|
||||
|
|
@ -153,7 +154,7 @@ function Connect-NetboxAPI {
|
|||
|
||||
if (-not $Credentials) {
|
||||
try {
|
||||
$Credentials = Get-NetboxCredentials -ErrorAction Stop
|
||||
$Credentials = Get-NetboxCredential -ErrorAction Stop
|
||||
} catch {
|
||||
# Credentials are not set... Try to obtain from the user
|
||||
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-NetboxCredentials -Credentials $Credentials
|
||||
$null = Set-NetboxCredential -Credential $Credentials
|
||||
|
||||
try {
|
||||
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))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,17 +34,17 @@ Describe "Setup tests" -Tag 'Core', 'Setup' -Fixture {
|
|||
}
|
||||
|
||||
It "Throws an error for empty credentials" {
|
||||
{ Get-NetboxCredentials } | Should -Throw
|
||||
{ Get-NetboxCredential } | Should -Throw
|
||||
}
|
||||
|
||||
Context "Plain text credentials" {
|
||||
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" {
|
||||
$Creds = Set-NetboxCredentials -Token "faketoken"
|
||||
(Get-NetboxCredentials).GetNetworkCredential().Password | Should -BeExactly "faketoken"
|
||||
$Creds = Set-NetboxCredential -Token (ConvertTo-SecureString -String "faketoken" -Force -AsPlainText)
|
||||
(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))
|
||||
|
||||
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" {
|
||||
(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))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue