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

function Invoke-FMGRestMethod {

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

      .DESCRIPTION
      Invoke RestMethod with FMG connection variable (session)

      .EXAMPLE
      Invoke-FMGRestMethod -method "get" -uri "pm/config/adom/root/obj/firewall/address"

      Invoke-RestMethod with FMG connection for get pm/config/adom/root/obj/firewall/address uri

      .EXAMPLE
      Invoke-FMGRestMethod "pm/config/global/obj/firewall/address"

      Invoke-RestMethod with FMG connection for get pm/config/adom/root/obj/firewall/address uri with default parameter

      .EXAMPLE
      Invoke-FMGRestMethod -method "get" -uri "pm/config/adom/root/obj/firewall/address" -vdom vdomX

      Invoke-RestMethod with FMG connection for get pm/config/adom/root/obj/firewall/address uri on vdomX

      .EXAMPLE
      Invoke-FMGRestMethod -method "post" -uri "pm/config/adom/root/obj/firewall/address" -body $body

      Invoke-RestMethod with FMG connection for post pm/config/adom/root/obj/firewall/address uri with $body payload

      .EXAMPLE
      Invoke-FMGRestMethod -method "get" -uri "pm/config/adom/root/obj/firewall/address" -connection $fw2

      Invoke-RestMethod with $fw2 connection for get pm/config/adom/root/obj/firewall/address uri

      .EXAMPLE
      Invoke-FMGRestMethod -method "get" -uri "pm/config/adom/root/obj/firewall/address" -filter @('name', '==', 'FMG')

      Invoke-RestMethod with FMG connection for get pm/config/adom/root/obj/firewall/address uri with only name equal FMG

      .EXAMPLE
      Invoke-FMGRestMethod -method "get" -uri "pm/config/adom/root/obj/firewall/address" -filter_attribute name -filter_value FMG

      Invoke-RestMethod with FMG connection for get pm/config/adom/root/obj/firewall/address uri with filter attribute equal name and filter value equal FMG

      .EXAMPLE
      Invoke-FMGRestMethod -method "get" -uri "pm/config/adom/root/obj/firewall/address" -filter_attribute name -filter_type contains -filter_value FMG

      Invoke-RestMethod with FMG connection for get pm/config/adom/root/obj/firewall/address uri with filter attribute equal name and filter value contains FMG

      .EXAMPLE
      Invoke-FMGRestMethod -method "get" -uri "firewall/address" -type pm

      Invoke-RestMethod with FMG connection for get firewall/address uri with set the adom from $DefaultFMGConnection (add pm/config/adom/XXX/obj to uri)
    #>


    [CmdletBinding(DefaultParameterSetName = "default")]
    Param(
        [Parameter(Mandatory = $true, position = 1)]
        [String]$uri,
        [Parameter(Mandatory = $false)]
        [ValidateSet("get", "set", "add", "update", "delete", "clone", "exec", IgnoreCase = $false)]
        [String]$method = "get",
        [Parameter(Mandatory = $false)]
        [ValidateSet("pm", "cli", "sys", "dvm", "dvmdb")]
        [String]$type,
        [Parameter(Mandatory = $false)]
        [psobject]$body,
        [Parameter (ParameterSetName = "filter")]
        [array]$filter,
        [Parameter(Mandatory = $false)]
        [Parameter (ParameterSetName = "filter_build")]
        [string]$filter_attribute,
        [Parameter(Mandatory = $false)]
        [ValidateSet('equal', 'contains')]
        [Parameter (ParameterSetName = "filter_build")]
        [string]$filter_type,
        [Parameter (Mandatory = $false)]
        [Parameter (ParameterSetName = "filter_build")]
        [psobject]$filter_value,
        [Parameter(Mandatory = $false)]
        [psobject]$connection
    )

    Begin {
    }

    Process {

        if ($null -eq $connection ) {
            if ($null -eq $DefaultFMGConnection) {
                Throw "Not Connected. Connect to the FortiManager with Connect-FMG"
            }
            $connection = $DefaultFMGConnection
        }

        $Server = $connection.Server
        $port = $connection.port
        $headers = $connection.headers
        $invokeParams = $connection.invokeParams
        $sessionvariable = $connection.websession

        $fullurl = "https://${Server}:${port}/jsonrpc"

        switch ($type) {
            'pm' {
                $url = "pm/config"
                #if you set the global ADOM...
                if ($connection.adom -eq "global") {
                    $url += "/global/obj/" + $uri
                }
                else {
                    $url += "/adom/" + $connection.adom + "/obj/" + $uri
                }
            }
            Default {
                $url = $uri
            }
        }

        #filter
        $afilter = @()
        switch ( $filter_type ) {
            "equal" {
                $afilter += ("==")
                $afilter += ($filter_value)
            }
            "contains" {
                $afilter += ("contain")
                $afilter += ($filter_value)
            }
            "like" {
                $afilter += ("like")
                $afilter += ("%" + $filter_value + "%")
            }
            #by default set to equal..
            default {
                $afilter += ("==")
                $afilter += ($filter_value)
            }
        }

        if ($filter_attribute) {
            $filter = @($filter_attribute) + $afilter
        }

        #Make params data (with uri, data, filter...)
        $params = @{
            url = $url
        }
        if ($body) {
            $params.data = $body
        }
        if ($filter) {
            $params.filter = $filter
        }
        #Make Invoke-RestMethod body query
        $irm_body = @{
            id      = $connection.id++
            method  = $method
            session = $connection.session
            verbose = 1
            params  = @($params)
        }

        try {
            Write-Verbose -message ($irm_body | ConvertTo-Json -Depth 10)

            $response = Invoke-RestMethod $fullurl -Method "POST" -body ($irm_body | ConvertTo-Json -Depth 10 -Compress) -Headers $headers -WebSession $sessionvariable @invokeParams
        }

        catch {
            Show-FMGException $_
            throw "Unable to use FortiManager API"
        }

        #Check status code
        if ($response.result.status.code -ne "0") {
            throw "Unable to use FortiManager API (" + $response.result.status.code + ") " + $response.result.status.message
        }
        $response.result.data

    }

}