mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-13 18:02:29 +00:00
Add pipeline support for Set/Remove-NetboxIPAMAddress and tests
This commit is contained in:
parent
0d70d51a3a
commit
6f26627fcb
2 changed files with 145 additions and 72 deletions
|
|
@ -508,13 +508,10 @@ function Remove-NetboxIPAMAddress {
|
||||||
Removes/deletes an IP address from Netbox by ID and optional other filters
|
Removes/deletes an IP address from Netbox by ID and optional other filters
|
||||||
|
|
||||||
.PARAMETER Id
|
.PARAMETER Id
|
||||||
A description of the Id parameter.
|
Database ID of the IP address object.
|
||||||
|
|
||||||
.PARAMETER Force
|
.PARAMETER Force
|
||||||
A description of the Force parameter.
|
Do not confirm.
|
||||||
|
|
||||||
.PARAMETER Query
|
|
||||||
A description of the Query parameter.
|
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS C:\> Remove-NetboxIPAMAddress -Id $value1
|
PS C:\> Remove-NetboxIPAMAddress -Id $value1
|
||||||
|
|
@ -527,35 +524,42 @@ function Remove-NetboxIPAMAddress {
|
||||||
SupportsShouldProcess = $true)]
|
SupportsShouldProcess = $true)]
|
||||||
param
|
param
|
||||||
(
|
(
|
||||||
[Parameter(Mandatory = $true)]
|
[Parameter(Mandatory = $true,
|
||||||
|
ValueFromPipelineByPropertyName = $true)]
|
||||||
[uint16[]]$Id,
|
[uint16[]]$Id,
|
||||||
|
|
||||||
[switch]$Force
|
[switch]$Force
|
||||||
)
|
)
|
||||||
|
|
||||||
$CurrentIPs = @(Get-NetboxIPAMAddress -Id $Id -ErrorAction Stop)
|
begin {
|
||||||
|
}
|
||||||
|
|
||||||
$Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses'))
|
process {
|
||||||
|
foreach ($IPId in $Id) {
|
||||||
foreach ($IP in $CurrentIPs) {
|
$CurrentIP = Get-NetboxIPAMAddress -Id $IPId -ErrorAction Stop
|
||||||
if ($Force -or $pscmdlet.ShouldProcess($IP.Address, "Delete")) {
|
|
||||||
$URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary @{'id' = $IP.Id}
|
|
||||||
|
|
||||||
$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 {
|
function Set-NetboxIPAMAddress {
|
||||||
[CmdletBinding(ConfirmImpact = 'High',
|
[CmdletBinding(ConfirmImpact = 'Medium',
|
||||||
SupportsShouldProcess = $true)]
|
SupportsShouldProcess = $true)]
|
||||||
param
|
param
|
||||||
(
|
(
|
||||||
[Parameter(Mandatory = $true,
|
[Parameter(Mandatory = $true,
|
||||||
ValueFromPipelineByPropertyName = $true)]
|
ValueFromPipelineByPropertyName = $true)]
|
||||||
[uint16]$Id,
|
[uint16[]]$Id,
|
||||||
|
|
||||||
[string]$Address,
|
[string]$Address,
|
||||||
|
|
||||||
|
|
@ -578,25 +582,34 @@ function Set-NetboxIPAMAddress {
|
||||||
[switch]$Force
|
[switch]$Force
|
||||||
)
|
)
|
||||||
|
|
||||||
if ($Status) {
|
begin {
|
||||||
$PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -IPAddressStatus
|
if ($Status) {
|
||||||
|
$PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -IPAddressStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($Role) {
|
||||||
|
$PSBoundParameters.Role = ValidateIPAMChoice -ProvidedValue $Role -IPAddressRole
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($Role) {
|
process{
|
||||||
$PSBoundParameters.Role = ValidateIPAMChoice -ProvidedValue $Role -IPAddressRole
|
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))
|
end{
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,11 +48,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
|
||||||
|
|
||||||
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
|
InModuleScope -ModuleName 'NetboxPS' -ScriptBlock {
|
||||||
$script:NetboxConfig.Choices.IPAM = (Get-Content "$PSScriptRoot\IPAMChoices.json" -ErrorAction Stop | ConvertFrom-Json)
|
$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 {
|
Context -Name "Get-NetboxIPAMAggregate" -Fixture {
|
||||||
It "Should request the default number of aggregates" {
|
It "Should request the default number of aggregates" {
|
||||||
$Result = Get-NetboxIPAMAggregate
|
$Result = Get-NetboxIPAMAggregate
|
||||||
|
|
@ -388,18 +384,18 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
|
||||||
}
|
}
|
||||||
|
|
||||||
Context -Name "Remove-NetboxIPAMAddress" -Fixture {
|
Context -Name "Remove-NetboxIPAMAddress" -Fixture {
|
||||||
It "Should remove a single IP" {
|
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
|
||||||
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
|
return @{
|
||||||
return @{
|
'address' = "10.1.1.1/$Id"
|
||||||
'address' = '10.1.1.1/24'
|
'id' = $id
|
||||||
'id' = 4109
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$Result = Remove-NetboxIPAMAddress -Id '4109' -Force
|
|
||||||
|
It "Should remove a single IP" {
|
||||||
|
$Result = Remove-NetboxIPAMAddress -Id 4109 -Force
|
||||||
|
|
||||||
Assert-VerifiableMock
|
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.Method | Should -Be 'DELETE'
|
||||||
$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/4109/'
|
||||||
|
|
@ -407,24 +403,43 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
|
||||||
$Result.Body | Should -Be $null
|
$Result.Body | Should -Be $null
|
||||||
}
|
}
|
||||||
|
|
||||||
It "Should remove multiple IPs" {
|
It "Should remove a single IP from the pipeline" {
|
||||||
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
|
$Result = [pscustomobject]@{
|
||||||
return @(
|
'id' = 4110
|
||||||
@{
|
} | Remove-NetboxIPAMAddress -Force
|
||||||
'address' = '10.1.1.1/24'
|
|
||||||
'id' = 4109
|
|
||||||
},
|
|
||||||
@{
|
|
||||||
'address' = '10.1.1.2/24'
|
|
||||||
'id' = 4110
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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
|
$Result = Remove-NetboxIPAMAddress -Id 4109, 4110 -Force
|
||||||
|
|
||||||
Assert-VerifiableMock
|
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.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.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 {
|
Context -Name "Set-NetboxIPAMAddress" -Fixture {
|
||||||
It "Should set an IP with a new status" {
|
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
|
||||||
Mock -CommandName "Get-NetboxIPAMAddress" -ModuleName NetboxPS -MockWith {
|
return @{
|
||||||
return @{
|
'address' = '10.1.1.1/24'
|
||||||
'address' = '10.1.1.1/24'
|
'id' = $id
|
||||||
'id' = 4109
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$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-VerifiableMock
|
||||||
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1
|
Assert-MockCalled -CommandName "Get-NetboxIPAMAddress" -Times 1 -Scope "It" -Exactly
|
||||||
|
|
||||||
$Result.Method | Should -Be 'PATCH'
|
$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/4109/'
|
||||||
|
|
@ -452,17 +467,62 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture {
|
||||||
$Result.Body | Should -Be '{"status":2}'
|
$Result.Body | Should -Be '{"status":2}'
|
||||||
}
|
}
|
||||||
|
|
||||||
It "Should set an IP with VRF, Tenant, and Description" {
|
It "Should set an IP from the pipeline" {
|
||||||
$Result = Set-NetboxIPAMAddress -Id 4109 -VRF 10 -Tenant 14 -Description 'Test description' -Force
|
$Result = [pscustomobject]@{
|
||||||
|
'Id' = 4501
|
||||||
|
} | Set-NetboxIPAMAddress -VRF 10 -Tenant 14 -Description 'Test description' -Force
|
||||||
|
|
||||||
Assert-VerifiableMock
|
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.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.Headers.Keys.Count | Should -BeExactly 1
|
||||||
$Result.Body | Should -Be '{"vrf":10,"description":"Test description","tenant":14}'
|
$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}'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue