Resources/Expirations.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
function Get-ITGlueExpirations {
    [CmdletBinding(DefaultParameterSetName = 'index')]
    Param (
        [Parameter(ParameterSetName = 'index')]
        [Parameter(ParameterSetName = 'show')]
        [Nullable[Int64]]$organization_id = $null,

        [Parameter(ParameterSetName = 'index')]
        [Nullable[Int64]]$filter_id = $null,

        [Parameter(ParameterSetName = 'index')]
        [Nullable[Int64]]$filter_resource_id = $null,

        [Parameter(ParameterSetName = 'index')]
        [String]$filter_resource_name = '',

        [Parameter(ParameterSetName = 'index')]
        [String]$filter_resource_type_name = '',

        [Parameter(ParameterSetName = 'index')]
        [String]$filter_description = '',

        [Parameter(ParameterSetName = 'index')]
        [String]$filter_expiration_date = '',

        [Parameter(ParameterSetName = 'index')]
        [Int64]$filter_organization_id = '',

        [Parameter(ParameterSetName = 'index')]
        [String]$filter_range = '',

        [Parameter(ParameterSetName = 'index')]
        [ValidateSet( 'id', 'organization_id', 'expiration_date', 'created_at', 'updated_at', `
                '-id', '-organization_id', '-expiration_date', '-created_at', '-updated_at')]
        [String]$sort,

        [Parameter(ParameterSetName = 'index')]
        [Nullable[Int64]]$page_number = $null,

        [Parameter(ParameterSetName = 'index')]
        [Nullable[int]]$page_size = $null,

        [Parameter(Mandatory = $true, ParameterSetName = 'show')]
        [Nullable[Int64]]$id
    )

    $resource_uri = ('/expirations/{0}' -f $id)
    if ($organization_id) {
        $resource_uri = ('/organizations/{0}/relationships' -f $organization_id) + $resource_uri
    }

    $body = @{}

    if ($PSCmdlet.ParameterSetName -eq 'index') {
        if ($filter_id) {
            $body += @{'filter[id]' = $filter_id}
        }
        if ($filter_resource_id) {
            $body += @{'filter[resource_id]' = $filter_resource_id}
        }
        if ($filter_resource_name) {
            $body += @{'filter[resource_name]' = $filter_resource_name}
        }
        if ($filter_resource_type_name) {
            $body += @{'filter[resource_type_name]' = $filter_resource_type_name}
        }
        if ($filter_description) {
            $body += @{'filter[description]' = $filter_description}
        }
        if ($filter_expiration_date) {
            $body += @{'filter[expiration_date]' = $filter_expiration_date}
        }
        if ($filter_organization_id) {
            $body += @{'filter[organization_id]' = $filter_organization_id}
        }
        if ($filter_range) {
            $body += @{'filter[range]' = $filter_range}
        }
        if ($sort) {
            $body += @{'sort' = $sort}
        }
        if ($page_number) {
            $body += @{'page[number]' = $page_number}
        }
        if ($page_size) {
            $body += @{'page[size]' = $page_size}
        }
    }

    try {
        $ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
        $rest_output = Invoke-RestMethod -method 'GET' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
            -body $body -ErrorAction Stop -ErrorVariable $web_error
    } catch {
        Write-Error $_
    } finally {
        [void] $ITGlue_Headers.Remove('x-api-key') # Quietly clean up scope so the API key doesn't persist
    }

    $data = @{}
    $data = $rest_output
    return $data
}