From 462357646e86b9d84d1f1c4a021cbad102605731 Mon Sep 17 00:00:00 2001 From: Ben Claussen Date: Tue, 22 May 2018 16:50:51 -0400 Subject: [PATCH] Changes - Add Remove-NetboxVirtualMachine - Rename Set-NetboxVirtualInterface - Update tests --- Functions/Virtualization/Virtualization.ps1 | 86 +++++++++++++- Tests/Virtualization.Tests.ps1 | 124 ++++++++++++++++---- 2 files changed, 182 insertions(+), 28 deletions(-) diff --git a/Functions/Virtualization/Virtualization.ps1 b/Functions/Virtualization/Virtualization.ps1 index d7241c3..5c3c845 100644 --- a/Functions/Virtualization/Virtualization.ps1 +++ b/Functions/Virtualization/Virtualization.ps1 @@ -502,7 +502,7 @@ function New-NetboxVirtualMachine { InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters } -function Add-NetboxVirtualInterface { +function Add-NetboxVirtualMachineInterface { [CmdletBinding()] param ( @@ -652,4 +652,86 @@ function Set-NetboxVirtualMachineInterface { -#endregion SET commands \ No newline at end of file +#endregion SET commands + + +#region REMOVE commands + +function Remove-NetboxVirtualMachine { +<# + .SYNOPSIS + Delete a virtual machine + + .DESCRIPTION + Deletes a virtual machine from Netbox by ID + + .PARAMETER Id + Database ID of the virtual machine + + .PARAMETER Force + Force deletion without any prompts + + .EXAMPLE + PS C:\> Remove-NetboxVirtualMachine -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 ($VMId in $Id) { + $CurrentVM = Get-NetboxVirtualMachine -Id $VMId -ErrorAction Stop + + if ($Force -or $pscmdlet.ShouldProcess("$($CurrentVM.Name)/$($CurrentVM.Id)", "Remove")) { + $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines', $CurrentVM.Id)) + + $URI = BuildNewURI -Segments $Segments + + InvokeNetboxRequest -URI $URI -Method DELETE + } + } + } + + end { + + } +} + + +#endregion REMOVE commands + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/Virtualization.Tests.ps1 b/Tests/Virtualization.Tests.ps1 index 2dcb9cd..327060a 100644 --- a/Tests/Virtualization.Tests.ps1 +++ b/Tests/Virtualization.Tests.ps1 @@ -128,6 +128,10 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { $Result.Uri | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/?status=1' $Result.Headers.Keys.Count | Should -BeExactly 1 } + + It "Should throw for an invalid status" { + { Get-NetboxVirtualMachine -Status 'Fake' } | Should -Throw + } } Context -Name "Get-NetboxVirtualMachineInterface" -Fixture { @@ -334,9 +338,9 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { } } - Context -Name "Add-NetboxVirtualInterface" -Fixture { + Context -Name "Add-NetboxVirtualMachineInterface" -Fixture { It "Should add a basic interface" { - $Result = Add-NetboxVirtualInterface -Name 'Ethernet0' -Virtual_Machine 10 + $Result = Add-NetboxVirtualMachineInterface -Name 'Ethernet0' -Virtual_Machine 10 Assert-VerifiableMock @@ -347,7 +351,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { } It "Should add an interface with a MAC, MTU, and Description" { - $Result = Add-NetboxVirtualInterface -Name 'Ethernet0' -Virtual_Machine 10 -Mac_Address '11:22:33:44:55:66' -MTU 1500 -Description "Test description" + $Result = Add-NetboxVirtualMachineInterface -Name 'Ethernet0' -Virtual_Machine 10 -Mac_Address '11:22:33:44:55:66' -MTU 1500 -Description "Test description" Assert-VerifiableMock @@ -358,18 +362,20 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { } } - Context -Name "Set-NetboxVirtualMachine" -Fixture { - Mock -CommandName "Get-NetboxVirtualMachine" -ModuleName NetboxPS -MockWith { - return @{ - 'Id' = 1234 - 'Name' = 'TestVM' - } + + Mock -CommandName "Get-NetboxVirtualMachine" -ModuleName NetboxPS -MockWith { + return [pscustomobject]@{ + 'Id' = $Id + 'Name' = $Name } - + } + + Context -Name "Set-NetboxVirtualMachine" -Fixture { It "Should set a VM to a new name" { $Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Force Assert-VerifiableMock + Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 1 -Exactly -Scope 'It' $Result.Method | Should -Be 'PATCH' $Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/' @@ -381,6 +387,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { $Result = Set-NetboxVirtualMachine -Id 1234 -Name 'newtestname' -Cluster 10 -Platform 15 -Status 'Offline' -Force Assert-VerifiableMock + Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 1 -Exactly -Scope 'It' $Result.Method | Should -Be 'PATCH' $Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/1234/' @@ -392,17 +399,19 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { { Set-NetboxVirtualMachine -Id 1234 -Status 'Fake' -Force } | Should -Throw Assert-VerifiableMock + Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 0 -Exactly -Scope 'It' + } + } + + + Mock -CommandName "Get-NetboxVirtualMachineInterface" -ModuleName NetboxPS -MockWith { + return [pscustomobject]@{ + 'Id' = $Id + 'Name' = $Name } } Context -Name "Set-NetboxVirtualMachineInterface" -Fixture { - Mock -CommandName "Get-NetboxVirtualMachineInterface" -ModuleName NetboxPS -MockWith { - return @{ - 'Id' = 1234 - 'Name' = 'TestVM' - } - } - It "Should set an interface to a new name" { $Result = Set-NetboxVirtualMachineInterface -Id 1234 -Name 'newtestname' -Force @@ -437,15 +446,6 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { } It "Should set multiple interfaces to a new name" { - Mock -CommandName "Get-NetboxVirtualMachineInterface" -ModuleName NetboxPS -MockWith { - return @( - @{ - 'Id' = $Id - 'Name' = 'TestVM' - } - ) - } - $Result = Set-NetboxVirtualMachineInterface -Id 1234, 4321 -Name 'newtestname' -Force Assert-VerifiableMock @@ -456,6 +456,78 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { $Result.Headers.Keys.Count | Should -BeExactly 2 $Result.Body | Should -Be '{"name":"newtestname"}', '{"name":"newtestname"}' } + + It "Should set multiple interfaces to a new name from the pipeline" { + $Result = @( + [pscustomobject]@{ + 'Id' = 4123 + }, + [pscustomobject]@{ + 'Id' = 4321 + } + ) | Set-NetboxVirtualMachineInterface -Name 'newtestname' -Force + + Assert-VerifiableMock + Assert-MockCalled -CommandName Get-NetboxVirtualMachineInterface -Times 2 -Scope 'It' -Exactly + + $Result.Method | Should -Be 'PATCH', 'PATCH' + $Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/4123/', 'https://netbox.domain.com/api/virtualization/interfaces/4321/' + $Result.Headers.Keys.Count | Should -BeExactly 2 + $Result.Body | Should -Be '{"name":"newtestname"}', '{"name":"newtestname"}' + } + } + + Context -Name "Remove-NetboxVirtualMachine" -Fixture { + It "Should remove a single VM" { + $Result = Remove-NetboxVirtualMachine -Id 4125 -Force + + Assert-VerifiableMock + Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 1 -Exactly -Scope 'It' + + $Result.Method | Should -Be 'DELETE' + $Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/4125/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + } + + It "Should remove mulitple VMs" { + $Result = Remove-NetboxVirtualMachine -Id 4125, 4132 -Force + + Assert-VerifiableMock + Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 2 -Exactly -Scope 'It' + + $Result.Method | Should -Be 'DELETE', 'DELETE' + $Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/4125/', 'https://netbox.domain.com/api/virtualization/virtual-machines/4132/' + $Result.Headers.Keys.Count | Should -BeExactly 2 + } + + It "Should remove a VM from the pipeline" { + $Result = Get-NetboxVirtualMachine -Id 4125 | Remove-NetboxVirtualMachine -Force + + Assert-VerifiableMock + Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 2 -Exactly -Scope 'It' + + $Result.Method | Should -Be 'DELETE' + $Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/4125/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + } + + It "Should remove multiple VMs from the pipeline" { + $Result = @( + [pscustomobject]@{ + 'Id' = 4125 + }, + [pscustomobject]@{ + 'Id' = 4132 + } + ) | Remove-NetboxVirtualMachine -Force + + Assert-VerifiableMock + Assert-MockCalled -CommandName 'Get-NetboxVirtualMachine' -Times 2 -Exactly -Scope 'It' + + $Result.Method | Should -Be 'DELETE', 'DELETE' + $Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/virtual-machines/4125/', 'https://netbox.domain.com/api/virtualization/virtual-machines/4132/' + $Result.Headers.Keys.Count | Should -BeExactly 2 + } } } }