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 PoE cmdlet #100

Merged
merged 9 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
232 changes: 232 additions & 0 deletions PowerArubaSW/Public/Poe.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
#
# Copyright 2018, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#


function Get-ArubaSWPoE {

<#
.SYNOPSIS
Get PoE info about ArubaOS Switch (Provision)

.DESCRIPTION
Get PoE Info (Status, Priority, Allocation...)

.EXAMPLE
Get-ArubaSWPoE

Get ALL PoE Settings on the switch

.EXAMPLE
Get-ArubaSWPoE -port 3

Get PoE settings on port 3
#>
Param(
[Parameter (Mandatory = $false, position = 1)]
[string]$port_id
)

Begin {
}

Process {

$url = "rest/v4/poe/ports"

if ( $port_id ) {
$url = "rest/v4/ports/$port_id/poe"
}

$response = Invoke-ArubaSWWebRequest -method "GET" -url $url

$poe = ($response.Content | ConvertFrom-Json)

if ( $port_id ) {
$poe
}
else {
$poe.port_poe
}
}

End {
}
}
function Set-ArubaSWPoE {

<#
.SYNOPSIS
Configure PoE Settings on ArubaOS Switch (Provision)

.DESCRIPTION
Configure PoE Settings (Status, Priority, Allocation...)

.EXAMPLE
$port_poe = Get-ArubaSWPoE -port 3
PS C:\>$port_poe | Set-ArubaSWPoE -is_poe_enabled:$false -poe_priority high -poe_allocation_method class

Configure port 3 and disable PoE with priority high and allocation method class

.EXAMPLE
Set-ArubaSWPoE -port_id 3 -poe_allocation_method value -allocated_power_in_watts 33 -pre_standard_detect_enabled:$false

Configure port 3 and set allocated method and allocated power to 33 (Watts) and disable pre_standard_detect

#>

Param(
[Parameter (Mandatory = $true, ParameterSetName = "port_id")]
[string]$port_id,
[Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "port_poe")]
#ValidateScript({ ValidatePoE $_ })]
[psobject]$port_poe,
[Parameter (Mandatory = $false)]
[switch]$is_poe_enabled,
[Parameter (Mandatory = $false)]
[ValidateSet ("low", "high", "critical")]
[string]$poe_priority,
[Parameter (Mandatory = $false)]
[ValidateSet ("usage", "class", "value")]
[string]$poe_allocation_method,
[Parameter (Mandatory = $false)]
[ValidateRange (1, 33)]
[int]$allocated_power_in_watts,
[Parameter (Mandatory = $false)]
[string]$port_configured_type,
[Parameter (Mandatory = $false)]
[switch]$pre_standard_detect_enabled
)

Begin {
}

Process {

#get port id from poe ps object
if ($port_poe) {
$port_id = $port_poe.port_id
}
$url = "rest/v4/ports/${port_id}/poe"

$_poe = New-Object -TypeName PSObject


if ( $PsBoundParameters.ContainsKey('is_poe_enabled') ) {
if ( $is_poe_enabled ) {
$_poe | Add-Member -name "is_poe_enabled" -membertype NoteProperty -Value $true
}
else {
$_poe | Add-Member -name "is_poe_enabled" -membertype NoteProperty -Value $false
}
}

if ( $PsBoundParameters.ContainsKey('poe_priority') ) {
switch ( $poe_priority ) {
low {
$priority = "PPP_LOW"
}
high {
$priority = "PPP_HIGH"
}
critical {
$priority = "PPP_CRITICAL"
}
}
$_poe | Add-Member -name "poe_priority" -membertype NoteProperty -Value $priority
}

if ( $PsBoundParameters.ContainsKey('poe_allocation_method') ) {
switch ( $poe_allocation_method ) {
usage {
$allocation_method = "PPAM_USAGE"
}
class {
$allocation_method = "PPAM_CLASS"
}
value {
$allocation_method = "PPAM_VALUE"
}
}
$_poe | Add-Member -name "poe_allocation_method" -membertype NoteProperty -Value $allocation_method
}

if ( $PsBoundParameters.ContainsKey('allocated_power_in_watts') ) {
$_poe | Add-Member -name "allocated_power_in_watts" -membertype NoteProperty -Value $allocated_power_in_watts
}

if ( $PsBoundParameters.ContainsKey('port_configured_type') ) {
$_poe | Add-Member -name "port_configured_type" -membertype NoteProperty -Value $port_configured_type
}

if ( $PsBoundParameters.ContainsKey('pre_standard_detect_enabled') ) {
if ( $pre_standard_detect_enabled ) {
$_poe | Add-Member -name "pre_standard_detect_enabled" -membertype NoteProperty -Value $true
}
else {
$_poe | Add-Member -name "pre_standard_detect_enabled" -membertype NoteProperty -Value $false
}
}

$response = Invoke-ArubaSWWebRequest -method "PUT" -body $_poe -url $url
$rep_poe = ($response.Content | ConvertFrom-Json)

$rep_poe
}

