From 7ba549828d6ac6cbe4a2d8a0e66f1454f8dcfb55 Mon Sep 17 00:00:00 2001 From: Ben Claussen Date: Mon, 21 May 2018 10:32:19 -0400 Subject: [PATCH] Set-NetboxVirtualMachine and tests --- Functions/Virtualization/Virtualization.ps1 | 52 +++++++++++++++++ NetboxPS.psm1 | 4 +- Tests/Virtualization.Tests.ps1 | 63 +++++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) diff --git a/Functions/Virtualization/Virtualization.ps1 b/Functions/Virtualization/Virtualization.ps1 index e0fe5a3..687970c 100644 --- a/Functions/Virtualization/Virtualization.ps1 +++ b/Functions/Virtualization/Virtualization.ps1 @@ -598,5 +598,57 @@ function Set-NetboxVirtualMachine { } } +function Set-NetboxVirtualMachineInterface { + [CmdletBinding(ConfirmImpact = 'Medium', + SupportsShouldProcess = $true)] + [OutputType([pscustomobject])] + param + ( + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true)] + [uint16[]]$Id, + + [string]$Name, + + [string]$MAC_Address, + + [uint16]$MTU, + + [string]$Description, + + [boolean]$Enabled, + + [uint16]$Virtual_Machine, + + [switch]$Force + ) + + begin { + + } + + process { + foreach ($VMI_ID in $Id) { + Write-Verbose "Obtaining VM Interface..." + $CurrentVMI = Get-NetboxVirtualMachineInterface -Id $VMI_ID -ErrorAction Stop + Write-Verbose "Finished obtaining VM Interface" + + $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'interfaces', $CurrentVMI.Id)) + + if ($Force -or $pscmdlet.ShouldProcess("Interface $($CurrentVMI.Id) on VM $($CurrentVMI.Virtual_Machine.Name)", "Set")) { + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' + + $URI = BuildNewURI -Segments $URIComponents.Segments + + InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH + } + } + } + end { + + } +} + + #endregion SET commands \ No newline at end of file diff --git a/NetboxPS.psm1 b/NetboxPS.psm1 index 2e24117..562b0f0 100644 --- a/NetboxPS.psm1 +++ b/NetboxPS.psm1 @@ -6,5 +6,5 @@ $script:CommonParameterNames = New-Object System.Collections.ArrayList SetupNetboxConfigVariable -#Export-ModuleMember -Function * -Export-ModuleMember -Function *-* \ No newline at end of file +Export-ModuleMember -Function * +#Export-ModuleMember -Function *-* \ No newline at end of file diff --git a/Tests/Virtualization.Tests.ps1 b/Tests/Virtualization.Tests.ps1 index 6b68819..83b587a 100644 --- a/Tests/Virtualization.Tests.ps1 +++ b/Tests/Virtualization.Tests.ps1 @@ -390,6 +390,69 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { Assert-VerifiableMock } } + + 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 + + Assert-VerifiableMock + Assert-MockCalled -CommandName Get-NetboxVirtualMachineInterface -Times 1 -Scope 'It' -Exactly + + $Result.Method | Should -Be 'PATCH' + $Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/1234/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Body | Should -Be '{"name":"newtestname"}' + } + + It "Should set an interface to a new name, MTU, MAC address and description" { + $paramSetNetboxVirtualMachineInterface = @{ + Id = 1234 + Name = 'newtestname' + MAC_Address = '11:22:33:44:55:66' + MTU = 9000 + Description = "Test description" + Force = $true + } + + $Result = Set-NetboxVirtualMachineInterface @paramSetNetboxVirtualMachineInterface + + Assert-VerifiableMock + Assert-MockCalled -CommandName Get-NetboxVirtualMachineInterface -Times 1 -Scope 'It' -Exactly + + $Result.Method | Should -Be 'PATCH' + $Result.URI | Should -Be 'https://netbox.domain.com/api/virtualization/interfaces/1234/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Body | Should -Be '{"mac_address":"11:22:33:44:55:66","mtu":9000,"description":"Test description","name":"newtestname"}' + } + + 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 + 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/1234/', 'https://netbox.domain.com/api/virtualization/interfaces/4321/' + $Result.Headers.Keys.Count | Should -BeExactly 2 + $Result.Body | Should -Be '{"name":"newtestname"}', '{"name":"newtestname"}' + } + } } }