diff --git a/Functions/Helpers.ps1 b/Functions/Helpers.ps1 index bad7235..dfa11e3 100644 --- a/Functions/Helpers.ps1 +++ b/Functions/Helpers.ps1 @@ -213,11 +213,11 @@ function GetChoiceValidValues { function ValidateChoice { [CmdletBinding()] - [OutputType([uint16])] + [OutputType([uint16],[string], [bool])] param ( [Parameter(Mandatory = $true)] - [ValidateSet('Circuits', 'Extras', 'IPAM', 'Virtualization', IgnoreCase = $true)] + [ValidateSet('Circuits', 'DCIM', 'Extras', 'IPAM', 'Virtualization', IgnoreCase = $true)] [string]$MajorObject, [Parameter(Mandatory = $true)] @@ -231,22 +231,43 @@ function ValidateChoice { Write-Verbose "Validating $ChoiceName" 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 ', ')" } - # 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 + switch -wildcard ("$MajorObject/$ChoiceName") { + "Circuits" { + # This has things that are not integers + } + + "DCIM/*connection_status" { + # This has true/false values instead of integers + 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 } diff --git a/Tests/Helpers.Tests.ps1 b/Tests/Helpers.Tests.ps1 index 7195980..236d694 100644 --- a/Tests/Helpers.Tests.ps1 +++ b/Tests/Helpers.Tests.ps1 @@ -246,6 +246,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture { Context -Name "Validating choices" -Fixture { $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.DCIM = (Get-Content "$PSScriptRoot\DCIMChoices.json" -ErrorAction Stop | ConvertFrom-Json) Context -Name "Virtualization choices" -Fixture { $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 + } + } + } } - - } }