End {
}
}

function Get-ArubaSWPoEStats {

<#
.SYNOPSIS
Get PoE statistics about ArubaOS Switch (Provision)

.DESCRIPTION
Get PoE statistics (Voltage, Class ...)

.EXAMPLE
Get-ArubaSWPoEStats

Get ALL PoE ports statistics on the switch

.EXAMPLE
Get-ArubaSWPoEstats -port 3

Get PoE statistics on port 3
#>
Param(
[Parameter (Mandatory = $false, position = 1)]
[string]$port_id
)

Begin {
}

Process {

$url = "rest/v4/poe/ports/stats"

if ( $port_id ) {
$url = "rest/v4/ports/$port_id/poe/stats"
}

$response = Invoke-ArubaSWWebRequest -method "GET" -url $url

$poe = ($response.Content | ConvertFrom-Json)

if ( $port_id ) {
$poe
}
else {
$poe.port_poe_stats
}
}

End {
}
}
3 changes: 3 additions & 0 deletions Tests/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ $pester_trunk_trk1 = "trk3" #Port trunk 1 name of Trunk test
$pester_trunk_trk2 = "trk7" #Port trunk 2 name of Trunk test
$pester_stp_port = 3 #Port Number of STP test
$pester_cli_port = 3 #Port Number of CLI test
$pester_poe_port = 4 #Port Number of PoE test

$here = Split-Path -Parent $MyInvocation.MyCommand.Path

Expand Down Expand Up @@ -67,6 +68,7 @@ if ($product_number -eq 'J9850A' -or $product_number -eq 'J9851A') {
$pester_trunk_port = "$pester_chassis_module$pester_trunk_port"
$pester_stp_port = "$pester_chassis_module$pester_stp_port"
$pester_cli_port = "$pester_chassis_module$pester_cli_port"
$pester_poe_port = "$pester_chassis_module$pester_poe_port"
}

#Add stack module to port number (if it is a stacked switch)
Expand All @@ -77,4 +79,5 @@ if ('ST_STACKED' -eq $defaultArubaSWConnection.switch_type) {
$pester_trunk_port = "$pester_stack_module/$pester_trunk_port"
$pester_stp_port = "$pester_stack_module/$pester_stp_port"
$pester_cli_port = "$pester_stack_module/$pester_cli_port"
$pester_poe_port = "$pester_stack_module/$pester_poe_port"
}
1 change: 1 addition & 0 deletions Tests/credential.example.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ $httpOnly = $false
#$pester_trunk_trk2 = "trk7"
#$pester_stp_port = 3
#$pester_cli_port = 3
#$pester_poe_port = 4
105 changes: 105 additions & 0 deletions Tests/integration/PoE.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#
# Copyright 2018, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#
. ../common.ps1

