Skip to content

Commit

Permalink
Merge pull request #69 from alagoutte/Attributes
Browse files Browse the repository at this point in the history
Enhance Attributes Support (Add/Set/Remove) for Endpoint/LocalUser/NetworkDevice
  • Loading branch information
alagoutte authored Apr 13, 2022
2 parents ece1ff2 + 5ca7d1f commit 56aa16f
Show file tree
Hide file tree
Showing 8 changed files with 945 additions and 140 deletions.
278 changes: 278 additions & 0 deletions PowerArubaCP/Public/Attributes.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
#
# Copyright 2022, Alexis La Goutte <alexis.lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Add-ArubaCPAttributesMember {

<#
.SYNOPSIS
Add an Attribute Member
.DESCRIPTION
Add an Attribute Member on Endpoint / Local User / Network Device
.EXAMPLE
Get-ArubaCPEndpoint -mac_address 00:01:02:03:04:05 | Add-ArubaCPAttributesMember -name "Disabled by" -value PowerArubaCP
Add Attribute name "Disabled By" with value PowerArubaCP to Endpoint 00:01:02:03:04:05
.EXAMPLE
Get-ArubaCPNetworkDevice -name NAD-PowerArubaCP | Add-ArubaCPAttributesMember -name "Location", "syslocation" -value "PowerArubaCP", "PowerArubaCP"
Add Attribute name "Location and Syslocation" with value PowerArubaCP to network Device NAD-PowerArubaCP
.EXAMPLE
Get-ArubaCPLocalUser -user_id MyPowerArubaCP_userid | Add-ArubaCPAttributesMember -attributes @{"Disabled by"="PowerArubaCP"}
Add Attribute name "Disabled By" with value PowerArubaCP to Local User MyPowerArubaCP_userid
#>

