From 6f26627fcb79900b1c36426e97224c1005f78a57 Mon Sep 17 00:00:00 2001 From: Ben Claussen Date: Wed, 23 May 2018 11:55:31 -0400 Subject: [PATCH] Add pipeline support for Set/Remove-NetboxIPAMAddress and tests --- Functions/IPAM/IPAM.ps1 | 75 ++++++++++++--------- Tests/IPAM.Tests.ps1 | 142 ++++++++++++++++++++++++++++------------ 2 files changed, 145 insertions(+), 72 deletions(-) diff --git a/Functions/IPAM/IPAM.ps1 b/Functions/IPAM/IPAM.ps1 index 2af915e..0beb3e5 100644 --- a/Functions/IPAM/IPAM.ps1 +++ b/Functions/IPAM/IPAM.ps1 @@ -508,13 +508,10 @@ function Remove-NetboxIPAMAddress { Removes/deletes an IP address from Netbox by ID and optional other filters .PARAMETER Id - A description of the Id parameter. + Database ID of the IP address object. .PARAMETER Force - A description of the Force parameter. - - .PARAMETER Query - A description of the Query parameter. + Do not confirm. .EXAMPLE PS C:\> Remove-NetboxIPAMAddress -Id $value1 @@ -527,35 +524,42 @@ function Remove-NetboxIPAMAddress { SupportsShouldProcess = $true)] param ( - [Parameter(Mandatory = $true)] + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true)] [uint16[]]$Id, [switch]$Force ) - $CurrentIPs = @(Get-NetboxIPAMAddress -Id $Id -ErrorAction Stop) + begin { + } - $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses')) - - foreach ($IP in $CurrentIPs) { - if ($Force -or $pscmdlet.ShouldProcess($IP.Address, "Delete")) { - $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary @{'id' = $IP.Id} + process { + foreach ($IPId in $Id) { + $CurrentIP = Get-NetboxIPAMAddress -Id $IPId -ErrorAction Stop - $URI = BuildNewURI -Segments $URIComponents.Segments + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses', $IPId)) - InvokeNetboxRequest -URI $URI -Method DELETE + if ($Force -or $pscmdlet.ShouldProcess($CurrentIP.Address, "Delete")) { + $URI = BuildNewURI -Segments $Segments + + InvokeNetboxRequest -URI $URI -Method DELETE + } } } + + end { + } } function Set-NetboxIPAMAddress { - [CmdletBinding(ConfirmImpact = 'High', + [CmdletBinding(ConfirmImpact = 'Medium', SupportsShouldProcess = $true)] param ( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] - [uint16]$Id, + [uint16[]]$Id, [string]$Address, @@ -578,25 +582,34 @@ function Set-NetboxIPAMAddress { [switch]$Force ) - if ($Status) { - $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -IPAddressStatus + begin { + if ($Status) { + $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -IPAddressStatus + } + + if ($Role) { + $PSBoundParameters.Role = ValidateIPAMChoice -ProvidedValue $Role -IPAddressRole + } } - if ($Role) { - $PSBoundParameters.Role = ValidateIPAMChoice -ProvidedValue $Role -IPAddressRole + process{ + foreach ($IPId in $Id) { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses', $IPId)) + + Write-Verbose "Obtaining IPs from ID $IPId" + $CurrentIP = Get-NetboxIPAMAddress -Id $IPId -ErrorAction Stop + + if ($Force -or $PSCmdlet.ShouldProcess($CurrentIP.Address, 'Set')) { + $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' + + $URI = BuildNewURI -Segments $URIComponents.Segments + + InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH + } + } } - $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses', $Id)) - - Write-Verbose "Obtaining IPs from ID $Id" - $CurrentIP = Get-NetboxIPAMAddress -Id $Id -ErrorAction Stop - - if ($Force -or $PSCmdlet.ShouldProcess($($CurrentIP | Select-Object -ExpandProperty 'Address'), 'Set')) { - $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' - - $URI = BuildNewURI -Segments $URIComponents.Segments - - InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH + end{ } } diff --git a/Tests/IPAM.Tests.ps1 b/Tests/IPAM.Tests.ps1 index 47e8447..e637f75 100644 --- a/Tests/IPAM.Tests.ps1 +++ b/Tests/IPAM.Tests.ps1 @@ -48,11 +48,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { InModuleScope -ModuleName 'NetboxPS' -ScriptBlock { $script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json) - - Context -Name "ValidateIPAMChoice" -Fixture { - #It "Should return a valid integer" - } - + Context -Name "Get-NetboxIPAMAggregate" -Fixture { It "Should request the default number of aggregates" { $Result = Get-NetboxIPAMAggregate @@ -388,18 +384,18 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { } Context -Name "Remove-NetboxIPAMAddress" -Fixture { - It "Should remove a single IP" { - Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith { - return @{ - 'address' = '10.1.1.1/24' - 'id' = 4109 - } + Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith { + return @{ + 'address' = "10.1.1.1/$Id" + 'id' = $id } - - $Result = Remove-NetboxIPAMAddress -Id '4109' -Force + } + + It "Should remove a single IP" { + $Result = Remove-NetboxIPAMAddress -Id 4109 -Force Assert-VerifiableMock - Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 + Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope 'It' -Exactly $Result.Method | Should -Be 'DELETE' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/' @@ -407,24 +403,43 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Body | Should -Be $null } - It "Should remove multiple IPs" { - Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith { - return @( - @{ - 'address' = '10.1.1.1/24' - 'id' = 4109 - }, - @{ - 'address' = '10.1.1.2/24' - 'id' = 4110 - } - ) - } + It "Should remove a single IP from the pipeline" { + $Result = [pscustomobject]@{ + 'id' = 4110 + } | Remove-NetboxIPAMAddress -Force + Assert-VerifiableMock + Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope 'It' -Exactly + + $Result.Method | Should -Be 'DELETE' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4110/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Body | Should -Be $null + } + + It "Should remove multiple IPs" { $Result = Remove-NetboxIPAMAddress -Id 4109, 4110 -Force Assert-VerifiableMock - Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2 + Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2 -Scope 'It' -Exactly + + $Result.Method | Should -Be 'DELETE', 'DELETE' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/', 'https://netbox.domain.com/api/ipam/ip-addresses/4110/' + $Result.Headers.Keys.Count | Should -BeExactly 2 + } + + It "Should remove multiple IPs from the pipeline" { + $Result = @( + [pscustomobject]@{ + 'id' = 4109 + }, + [pscustomobject]@{ + 'id' = 4110 + } + ) | Remove-NetboxIPAMAddress -Force + + Assert-VerifiableMock + Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2 -Scope 'It' -Exactly $Result.Method | Should -Be 'DELETE', 'DELETE' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/', 'https://netbox.domain.com/api/ipam/ip-addresses/4110/' @@ -433,18 +448,18 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { } Context -Name "Set-NetboxIPAMAddress" -Fixture { - It "Should set an IP with a new status" { - Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith { - return @{ - 'address' = '10.1.1.1/24' - 'id' = 4109 - } + Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith { + return @{ + 'address' = '10.1.1.1/24' + 'id' = $id } - - $Result = Set-NetboxIPAMAddress -Id '4109' -Status 2 -Force + } + + It "Should set an IP with a new status" { + $Result = Set-NetboxIPAMAddress -Id 4109 -Status 2 -Force Assert-VerifiableMock - Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 + Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope "It" -Exactly $Result.Method | Should -Be 'PATCH' $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/' @@ -452,17 +467,62 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { $Result.Body | Should -Be '{"status":2}' } - It "Should set an IP with VRF, Tenant, and Description" { - $Result = Set-NetboxIPAMAddress -Id 4109 -VRF 10 -Tenant 14 -Description 'Test description' -Force + It "Should set an IP from the pipeline" { + $Result = [pscustomobject]@{ + 'Id' = 4501 + } | Set-NetboxIPAMAddress -VRF 10 -Tenant 14 -Description 'Test description' -Force Assert-VerifiableMock - Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 + Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope "It" -Exactly $Result.Method | Should -Be 'PATCH' - $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4501/' $Result.Headers.Keys.Count | Should -BeExactly 1 $Result.Body | Should -Be '{"vrf":10,"description":"Test description","tenant":14}' } + + It "Should set mulitple IPs to a new status" { + $Result = Set-NetboxIPAMAddress -Id 4109, 4555 -Status 2 -Force + + Assert-VerifiableMock + Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2 -Scope "It" -Exactly + + $Result.Method | Should -Be 'PATCH', 'PATCH' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4109/', 'https://netbox.domain.com/api/ipam/ip-addresses/4555/' + $Result.Headers.Keys.Count | Should -BeExactly 2 + $Result.Body | Should -Be '{"status":2}', '{"status":2}' + } + + It "Should set an IP with VRF, Tenant, and Description" { + $Result = Set-NetboxIPAMAddress -Id 4110 -VRF 10 -Tenant 14 -Description 'Test description' -Force + + Assert-VerifiableMock + Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope "It" -Exactly + + $Result.Method | Should -Be 'PATCH' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4110/' + $Result.Headers.Keys.Count | Should -BeExactly 1 + $Result.Body | Should -Be '{"vrf":10,"description":"Test description","tenant":14}' + } + + It "Should set multiple IPs from the pipeline" { + $Result = @( + [pscustomobject]@{ + 'Id' = 4501 + }, + [pscustomobject]@{ + 'Id' = 4611 + } + ) | Set-NetboxIPAMAddress -Status 2 -Force + + Assert-VerifiableMock + Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 2 -Scope "It" -Exactly + + $Result.Method | Should -Be 'PATCH', 'PATCH' + $Result.Uri | Should -Be 'https://netbox.domain.com/api/ipam/ip-addresses/4501/', 'https://netbox.domain.com/api/ipam/ip-addresses/4611/' + $Result.Headers.Keys.Count | Should -BeExactly 2 + $Result.Body | Should -Be '{"status":2}', '{"status":2}' + } } } }