Functions/Public/Get-GocdEnvironment.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
function Get-GocdEnvironment {
    [CmdletBinding()]
    param(
        [Parameter()]
        [switch]
        $Raw
    )

    $Response = Invoke-GocdApi -Path '/api/admin/environments' -Accept 'application/vnd.go.cd.v2+json'
    $ContentBytes = $Response | Select-Object -ExpandProperty Content
    $Content = ConvertFrom-Bytes -Data $ContentBytes -Encoding ASCII | ConvertFrom-Json

    if ($Raw) {
        $Content
        return
    }

    $Content._embedded.environments | ForEach-Object {
        
        $Pipelines = @()
        if ($_.pipelines.Length -gt 0) {
            $Pipelines = @($_.pipelines.name)
        }
        
        $Agents = @()
        if ($_.agents.Length -gt 0) {
            $Agents = @($_.agents.uuid)
        }
        
        $Variables = @()
        if ($_.environment_variables.Length -gt 0) {
            $Variables = @($_.environment_variables.name)
        }

        [pscustomobject]@{
            Name       = $_.name
            Pipelines  = $Pipelines
            Agents     = $Agents
            Variables  = $Variables
            PSTypeName = 'GocdEnvironment'
        }
    }
}