Param(
[Parameter (Mandatory = $true, ValueFromPipeline = $true)]
#[ValidateScript( { (Confirm-ArubaCPEndpoint $_) -or (Confirm-ArubaCPNetworkDevice $_) })]
[psobject]$atts,
[Parameter (Mandatory = $true, ParameterSetName = "nv")]
[string[]]$name,
[Parameter (Mandatory = $true, ParameterSetName = "nv")]
[string[]]$value,
[Parameter (Mandatory = $true, ParameterSetName = "att")]
[psobject]$attributes,
[Parameter (Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[PSObject]$connection = $DefaultArubaCPConnection
)

Begin {
}

Process {

$id = $atts.id
if ($atts.mac_address -and $atts.status) {
$uri = "api/endpoint/${id}"
}
elseif ($atts.user_id -and $atts.role_name) {
$uri = "api/local-user/${id}"
}
elseif ($atts.ip_address -and $atts.vendor_name) {
$uri = "api/network-device/${id}"
}
else {
Throw "Not an Endpoint, a Local User or a Network Device"
}

if ($PSCmdlet.ParameterSetName -eq "nv") {
if (@($name).count -ne @($value).count) {
Throw "You need to have the same number of name and value parameters"
}

$attributes = New-Object -TypeName PSObject
$i = 0
foreach ($n in $name) {
$attributes | Add-Member -name $n -MemberType NoteProperty -Value $value[$i]
$i++
}

}

$_att = New-Object -TypeName PSObject

$_att | Add-Member -name "attributes" -MemberType NoteProperty -Value $attributes

$att = Invoke-ArubaCPRestMethod -method "PATCH" -body $_att -uri $uri -connection $connection
$att
}

End {
}

}

function Set-ArubaCPAttributesMember {

<#
.SYNOPSIS
Set an Attribute Member
.DESCRIPTION
Configure an Attribute Member on Endpoint / Local User / Network Device
Remove existing attributes
.EXAMPLE
Get-ArubaCPEndpoint -mac_address 00:01:02:03:04:05 | Set-ArubaCPAttributesMember -name "Disabled by" -value PowerArubaCP
Set Attribute name "Disabled By" with value PowerArubaCP to Endpoint 00:01:02:03:04:05
.EXAMPLE
Get-ArubaCPNetworkDevice -name NAD-PowerArubaCP | Set-ArubaCPAttributesMember -name "Location", "syslocation" -value "PowerArubaCP", "PowerArubaCP"
Set Attribute name "Location and Syslocation" with value PowerArubaCP to network Device NAD-PowerArubaCP
.EXAMPLE
Get-ArubaCPLocalUser -user_id MyPowerArubaCP_userid | Set-ArubaCPAttributesMember -attributes @{"Disabled by"="PowerArubaCP"}
Set Attribute name "Disabled By" with value PowerArubaCP to Local User MyPowerArubaCP_userid
#>

[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
Param(
[Parameter (Mandatory = $true, ValueFromPipeline = $true)]
#[ValidateScript( { ( Confirm-ArubaCPEndpoint $_ -or Confirm-ArubaCPNetworkDevice $_) })]
[psobject]$atts,
[Parameter (Mandatory = $true, ParameterSetName = "nv")]
[string[]]$name,
[Parameter (Mandatory = $true, ParameterSetName = "nv")]
[string[]]$value,
[Parameter (Mandatory = $true, ParameterSetName = "att")]
[psobject]$attributes,
[Parameter (Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[PSObject]$connection = $DefaultArubaCPConnection
)

Begin {
}

Process {

#get atts id from Endpoint/Local User/Network Device PS object
if ($atts) {
$id = $atts.id
$old_name = "(" + $atts.name + ")"
}

if ($atts.mac_address -and $atts.status) {
$uri = "api/endpoint/${id}"
$delete_value = "--For-Delete--"
}
elseif ($atts.user_id -and $atts.role_name) {
$uri = "api/local-user/${id}"
$delete_value = ""
}
elseif ($atts.ip_address -and $atts.vendor_name) {
$uri = "api/network-device/${id}"
$delete_value = ""
}
else {
Throw "Not an Endpoint, a Local User or a Network Device"
}

$_att = New-Object -TypeName PSObject

#Add new name/value (or attributes)
if ($PSCmdlet.ParameterSetName -eq "nv") {
if (@($name).count -ne @($value).count) {
Throw "You need to have the same number of name and value parameters"
}

$attributes = New-Object -TypeName PSObject
$i = 0
foreach ($n in $name) {
$attributes | Add-Member -name $n -MemberType NoteProperty -Value $value[$i]
$i++
}
}

#Remove existing attributes (set value to null)
foreach ($n in ($atts.attributes | Get-Member -MemberType NoteProperty).name ) {
if ($attributes.$n) {
#Skip if there is already an attribute with this name...
continue
}
$attributes | Add-Member -name $n -MemberType NoteProperty -Value $delete_value
}

$_att | Add-Member -name "attributes" -MemberType NoteProperty -Value $attributes

if ($PSCmdlet.ShouldProcess("$id $old_name", 'Configure Attribute Member')) {
$att = Invoke-ArubaCPRestMethod -method "PATCH" -body $_att -uri $uri -connection $connection
$att
}

}

End {
}

}

function Remove-ArubaCPAttributesMember {

<#
.SYNOPSIS
Remove an Attribute member
.DESCRIPTION
Remove an Attribute Member on Endpoint / Local User / Network Device
.EXAMPLE
Get-ArubaCPEndpoint -mac_address 00:01:02:03:04:05 | Remove-ArubaCPAttributesMember -name "Disabled by"
Remove Attribute name "Disabled By" with value PowerArubaCP to Endpoint 00:01:02:03:04:05
.EXAMPLE
Get-ArubaCPNetworkDevice -name NAD-PowerArubaCP | Remove-ArubaCPAttributesMember -name "Location", "syslocation"
Remove Attribute name "Location and Syslocation" to network Device NAD-PowerArubaCP
#>

[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
Param(
[Parameter (Mandatory = $true, ValueFromPipeline = $true)]
#[ValidateScript( { ( Confirm-ArubaCPEndpoint $_ -or Confirm-ArubaCPNetworkDevice $_) })]
[psobject]$atts,
[Parameter (Mandatory = $true)]
[string[]]$name,
[Parameter (Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[PSObject]$connection = $DefaultArubaCPConnection
)

Begin {
}

Process {

$id = $atts.id
$rname = "(" + $atts.name + ")"
if ($atts.mac_address -and $atts.status) {
$uri = "api/endpoint/${id}"
$delete_value = "--For-Delete--"
}
elseif ($atts.user_id -and $atts.role_name) {
$uri = "api/local-user/${id}"
$delete_value = ""
}
elseif ($atts.ip_address -and $atts.vendor_name) {
$uri = "api/network-device/${id}"
$delete_value = ""
}
else {
Throw "Not an Endpoint, a Local User or a Network Device"
}

$_att = New-Object -TypeName PSObject

$attributes = New-Object -TypeName PSObject

foreach ($n in $name) {
$attributes | Add-Member -name $n -MemberType NoteProperty -Value $delete_value
}

$_att | Add-Member -name "attributes" -MemberType NoteProperty -Value $attributes

if ($PSCmdlet.ShouldProcess("$id $rname", 'Remove Attribute Member')) {
$att = Invoke-ArubaCPRestMethod -method "PATCH" -body $_att -uri $uri -connection $connection
$att
}

}

End {
}

}
13 changes: 0 additions & 13 deletions PowerArubaCP/Public/Endpoint.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,6 @@ function Set-ArubaCPEndpoint {
Change Description for MAC Address 00:01:02:03:04:05
.EXAMPLE
$ep = Get-ArubaCPEndpoint -mac_address 00:01:02:03:04:05
PS C:\>$attributes = @{"Disabled by"= "PowerArubaCP"}
PS C:\>$ep | Set-ArubaCPEndpoint -attributes $attributes
Change attributes for MAC Address 00:01:02:03:04:05
.EXAMPLE
$ep = Get-ArubaCPEndpoint -mac_address 00:01:02:03:04:05
PS C:\>$ep | Set-ArubaCPEndpoint -mac_address 00:01:02:03:04:06
Expand All @@ -253,8 +246,6 @@ function Set-ArubaCPEndpoint {
[Parameter (Mandatory = $false)]
[ValidateSet('Known', 'Unknown', 'Disabled', IgnoreCase = $false)]
[string]$status,
[Parameter (Mandatory = $false)]
[psobject]$attributes,
[Parameter (Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[PSObject]$connection = $DefaultArubaCPConnection
Expand Down Expand Up @@ -289,10 +280,6 @@ function Set-ArubaCPEndpoint {
$_ep | add-member -name "status" -membertype NoteProperty -Value $status
}

if ( $PsBoundParameters.ContainsKey('attributes') ) {
$_ep | add-member -name "attributes" -membertype NoteProperty -Value $attributes
}

if ($PSCmdlet.ShouldProcess($id, 'Configure Endpoint')) {
$ep = Invoke-ArubaCPRestMethod -method "PATCH" -body $_ep -uri $uri -connection $connection
$ep
Expand Down
13 changes: 0 additions & 13 deletions PowerArubaCP/Public/LocalUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,6 @@ function Set-ArubaCPLocalUser {
Change Role (Guest) for user(name) MyPowerArubaCP
.EXAMPLE
$lu = Get-ArubaCPLocalUser -username MyPowerArubaCP
PS >$attributes = @{ "Sponsor" = "PowerArubaCP" }
PS >$lu | Set-ArubaCPLocalUser -attributes $attributes
Change attributes (Sponsor) for user(name) MyPowerArubaCP
#>

[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
Expand All @@ -289,8 +282,6 @@ function Set-ArubaCPLocalUser {
[Parameter (Mandatory = $false)]
[switch]$change_pwd_next_login,
[Parameter (Mandatory = $false)]
[psobject]$attributes,
[Parameter (Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[PSObject]$connection = $DefaultArubaCPConnection
)
Expand Down Expand Up @@ -343,10 +334,6 @@ function Set-ArubaCPLocalUser {
}
}

if ( $PsBoundParameters.ContainsKey('attributes') ) {
$_lu | add-member -name "attributes" -membertype NoteProperty -Value $attributes
}

if ($PSCmdlet.ShouldProcess($id, 'Configure Local User')) {
$lu = Invoke-ArubaCPRestMethod -method "PATCH" -body $_lu -uri $uri -connection $connection
$lu
Expand Down
13 changes: 0 additions & 13 deletions PowerArubaCP/Public/NetworkDevice.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,6 @@ function Set-ArubaCPNetworkDevice {
Enable COA and set COA Port to 5000 of NAD-PowerArubaCP
.EXAMPLE
$attributes = @{ "Location" = "PowerArubaCP" }
PS > $nad = Get-ArubaCPNetworkDevice -name NAD-PowerArubaCP
PS > $nad | Set-ArubaCPNetworkDevice -attributes $attributes
Set Attributes Location to PowerArubaCP of NAD-PowerArubaCP
#>

[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
Expand Down Expand Up @@ -313,8 +306,6 @@ function Set-ArubaCPNetworkDevice {
[int]$coa_port,
[Parameter (Mandatory = $false)]
[switch]$radsec_enabled,
[Parameter (Mandatory = $false)]
[hashtable]$attributes,
[Parameter (Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[PSObject]$connection = $DefaultArubaCPConnection
Expand Down Expand Up @@ -384,10 +375,6 @@ function Set-ArubaCPNetworkDevice {
}
}

if ( $PsBoundParameters.ContainsKey('attributes') ) {
$_nad | add-member -name "attributes" -membertype NoteProperty -Value $attributes
}

if ($PSCmdlet.ShouldProcess("$id $old_name", 'Configure Network device')) {
$nad = Invoke-ArubaCPRestMethod -method "PATCH" -body $_nad -uri $uri -connection $connection
$nad
Expand Down
Loading

0 comments on commit 56aa16f

Please sign in to comment.