Public/STP.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#
# Copyright 2018, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
# Copyright 2018, Cédric Moreau <moreaucedric0 at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Get-ArubaSWSTP {

    <#
        .SYNOPSIS
        Get spanning-tree information on ArubaOS Switch.
 
        .DESCRIPTION
        Get spanning-tree configuration.
 
        .EXAMPLE
        Get-ArubaSWSTP
 
        This function give you the spanning-tree configuration
    #>


    Param(
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )
    Begin {
    }

    Process {

        $uri = "rest/v4/stp"

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

        $stp = $response | ConvertFrom-Json

        $stp
    }

    End {
    }
}

function Set-ArubaSWSTP {

    <#
        .SYNOPSIS
        Set spanning-tree configuration on ArubaOS Switch.
 
        .DESCRIPTION
        Set spanning-tree configuration.
 
        .EXAMPLE
        Set-ArubaSWSTPGlobal -enable -priority 7 -mode mstp
 
        Set the spanning-tree protocol on, the priority to 7 and the mode to MSTP
 
        .EXAMPLE
        Set-ArubaSWSTPGlobal $false 4 rpvst
 
        Set the spanning-tree protocol off, the priority to 4 and the mode to RPVST
    #>


    Param(
        [Parameter (Mandatory = $true, Position = 1)]
        [switch]$enable,
        [Parameter (Mandatory = $false, Position = 2)]
        [ValidateRange (0, 15)]
        [int]$priority,
        [Parameter (Mandatory = $false, Position = 3)]
        [ValidateSet ("MSTP", "RPVST")]
        [string]$mode,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        $uri = "rest/v4/stp"

        $_stp = New-Object -TypeName PSObject

        if ( $PsBoundParameters.ContainsKey('enable') ) {
            if ( $enable ) {
                $_stp | Add-Member -name "is_enabled" -membertype NoteProperty -Value $True
            }
            else {
                $_stp | Add-Member -name "is_enabled" -membertype NoteProperty -Value $false
            }
        }

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

        if ( $PsBoundParameters.ContainsKey('mode') ) {
            switch ( $mode ) {
                mstp {
                    $_mode = "STM_MSTP"
                }
                rpvst {
                    $_mode = "STM_RPVST"
                }
            }

            $_stp | Add-Member -name "mode" -membertype NoteProperty -Value $_mode
        }

        $response = Invoke-ArubaSWWebRequest -method "PUT" -body $_stp -uri $uri -connection $connection

        $run = $response | ConvertFrom-Json

        $run

    }

    End {
    }
}

function Get-ArubaSWSTPPort {

    <#
        .SYNOPSIS
        Get spanning-tree information per port on ArubaOS Switch.
 
        .DESCRIPTION
        Get spanning-tree configuration per port.
 
        .EXAMPLE
        Get-ArubaSWSTPPort
 
        Get the spanning-tree configuration for ALL ports.
 
        .EXAMPLE
        Get-ArubaSWSTPPort -port 5
 
        Get the spanning-tree configuration for the port 5.
    #>



    Param(
        [Parameter (Mandatory = $false)]
        [string]$port,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {


        $uri = "rest/v4/stp/ports"

        if ($port) {
            $uri += "/$port"
        }

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

        $stp = $response | ConvertFrom-Json
        if ($port) {
            $stp
        }
        else {
            $stp.stp_port_element
        }
    }

    End {
    }
}

function Set-ArubaSWSTPPort {

    <#
        .SYNOPSIS
        Set spanning-tree configuration per port on ArubaOS Switch.
 
        .DESCRIPTION
        Set spanning-tree configuration per port.
 
        .EXAMPLE
        $stp_port = Get-ArubaSWSTPPort -port 5
        PS C:\>$stp_port | Set-ArubaSWSTPPort -priority 4 -admin_edge:$false -bpdu_protection -bpdu_filter -root_guard
 
        Configure the port 5 and set the priority 4, disable admin edge, and enable bpdu protection, bpdu filter and root guard.
 
        .EXAMPLE
        Set-ArubaSWSTPPort -port 4 -priority 6 -admin_edge -bpdu_protection:$false -bpdu_filter:$false -root_guard:$false
 
        Configure the port 4 and set the priority 6, enable admin edge, and disable bpdu protection, bpdu filter and root guard.
    #>


    Param(
        [Parameter (Mandatory = $true, ParameterSetName = "port_id")]
        [string]$port,
        [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "port_stp")]
        #ValidateScript({ ValidateSTPPort $_ })]
        [psobject]$port_stp,
        [Parameter (Mandatory = $false)]
        [ValidateRange (0, 15)]
        [int]$priority,
        [Parameter (Mandatory = $false)]
        [switch]$admin_edge,
        [Parameter (Mandatory = $false)]
        [switch]$bpdu_protection,
        [Parameter (Mandatory = $false)]
        [switch]$bpdu_filter,
        [Parameter (Mandatory = $false)]
        [switch]$root_guard,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        #get port id from port STP ps object
        if ($port_stp) {
            $port = $port_stp.port_id
        }

        $_stp = New-Object -TypeName PSObject

        $_stp | Add-Member -name "port_id" -membertype NoteProperty -Value $port

        $uri = "rest/v4/stp/ports/${port}"

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

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

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

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

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

        $response = Invoke-ArubaSWWebRequest -method "PUT" -body $_stp -uri $uri -connection $connection

        $run = $response | ConvertFrom-Json

        $run

    }

    End {
    }
}