-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cmdlet for StaticRoutes (Add/Get/Remove) #43
base: master
Are you sure you want to change the base?
Changes from 20 commits
be76800
3f11d50
87f9d3c
26e521c
9c3ae26
1f0f337
28095cc
c0726e7
33d8976
42a733a
bed972f
c491e2e
31ca21c
a3c5fd4
a4401eb
3db2cf3
7bd8865
83e8b24
59e7850
7a4440c
f37258c
18f6140
72403b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,34 @@ function Confirm-ArubaCXInterfaces { | |
$true | ||
|
||
} | ||
|
||
function Confirm-ArubaCXStaticRoutes { | ||
|
||
Param ( | ||
[Parameter (Mandatory = $true)] | ||
[object]$argument | ||
) | ||
#Check if it looks like an Static Routes element | ||
|
||
if ( -not ( $argument | get-member -name address_family -Membertype Properties)) { | ||
throw "Element specified does not contain a address_family property." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is "an address..." |
||
} | ||
if ( -not ( $argument | get-member -name prefix -Membertype Properties)) { | ||
throw "Element specified does not contain an prefix property." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is "a prefix" not "an" |
||
} | ||
if ( -not ( $argument | get-member -name static_nexthops -Membertype Properties)) { | ||
throw "Element specified does not contain a static_nexthops property." | ||
} | ||
if ( -not ( $argument | get-member -name type -Membertype Properties)) { | ||
throw "Element specified does not contain a type property." | ||
} | ||
if ( -not ( $argument | get-member -name vrf -Membertype Properties)) { | ||
throw "Element specified does not contain a vrf property." | ||
} | ||
$true | ||
|
||
} | ||
|
||
function Confirm-ArubaCXSystem { | ||
|
||
Param ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
# | ||
# Copyright 2020, Alexis La Goutte <alexis dot lagoutte at gmail dot com> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
function Add-ArubaCXStaticRoutes { | ||
|
||
<# | ||
.SYNOPSIS | ||
Add Aruba CX Static Static Route | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Static Static ? |
||
|
||
.DESCRIPTION | ||
Add Static Route (address_family, prefix, static_nexthops, type) | ||
|
||
.EXAMPLE | ||
Add-ArubaCXStaticRoutes -address_family ipv4 -prefix_ip4 192.0.2.0 -prefix_ip4_mask 24 -type forward | ||
|
||
Add Static Route type forward for network 192.0.2.0/24 (on default vrf) | ||
|
||
.EXAMPLE | ||
Add-ArubaCXStaticRoutes -address_family ipv4 -prefix_ip4 192.0.2.0 -prefix_ip4_mask 24 -type blackhole -vrf MyVrf | ||
|
||
Add Static Route type blackhole for network 192.0.2.0/24 on MyVRF | ||
|
||
.EXAMPLE | ||
Get-ArubaCXVrf MyVRF | Add-ArubaCXStaticRoutes -address_family ipv4 -prefix_ip4 192.0.2.0 -prefix_ip4_mask 24 -type reject | ||
|
||
Add Static Route type reject for network 192.0.2.0/24 on MyVRF (using pipeline) | ||
#> | ||
Param( | ||
[Parameter (Mandatory = $false, ValueFromPipeline = $true)] | ||
[ValidateScript( { Confirm-ArubaCXVrfs $_ })] | ||
[psobject]$vrf_pp, | ||
[Parameter(Mandatory = $false)] | ||
[string]$vrf = "default", | ||
[Parameter (Mandatory = $true)] | ||
[ValidateSet('ipv4', IgnoreCase = $false)] | ||
[string]$address_family = "ipv4", | ||
[Parameter (Mandatory = $true)] | ||
[ipaddress]$prefix_ip4, | ||
[Parameter (Mandatory = $true)] | ||
[ValidateRange(0, 32)] | ||
[int]$prefix_ip4_mask, | ||
[Parameter (Mandatory = $true)] | ||
[ValidateSet('forward', 'reject', 'blackhole')] | ||
[string]$type, | ||
[Parameter (Mandatory = $False)] | ||
[ValidateNotNullOrEmpty()] | ||
[PSObject]$connection = $DefaultArubaCXConnection | ||
) | ||
|
||
Begin { | ||
} | ||
|
||
Process { | ||
|
||
#get vrf name from vrf_pp ps object (based by pipeline) | ||
if ($vrf_pp) { | ||
$vrf = $vrf_pp.name | ||
} | ||
|
||
if ( -not ($prefix_ip4.AddressFamily -eq "InterNetwork" )) { | ||
Throw "You need to specify a IPv4 Address" | ||
} | ||
$prefix = $prefix_ip4.ToString() + "/" + $prefix_ip4_mask | ||
|
||
$uri = "system/vrfs/$vrf/static_routes" | ||
|
||
$_sr = new-Object -TypeName PSObject | ||
|
||
$_sr | add-member -name "address_family" -membertype NoteProperty -Value $address_family | ||
|
||
$_sr | add-member -name "prefix" -membertype NoteProperty -Value $prefix | ||
|
||
$_sr | add-member -name "type" -membertype NoteProperty -Value $type | ||
|
||
$_sr | add-member -name "vrf" -membertype NoteProperty -Value ("/rest/" + $($connection.version) + "/system/vrfs/" + $vrf) | ||
|
||
$response = Invoke-ArubaCXRestMethod -uri $uri -method 'POST' -body $_sr -connection $connection | ||
$response | ||
|
||
Get-ArubaCXStaticRoutes -vrf $vrf -prefix $prefix -connection $connection | ||
} | ||
|
||
End { | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a blank line ? |
||
function Get-ArubaCXStaticRoutes { | ||
|
||
<# | ||
.SYNOPSIS | ||
Get list of all Aruba CX Route | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. routes* |
||
|
||
.DESCRIPTION | ||
Get list of all Aruba CX Static Rout (address_family, prefix, static_nexthops, type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
|
||
.EXAMPLE | ||
Get-ArubaCXStaticRoutes | ||
|
||
Get list of all Static Route information (address_family, prefix, static_nexthops, type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. routes |
||
|
||
.EXAMPLE | ||
Get-ArubaCXStaticRoutes -prefix 192.0.2.0/24 | ||
|
||
Get Static Route information of prefix 192.0.2.0/24 | ||
|
||
.EXAMPLE | ||
Get-ArubaCXVrfs -vrf MyVRF | Get-ArubaCXStaticRoutes | ||
|
||
Get Static Route information from VRF named MyVRF (using pipeline) | ||
|
||
.EXAMPLE | ||
Get-ArubaCXStaticRoutes -vrf MyVRF | ||
|
||
Get Static Route information from VRF named MyVRF | ||
#> | ||
|
||
[CmdletBinding(DefaultParametersetname = "Default")] | ||
Param( | ||
[Parameter (Mandatory = $false, ParameterSetName = "prefix", position = "1")] | ||
[string]$prefix, | ||
[Parameter (Mandatory = $false, ValueFromPipeline = $true)] | ||
[ValidateScript( { Confirm-ArubaCXVrfs $_ })] | ||
[psobject]$vrf_pp, | ||
[Parameter(Mandatory = $false)] | ||
[string]$vrf = "default", | ||
[Parameter(Mandatory = $false)] | ||
[ValidateRange(1, 4)] | ||
[Int]$depth, | ||
[Parameter(Mandatory = $false)] | ||
[ValidateSet("configuration", "status", "statistics", "writable")] | ||
[String]$selector, | ||
[Parameter(Mandatory = $false)] | ||
[String[]]$attributes, | ||
[Parameter(Mandatory = $false)] | ||
[switch]$vsx_peer, | ||
[Parameter (Mandatory = $False)] | ||
[ValidateNotNullOrEmpty()] | ||
[PSObject]$connection = $DefaultArubaCXConnection | ||
) | ||
|
||
Begin { | ||
} | ||
|
||
Process { | ||
|
||
#get vrf name from vrf_pp ps object (based by pipeline) | ||
if ($vrf_pp) { | ||
$vrf = $vrf_pp.name | ||
} | ||
|
||
$invokeParams = @{ } | ||
if ( $PsBoundParameters.ContainsKey('depth') ) { | ||
$invokeParams.add( 'depth', $depth ) | ||
} | ||
if ( $PsBoundParameters.ContainsKey('selector') ) { | ||
$invokeParams.add( 'selector', $selector ) | ||
} | ||
if ( $PsBoundParameters.ContainsKey('attributes') ) { | ||
$invokeParams.add( 'attributes', $attributes ) | ||
} | ||
if ( $PsBoundParameters.ContainsKey('vsx_peer') ) { | ||
$invokeParams.add( 'vsx_peer', $true ) | ||
} | ||
|
||
$uri = "system/vrfs/$vrf/static_routes" | ||
|
||
# you can directly filter by prefix | ||
if ( $PsBoundParameters.ContainsKey('prefix') ) { | ||
#replace / by %2F | ||
$prefix = $prefix -replace '/', '%2F' | ||
$uri += "/$prefix" | ||
} | ||
|
||
$response = Invoke-ArubaCXRestMethod -uri $uri -method 'GET' -connection $connection @invokeParams | ||
|
||
#Add vref parameter when use prefix (more easy to remove and also for get vrf source...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vrf instead of vref |
||
if ( $PsBoundParameters.ContainsKey('prefix') ) { | ||
$response | add-member -name "vrf" -membertype NoteProperty -Value $vrf | ||
} | ||
|
||
$response | ||
} | ||
|
||
End { | ||
} | ||
} | ||
|
||
function Remove-ArubaCXStaticRoutes { | ||
|
||
<# | ||
.SYNOPSIS | ||
Remove a Static Route on Aruba CX Switch | ||
|
||
.DESCRIPTION | ||
Remove a Static Route on Aruba CX Switch | ||
|
||
.EXAMPLE | ||
$sr = Get-ArubaCXStaticRoutes -prefix 192.0.2.0/24 | ||
PS C:\>$sr | Remove-ArubaCXStaticRoutes | ||
|
||
Remove Static Route with prefix 192.0.2.0/24 (on default vrf) | ||
|
||
.EXAMPLE | ||
Remove-ArubaCXStaticRoutes -prefix 192.0.2.0/24 -confirm:$false -vrf MyVRF | ||
|
||
Remove Static Route 192.0.2.0/24 on MyVRF with no confirmation | ||
#> | ||
|
||
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'high')] | ||
Param( | ||
[Parameter (Mandatory = $true, ParameterSetName = "prefix")] | ||
[string]$prefix, | ||
[Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "route")] | ||
[ValidateScript( { Confirm-ArubaCXStaticRoutes $_ })] | ||
[psobject]$sr, | ||
[Parameter(Mandatory = $true, ParameterSetName = "prefix")] | ||
[string]$vrf, | ||
[Parameter (Mandatory = $False)] | ||
[ValidateNotNullOrEmpty()] | ||
[PSObject]$connection = $DefaultArubaCXConnection | ||
) | ||
|
||
Begin { | ||
} | ||
|
||
Process { | ||
|
||
#get prefix and vrf from static route ps object | ||
if ($sr) { | ||
$prefix = $sr.prefix | ||
$vrf = $sr.vrf | ||
} | ||
|
||
#replace / by %2F | ||
$uri_prefix = $prefix -replace '/', '%2F' | ||
|
||
$uri = "system/vrfs/$vrf/static_routes/$uri_prefix" | ||
|
||
if ($PSCmdlet.ShouldProcess("Static Route (VRF: ${vrf})", "Remove ${prefix}")) { | ||
Write-Progress -activity "Remove Static Route" | ||
Invoke-ArubaCXRestMethod -method "DELETE" -uri $uri -connection $connection | ||
Write-Progress -activity "Remove Static Route" -completed | ||
} | ||
} | ||
|
||
End { | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo an => a