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

function Invoke-ArubaCPRestMethod {

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

      .DESCRIPTION
       Invoke RestMethod with ArubaCP connection variable (token, csrf..)

      .EXAMPLE
      Invoke-ArubaCPRestMethod -method "get" -uri "api/cppm-version"

      Invoke-RestMethod with ArubaCP connection for get api/cppm-version

      .EXAMPLE
      Invoke-ArubaCPRestMethod "api/cppm-version"

      Invoke-RestMethod with ArubaCP connection for get api/cppm-version uri with default GET method parameter

      .EXAMPLE
      Invoke-ArubaCPRestMethod -method "post" -uri "api/cppm-version" -body $body

      Invoke-RestMethod with ArubaCP connection for post api/cppm-version uri with $body payload

      .EXAMPLE
      Invoke-ArubaCPRestMethod -method "post" -uri "api/cppm-version" -body $body

      Invoke-RestMethod with ArubaCP connection for post api/cppm-version uri with $body payload

      .EXAMPLE
      Invoke-ArubaCPRestMethod -method "get" -uri "api/network-device" -limit 1000

      Invoke-RestMethod with ArubaCP connection for get api/network-device uri with limit to 1000

     .EXAMPLE
      Invoke-ArubaCPRestMethod -method "get" -uri "api/network-device" -filter @{ "name" = "PowerArubaCP" }

      Invoke-RestMethod with ArubaCP connection for get api/network-device uri with filter name equal PowerArubaCP

      .EXAMPLE
      Invoke-ArubaCPRestMethod -method "get" -uri "api/network-device" -filter @{ "name" = @{ "`$contains" = "PowerArubaCP" } }

      Invoke-RestMethod with ArubaCP connection for get api/network-device uri with filter name contains PowerArubaCP
    #>


    Param(
        [Parameter(Mandatory = $true, position = 1)]
        [String]$uri,
        [Parameter(Mandatory = $false)]
        [ValidateSet("GET", "PUT", "POST", "DELETE", "PATCH")]
        [String]$method = "GET",
        [Parameter(Mandatory = $false)]
        [psobject]$body,
        [Parameter(Mandatory = $false)]
        [ValidateRange(1, 1000)]
        [int]$limit,
        [Parameter(Mandatory = $false)]
        [array]$filter,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultArubaCPConnection
    )

    Begin {
    }

    Process {

        if ($null -eq $connection) {
            Throw "Not Connected. Connect to the ClearPass with Connect-ArubaCP"
        }

        $port = $connection.port
        $Server = $connection.Server
        $invokeParams = $connection.invokeParams
        $fullurl = "https://${Server}:${port}/${uri}"

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

        #Add calculate_count to each get command to get the number of
        if ($method -eq "GET") {
            $fullurl += "&calculate_count=true"
        }
        if ($limit) {
            $fullurl += "&limit=$limit"
        }
        if ($filter) {
            $fullurl += "&filter=$($filter | ConvertTo-Json -Compress)"
        }

        #Display (Full)url when verbose (not longer available with PS 7.2.x...)
        Write-Verbose $fullurl

        #When headers, We need to have Accept set to application/json...
        $headers = @{ Authorization = "Bearer " + $connection.token; Accept = "application/json" }

        try {
            if ($body) {

                #Add Content-Type to application/json only when there is a body

                $headers.add("Content-type", "application/json")

                Write-Verbose ($body | ConvertTo-Json -depth 10)

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

        catch {
            Show-ArubaCPException $_
            throw "Unable to use ClearPass API"
        }
        #Only if limit is no set and $response._embedded.items(.count) is not empty
        if (-Not $limit -and $response._embedded.items.count) {
            #Check if number a item calculate by CPPM (calculate_count) is superior to return item (and generate a warning about use -limit)
            if ($response.count -gt $response._embedded.items.count) {
                Write-Warning "There is extra items use -limit parameter to display"
            }
        }
        $response

    }

}