Describe "Get PoE" {
It "Get PoE Does not throw an error" {
{
Get-ArubaSWPoE
} | Should Not Throw
}

It "Get ALL PoE (Port)" {
$poe = Get-ArubaSWPoE
$poe | Should not be $NULL
$poe.port_id | Should -BeOfType string
$poe.is_poe_enabled | Should -BeOfType boolean
$poe.poe_priority | Should -BeOfType string
$poe.poe_allocation_method | Should -BeOfType string
$poe.allocated_power_in_watts | Should -BeOfType long
$poe.port_configured_type | Should -BeOfType string
$poe.pre_standard_detect_enabled | Should -BeOfType boolean
}

It "Get PoE Port $pester_poe_port" {
$poe = Get-ArubaSWPoE -port $pester_poe_port
$poe | Should not be $NULL
$poe.port_id | Should -Be $pester_poe_port
$poe.is_poe_enabled | Should -BeOfType boolean
$poe.poe_priority | Should -BeOfType string
$poe.poe_allocation_method | Should -BeOfType string
$poe.allocated_power_in_watts | Should -BeOfType long
$poe.port_configured_type | Should -BeOfType string
$poe.pre_standard_detect_enabled | Should -BeOfType boolean
}
}

Describe "Configure PoE" {
Context "Configure PoE via Port ID" {
BeforeAll {
$script:poe_default = Get-ArubaSWPoE -port $pester_poe_port
}
It "Configure PoE Port $pester_poe_port : status, priority, allocation Method/Power in Watt, pre standard detect" {
Set-ArubaSWPoE -port_id $pester_poe_port -is_poe_enabled:$false -poe_priority critical -poe_allocation_method value -allocated_power_in_watt 1 -pre_standard_detect_enabled:$true
$poe = Get-ArubaSWPoE -port $pester_poe_port
$poe.port_id | Should -Be $pester_poe_port
$poe.is_poe_enabled | Should -Be $false
$poe.poe_priority | Should -Be "PPP_CRITICAL"
$poe.poe_allocation_method | Should -Be "PPAM_VALUE"
$poe.allocated_power_in_watts | Should -Be 1
$poe.pre_standard_detect_enabled | Should -Be $true
}
}
Context "Configure PoE Port via pipeline" {
It "Configure PoE Port $pester_poe_port : status, priority, allocation Method/Power in Watt, pre standard detect" {
Get-ArubaSWSTPPort $pester_poe_port | Set-ArubaSWPoE -is_poe_enabled:$true -poe_priority low -poe_allocation_method usage -pre_standard_detect_enabled:$false
$poe = Get-ArubaSWPoE -port $pester_poe_port
$poe.port_id | Should -Be $pester_poe_port
$poe.is_poe_enabled | Should -Be $false
$poe.poe_priority | Should -Be "PPP_LOW"
$poe.poe_allocation_method | Should -Be "PPAM_USAGE"
$poe.allocated_power_in_watts | Should -Be $poe_default.allocated_power_in_watts
$poe.pre_standard_detect_enabled | Should -Be $false
}
}
}


Describe "Get PoE Stats" {
It "Get PoE StatsDoes not throw an error" {
{
Get-ArubaSWPoEStats
} | Should Not Throw
}

It "Get ALL PoE Stats (Port)" {
$poe = Get-ArubaSWPoEStats
$poe | Should not be $NULL
$poe.port_id | Should -BeOfType string
$poe.port_voltage_in_volts | Should -BeOfType long
$poe.power_denied_count | Should -BeOfType long
$poe.over_current_count | Should -BeOfType long
$poe.mps_absent_count | Should -BeOfType long
$poe.short_count | Should -BeOfType long
$poe.actual_power_in_watts | Should -BeOfType long
$poe.power_class | Should -BeOfType long
}

It "Get PoE Port Stats $pester_poe_port" {
$poe = Get-ArubaSWPoEStats -port $pester_poe_port
$poe | Should not be $NULL
$poe.port_id | Should -Be $pester_poe_port
$poe.port_voltage_in_volts | Should -BeOfType long
$poe.power_denied_count | Should -BeOfType long
$poe.over_current_count | Should -BeOfType long
$poe.mps_absent_count | Should -BeOfType long
$poe.short_count | Should -BeOfType long
$poe.actual_power_in_watts | Should -BeOfType long
$poe.power_class | Should -BeOfType long
}
}

Disconnect-ArubaSW -noconfirm