mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-13 01:42:28 +00:00
Add DCIMDevices add/set/remove functions and tests
This commit is contained in:
parent
6f26627fcb
commit
3c30d37fd2
4 changed files with 804 additions and 55 deletions
|
|
@ -124,4 +124,349 @@ function ValidateDCIMChoice {
|
|||
)
|
||||
|
||||
ValidateChoice -MajorObject 'DCIM' -ChoiceName $PSCmdlet.ParameterSetName -ProvidedValue $ProvidedValue
|
||||
}
|
||||
}
|
||||
|
||||
#region GET Commands
|
||||
function Get-NetboxDCIMDevice {
|
||||
[CmdletBinding()]
|
||||
#region Parameters
|
||||
param
|
||||
(
|
||||
[uint16]$Limit,
|
||||
|
||||
[uint16]$Offset,
|
||||
|
||||
[uint16[]]$Id,
|
||||
|
||||
[string]$Query,
|
||||
|
||||
[string]$Name,
|
||||
|
||||
[uint16]$Manufacturer_Id,
|
||||
|
||||
[string]$Manufacturer,
|
||||
|
||||
[uint16]$Device_Type_Id,
|
||||
|
||||
[uint16]$Role_Id,
|
||||
|
||||
[string]$Role,
|
||||
|
||||
[uint16]$Tenant_Id,
|
||||
|
||||
[string]$Tenant,
|
||||
|
||||
[uint16]$Platform_Id,
|
||||
|
||||
[string]$Platform,
|
||||
|
||||
[string]$Asset_Tag,
|
||||
|
||||
[uint16]$Site_Id,
|
||||
|
||||
[string]$Site,
|
||||
|
||||
[uint16]$Rack_Group_Id,
|
||||
|
||||
[uint16]$Rack_Id,
|
||||
|
||||
[uint16]$Cluster_Id,
|
||||
|
||||
[uint16]$Model,
|
||||
|
||||
[object]$Status,
|
||||
|
||||
[bool]$Is_Full_Depth,
|
||||
|
||||
[bool]$Is_Console_Server,
|
||||
|
||||
[bool]$Is_PDU,
|
||||
|
||||
[bool]$Is_Network_Device,
|
||||
|
||||
[string]$MAC_Address,
|
||||
|
||||
[bool]$Has_Primary_IP,
|
||||
|
||||
[uint16]$Virtual_Chassis_Id,
|
||||
|
||||
[uint16]$Position,
|
||||
|
||||
[string]$Serial,
|
||||
|
||||
[switch]$Raw
|
||||
)
|
||||
#endregion Parameters
|
||||
|
||||
if ($null -ne $Status) {
|
||||
$PSBoundParameters.Status = ValidateDCIMChoice -ProvidedValue $Status -DeviceStatus
|
||||
}
|
||||
|
||||
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'devices'))
|
||||
|
||||
$URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw'
|
||||
|
||||
$URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters
|
||||
|
||||
InvokeNetboxRequest -URI $URI -Raw:$Raw
|
||||
}
|
||||
|
||||
function Get-NetboxDCIMDeviceType {
|
||||
[CmdletBinding()]
|
||||
#region Parameters
|
||||
param
|
||||
(
|
||||
[uint16]$Offset,
|
||||
|
||||
[uint16]$Limit,
|
||||
|
||||
[uint16[]]$Id,
|
||||
|
||||
[string]$Query,
|
||||
|
||||
[string]$Slug,
|
||||
|
||||
[string]$Manufacturer,
|
||||
|
||||
[uint16]$Manufacturer_Id,
|
||||
|
||||
[string]$Model,
|
||||
|
||||
[string]$Part_Number,
|
||||
|
||||
[uint16]$U_Height,
|
||||
|
||||
[bool]$Is_Full_Depth,
|
||||
|
||||
[bool]$Is_Console_Server,
|
||||
|
||||
[bool]$Is_PDU,
|
||||
|
||||
[bool]$Is_Network_Device,
|
||||
|
||||
[uint16]$Subdevice_Role,
|
||||
|
||||
[switch]$Raw
|
||||
)
|
||||
#endregion Parameters
|
||||
|
||||
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-types'))
|
||||
|
||||
$URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw'
|
||||
|
||||
$URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters
|
||||
|
||||
InvokeNetboxRequest -URI $URI -Raw:$Raw
|
||||
}
|
||||
|
||||
function Get-NetboxDCIMDeviceRole {
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[uint16]$Limit,
|
||||
|
||||
[uint16]$Offset,
|
||||
|
||||
[Parameter(ParameterSetName = 'ById')]
|
||||
[uint16[]]$Id,
|
||||
|
||||
[string]$Name,
|
||||
|
||||
[string]$Slug,
|
||||
|
||||
[string]$Color,
|
||||
|
||||
[bool]$VM_Role,
|
||||
|
||||
[switch]$Raw
|
||||
)
|
||||
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
'ById' {
|
||||
foreach ($DRId in $Id) {
|
||||
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-roles', $DRId))
|
||||
|
||||
$URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Raw'
|
||||
|
||||
$URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters
|
||||
|
||||
InvokeNetboxRequest -URI $URI -Raw:$Raw
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
default {
|
||||
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-roles'))
|
||||
|
||||
$URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw'
|
||||
|
||||
$URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters
|
||||
|
||||
InvokeNetboxRequest -URI $URI -Raw:$Raw
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion GET Commands
|
||||
|
||||
|
||||
#region ADD/NEW commands
|
||||
function New-NetboxDCIMDevice {
|
||||
[CmdletBinding()]
|
||||
[OutputType([pscustomobject])]
|
||||
#region Parameters
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Name,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[object]$Device_Role,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[object]$Device_Type,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[uint16]$Site,
|
||||
|
||||
[object]$Status = 'Active',
|
||||
|
||||
[uint16]$Platform,
|
||||
|
||||
[uint16]$Tenant,
|
||||
|
||||
[uint16]$Cluster,
|
||||
|
||||
[uint16]$Rack,
|
||||
|
||||
[uint16]$Position,
|
||||
|
||||
[object]$Face,
|
||||
|
||||
[string]$Serial,
|
||||
|
||||
[string]$Asset_Tag,
|
||||
|
||||
[uint16]$Virtual_Chassis,
|
||||
|
||||
[uint16]$VC_Priority,
|
||||
|
||||
[uint16]$VC_Position,
|
||||
|
||||
[uint16]$Primary_IP4,
|
||||
|
||||
[uint16]$Primary_IP6,
|
||||
|
||||
[string]$Comments,
|
||||
|
||||
[hashtable]$Custom_Fields
|
||||
)
|
||||
#endregion Parameters
|
||||
|
||||
if ($null -ne $Device_Role) {
|
||||
# Validate device role?
|
||||
}
|
||||
|
||||
if ($null -ne $Device_Type) {
|
||||
# Validate device type?
|
||||
}
|
||||
|
||||
if ($null -ne $Status) {
|
||||
$PSBoundParameters.Status = ValidateDCIMChoice -ProvidedValue $Status -DeviceStatus
|
||||
}
|
||||
|
||||
if ($null -ne $Face) {
|
||||
$PSBoundParameters.Face = ValidateDCIMChoice -ProvidedValue $Face -DeviceFace
|
||||
}
|
||||
|
||||
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'devices'))
|
||||
|
||||
$URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters
|
||||
|
||||
$URI = BuildNewURI -Segments $URIComponents.Segments
|
||||
|
||||
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST
|
||||
}
|
||||
|
||||
#endregion ADD/NEW commands
|
||||
|
||||
|
||||
#region SET Commands
|
||||
function Set-NetboxDCIMDevice {
|
||||
|
||||
}
|
||||
|
||||
#endregion SET Commands
|
||||
|
||||
|
||||
#region REMOVE commands
|
||||
function Remove-NetboxDCIMDevice {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Delete a device
|
||||
|
||||
.DESCRIPTION
|
||||
Deletes a device from Netbox by ID
|
||||
|
||||
.PARAMETER Id
|
||||
Database ID of the device
|
||||
|
||||
.PARAMETER Force
|
||||
Force deletion without any prompts
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\> Remove-NetboxDCIMDevice -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 ($DeviceID in $Id) {
|
||||
$CurrentDevice = Get-NetboxDCIMDevice -Id $DeviceID -ErrorAction Stop
|
||||
|
||||
if ($Force -or $pscmdlet.ShouldProcess("Name: $($CurrentDevice.Name) | ID: $($CurrentDevice.Id)", "Remove")) {
|
||||
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'devices', $CurrentDevice.Id))
|
||||
|
||||
$URI = BuildNewURI -Segments $Segments
|
||||
|
||||
InvokeNetboxRequest -URI $URI -Method DELETE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end {
|
||||
|
||||
}
|
||||
}
|
||||
#endregion REMOVE commands
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@
|
|||
<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>
|
||||
<File Build="2" Shared="True" ReferenceFunction="Invoke-DCIM_Devices_Tests_ps1">Tests\DCIM.Devices.Tests.ps1</File>
|
||||
</Files>
|
||||
<StartupScript>R:\Netbox\NetboxPS\Test-Module.ps1</StartupScript>
|
||||
</Project>
|
||||
457
Tests/DCIM.Devices.Tests.ps1
Normal file
457
Tests/DCIM.Devices.Tests.ps1
Normal file
|
|
@ -0,0 +1,457 @@
|
|||
<#
|
||||
.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', 'Devices' -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-NetboxDCIMDevice" -Fixture {
|
||||
It "Should request the default number of devices" {
|
||||
$Result = Get-NetboxDCIMDevice
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a limit and offset" {
|
||||
$Result = Get-NetboxDCIMDevice -Limit 10 -Offset 100
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?offset=100&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a query" {
|
||||
$Result = Get-NetboxDCIMDevice -Query 'testdevice'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?q=testdevice'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with an escaped query" {
|
||||
$Result = Get-NetboxDCIMDevice -Query 'test device'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?q=test+device'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a name" {
|
||||
$Result = Get-NetboxDCIMDevice -Name 'testdevice'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?name=testdevice'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a single ID" {
|
||||
$Result = Get-NetboxDCIMDevice -Id 10
|
||||
|
||||
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
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?id__in=10,12,15'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request a status" {
|
||||
$Result = Get-NetboxDCIMDevice -Status 'Active'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/?status=1'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should throw for an invalid status" {
|
||||
{
|
||||
Get-NetboxDCIMDevice -Status 'Fake'
|
||||
} | Should -Throw
|
||||
}
|
||||
|
||||
It "Should request devices that are a PDU" {
|
||||
$Result = Get-NetboxDCIMDevice -Is_PDU $True
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/devices/?is_pdu=True'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Get-NetboxDCIMDeviceType" -Fixture {
|
||||
It "Should request the default number of devices types" {
|
||||
$Result = Get-NetboxDCIMDeviceType
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a limit and offset" {
|
||||
$Result = Get-NetboxDCIMDeviceType -Limit 10 -Offset 100
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?offset=100&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a query" {
|
||||
$Result = Get-NetboxDCIMDeviceType -Query 'testdevice'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?q=testdevice'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with an escaped query" {
|
||||
$Result = Get-NetboxDCIMDeviceType -Query 'test device'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?q=test+device'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a slug" {
|
||||
$Result = Get-NetboxDCIMDeviceType -Slug 'testdevice'
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?slug=testdevice'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a single ID" {
|
||||
$Result = Get-NetboxDCIMDeviceType -Id 10
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/10/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with multiple IDs" {
|
||||
$Result = Get-NetboxDCIMDeviceType -Id 10, 12, 15
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?id__in=10,12,15'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request a device type that is PDU" {
|
||||
$Result = Get-NetboxDCIMDeviceType -Is_PDU $true
|
||||
|
||||
Assert-VerifiableMock
|
||||
|
||||
$Result.Method | Should -Be 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-types/?is_pdu=True'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Get-NetboxDCIMDeviceRole" -Fixture {
|
||||
It "Should request the default number of devices types" {
|
||||
$Result = Get-NetboxDCIMDeviceRole
|
||||
|
||||
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/device-roles/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request a device role by Id" {
|
||||
$Result = Get-NetboxDCIMDeviceRole -Id 10
|
||||
|
||||
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/device-roles/10/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request multiple roles by Id" {
|
||||
$Result = Get-NetboxDCIMDeviceRole -Id 10, 12
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 2 -Scope 'It' -Exactly
|
||||
|
||||
$Result.Method | Should -Be 'GET', 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/10/', 'https://netbox.domain.com/api/dcim/device-roles/12/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 2
|
||||
}
|
||||
|
||||
It "Should request single role by Id and color" {
|
||||
$Result = Get-NetboxDCIMDeviceRole -Id 10 -Color '0fab12'
|
||||
|
||||
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/device-roles/10/?color=0fab12'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request multiple roles by Id and color" {
|
||||
$Result = Get-NetboxDCIMDeviceRole -Id 10, 12 -Color '0fab12'
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName "Invoke-RestMethod" -Times 2 -Scope 'It' -Exactly
|
||||
|
||||
$Result.Method | Should -Be 'GET', 'GET'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/device-roles/10/?color=0fab12', 'https://netbox.domain.com/api/dcim/device-roles/12/?color=0fab12'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 2
|
||||
}
|
||||
|
||||
It "Should request with a limit and offset" {
|
||||
$Result = Get-NetboxDCIMDeviceRole -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/device-roles/?offset=100&limit=10'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a slug" {
|
||||
$Result = Get-NetboxDCIMDeviceRole -Slug 'testdevice'
|
||||
|
||||
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/device-roles/?slug=testdevice'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request with a name" {
|
||||
$Result = Get-NetboxDCIMDeviceRole -Name 'TestRole'
|
||||
|
||||
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/device-roles/?name=TestRole'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should request those that are VM role" {
|
||||
$Result = Get-NetboxDCIMDeviceRole -VM_Role $true
|
||||
|
||||
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/device-roles/?vm_role=True'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "New-NetboxDCIMDevice" -Fixture {
|
||||
It "Should create a new device" {
|
||||
$Result = New-NetboxDCIMDevice -Name "newdevice" -Device_Role 4 -Device_Type 10 -Site 1 -Face 0
|
||||
|
||||
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope 'It'
|
||||
|
||||
$Result.Method | Should -Be 'POST'
|
||||
$Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/devices/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"site":1,"face":0,"name":"newdevice","status":1,"device_type":10,"device_role":4}'
|
||||
}
|
||||
|
||||
It "Should throw because of an invalid status" {
|
||||
{
|
||||
New-NetboxDCIMDevice -Name "newdevice" -Device_Role 4 -Device_Type 10 -Site 1 -Status 5555
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Mock -CommandName "Get-NetboxDCIMDevice" -ModuleName NetboxPS -MockWith {
|
||||
return [pscustomobject]@{
|
||||
'Id' = $Id
|
||||
'Name' = $Name
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Set-NetboxDCIMDevice" -Fixture {
|
||||
It "Should set a device to a new name" {
|
||||
$Result = Set-NetboxDCIMDevice -Id 1234 -Name 'newtestname' -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 1 -Exactly -Scope 'It'
|
||||
|
||||
$Result.Method | Should -Be 'PATCH'
|
||||
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/1234/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
$Result.Body | Should -Be '{"name":"newtestname"}'
|
||||
}
|
||||
}
|
||||
|
||||
Context -Name "Remove-NetboxDCIMDevice" -Fixture {
|
||||
It "Should remove a device" {
|
||||
$Result = Remove-NetboxDCIMDevice -Id 10 -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 1 -Exactly -Scope 'It'
|
||||
|
||||
$Result.Method | Should -Be 'DELETE'
|
||||
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/10/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should remove multiple devices" {
|
||||
$Result = Remove-NetboxDCIMDevice -Id 10, 12 -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
|
||||
|
||||
$Result.Method | Should -Be 'DELETE', 'DELETE'
|
||||
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/10/', 'https://netbox.domain.com/api/dcim/devices/12/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 2
|
||||
}
|
||||
|
||||
It "Should remove a device from the pipeline" {
|
||||
$Result = Get-NetboxDCIMDevice -Id 20 | Remove-NetboxDCIMDevice -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
|
||||
|
||||
$Result.Method | Should -Be 'DELETE'
|
||||
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/20/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 1
|
||||
}
|
||||
|
||||
It "Should remove mulitple devices from the pipeline" {
|
||||
$Result = @(
|
||||
[pscustomobject]@{
|
||||
'Id' = 30
|
||||
},
|
||||
[pscustomobject]@{
|
||||
'Id' = 40
|
||||
}
|
||||
) | Remove-NetboxDCIMDevice -Force
|
||||
|
||||
Assert-VerifiableMock
|
||||
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
|
||||
|
||||
$Result.Method | Should -Be 'DELETE', 'DELETE'
|
||||
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/30/', 'https://netbox.domain.com/api/dcim/devices/40/'
|
||||
$Result.Headers.Keys.Count | Should -BeExactly 2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
<#
|
||||
.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)
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue