Functions/Private/Invoke-RancherApi.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
#requires -Version 4
#requires -Modules WebRequest

function Invoke-RancherApi {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Path
        ,
        [Parameter()]
        [ValidateSet('Delete', 'Get', 'Post', 'Put')]
        [string]
        $Method = 'Get'
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [hashtable]
        $Headers = @{}
    )

    if (-not $Headers.ContainsKey('Accept')) {
        $Headers.Add('Accept', 'application/json')
    }

    $Rancher = Get-RancherServer

    $Next = "$($Rancher.Server)/v2-beta/$Path"
    $Data = @()

    $ConfiguredProtocols = [System.Net.ServicePointManager]::SecurityProtocol
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls11,Tls12'
    while ($Next) {
        $Response = Invoke-AuthenticatedWebRequest -Uri $Next -Method $Method -User $Rancher.AccessKey -Token $Rancher.SecretKey -Headers $Headers

        $Content = $Response.Content.Replace('"HTTP_PROXY="', '"http_proxy_uppercase="').Replace('"HTTPS_PROXY="', '"https_proxy_uppercase="').Replace('"NO_PROXY="', '"no_proxy_uppercase="')
        $Content = $Response.Content.Replace('"HTTP_PROXY"', '"http_proxy_uppercase"').Replace('"HTTPS_PROXY"', '"https_proxy_uppercase"').Replace('"NO_PROXY"', '"no_proxy_uppercase"')
        $Content = $Content | ConvertFrom-Json

        $Data += $Content.data

        $Next = $Content.pagination.next
    }
    [System.Net.ServicePointManager]::SecurityProtocol = $ConfiguredProtocols

    $Data
}