Add Interface functions and tests

This commit is contained in:
Ben Claussen 2018-05-24 16:39:45 -04:00
parent bac691c196
commit 856a51e187
4 changed files with 669 additions and 2 deletions

View file

@ -129,13 +129,13 @@ function ValidateDCIMChoice {
#region GET Commands
function Get-NetboxDCIMDevice {
[CmdletBinding()]
#region Parameters
param
(
[uint16]$Limit,
[uint16]$Offset,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[uint16[]]$Id,
[string]$Query,
@ -196,6 +196,7 @@ function Get-NetboxDCIMDevice {
[switch]$Raw
)
#endregion Parameters
if ($null -ne $Status) {
@ -307,6 +308,88 @@ function Get-NetboxDCIMDeviceRole {
}
}
}
function Get-NetboxDCIMInterface {
[CmdletBinding()]
[OutputType([pscustomobject])]
param
(
[uint16]$Limit,
[uint16]$Offset,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[uint16]$Id,
[uint16]$Name,
[object]$Form_Factor,
[bool]$Enabled,
[uint16]$MTU,
[bool]$MGMT_Only,
[string]$Device,
[uint16]$Device_Id,
[uint16]$Type,
[uint16]$LAG_Id,
[string]$MAC_Address,
[switch]$Raw
)
if ($null -ne $Form_Factor) {
$PSBoundParameters.Form_Factor = ValidateDCIMChoice -ProvidedValue $Form_Factor -InterfaceFormFactor
}
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'interfaces'))
$URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters
$URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters
InvokeNetboxRequest -URI $URI -Raw:$Raw
}
function Get-NetboxDCIMInterfaceConnection {
[CmdletBinding()]
[OutputType([pscustomobject])]
param
(
[uint16]$Limit,
[uint16]$Offset,
[uint16]$Id,
[object]$Connection_Status,
[uint16]$Site,
[uint16]$Device,
[switch]$Raw
)
if ($null -ne $Connection_Status) {
$PSBoundParameters.Connection_Status = ValidateDCIMChoice -ProvidedValue $Connection_Status -InterfaceConnectionStatus
}
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'interface-connections'))
$URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw'
$URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters
InvokeNetboxRequest -URI $URI -Raw:$Raw
}
#endregion GET Commands
@ -388,6 +471,78 @@ function New-NetboxDCIMDevice {
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST
}
function Add-NetboxDCIMInterface {
[CmdletBinding()]
[OutputType([pscustomobject])]
param
(
[Parameter(Mandatory = $true)]
[uint16]$Device,
[Parameter(Mandatory = $true)]
[string]$Name,
[bool]$Enabled,
[object]$Form_Factor,
[uint16]$MTU,
[string]$MAC_Address,
[bool]$MGMT_Only,
[uint16]$LAG,
[string]$Description,
[ValidateSet('Access', 'Tagged', 'Tagged All', '100', '200', '300', IgnoreCase = $true)]
[string]$Mode,
[ValidateRange(1, 4094)]
[uint16]$Untagged_VLAN,
[ValidateRange(1, 4094)]
[uint16[]]$Tagged_VLANs
)
if ($null -ne $Form_Factor) {
$PSBoundParameters.Form_Factor = ValidateDCIMChoice -ProvidedValue $Form_Factor -InterfaceFormFactor
}
if (-not [System.String]::IsNullOrWhiteSpace($Mode)) {
$PSBoundParameters.Mode = switch ($Mode) {
'Access' {
100
break
}
'Tagged' {
200
break
}
'Tagged All' {
300
break
}
default {
$_
}
}
}
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'interfaces'))
$URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters
$URI = BuildNewURI -Segments $URIComponents.Segments
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST
}
#endregion ADD/NEW commands
@ -474,6 +629,91 @@ function Set-NetboxDCIMDevice {
}
}
function Set-NetboxDCIMInterface {
[CmdletBinding()]
[OutputType([pscustomobject])]
param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true)]
[uint16[]]$Id,
[uint16]$Device,
[string]$Name,
[bool]$Enabled,
[object]$Form_Factor,
[uint16]$MTU,
[string]$MAC_Address,
[bool]$MGMT_Only,
[uint16]$LAG,
[string]$Description,
[ValidateSet('Access', 'Tagged', 'Tagged All', '100', '200', '300', IgnoreCase = $true)]
[string]$Mode,
[ValidateRange(1, 4094)]
[uint16]$Untagged_VLAN,
[ValidateRange(1, 4094)]
[uint16[]]$Tagged_VLANs
)
begin {
if ($null -ne $Form_Factor) {
$PSBoundParameters.Form_Factor = ValidateDCIMChoice -ProvidedValue $Form_Factor -InterfaceFormFactor
}
if (-not [System.String]::IsNullOrWhiteSpace($Mode)) {
$PSBoundParameters.Mode = switch ($Mode) {
'Access' {
100
break
}
'Tagged' {
200
break
}
'Tagged All' {
300
break
}
default {
$_
}
}
}
}
process {
foreach ($InterfaceId in $Id) {
$CurrentInterface = Get-NetboxDCIMInterface -Id $InterfaceId -ErrorAction Stop
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'interfaces', $CurrentInterface.Id))
$URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id'
$URI = BuildNewURI -Segments $Segments
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH
}
}
end {
}
}
#endregion SET Commands
@ -533,6 +773,62 @@ function Remove-NetboxDCIMDevice {
}
}
function Remove-NetboxDCIMInterface {
<#
.SYNOPSIS
Removes an interface
.DESCRIPTION
Removes an interface by ID from a device
.PARAMETER Id
A description of the Id parameter.
.PARAMETER Force
A description of the Force parameter.
.EXAMPLE
PS C:\> Remove-NetboxDCIMInterface -Id $value1
.NOTES
Additional information about the function.
#>
[CmdletBinding(ConfirmImpact = 'High',
SupportsShouldProcess = $true)]
param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true)]
[uint16[]]$Id,
[switch]$Force
)
begin {
}
process {
foreach ($InterfaceId in $Id) {
$CurrentInterface = Get-NetboxDCIMInterface -Id $InterfaceId -ErrorAction Stop
if ($Force -or $pscmdlet.ShouldProcess("Name: $($CurrentInterface.Name) | ID: $($CurrentInterface.Id)", "Remove")) {
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'interfaces', $CurrentInterface.Id))
$URI = BuildNewURI -Segments $Segments
InvokeNetboxRequest -URI $URI -Method DELETE
}
}
}
end {
}
}
#endregion REMOVE commands

View file

@ -30,6 +30,7 @@
<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_Devices_Tests_ps1">Tests\DCIM.Devices.Tests.ps1</File>
<File Build="2" Shared="True" ReferenceFunction="Invoke-DCIM_Interfaces_Tests_ps1">Tests\DCIM.Interfaces.Tests.ps1</File>
</Files>
<StartupScript>R:\Netbox\NetboxPS\Test-Module.ps1</StartupScript>
</Project>

View file

@ -20,7 +20,7 @@ if (Test-Path $ModulePath) {
Import-Module $ModulePath -ErrorAction Stop
}
Describe -Name "DCIM Tests" -Tag 'DCIM', 'Devices' -Fixture {
Describe -Name "DCIM Devices Tests" -Tag 'DCIM', 'Devices' -Fixture {
Mock -CommandName 'CheckNetboxIsConnected' -Verifiable -ModuleName 'NetboxPS' -MockWith {
return $true
}
@ -109,6 +109,18 @@ Describe -Name "DCIM Tests" -Tag 'DCIM', 'Devices' -Fixture {
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request a device by ID from the pipeline" {
$Result = [pscustomobject]@{
'id' = 10
} | Get-NetboxDCIMDevice
Assert-VerifiableMock
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with multiple IDs" {
$Result = Get-NetboxDCIMDevice -Id 10, 12, 15

View file

@ -0,0 +1,358 @@
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.152
Created on: 5/24/2018 10:50 AM
Created by: Ben Claussen
Organization: NEOnet
Filename: DCIM.Interfaces.Tests.ps1
===========================================================================
.DESCRIPTION
A description of the file.
#>
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 Interfaces Tests" -Tag 'DCIM', 'Interfaces' -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)
Context -Name "Get-NetboxDCIMInterface" -Fixture {
It "Should request the default number of interfaces" {
$Result = Get-NetboxDCIMInterface
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMInterface -Limit 10 -Offset 100
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with enabled" {
$Result = Get-NetboxDCIMInterface -Enabled $true
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/?enabled=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a form factor name" {
$Result = Get-NetboxDCIMInterface -Form_Factor '10GBASE-T (10GE)'
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/?form_factor=1150'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should throw for an invalid form factor" {
{
Get-NetboxDCIMInterface -Form_Factor 'Fake'
} | Should -Throw
}
It "Should request devices that are mgmt only" {
$Result = Get-NetboxDCIMInterface -MGMT_Only $True
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/?mgmt_only=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request an interface from the pipeline" {
$Result = [pscustomobject]@{
'Id' = 1234
} | Get-NetboxDCIMInterface
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/1234/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
}
Mock -CommandName "Get-NetboxDCIMInterface" -ModuleName "NetboxPS" -MockWith {
return [pscustomobject]@{
'Id' = $Id
}
}
Context -Name "Add-NetboxDCIMInterface" -Fixture {
It "Should add a basic interface to a device" {
$Result = Add-NetboxDCIMInterface -Device 111 -Name "TestInterface"
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"TestInterface","device":111}'
}
It "Should add an interface to a device with lots of properties" {
$paramAddNetboxDCIMInterface = @{
Device = 123
Name = "TestInterface"
Form_Factor = '10GBASE-T (10GE)'
MTU = 9000
MGMT_Only = $true
Description = 'Test Description'
Mode = 'Access'
}
$Result = Add-NetboxDCIMInterface @paramAddNetboxDCIMInterface
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"mtu":9000,"mgmt_only":true,"description":"Test Description","mode":100,"name":"TestInterface","device":123,"form_factor":1150}'
}
It "Should add an interface with multiple tagged VLANs" {
$Result = Add-NetboxDCIMInterface -Device 444 -Name "TestInterface" -Mode 'Tagged' -Tagged_VLANs 1, 2, 3, 4
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'POST'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"mode":200,"name":"TestInterface","device":444,"tagged_vlans":[1,2,3,4]}'
}
It "Should throw for invalid mode" {
{
Add-NetboxDCIMInterface -Device 321 -Name "Test123" -Mode 'Fake'
} | Should -Throw
}
It "Should throw for out of range VLAN" {
{
Add-NetboxDCIMInterface -Device 321 -Name "Test123" -Untagged_VLAN 4100
} | Should -Throw
}
}
Context -Name "Set-NetboxDCIMInterface" -Fixture {
It "Should set an interface to a new name" {
$Result = Set-NetboxDCIMInterface -Id 123 -Name "TestInterface"
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 1 -Exactly -Scope 'It'
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/123/'
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"TestInterface"}'
}
It "Should set multiple interfaces to a new name" {
$Result = Set-NetboxDCIMInterface -Id 456, 789 -Name "TestInterface"
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/456/', 'https://netbox.domain.com/api/dcim/interfaces/789/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"name":"TestInterface"}', '{"name":"TestInterface"}'
}
It "Should set multiple interfaces to a new name from the pipeline" {
$Result = @(
[pscustomobject]@{
'Id' = 1234
},
[pscustomobject]@{
'Id' = 4231
}
) | Set-NetboxDCIMInterface -Name "TestInterface"
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interfaces/1234/', 'https://netbox.domain.com/api/dcim/interfaces/4231/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"name":"TestInterface"}', '{"name":"TestInterface"}'
}
It "Should throw for invalid form factor" {
{
Set-NetboxDCIMInterface -Id 1234 -Form_Factor 'fake'
} | Should -Throw
}
}
Context -Name "Remove-NetboxDCIMInterface" -Fixture {
It "Should remove an interface" {
$Result = Remove-NetboxDCIMInterface -Id 10 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 1 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/10/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove multiple interfaces" {
$Result = Remove-NetboxDCIMInterface -Id 10, 12 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/10/', 'https://netbox.domain.com/api/dcim/interfaces/12/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
It "Should remove an interface from the pipeline" {
$Result = Get-NetboxDCIMInterface -Id 20 | Remove-NetboxDCIMInterface -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/20/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should remove mulitple interfaces from the pipeline" {
$Result = @(
[pscustomobject]@{
'Id' = 30
},
[pscustomobject]@{
'Id' = 40
}
) | Remove-NetboxDCIMInterface -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMInterface' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'DELETE', 'DELETE'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/interfaces/30/', 'https://netbox.domain.com/api/dcim/interfaces/40/'
$Result.Headers.Keys.Count | Should -BeExactly 2
}
}
Context -Name "Get-NetboxDCIMInterfaceConnection" -Fixture {
It "Should request the default number of interface connections" {
$Result = Get-NetboxDCIMInterfaceConnection
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interface-connections/'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request with a limit and offset" {
$Result = Get-NetboxDCIMInterfaceConnection -Limit 10 -Offset 100
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/interface-connections/?offset=100&limit=10'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should request connected interfaces" {
$Result = Get-NetboxDCIMInterfaceConnection -Connection_Status 'Connected'
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly
$Result.Method | Should -Be 'GET'
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/interface-connections/?connection_status=True'
$Result.Headers.Keys.Count | Should -BeExactly 1
}
It "Should throw for an invalid connection status" {
{
Get-NetboxDCIMInterfaceConnection -Connection_Status 'Fake'
} | Should -Throw
}
}
}
}