From 985f9209001ef3c22c9112076cd37bb7abdaf5bc Mon Sep 17 00:00:00 2001 From: Ben Claussen Date: Tue, 7 Nov 2023 09:37:47 -0500 Subject: [PATCH] Add IPAMAddressRange functions --- .../IPAM/Range/Get-NetboxIPAMAddressRange.ps1 | 73 ++++++++++++ .../IPAM/Range/New-NetboxIPAMAddressRange.ps1 | 108 ++++++++++++++++++ .../Range/Remove-NetboxIPAMAddressRange.ps1 | 47 ++++++++ 3 files changed, 228 insertions(+) create mode 100644 Functions/IPAM/Range/Get-NetboxIPAMAddressRange.ps1 create mode 100644 Functions/IPAM/Range/New-NetboxIPAMAddressRange.ps1 create mode 100644 Functions/IPAM/Range/Remove-NetboxIPAMAddressRange.ps1 diff --git a/Functions/IPAM/Range/Get-NetboxIPAMAddressRange.ps1 b/Functions/IPAM/Range/Get-NetboxIPAMAddressRange.ps1 new file mode 100644 index 0000000..e512a19 --- /dev/null +++ b/Functions/IPAM/Range/Get-NetboxIPAMAddressRange.ps1 @@ -0,0 +1,73 @@ + +function Get-NetboxIPAMAddressRange { + [CmdletBinding(DefaultParameterSetName = 'Query')] + param + ( + [Parameter(ParameterSetName = 'Query', + Position = 0)] + [string]$Range, + + [Parameter(ParameterSetName = 'ByID')] + [uint64[]]$Id, + + [Parameter(ParameterSetName = 'Query')] + [string]$Query, + + [Parameter(ParameterSetName = 'Query')] + [object]$Family, + + [Parameter(ParameterSetName = 'Query')] + [string]$VRF, + + [Parameter(ParameterSetName = 'Query')] + [uint32]$VRF_Id, + + [Parameter(ParameterSetName = 'Query')] + [string]$Tenant, + + [Parameter(ParameterSetName = 'Query')] + [uint32]$Tenant_Id, + + [Parameter(ParameterSetName = 'Query')] + [object]$Status, + + [Parameter(ParameterSetName = 'Query')] + [object]$Role, + + [Parameter(ParameterSetName = 'Query')] + [uint16]$Limit, + + [Parameter(ParameterSetName = 'Query')] + [uint16]$Offset, + + [switch]$Raw + ) + + switch ($PSCmdlet.ParameterSetName) { + 'ById' { + foreach ($Range_ID in $Id) { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-ranges', $Range_ID)) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + } + + break + } + + default { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-ranges')) + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters + + $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters + + InvokeNetboxRequest -URI $uri -Raw:$Raw + + break + } + } +} \ No newline at end of file diff --git a/Functions/IPAM/Range/New-NetboxIPAMAddressRange.ps1 b/Functions/IPAM/Range/New-NetboxIPAMAddressRange.ps1 new file mode 100644 index 0000000..c6db29c --- /dev/null +++ b/Functions/IPAM/Range/New-NetboxIPAMAddressRange.ps1 @@ -0,0 +1,108 @@ + + +function New-NetboxIPAMAddressRange { +<# + .SYNOPSIS + Create a new IP address range to Netbox + + .DESCRIPTION + Create a new IP address range to Netbox with a status of Active by default. The maximum supported + size of an IP range is 2^32 - 1. + + .PARAMETER Start_Address + Starting IPv4 or IPv6 address (with mask). The maximum supported size of an IP range is 2^32 - 1. + + .PARAMETER End_Address + Ending IPv4 or IPv6 address (with mask). The maximum supported size of an IP range is 2^32 - 1. + + .PARAMETER Status + Operational status of this range. Defaults to Active + + .PARAMETER Tenant + Tenant ID + + .PARAMETER VRF + VRF ID + + .PARAMETER Role + Role such as backup, customer, development, etc... Defaults to nothing + + .PARAMETER Custom_Fields + Custom field hash table. Will be validated by the API service + + .PARAMETER Description + Description of IP address range + + .PARAMETER Comments + Extra comments (markdown supported). + + .PARAMETER Tags + One or more tags. + + .PARAMETER Mark_Utilized + Treat as 100% utilized + + .PARAMETER Raw + Return raw results from API service + + .EXAMPLE + New-NetboxIPAMAddressRange -Start_Address 192.0.2.20/24 -End_Address 192.0.2.20/24 + + Add new IP Address range from 192.0.2.20/24 to 192.0.2.20/24 with status active + + .NOTES + https://netbox.neonet.org/static/docs/models/ipam/iprange/ +#> + + [CmdletBinding(ConfirmImpact = 'Low', + SupportsShouldProcess = $true)] + [OutputType([pscustomobject])] + param + ( + [Parameter(Mandatory = $true)] + [string]$Start_Address, + + [Parameter(Mandatory = $true)] + [string]$End_Address, + + [object]$Status = 'Active', + + [uint64]$Tenant, + + [uint64]$VRF, + + [object]$Role, + + [hashtable]$Custom_Fields, + + [uint64]$Interface, + + [string]$Description, + + [string]$Comments, + + [object[]]$Tags, + + [switch]$Mark_Utilized, + + [switch]$Raw + ) + + process { + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-ranges')) + $Method = 'POST' + + $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters + + $URI = BuildNewURI -Segments $URIComponents.Segments + + if ($PSCmdlet.ShouldProcess($Start_Address, 'Create new IP address range')) { + InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw + } + } +} + + + + + diff --git a/Functions/IPAM/Range/Remove-NetboxIPAMAddressRange.ps1 b/Functions/IPAM/Range/Remove-NetboxIPAMAddressRange.ps1 new file mode 100644 index 0000000..79aaf2b --- /dev/null +++ b/Functions/IPAM/Range/Remove-NetboxIPAMAddressRange.ps1 @@ -0,0 +1,47 @@ + +function Remove-NetboxIPAMAddressRange { + <# + .SYNOPSIS + Remove an IP address range from Netbox + + .DESCRIPTION + Removes/deletes an IP address range from Netbox by ID + + .PARAMETER Id + Database ID of the IP address range object. + + .PARAMETER Force + Do not confirm. + + .EXAMPLE + PS C:\> Remove-NetboxIPAMAddressRange -Id 1234 + + .NOTES + Additional information about the function. +#> + + [CmdletBinding(ConfirmImpact = 'High', + SupportsShouldProcess = $true)] + param + ( + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true)] + [uint64[]]$Id, + + [switch]$Force + ) + + process { + foreach ($Range_Id in $Id) { + $CurrentRange = Get-NetboxIPAMAddressRange -Id $Range_Id -ErrorAction Stop + + $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-ranges', $Range_Id)) + + if ($Force -or $pscmdlet.ShouldProcess($CurrentRange.start_address, "Delete")) { + $URI = BuildNewURI -Segments $Segments + + InvokeNetboxRequest -URI $URI -Method DELETE + } + } + } +} \ No newline at end of file