Skip to content
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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
be76800
StaticRoutes : Add Get-ArubaCXStaticRoutes
alagoutte Apr 2, 2020
3f11d50
StaticRoutes: Add pipeline support for vrf
alagoutte Apr 2, 2020
87f9d3c
Confirm: Add Confirm-ArubaCXStaticRoutes for check if it is a SR object
alagoutte Apr 2, 2020
26e521c
StaticRoutes: Add Add-ArubaCXStaticRoutes for add Static Route
alagoutte Apr 7, 2020
9c3ae26
Confirm: Fix typo for Confirm-ArubaCXStaticRoutes
alagoutte Apr 8, 2020
1f0f337
StaticRoutes: Add Remove-ArubaCXStaticRoutes for remove a Static Route
alagoutte Apr 8, 2020
28095cc
StaticRoutes(Add): Fix add Static Routes, need to set the full URI fo…
alagoutte Apr 9, 2020
c0726e7
StaticRoutes(Add): Don't need to replace / on prefix when add static …
alagoutte Apr 9, 2020
33d8976
Connection(Tests): Add Static Routes to Multi Connection tests
alagoutte Apr 12, 2020
42a733a
StaticRoutes(Tests): Add Tests for Add/Get/Remove Static Routes
alagoutte Apr 12, 2020
bed972f
StaticRoutes: Fix typo (rject -> reject)
alagoutte Apr 12, 2020
c491e2e
StaticRoutes: Add vrf when use Get-ArubaCXStaticRoutes with prefix pa…
alagoutte Apr 12, 2020
31ca21c
Confirm(StaticRoutes): Add check of vrf attributes
alagoutte Apr 12, 2020
a3c5fd4
StaticRoutes: Get vrf info from SR ps object when using pipeline
alagoutte Apr 12, 2020
a4401eb
StaticRoutes(Tests): rename for add Tests to filename
alagoutte Apr 12, 2020
3db2cf3
StaticRoutes(Tests): add first part of VRF support for Tests Static R…
alagoutte Apr 12, 2020
7bd8865
StaticRoutes: Add Tests with a different VRF (for Get)
alagoutte Apr 17, 2020
83e8b24
StaticRoutes: Add Tests with a different VRF (for Add)
alagoutte Apr 17, 2020
59e7850
StaticRoutes: Add Tests with a different VRF (for Remove)
alagoutte Apr 17, 2020
7a4440c
StaticRoutes: Enhance Example for remove
alagoutte Apr 17, 2020
f37258c
StaticRoutes(Tests): Fix blank line
alagoutte Apr 19, 2020
18f6140
Confirm(Static Routes): fix typo
alagoutte Apr 19, 2020
72403b2
StaticRoutes: fix typo
alagoutte Apr 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions PowerArubaCX/Private/Confirm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo an => a


if ( -not ( $argument | get-member -name address_family -Membertype Properties)) {
throw "Element specified does not contain a address_family property."

Choose a reason for hiding this comment

The 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."

Choose a reason for hiding this comment

The 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 (
Expand Down
250 changes: 250 additions & 0 deletions PowerArubaCX/Public/StaticRoutes.ps1
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

Choose a reason for hiding this comment

The 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 {
}
}

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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...)

Choose a reason for hiding this comment

The 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 {
}
}
3 changes: 3 additions & 0 deletions Tests/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ $script:pester_vlan = 85 #vlan id for Vlan test
$script:pester_vlan2 = 86 #vlan id for Vlan test (for affect a second vlan to interface)
$script:pester_interface = "1/1/1" #interface id for test...
$script:pester_vrf = "pester_vrf" #interface id for test...
$script:pester_sr_ip4 = "192.0.2.0" # Network for Static Route
$script:pester_sr_mask = "24" # Netmask for Static Route
$script:pester_sr = $pester_sr_ip4 + "/" + $pester_sr_mask

. ../credential.ps1
#TODO: Add check if no ipaddress/login/password info...
Expand Down
3 changes: 3 additions & 0 deletions Tests/integration/Connection.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Describe "Connect to a switch (using multi connection)" {
It "Use Multi connection for call Get Vrfs" {
{ Get-ArubaCXVrfs -connection $cx } | Should Not throw
}
It "Use Multi connection for call Get Static Routes" {
{ Get-ArubaCXStaticRoutes -connection $cx } | Should Not throw
}
}

It "Disconnect to a switch (Multi connection)" {
Expand Down
Loading