From 9a91803014e077f1691eb33b2b0fbd160f86b0d6 Mon Sep 17 00:00:00 2001 From: Ben Claussen Date: Thu, 24 May 2018 10:41:11 -0400 Subject: [PATCH] Add Set-NetboxDCIMDevice function and tests --- Functions/DCIM/DCIM.ps1 | 79 ++++++++++++++++++++++++++++++++++++ Tests/DCIM.Devices.Tests.ps1 | 44 ++++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/Functions/DCIM/DCIM.ps1 b/Functions/DCIM/DCIM.ps1 index 8ec9ee6..6363424 100644 --- a/Functions/DCIM/DCIM.ps1 +++ b/Functions/DCIM/DCIM.ps1 @@ -393,9 +393,88 @@ 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 diff --git a/Tests/DCIM.Devices.Tests.ps1 b/Tests/DCIM.Devices.Tests.ps1 index 3d252a7..4ede912 100644 --- a/Tests/DCIM.Devices.Tests.ps1 +++ b/Tests/DCIM.Devices.Tests.ps1 @@ -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 {