Private/RestMethod.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
#
# Copyright 2018, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Invoke-ArubaCXRestMethod {

    <#
      .SYNOPSIS
      Invoke RestMethod with ArubaCX connection (internal) variable

      .DESCRIPTION
       Invoke RestMethod with ArubaCX connection variable (token, csrf..)
       rest/vX is automatically add to uri, use -noapiversion for remove

      .EXAMPLE
      Invoke-ArubaCXRestMethod -method "get" -uri "system"

      Invoke-RestMethod with ArubaCX connection for get rest/vX/system

      .EXAMPLE
      Invoke-ArubaCXRestMethod "system"

      Invoke-RestMethod with ArubaCX connection for get rest/vX/system uri with default GET method parameter

      .EXAMPLE
      Invoke-ArubaCXRestMethod -method "post" -uri "system" -body $body

      Invoke-RestMethod with ArubaCX connection for post rest/vX/system uri with $body payload

      .EXAMPLE
      Invoke-ArubaCXRestMethod -method "get" -uri "system" -depth 1 -selector configuration

      Invoke-RestMethod with ArubaCX connection for get rest/vX/system with depth 1 and select only configuration

      .EXAMPLE
      Invoke-ArubaCXRestMethod -method "get" -uri "system" -attributes hostname, dns_servers

      Invoke-RestMethod with ArubaCX connection for get rest/vX/system with display only attributes hostname and dns_servers

      .EXAMPLE
      Invoke-ArubaCXRestMethod -method "get" -uri "rest/v10.04/system" -noapiversion

      Invoke-RestMethod with ArubaCX connection for get rest/v10.04/system (need to specify full uri with rest/vX)
    #>


    [CmdletBinding(DefaultParametersetname = "default")]
    Param(
        [Parameter(Mandatory = $true, position = 1)]
        [String]$uri,
        [Parameter(Mandatory = $false)]
        [ValidateSet("GET", "PUT", "POST", "DELETE")]
        [String]$method = "get",
        [Parameter(Mandatory = $false)]
        [psobject]$body,
        [Parameter(Mandatory = $false)]
        [ValidateRange(1, 4)]
        [Int]$depth,
        [Parameter(Mandatory = $false, ParameterSetName = "selector")]
        [ValidateSet("configuration", "status", "statistics", "writable")]
        [String]$selector,
        [Parameter(Mandatory = $false, ParameterSetName = "attributes")]
        [String[]]$attributes,
        [Parameter(Mandatory = $false)]
        [switch]$vsx_peer,
        [Parameter(Mandatory = $false)]
        [switch]$noapiversion,
        [Parameter(Mandatory = $false)]
        [psobject]$connection
    )

    Begin {
    }

    Process {

        if ($null -eq $connection) {
            if ($null -eq $DefaultArubaCXConnection) {
                Throw "Not Connected. Connect to the Switch with Connect-ArubaCX"
            }
            $connection = $DefaultArubaCXConnection
        }

        $Server = $connection.Server
        $port = $connection.port
        $headers = $connection.headers
        $invokeParams = $connection.invokeParams
        $sessionvariable = $connection.session
        $rest = 'rest/' + $connection.api_version + '/'

        #Remove rest/version on uri
        if ($noapiversion) {
            $rest = ""
        }

        if ( $PsBoundParameters.ContainsKey('vsx_peer') ) {
            #Add /vsx-peer/ before uri
            $fullurl = "https://${Server}:${port}/vsx-peer/${rest}${uri}"
        }
        else {
            $fullurl = "https://${Server}:${port}/${rest}${uri}"
        }

        if ($fullurl -NotMatch "\?") {
            $fullurl += "?"
        }

        if ( $PsBoundParameters.ContainsKey('depth') ) {
            $fullurl += "&depth=$depth"
        }
        if ( $PsBoundParameters.ContainsKey('selector') ) {
            $fullurl += "&selector=$selector"
        }
        if ( $PsBoundParameters.ContainsKey('attributes') ) {
            $attributes = $attributes -Join ','
            $fullurl += "&attributes=$attributes"
        }

        try {
            if ($body) {

                Write-Verbose ($body | ConvertTo-Json)

                $response = Invoke-RestMethod $fullurl -Method $method -body ($body | ConvertTo-Json) -Headers $headers -WebSession $sessionvariable @invokeParams
            }
            else {
                $response = Invoke-RestMethod $fullurl -Method $method -Headers $headers -WebSession $sessionvariable @invokeParams
            }
        }

        catch {
            Show-ArubaCXException $_
            throw "Unable to use ArubaCX API"
        }
        $response

    }

}