mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-13 18:02:29 +00:00
Add DCIM choices and tests
- Update ValidateChoice to handle strings and booleans
This commit is contained in:
parent
462357646e
commit
b50375722e
2 changed files with 276 additions and 17 deletions
|
|
@ -213,11 +213,11 @@ function GetChoiceValidValues {
|
||||||
|
|
||||||
function ValidateChoice {
|
function ValidateChoice {
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
[OutputType([uint16])]
|
[OutputType([uint16],[string], [bool])]
|
||||||
param
|
param
|
||||||
(
|
(
|
||||||
[Parameter(Mandatory = $true)]
|
[Parameter(Mandatory = $true)]
|
||||||
[ValidateSet('Circuits', 'Extras', 'IPAM', 'Virtualization', IgnoreCase = $true)]
|
[ValidateSet('Circuits', 'DCIM', 'Extras', 'IPAM', 'Virtualization', IgnoreCase = $true)]
|
||||||
[string]$MajorObject,
|
[string]$MajorObject,
|
||||||
|
|
||||||
[Parameter(Mandatory = $true)]
|
[Parameter(Mandatory = $true)]
|
||||||
|
|
@ -231,22 +231,43 @@ function ValidateChoice {
|
||||||
|
|
||||||
Write-Verbose "Validating $ChoiceName"
|
Write-Verbose "Validating $ChoiceName"
|
||||||
Write-Verbose "Checking '$ProvidedValue' against [$($ValidValues -join ', ')]"
|
Write-Verbose "Checking '$ProvidedValue' against [$($ValidValues -join ', ')]"
|
||||||
|
|
||||||
if ($ValidValues -inotcontains $ProvidedValue) {
|
# Coercing everything to strings for matching...
|
||||||
|
# some values are integers, some are strings, some are booleans
|
||||||
|
# Join the valid values with a pipe as a delimeter, because some values have spaces
|
||||||
|
if (([string]($ValidValues -join '|') -split '\|') -inotcontains [string]$ProvidedValue) {
|
||||||
throw "Invalid value '$ProvidedValue' for '$ChoiceName'. Must be one of: $($ValidValues -join ', ')"
|
throw "Invalid value '$ProvidedValue' for '$ChoiceName'. Must be one of: $($ValidValues -join ', ')"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Convert the ProvidedValue to the integer value
|
switch -wildcard ("$MajorObject/$ChoiceName") {
|
||||||
try {
|
"Circuits" {
|
||||||
$intVal = [uint16]"$ProvidedValue"
|
# This has things that are not integers
|
||||||
} catch {
|
}
|
||||||
# It must not be a number, get the value from the label
|
|
||||||
$intVal = [uint16]$script:NetboxConfig.Choices.$MajorObject.$ChoiceName.Where({
|
"DCIM/*connection_status" {
|
||||||
$_.Label -eq $ProvidedValue
|
# This has true/false values instead of integers
|
||||||
}).Value
|
try {
|
||||||
|
$val = [bool]::Parse($ProvidedValue)
|
||||||
|
} catch {
|
||||||
|
# It must not be a true/false value
|
||||||
|
$val = $script:NetboxConfig.Choices.$MajorObject.$ChoiceName.Where({ $_.Label -eq $ProvidedValue }).Value
|
||||||
|
}
|
||||||
|
|
||||||
|
return $val
|
||||||
|
}
|
||||||
|
|
||||||
|
default {
|
||||||
|
# Convert the ProvidedValue to the integer value
|
||||||
|
try {
|
||||||
|
$intVal = [uint16]"$ProvidedValue"
|
||||||
|
} catch {
|
||||||
|
# It must not be a number, get the value from the label
|
||||||
|
$intVal = [uint16]$script:NetboxConfig.Choices.$MajorObject.$ChoiceName.Where({ $_.Label -eq $ProvidedValue }).Value
|
||||||
|
}
|
||||||
|
|
||||||
|
return $intVal
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $intVal
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -246,6 +246,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
|
||||||
Context -Name "Validating choices" -Fixture {
|
Context -Name "Validating choices" -Fixture {
|
||||||
$script:NetboxConfig.Choices.Virtualization = (Get-Content "$PSScriptRoot\VirtualizationChoices.json" -ErrorAction Stop | ConvertFrom-Json)
|
$script:NetboxConfig.Choices.Virtualization = (Get-Content "$PSScriptRoot\VirtualizationChoices.json" -ErrorAction Stop | ConvertFrom-Json)
|
||||||
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
|
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
|
||||||
|
$script:NetboxConfig.Choices.DCIM = (Get-Content "$PSScriptRoot\DCIMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
|
||||||
|
|
||||||
Context -Name "Virtualization choices" -Fixture {
|
Context -Name "Virtualization choices" -Fixture {
|
||||||
$MajorObject = 'Virtualization'
|
$MajorObject = 'Virtualization'
|
||||||
|
|
@ -467,10 +468,247 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Context -Name "DCIM choices" -Fixture {
|
||||||
|
$MajorObject = 'DCIM'
|
||||||
|
|
||||||
|
Context -Name "device:face" -Fixture {
|
||||||
|
$ChoiceName = 'device:face'
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided a name" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Front'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 0
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided an integer" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 1
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should throw because of an invalid choice" {
|
||||||
|
{
|
||||||
|
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
|
||||||
|
} | Should -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Context -Name "device:status" -Fixture {
|
||||||
|
$ChoiceName = 'device:status'
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided a name" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Active'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 1
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided an integer" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 0
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 0
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should throw because of an invalid choice" {
|
||||||
|
{
|
||||||
|
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
|
||||||
|
} | Should -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Context -Name "console-port:connection_status" -Fixture {
|
||||||
|
$ChoiceName = 'console-port:connection_status'
|
||||||
|
|
||||||
|
It "Should return a valid string when provided a name" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Planned'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [bool]
|
||||||
|
$Result | Should -Be $false
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid string when provided a string" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'false'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [bool]
|
||||||
|
$Result | Should -Be $false
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid string when provided a boolean" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue $true
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [bool]
|
||||||
|
$Result | Should -Be $true
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should throw because of an invalid choice" {
|
||||||
|
{
|
||||||
|
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
|
||||||
|
} | Should -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Context -Name "interface:form_factor" -Fixture {
|
||||||
|
$ChoiceName = 'interface:form_factor'
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided a name" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue '10GBASE-CX4 (10GE)'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 1170
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided an integer" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1500
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 1500
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should throw because of an invalid choice" {
|
||||||
|
{
|
||||||
|
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
|
||||||
|
} | Should -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Context -Name "interface-connection:connection_status" -Fixture {
|
||||||
|
$ChoiceName = 'interface-connection:connection_status'
|
||||||
|
|
||||||
|
It "Should return a valid string when provided a name" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Planned'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [bool]
|
||||||
|
$Result | Should -Be $false
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid string when provided a string" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'false'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [bool]
|
||||||
|
$Result | Should -Be $false
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid string when provided a boolean" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue $true
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [bool]
|
||||||
|
$Result | Should -Be $true
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should throw because of an invalid choice" {
|
||||||
|
{
|
||||||
|
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
|
||||||
|
} | Should -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Context -Name "interface-template:form_factor" -Fixture {
|
||||||
|
$ChoiceName = 'interface-template:form_factor'
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided a name" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue '10GBASE-CX4 (10GE)'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 1170
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided an integer" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 1500
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 1500
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should throw because of an invalid choice" {
|
||||||
|
{
|
||||||
|
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
|
||||||
|
} | Should -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Context -Name "power-port:connection_status" -Fixture {
|
||||||
|
$ChoiceName = 'power-port:connection_status'
|
||||||
|
|
||||||
|
It "Should return a valid string when provided a name" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'Planned'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [bool]
|
||||||
|
$Result | Should -Be $false
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid string when provided a string" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'false'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [bool]
|
||||||
|
$Result | Should -Be $false
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid string when provided a boolean" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue $true
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [bool]
|
||||||
|
$Result | Should -Be $true
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should throw because of an invalid choice" {
|
||||||
|
{
|
||||||
|
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
|
||||||
|
} | Should -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Context -Name "rack:type" -Fixture {
|
||||||
|
$ChoiceName = 'rack:type'
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided a name" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue '2-post frame'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 100
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided an integer" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 300
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 300
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should throw because of an invalid choice" {
|
||||||
|
{
|
||||||
|
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
|
||||||
|
} | Should -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Context -Name "rack:width" -Fixture {
|
||||||
|
$ChoiceName = 'rack:width'
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided a name" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue '19 inches'
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 19
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should return a valid integer when provided an integer" {
|
||||||
|
$Result = ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 23
|
||||||
|
|
||||||
|
$Result | Should -BeOfType [uint16]
|
||||||
|
$Result | Should -BeExactly 23
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should throw because of an invalid choice" {
|
||||||
|
{
|
||||||
|
ValidateChoice -MajorObject $MajorObject -ChoiceName $ChoiceName -ProvidedValue 'fake'
|
||||||
|
} | Should -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue