Set-NetboxVirtualMachine and tests

This commit is contained in:
Ben Claussen 2018-05-21 10:32:19 -04:00
parent 53a64915b5
commit 7ba549828d
3 changed files with 117 additions and 2 deletions

View file

@ -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

View file

@ -6,5 +6,5 @@ $script:CommonParameterNames = New-Object System.Collections.ArrayList
SetupNetboxConfigVariable
#Export-ModuleMember -Function *
Export-ModuleMember -Function *-*
Export-ModuleMember -Function *
#Export-ModuleMember -Function *-*

View file

@ -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"}'
}
}
}
}