From 3f612e27b6aec65a56e9facf2bc6221a7cae7769 Mon Sep 17 00:00:00 2001 From: staley1975 Date: Fri, 17 Nov 2023 13:15:38 -0600 Subject: [PATCH 1/4] modified NetworkDevice.ps1 to allow snmp parameters --- PowerArubaCP/Public/NetworkDevice.ps1 | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/PowerArubaCP/Public/NetworkDevice.ps1 b/PowerArubaCP/Public/NetworkDevice.ps1 index 2dd9bbf..9867134 100644 --- a/PowerArubaCP/Public/NetworkDevice.ps1 +++ b/PowerArubaCP/Public/NetworkDevice.ps1 @@ -23,6 +23,11 @@ function Add-ArubaCPNetworkDevice { Add Network Device SW2 with COA Capability on port 5000 + .EXAMPLE + Add-ArubaCPNetworkDevice -name SW3 -ip_address 192.0.2.3 -radius_secret MySecurePassword -vendor Cisco -snmp_version V2C -community_string CommString + + Add Network Device SW3 with a snmp-read community string from vendor Cisco + .EXAMPLE Add-ArubaCPNetworkDevice -name SW3 -ip_address 192.0.2.3 -radius_secret MySecurePassword -vendor Cisco -tacacs_secret MySecurePassword @@ -53,6 +58,10 @@ function Add-ArubaCPNetworkDevice { [Parameter (Mandatory = $true)] [string]$radius_secret, [Parameter (Mandatory = $false)] + [string]$snmp_version, + [Parameter (Mandatory = $false)] + [string]$community_string, + [Parameter (Mandatory = $false)] [string]$tacacs_secret, [Parameter (Mandatory = $true)] [string]$vendor_name, @@ -124,6 +133,15 @@ function Add-ArubaCPNetworkDevice { $_nad | add-member -name "attributes" -membertype NoteProperty -Value $attributes } + if ($PsBoundParameters.ContainsKey('snmp_version')) { + $snmp_read = @{ + snmp_version = $snmp_version + community_string = $community_string + zone_name = "default" + } + $_nad | add-member -name "snmp_read" -membertype NoteProperty -Value $snmp_read + } + $nad = invoke-ArubaCPRestMethod -method "POST" -body $_nad -uri $uri -connection $connection $nad } @@ -273,6 +291,12 @@ function Set-ArubaCPNetworkDevice { Set Vendor Name to Cisco and (re)configure TACACS Secret of NAD-PowerArubaCP + .EXAMPLE + $nad = Get-ArubaCPNetworkDevice -name NAD-PowerArubaCP + PS > $nad | Set-ArubaCPNetworkDevice -snmp_version V2C -community_string MyComm + + Set SNMP version and community string of NAD-PowerArubaCP + .EXAMPLE $nad = Get-ArubaCPNetworkDevice -name NAD-PowerArubaCP PS > $nad | Set-ArubaCPNetworkDevice -coa_capable -coa_port 5000 @@ -297,6 +321,10 @@ function Set-ArubaCPNetworkDevice { [Parameter (Mandatory = $false)] [string]$radius_secret, [Parameter (Mandatory = $false)] + [string]$snmp_version, + [Parameter (Mandatory = $false)] + [string]$community_string, + [Parameter (Mandatory = $false)] [string]$tacacs_secret, [Parameter (Mandatory = $false)] [string]$vendor_name, @@ -345,6 +373,16 @@ function Set-ArubaCPNetworkDevice { $_nad | add-member -name "radius_secret" -membertype NoteProperty -Value $radius_secret } + if ($PsBoundParameters.ContainsKey('snmp_version')) { + $snmp_read = @{ + snmp_version = $snmp_version + community_string = $community_string + zone_name = "default" + } + $_nad | add-member -name "snmp_read" -membertype NoteProperty -Value $snmp_read + } + + if ( $PsBoundParameters.ContainsKey('tacacs_secret') ) { $_nad | add-member -name "tacacs_secret" -membertype NoteProperty -Value $tacacs_secret } From f71ad2bc893eee1b2b53442bcd549a343fc92ed3 Mon Sep 17 00:00:00 2001 From: Curtis Lahr Date: Fri, 17 Nov 2023 16:38:41 -0600 Subject: [PATCH 2/4] fixed alignment, add ValidateSet for snmp_version, check for snmp_community if snmp_version is set --- PowerArubaCP/Public/NetworkDevice.ps1 | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/PowerArubaCP/Public/NetworkDevice.ps1 b/PowerArubaCP/Public/NetworkDevice.ps1 index 9867134..d527aab 100644 --- a/PowerArubaCP/Public/NetworkDevice.ps1 +++ b/PowerArubaCP/Public/NetworkDevice.ps1 @@ -58,6 +58,7 @@ function Add-ArubaCPNetworkDevice { [Parameter (Mandatory = $true)] [string]$radius_secret, [Parameter (Mandatory = $false)] + [ValidateSet('v1', 'v2c')] [string]$snmp_version, [Parameter (Mandatory = $false)] [string]$community_string, @@ -134,10 +135,14 @@ function Add-ArubaCPNetworkDevice { } if ($PsBoundParameters.ContainsKey('snmp_version')) { + # Check if snmp_version is provided, and if so, make community_string mandatory + if (-not $PsBoundParameters.ContainsKey('community_string')) { + throw "If snmp_version is specified, community_string is mandatory." + } $snmp_read = @{ - snmp_version = $snmp_version - community_string = $community_string - zone_name = "default" + snmp_version = $snmp_version + community_string = $community_string + zone_name = "default" } $_nad | add-member -name "snmp_read" -membertype NoteProperty -Value $snmp_read } @@ -321,6 +326,7 @@ function Set-ArubaCPNetworkDevice { [Parameter (Mandatory = $false)] [string]$radius_secret, [Parameter (Mandatory = $false)] + [ValidateSet('v1', 'v2c')] [string]$snmp_version, [Parameter (Mandatory = $false)] [string]$community_string, @@ -374,10 +380,14 @@ function Set-ArubaCPNetworkDevice { } if ($PsBoundParameters.ContainsKey('snmp_version')) { + # Check if snmp_version is provided, and if so, make community_string mandatory + if (-not $PsBoundParameters.ContainsKey('community_string')) { + throw "If snmp_version is specified, community_string is mandatory." + } $snmp_read = @{ - snmp_version = $snmp_version - community_string = $community_string - zone_name = "default" + snmp_version = $snmp_version + community_string = $community_string + zone_name = "default" } $_nad | add-member -name "snmp_read" -membertype NoteProperty -Value $snmp_read } From c268f57fd64e009694b6c91561b51cd6e5f9b360 Mon Sep 17 00:00:00 2001 From: staley1975 Date: Sat, 18 Nov 2023 09:28:46 -0600 Subject: [PATCH 3/4] Convert snmp params to uppercase. Added tests --- PowerArubaCP/Public/NetworkDevice.ps1 | 4 +- Tests/integration/NetworkDevice.Tests.ps1 | 54 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/PowerArubaCP/Public/NetworkDevice.ps1 b/PowerArubaCP/Public/NetworkDevice.ps1 index d527aab..8b96cfa 100644 --- a/PowerArubaCP/Public/NetworkDevice.ps1 +++ b/PowerArubaCP/Public/NetworkDevice.ps1 @@ -140,7 +140,7 @@ function Add-ArubaCPNetworkDevice { throw "If snmp_version is specified, community_string is mandatory." } $snmp_read = @{ - snmp_version = $snmp_version + snmp_version = $snmp_version.ToUpper() community_string = $community_string zone_name = "default" } @@ -385,7 +385,7 @@ function Set-ArubaCPNetworkDevice { throw "If snmp_version is specified, community_string is mandatory." } $snmp_read = @{ - snmp_version = $snmp_version + snmp_version = $snmp_version.ToUpper() community_string = $community_string zone_name = "default" } diff --git a/Tests/integration/NetworkDevice.Tests.ps1 b/Tests/integration/NetworkDevice.Tests.ps1 index cdddd3f..37cc46b 100644 --- a/Tests/integration/NetworkDevice.Tests.ps1 +++ b/Tests/integration/NetworkDevice.Tests.ps1 @@ -403,6 +403,60 @@ Describe "Remove Network Device" { } +Describe "Add Network Device" { + + It "Add Network Device with snmp v2c" { + Add-ArubaCPNetworkDevice -name pester_SW6 -ip_address 192.0.2.6 -radius_secret MySecurePassword -vendor Cisco -snmp_version v2c -community_string myString + $nad = Get-ArubaCPNetworkDevice -name pester_SW6 + $nad.name | Should -Be "pester_SW6" + $nad.ip_address | Should -Be "192.0.2.6" + $nad.vendor_name | Should -Be "Cisco" + $nad.snmp_read.snmp_version | Should -Be "v2c" + $nad.snmp_read.zone_name | Should -Be "default" + # community_string is always empty + } + + It "Add Network Device with snmp v1" { + Add-ArubaCPNetworkDevice -name pester_SW6 -ip_address 192.0.2.6 -radius_secret MySecurePassword -vendor Cisco -snmp_version v1 -community_string myString + $nad = Get-ArubaCPNetworkDevice -name pester_SW6 + $nad.name | Should -Be "pester_SW6" + $nad.ip_address | Should -Be "192.0.2.6" + $nad.vendor_name | Should -Be "Cisco" + $nad.snmp_read.snmp_version | Should -Be "v1" + $nad.snmp_read.zone_name | Should -Be "default" + # community_string is always empty + } + + It "Add Network Device with snmp v2c then change it to v1" { + Add-ArubaCPNetworkDevice -name pester_SW6 -ip_address 192.0.2.6 -radius_secret MySecurePassword -vendor Cisco -snmp_version v2c -community_string myString + Get-ArubaCPNetworkDevice -name pester_SW6 | Set-ArubaCPNetworkDevice -snmp_version v1 -community_string myString + $nad = Get-ArubaCPNetworkDevice -name pester_SW6 + $nad.name | Should -Be "pester_SW6" + $nad.ip_address | Should -Be "192.0.2.6" + $nad.vendor_name | Should -Be "Cisco" + $nad.snmp_read.snmp_version | Should -Be "v1" + $nad.snmp_read.zone_name | Should -Be "default" + # community_string is always empty + } + + It "Add Network Device with snmp v1 then change it to v2c" { + Add-ArubaCPNetworkDevice -name pester_SW6 -ip_address 192.0.2.6 -radius_secret MySecurePassword -vendor Cisco -snmp_version v1 -community_string myString + Get-ArubaCPNetworkDevice -name pester_SW6 | Set-ArubaCPNetworkDevice -snmp_version v2c -community_string myString + $nad = Get-ArubaCPNetworkDevice -name pester_SW6 + $nad.name | Should -Be "pester_SW6" + $nad.ip_address | Should -Be "192.0.2.6" + $nad.vendor_name | Should -Be "Cisco" + $nad.snmp_read.snmp_version | Should -Be "v2c" + $nad.snmp_read.zone_name | Should -Be "default" + # community_string is always empty + } + + AfterEach { + Get-ArubaCPNetworkDevice -name pester_SW6 | Remove-ArubaCPNetworkDevice -confirm:$false + } + +} + AfterAll { Disconnect-ArubaCP -confirm:$false } \ No newline at end of file From eebb05a94ca494b9e688ac70a3301bfebfc829d1 Mon Sep 17 00:00:00 2001 From: staley1975 Date: Sat, 18 Nov 2023 18:16:56 -0600 Subject: [PATCH 4/4] moved example to bottom and used SW6 --- PowerArubaCP/Public/NetworkDevice.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PowerArubaCP/Public/NetworkDevice.ps1 b/PowerArubaCP/Public/NetworkDevice.ps1 index 8b96cfa..5289974 100644 --- a/PowerArubaCP/Public/NetworkDevice.ps1 +++ b/PowerArubaCP/Public/NetworkDevice.ps1 @@ -23,11 +23,6 @@ function Add-ArubaCPNetworkDevice { Add Network Device SW2 with COA Capability on port 5000 - .EXAMPLE - Add-ArubaCPNetworkDevice -name SW3 -ip_address 192.0.2.3 -radius_secret MySecurePassword -vendor Cisco -snmp_version V2C -community_string CommString - - Add Network Device SW3 with a snmp-read community string from vendor Cisco - .EXAMPLE Add-ArubaCPNetworkDevice -name SW3 -ip_address 192.0.2.3 -radius_secret MySecurePassword -vendor Cisco -tacacs_secret MySecurePassword @@ -44,6 +39,11 @@ function Add-ArubaCPNetworkDevice { Add Network Device SW5 with hashtable attribute (Location) from vendor Aruba + .EXAMPLE + Add-ArubaCPNetworkDevice -name SW6 -ip_address 192.0.2.6 -radius_secret MySecurePassword -vendor Cisco -snmp_version V2C -community_string CommString + + Add Network Device SW6 with a snmp-read community string from vendor Cisco + #> Param(