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 Multi (API) Version Connection support #128

Merged
merged 20 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4c76b99
Add Version (min, cur, max) when connect
alagoutte Jan 3, 2020
3595626
Connection: Add product_number info
alagoutte Jan 3, 2020
9926e04
Connection: Move get version before other API call
alagoutte Dec 6, 2021
b513505
WebRequest: Add version parameter to pass version parameter
alagoutte Dec 6, 2021
2e2ec6a
WebRequest: Fix typo
alagoutte Dec 6, 2021
8ac9f80
RestVersion: Add version to 0 for don't append uri
alagoutte Dec 6, 2021
a181d69
Connection: Fix Multi Connection with Get REST Version
alagoutte Dec 7, 2021
b085ca8
Public: Update URI for don't specify API version
alagoutte Dec 7, 2021
53e0e08
Connection(Tests): Add Tests about (api)version
alagoutte Dec 7, 2021
eae072f
Connection: Rename version to api_version
alagoutte Dec 7, 2021
05505de
WebRequest: rename version to api_version
alagoutte Dec 7, 2021
ad443dd
WebRequest: Add ValidateRange (0 to 10)
alagoutte Dec 7, 2021
331cd84
Connection: Add api_version parameter to select version when connect
alagoutte Dec 7, 2021
3475e3b
RestVersion: Fix parameter name...
alagoutte Dec 7, 2021
cff0941
Connection: Fix typo and use last release or select api_version for o…
alagoutte Dec 7, 2021
82d8f25
Connection: Remove -noverbose parameter for connection
alagoutte Dec 13, 2021
905e18b
Connection: Add Set-ArubaSWConnection for specify api_version
alagoutte Dec 13, 2021
7121ef1
Connection(Tests): Add Multi Version tests (with multi connection)
alagoutte Dec 13, 2021
ed83175
Connection(Tests): Add Tests of Set-ArubaSWConnection (with api_versi…
alagoutte Dec 13, 2021
ff2995a
WebRequest: Fix indent for .EXAMPLE
alagoutte Dec 14, 2021
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
42 changes: 34 additions & 8 deletions PowerArubaSW/Private/Webrequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@ function Invoke-ArubaSWWebRequest() {
Invoke WebRequest with ArubaSW connection variable (IP Address, cookie, port...)

.EXAMPLE
Invoke-ArubaSWWebRequest -method "get" -uri "rest/v4/vlan"
Invoke-ArubaSWWebRequest -method "get" -uri "vlan"

Invoke-WebRequest with ArubaSW connection for get rest/v4/vlan
Invoke-WebRequest with ArubaSW connection for get rest/vX/vlan

.EXAMPLE
Invoke-ArubaSWWebRequest "rest/v4/system"
Invoke-ArubaSWWebRequest "system"

Invoke-WebRequest with ArubaSW connection for get rest/v4/system uri with default GET method parameter
Invoke-WebRequest with ArubaSW connection for get rest/vX/system uri with default GET method parameter

.EXAMPLE
Invoke-ArubaSWWebRequest -method "post" -uri "rest/v4/system" -body $body
Invoke-ArubaSWWebRequest -method "post" -uri "system" -body $body

Invoke-WebRequest with ArubaSW connection for post rest/v4/system uri with $body payload
Invoke-WebRequest with ArubaSW connection for post est/vX/ssystem uri with $body payload

.EXAMPLE
Invoke-ArubaSWWebRequest -method "get" -uri "system" -api_version 4

Invoke-WebRequest with ArubaSW connection for get rest/v4/ssystem uri

.EXAMPLE
Invoke-ArubaSWWebRequest -method "get" -uri "/rest/v8/system" -api_version 0

Invoke-WebRequest with ArubaSW connection for get /rest/v8/system uri

#>

Expand All @@ -36,6 +46,9 @@ function Invoke-ArubaSWWebRequest() {
[ValidateSet("GET", "POST", "DELETE", "PUT")]
[String]$method = "get",
[Parameter(Mandatory = $false)]
[ValidateRange(0, 10)]
[int]$api_version,
[Parameter(Mandatory = $false)]
[psobject]$body,
[Parameter(Mandatory = $false)]
[psobject]$connection
Expand All @@ -60,12 +73,25 @@ function Invoke-ArubaSWWebRequest() {
$sessionvariable = $connection.session

if ($httpOnly) {
$fullurl = "http://${Server}:${port}/${uri}"
$fullurl = "http://${Server}:${port}/"
}
else {
$fullurl = "https://${Server}:${port}/${uri}"
$fullurl = "https://${Server}:${port}/"
}

if ( $PsBoundParameters.ContainsKey('api_version') ) {
#Not Equal to 0, we add $api_version (if 0 we don't add rest info..)
if ($api_version -ne "0") {
$fullurl += "rest/v" + $api_version + "/"
}
}
else {
#Get info from connection
$fullurl += "rest/v" + $connection.api_version.cur + "/"
}

$fullurl += $uri

try {
if ($body) {

Expand Down
4 changes: 2 additions & 2 deletions PowerArubaSW/Public/Banner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Get-ArubaSWBanner {

Process {

$uri = "rest/v4/banner"
$uri = "banner"

$response = Invoke-ArubaSWWebRequest -method "GET" -uri $uri -connection $connection

Expand Down Expand Up @@ -105,7 +105,7 @@ function Set-ArubaSWBanner {
}

Process {
$uri = "rest/v4/banner"
$uri = "banner"

$banner = New-Object -TypeName PSObject

Expand Down
6 changes: 3 additions & 3 deletions PowerArubaSW/Public/CLI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function Get-ArubaSWCli {

Process {

$uri = "rest/v4/cli"
$uri = "cli"

$run = New-Object -TypeName PSObject

Expand Down Expand Up @@ -120,7 +120,7 @@ function Send-ArubaSWCliBatch {
$nb = $nb + 1
}

$uri = "rest/v4/cli_batch"
$uri = "cli_batch"

$conf = New-Object -TypeName PSObject

Expand Down Expand Up @@ -167,7 +167,7 @@ function Get-ArubaSWCliBatchStatus {

Process {

$uri = "rest/v4/cli_batch/status"
$uri = "cli_batch/status"

$response = Invoke-ArubaSWWebRequest -method "GET" -uri $uri -connection $connection

Expand Down
121 changes: 100 additions & 21 deletions PowerArubaSW/Public/Connection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ function Connect-ArubaSW {

Connect to an ArubaOS Switch with IP 192.0.2.1 and store connection info to $sw2 variable
and don't store connection on global ($DefaultArubaSWConnection) variable

.EXAMPLE
Connect-ArubaSW -Server 192.0.2.1 -api_version 2

Connect to an ArubaOS Switch with IP 192.0.2.1 using v2 API
#>

Param(
Expand All @@ -69,15 +74,16 @@ function Connect-ArubaSW {
[Parameter(Mandatory = $false)]
[PSCredential]$Credentials,
[Parameter(Mandatory = $false)]
[switch]$noverbose = $false,
[Parameter(Mandatory = $false)]
[switch]$httpOnly = $false,
[Parameter(Mandatory = $false)]
[switch]$SkipCertificateCheck = $false,
[Parameter(Mandatory = $false)]
[ValidateRange(1, 65535)]
[int]$port,
[Parameter(Mandatory = $false)]
[ValidateRange(1, 10)]
[int]$api_version,
[Parameter(Mandatory = $false)]
[boolean]$DefaultConnection = $true
)

Expand All @@ -86,7 +92,8 @@ function Connect-ArubaSW {

Process {

$connection = @{server = ""; session = ""; cookie = ""; httpOnly = $false; port = ""; invokeParams = ""; switch_type = "" }
$version = @{min = ""; cur = ""; max = "" }
$connection = @{server = ""; session = ""; cookie = ""; httpOnly = $false; port = ""; invokeParams = ""; switch_type = "" ; api_version = $version ; product_number = "" }

#If there is a password (and a user), create a credentials
if ($Password) {
Expand Down Expand Up @@ -115,7 +122,7 @@ function Connect-ArubaSW {
$port = 80
}
$connection.httpOnly = $true
$uri = "http://${Server}:${port}/rest/v3/login-sessions"
$uri = "http://${Server}:${port}/"
}
else {
if (!$port) {
Expand All @@ -131,7 +138,15 @@ function Connect-ArubaSW {
Set-ArubaSWuntrustedSSL
}
}
$uri = "https://${Server}:${port}/rest/v3/login-sessions"
$uri = "https://${Server}:${port}/"
}

if ($PsBoundParameters.ContainsKey('api_version')) {
$uri += "rest/v${api_version}/login-sessions"
}
else {
#By default use v3 API (some 'new' device don't support v1/v2 API...)
$uri += "rest/v3/login-sessions"
}

try {
Expand All @@ -155,30 +170,54 @@ function Connect-ArubaSW {
Set-Variable -name DefaultArubaSWConnection -value $connection -scope Global
}

$restversion = Get-ArubaSWRestversion -connection $connection
#Remove v and .x (.0, 1)
$vers = $restversion.version -replace "v" -replace ".0" -replace ".1"

$connection.api_version.min = ($vers | Measure-Object -Minimum).Minimum
$connection.api_version.max = ($vers | Measure-Object -Maximum).Maximum

if ($PsBoundParameters.ContainsKey('api_version')) {
$connection.api_version.cur = $api_version
}
else {
#use by default the high version release supported
$connection.api_version.cur = $connection.api_version.max
}

$switchstatus = Get-ArubaSWSystemStatusSwitch -connection $connection
$connection.switch_type = $switchstatus.switch_type

if (-not $noverbose) {
$switchsystem = Get-ArubaSWSystem -connection $connection
if ('ST_STACKED' -eq $switchstatus.switch_type) {
if ( $switchstatus.blades.count -eq "1") {
$connection.product_number = $switchstatus.blades.product_number
}
else {
$connection.product_number = $switchstatus.blades.product_number[0]
}

}
else {
$connection.product_number = $switchstatus.product_number
}

$switchsystem = Get-ArubaSWSystem -connection $connection

if ($switchstatus.switch_type -eq "ST_STACKED") {
$product_name = $NULL;
foreach ($blades in $switchstatus.blades) {
if ($blades.product_name) {
if ($product_name) {
$product_name += ", "
}
$product_name += $blades.product_name
if ($switchstatus.switch_type -eq "ST_STACKED") {
$product_name = $NULL;
foreach ($blades in $switchstatus.blades) {
if ($blades.product_name) {
if ($product_name) {
$product_name += ", "
}
$product_name += $blades.product_name
}
}
else {
$product_name = $switchstatus.product_name
}
Write-Output "Welcome on $($switchsystem.name) -$product_name"

}
else {
$product_name = $switchstatus.product_name
}
Write-Verbose "Welcome on $($switchsystem.name) -$product_name"

#Return connection info
$connection
Expand All @@ -188,6 +227,46 @@ function Connect-ArubaSW {
}
}

function Set-ArubaSWConnection {

<#
.SYNOPSIS
Configure Aruba SW connection Setting

.DESCRIPTION
Configure Aruba SW connection Setting (api_version...)

.EXAMPLE
Set-ArubaSWConnection -api_version 4

Configure default connection api_version to 4

#>

[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
Param(
[Parameter(Mandatory = $false)]
[ValidateRange(1, 10)]
[int]$api_version,
[Parameter(Mandatory = $false)]
[psobject]$connection = $DefaultArubaSWConnection
)

Begin {
}

Process {

if ($PSCmdlet.ShouldProcess($connection.server, 'Set default api_version on connection')) {
$connection.api_version.cur = $api_version
}

}

End {
}
}

function Disconnect-ArubaSW {

<#
Expand Down Expand Up @@ -221,7 +300,7 @@ function Disconnect-ArubaSW {

Process {

$uri = "rest/v3/login-sessions"
$uri = "login-sessions"

if ($PSCmdlet.ShouldProcess($connection.server, 'Remove Connection')) {
$null = Invoke-ArubaSWWebRequest -method "DELETE" -uri $uri -connection $connection
Expand Down
6 changes: 3 additions & 3 deletions PowerArubaSW/Public/DNS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Get-ArubaSWDns {

Process {

$uri = "rest/v4/dns"
$uri = "dns"

$response = Invoke-ArubaSWWebRequest -method "GET" -uri $uri -connection $connection

Expand Down Expand Up @@ -90,7 +90,7 @@ function Set-ArubaSWDns {

Process {

$uri = "rest/v4/dns"
$uri = "dns"

$conf = New-Object -TypeName PSObject

Expand Down Expand Up @@ -186,7 +186,7 @@ function Remove-ArubaSWDns {

$dns | Add-Member -name "dns_config_mode" -membertype NoteProperty -Value "DCM_DISABLED"

$uri = "rest/v4/dns"
$uri = "dns"

if ($PSCmdlet.ShouldProcess($connection.server, 'Remove DNS')) {
$null = Invoke-ArubaSWWebRequest -method "PUT" -body $dns -uri $uri -connection $connection
Expand Down
2 changes: 1 addition & 1 deletion PowerArubaSW/Public/IPAddress.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Get-ArubaSWIpAddress {

Process {

$uri = "rest/v4/ipaddresses"
$uri = "ipaddresses"

$response = Invoke-ArubaSWWebRequest -method "GET" -uri $uri -connection $connection

Expand Down
6 changes: 3 additions & 3 deletions PowerArubaSW/Public/LACP.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Get-ArubaSWLACP {

Process {

$uri = "rest/v4/lacp/port"
$uri = "lacp/port"

$response = Invoke-ArubaSWWebRequest -method "GET" -uri $uri -connection $connection

Expand Down Expand Up @@ -84,7 +84,7 @@ function Add-ArubaSWLACP {

Process {

$uri = "rest/v4/lacp/port"
$uri = "lacp/port"

$lacp = New-Object -TypeName PSObject

Expand Down Expand Up @@ -149,7 +149,7 @@ function Remove-ArubaSWLACP {

$id = $lacp.port_id

$uri = "rest/v4/lacp/port/${id}"
$uri = "lacp/port/${id}"

if ($PSCmdlet.ShouldProcess($id, 'Remove LACP')) {
$null = Invoke-ArubaSWWebRequest -method "DELETE" -body $lacp -uri $uri -connection $connection
Expand Down
Loading