Add Set-NetboxDCIMDevice function and tests

This commit is contained in:
Ben Claussen 2018-05-24 10:41:11 -04:00
parent 3c30d37fd2
commit 9a91803014
2 changed files with 123 additions and 0 deletions

View file

@ -393,8 +393,87 @@ function New-NetboxDCIMDevice {
#region SET Commands
function Set-NetboxDCIMDevice {
[CmdletBinding(SupportsShouldProcess = $true)]
param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true)]
[uint16[]]$Id,
[string]$Name,
[object]$Device_Role,
[object]$Device_Type,
[uint16]$Site,
[object]$Status,
[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,
[switch]$Force
)
begin {
if ($null -ne $Status) {
$PSBoundParameters.Status = ValidateDCIMChoice -ProvidedValue $Status -DeviceStatus
}
if ($null -ne $Face) {
$PSBoundParameters.Face = ValidateDCIMChoice -ProvidedValue $Face -DeviceFace
}
}
process {
foreach ($DeviceID in $Id) {
$CurrentDevice = Get-NetboxDCIMDevice -Id $DeviceID -ErrorAction Stop
if ($Force -or $pscmdlet.ShouldProcess("$($CurrentDevice.Name)", "Set")) {
$Segments = [System.Collections.ArrayList]::new(@('dcim', 'devices', $CurrentDevice.Id))
$URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force'
$URI = BuildNewURI -Segments $URIComponents.Segments
InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH
}
}
}
end {
}
}
#endregion SET Commands

View file

@ -329,6 +329,7 @@ Describe -Name "DCIM Tests" -Tag 'DCIM', 'Devices' -Fixture {
}
}
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
@ -368,6 +369,49 @@ Describe -Name "DCIM Tests" -Tag 'DCIM', 'Devices' -Fixture {
$Result.Headers.Keys.Count | Should -BeExactly 1
$Result.Body | Should -Be '{"name":"newtestname"}'
}
It "Should set a device with new properties" {
$Result = Set-NetboxDCIMDevice -Id 1234 -Name 'newtestname' -Cluster 10 -Platform 20 -Site 15 -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 '{"cluster":10,"platform":20,"name":"newtestname","site":15}'
}
It "Should set multiple devices with new properties" {
$Result = Set-NetboxDCIMDevice -Id 1234, 3214 -Cluster 10 -Platform 20 -Site 15 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/1234/', 'https://netbox.domain.com/api/dcim/devices/3214/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"cluster":10,"platform":20,"site":15}', '{"cluster":10,"platform":20,"site":15}'
}
It "Should set multiple devices with new properties from the pipeline" {
$Result = @(
[pscustomobject]@{
'id' = 4432
},
[pscustomobject]@{
'id' = 3241
}
) | Set-NetboxDCIMDevice -Cluster 10 -Platform 20 -Site 15 -Force
Assert-VerifiableMock
Assert-MockCalled -CommandName 'Get-NetboxDCIMDevice' -Times 2 -Exactly -Scope 'It'
$Result.Method | Should -Be 'PATCH', 'PATCH'
$Result.URI | Should -Be 'https://netbox.domain.com/api/dcim/devices/4432/', 'https://netbox.domain.com/api/dcim/devices/3241/'
$Result.Headers.Keys.Count | Should -BeExactly 2
$Result.Body | Should -Be '{"cluster":10,"platform":20,"site":15}', '{"cluster":10,"platform":20,"site":15}'
}
}
Context -Name "Remove-NetboxDCIMDevice" -Fixture {