mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-13 01:42:28 +00:00
Begin DCIM cmdlets and tests
This commit is contained in:
parent
b8bfcf33e3
commit
0d70d51a3a
5 changed files with 184 additions and 1 deletions
127
Functions/DCIM/DCIM.ps1
Normal file
127
Functions/DCIM/DCIM.ps1
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<#
|
||||
.NOTES
|
||||
===========================================================================
|
||||
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152
|
||||
Created on: 5/22/2018 4:47 PM
|
||||
Created by: Ben Claussen
|
||||
Organization: NEOnet
|
||||
Filename: DCIM.ps1
|
||||
===========================================================================
|
||||
.DESCRIPTION
|
||||
A description of the file.
|
||||
#>
|
||||
|
||||
function Get-NetboxDCIMChoices {
|
||||
[CmdletBinding()]
|
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification = "These are literally 'choices' in Netbox")]
|
||||
param ()
|
||||
|
||||
$uriSegments = [System.Collections.ArrayList]::new(@('dcim', '_choices'))
|
||||
|
||||
$uri = BuildNewURI -Segments $uriSegments -Parameters $Parameters
|
||||
|
||||
InvokeNetboxRequest -URI $uri
|
||||
}
|
||||
|
||||
function ValidateDCIMChoice {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Internal function to validate provided values for static choices
|
||||
|
||||
.DESCRIPTION
|
||||
When users connect to the API, choices for each major object are cached to the config variable.
|
||||
These values are then utilized to validate if the provided value from a user is valid.
|
||||
|
||||
.PARAMETER ProvidedValue
|
||||
The value to validate against static choices
|
||||
|
||||
.PARAMETER PowerConnectionStatus
|
||||
Validate against power connection status values
|
||||
|
||||
.PARAMETER InterfaceTemplateFormFactor
|
||||
Validate against interface template form factor values
|
||||
|
||||
.PARAMETER InterfaceConnectionStatus
|
||||
Validate against interface connection status values
|
||||
|
||||
.PARAMETER InterfaceFormFactor
|
||||
Validate against interface form factor values
|
||||
|
||||
.PARAMETER ConsolePortConnectionStatus
|
||||
Validate against console port connection status values
|
||||
|
||||
.PARAMETER DeviceStatus
|
||||
Validate against device status values
|
||||
|
||||
.PARAMETER DeviceFace
|
||||
Validate against device face values
|
||||
|
||||
.PARAMETER RackType
|
||||
Validate against rack type values
|
||||
|
||||
.PARAMETER RackWidth
|
||||
Validate against rack width values.
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\> ValidateDCIMChoice -ProvidedValue 'rear' -DeviceFace
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\> ValidateDCIMChoice -ProvidedValue 'middle' -DeviceFace
|
||||
>> Invalid value middle for device:face. Must be one of: 0, 1, Front, Rear
|
||||
|
||||
.OUTPUTS
|
||||
This function returns the integer value if valid. Otherwise, it will throw an error.
|
||||
|
||||
.NOTES
|
||||
Additional information about the function.
|
||||
|
||||
.FUNCTIONALITY
|
||||
This cmdlet is intended to be used internally and not exposed to the user
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
[OutputType([uint16])]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[object]$ProvidedValue,
|
||||
|
||||
[Parameter(ParameterSetName = 'power-port:connection_status',
|
||||
Mandatory = $true)]
|
||||
[switch]$PowerConnectionStatus,
|
||||
|
||||
[Parameter(ParameterSetName = 'interface-template:form_factor',
|
||||
Mandatory = $true)]
|
||||
[switch]$InterfaceTemplateFormFactor,
|
||||
|
||||
[Parameter(ParameterSetName = 'interface-connection:connection_status',
|
||||
Mandatory = $true)]
|
||||
[switch]$InterfaceConnectionStatus,
|
||||
|
||||
[Parameter(ParameterSetName = 'interface:form_factor',
|
||||
Mandatory = $true)]
|
||||
[switch]$InterfaceFormFactor,
|
||||
|
||||
[Parameter(ParameterSetName = 'console-port:connection_status',
|
||||
Mandatory = $true)]
|
||||
[switch]$ConsolePortConnectionStatus,
|
||||
|
||||
[Parameter(ParameterSetName = 'device:status',
|
||||
Mandatory = $true)]
|
||||
[switch]$DeviceStatus,
|
||||
|
||||
[Parameter(ParameterSetName = 'device:face',
|
||||
Mandatory = $true)]
|
||||
[switch]$DeviceFace,
|
||||
|
||||
[Parameter(ParameterSetName = 'rack:type',
|
||||
Mandatory = $true)]
|
||||
[switch]$RackType,
|
||||
|
||||
[Parameter(ParameterSetName = 'rack:width',
|
||||
Mandatory = $true)]
|
||||
[switch]$RackWidth
|
||||
)
|
||||
|
||||
ValidateChoice -MajorObject 'DCIM' -ChoiceName $PSCmdlet.ParameterSetName -ProvidedValue $ProvidedValue
|
||||
}
|
||||
|
|
@ -183,7 +183,7 @@ function Connect-NetboxAPI {
|
|||
|
||||
Write-Verbose "Caching static choices"
|
||||
$script:NetboxConfig.Choices.Circuits = Get-NetboxCircuitsChoices
|
||||
#$script:NetboxConfig.Choices.DCIM = Get-NetboxDCIMChoices # Not completed yet
|
||||
$script:NetboxConfig.Choices.DCIM = Get-NetboxDCIMChoices # Not completed yet
|
||||
$script:NetboxConfig.Choices.Extras = Get-NetboxExtrasChoices
|
||||
$script:NetboxConfig.Choices.IPAM = Get-NetboxIPAMChoices
|
||||
#$script:NetboxConfig.Choices.Secrets = Get-NetboxSecretsChoices # Not completed yet
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@
|
|||
<File Build="0" Shared="True" ReferenceFunction="Invoke-IPAM_ps1">Functions\IPAM\IPAM.ps1</File>
|
||||
<File Build="2">Tests\IPAMChoices.json</File>
|
||||
<File Build="2">Tests\VirtualizationChoices.json</File>
|
||||
<File Build="0" Shared="True" ReferenceFunction="Invoke-DCIM_ps1">Functions\DCIM\DCIM.ps1</File>
|
||||
<File Build="2" Shared="True" ReferenceFunction="Invoke-DCIM_Tests_ps1">Tests\DCIM.Tests.ps1</File>
|
||||
</Files>
|
||||
<StartupScript>R:\Netbox\NetboxPS\Test-Module.ps1</StartupScript>
|
||||
</Project>
|
||||
53
Tests/DCIM.Tests.ps1
Normal file
53
Tests/DCIM.Tests.ps1
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<#
|
||||
.NOTES
|
||||
===========================================================================
|
||||
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152
|
||||
Created on: 5/22/2018 4:48 PM
|
||||
Created by: Ben Claussen
|
||||
Organization: NEOnet
|
||||
Filename: DCIM.Tests.ps1
|
||||
===========================================================================
|
||||
.DESCRIPTION
|
||||
DCIM tests.
|
||||
#>
|
||||
|
||||
Import-Module Pester
|
||||
Remove-Module NetboxPS -Force -ErrorAction SilentlyContinue
|
||||
|
||||
$ModulePath = "$PSScriptRoot\..\dist\NetboxPS.psd1"
|
||||
|
||||
if (Test-Path $ModulePath) {
|
||||
Import-Module $ModulePath -ErrorAction Stop
|
||||
}
|
||||
|
||||
Describe -Name "DCIM Tests" -Tag 'DCIM' -Fixture {
|
||||
Mock -CommandName 'CheckNetboxIsConnected' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
||||
return $true
|
||||
}
|
||||
|
||||
Mock -CommandName 'Invoke-RestMethod' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
||||
# Return a hashtable of the items we would normally pass to Invoke-RestMethod
|
||||
return [ordered]@{
|
||||
'Method' = $Method
|
||||
'Uri' = $Uri
|
||||
'Headers' = $Headers
|
||||
'Timeout' = $Timeout
|
||||
'ContentType' = $ContentType
|
||||
'Body' = $Body
|
||||
}
|
||||
}
|
||||
|
||||
Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
||||
return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force))
|
||||
}
|
||||
|
||||
Mock -CommandName 'Get-NetboxHostname' -Verifiable -ModuleName 'NetboxPS' -MockWith {
|
||||
return 'netbox.domain.com'
|
||||
}
|
||||
|
||||
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
|
||||
$script:NetboxConfig.Choices.DCIM = (Get-Content "$PSScriptRoot\DCIMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
1
Tests/DCIMChoices.json
Normal file
1
Tests/DCIMChoices.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"device:face":[{"value":0,"label":"Front"},{"value":1,"label":"Rear"}],"device:status":[{"value":1,"label":"Active"},{"value":0,"label":"Offline"},{"value":2,"label":"Planned"},{"value":3,"label":"Staged"},{"value":4,"label":"Failed"},{"value":5,"label":"Inventory"}],"console-port:connection_status":[{"value":false,"label":"Planned"},{"value":true,"label":"Connected"}],"interface:form_factor":[{"value":0,"label":"Virtual"},{"value":200,"label":"Link Aggregation Group (LAG)"},{"value":800,"label":"100BASE-TX (10/100ME)"},{"value":1000,"label":"1000BASE-T (1GE)"},{"value":1150,"label":"10GBASE-T (10GE)"},{"value":1170,"label":"10GBASE-CX4 (10GE)"},{"value":1050,"label":"GBIC (1GE)"},{"value":1100,"label":"SFP (1GE)"},{"value":1200,"label":"SFP+ (10GE)"},{"value":1300,"label":"XFP (10GE)"},{"value":1310,"label":"XENPAK (10GE)"},{"value":1320,"label":"X2 (10GE)"},{"value":1350,"label":"SFP28 (25GE)"},{"value":1400,"label":"QSFP+ (40GE)"},{"value":1500,"label":"CFP (100GE)"},{"value":1510,"label":"CFP2 (100GE)"},{"value":1520,"label":"CFP4 (100GE)"},{"value":1550,"label":"Cisco CPAK (100GE)"},{"value":1600,"label":"QSFP28 (100GE)"},{"value":2600,"label":"IEEE 802.11a"},{"value":2610,"label":"IEEE 802.11b/g"},{"value":2620,"label":"IEEE 802.11n"},{"value":2630,"label":"IEEE 802.11ac"},{"value":2640,"label":"IEEE 802.11ad"},{"value":3010,"label":"SFP (1GFC)"},{"value":3020,"label":"SFP (2GFC)"},{"value":3040,"label":"SFP (4GFC)"},{"value":3080,"label":"SFP+ (8GFC)"},{"value":3160,"label":"SFP+ (16GFC)"},{"value":4000,"label":"T1 (1.544 Mbps)"},{"value":4010,"label":"E1 (2.048 Mbps)"},{"value":4040,"label":"T3 (45 Mbps)"},{"value":4050,"label":"E3 (34 Mbps)"},{"value":5000,"label":"Cisco StackWise"},{"value":5050,"label":"Cisco StackWise Plus"},{"value":5100,"label":"Cisco FlexStack"},{"value":5150,"label":"Cisco FlexStack Plus"},{"value":5200,"label":"Juniper VCP"},{"value":32767,"label":"Other"}],"interface-connection:connection_status":[{"value":false,"label":"Planned"},{"value":true,"label":"Connected"}],"interface-template:form_factor":[{"value":0,"label":"Virtual"},{"value":200,"label":"Link Aggregation Group (LAG)"},{"value":800,"label":"100BASE-TX (10/100ME)"},{"value":1000,"label":"1000BASE-T (1GE)"},{"value":1150,"label":"10GBASE-T (10GE)"},{"value":1170,"label":"10GBASE-CX4 (10GE)"},{"value":1050,"label":"GBIC (1GE)"},{"value":1100,"label":"SFP (1GE)"},{"value":1200,"label":"SFP+ (10GE)"},{"value":1300,"label":"XFP (10GE)"},{"value":1310,"label":"XENPAK (10GE)"},{"value":1320,"label":"X2 (10GE)"},{"value":1350,"label":"SFP28 (25GE)"},{"value":1400,"label":"QSFP+ (40GE)"},{"value":1500,"label":"CFP (100GE)"},{"value":1510,"label":"CFP2 (100GE)"},{"value":1520,"label":"CFP4 (100GE)"},{"value":1550,"label":"Cisco CPAK (100GE)"},{"value":1600,"label":"QSFP28 (100GE)"},{"value":2600,"label":"IEEE 802.11a"},{"value":2610,"label":"IEEE 802.11b/g"},{"value":2620,"label":"IEEE 802.11n"},{"value":2630,"label":"IEEE 802.11ac"},{"value":2640,"label":"IEEE 802.11ad"},{"value":3010,"label":"SFP (1GFC)"},{"value":3020,"label":"SFP (2GFC)"},{"value":3040,"label":"SFP (4GFC)"},{"value":3080,"label":"SFP+ (8GFC)"},{"value":3160,"label":"SFP+ (16GFC)"},{"value":4000,"label":"T1 (1.544 Mbps)"},{"value":4010,"label":"E1 (2.048 Mbps)"},{"value":4040,"label":"T3 (45 Mbps)"},{"value":4050,"label":"E3 (34 Mbps)"},{"value":5000,"label":"Cisco StackWise"},{"value":5050,"label":"Cisco StackWise Plus"},{"value":5100,"label":"Cisco FlexStack"},{"value":5150,"label":"Cisco FlexStack Plus"},{"value":5200,"label":"Juniper VCP"},{"value":32767,"label":"Other"}],"power-port:connection_status":[{"value":false,"label":"Planned"},{"value":true,"label":"Connected"}],"rack:type":[{"value":100,"label":"2-post frame"},{"value":200,"label":"4-post frame"},{"value":300,"label":"4-post cabinet"},{"value":1000,"label":"Wall-mounted frame"},{"value":1100,"label":"Wall-mounted cabinet"}],"rack:width":[{"value":19,"label":"19 inches"},{"value":23,"label":"23 inches"}]}
|
||||
Loading…
Add table
Reference in a new issue