Functions/Invoke-BambooRestMethod.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
<#
.SYNOPSIS
    Generic helper cmdlet to invoke Rest methods agains a target Bamboo server.
.DESCRIPTION
    This cmdlet extends the original Invoke-RestMethod cmdlet with Bamboo REST
    API specific parameters, so it does user authorization and provides easier
    resource access.

.PARAMETER Resource
    Mandatory - Bamboo REST API Resource that needs to be accessed
.PARAMETER Method
    Optional - REST method to be used for the call. (Default is GET)
.PARAMETER ApiVersion
    Optional - REST API version that needs to be targeted for the call,
    for future purposes. Default is the latest.

.PARAMETER AuthenticationMode
    Optional - Authentication Mode to access Bamboo Server
.PARAMETER AuthenticationToken
    Optional - Authentication Token to access Bamboo Server

.PARAMETER UriParams
    Optional - Parameters that needs to be appended to the GET url.
.PARAMETER Headers
    Optional - HTTP Headers that needs to be added for the REST call.
.PARAMETER Body
    Optional - HTTP Body payload

.EXAMPLE
    Invoke-BambooRestMethod -Resource "plan"
.EXAMPLE
    Invoke-BambooRestMethod -Resource "plan/PRJ-PLAN/enable" -Method Delete

.LINK
    https://developer.atlassian.com/bamboodev/rest-apis
#>

function Invoke-BambooRestMethod {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Resource,

        [ValidateSet('Get','Put','Post','Delete')]
        [string]$Method = 'Get',

        [ValidateSet('latest')]
        [string]$ApiVersion='latest',
        [string]$Server = $script:BambooServer,
        [string]$Uri="$Server/rest/api/$ApiVersion/$Resource",

        [string]$AuthenticationMode=$script:AuthenticationMode,
        [string]$AuthenticationToken = $script:AuthenticationToken,

        [string]$Expand,
        [psobject]$UriParams=@{},
        [psobject]$Headers=@{},
        [psobject]$Body
    )

    if (-Not $UriParams) { $UriParams = @{} }

    if ($Expand) { $UriParams.expand=$Expand }

    if ($Method -match 'Post|Get|Put') {
        $Headers."Content-Type"="application/xml"
    }

    switch ($AuthenticationMode) {
        "BASIC" {
            $UriParams.os_authType='basic'
            $Headers.Authorization = "$AuthenticationMode $AuthenticationToken"
        }
        default {
            Write-Verbose "Accessing Bamboo without credentials."
        }
    }

    if ($UriParams -and $UriParams.Keys) {
        $Params = ''
        foreach($key in $UriParams.Keys) {
            $Params+="$key=$($UriParams.$key)&"
        }
        if ($Params) {
            $Uri = "$($Uri)?$Params"
        }
    }
    $response = $null

    try {
        Write-Verbose "$Method : $Uri"
        $response = Invoke-RestMethod -Uri $Uri -Method:$Method -Headers:$Headers
    } catch {
        if ($_.ErrorDetails) {
            Write-Error $_.ErrorDetails.Message
        } else {
            Write-Error $_
        }
    }

    Write-Verbose "Response: $response"
    if ($response -is [xml]) {
        Write-Debug ($response.OuterXml -replace '><',">`n<")
    }
    $response
}