TFState.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
<# AVAILABLE
Create a State Version
    POST /workspaces/:workspace_id/state-versions
#>


function Get-TFState {
    <#
    .SYNOPSIS
    Get workspace state.
 
    Run cmdlet to list all state versions for a given workspace. Use the CURRENT switch to fetch the current state.
    or
    Get properties of a specify a state ID.
 
    Specify the SERVER, APITOKEN and ORG within the cmdlet or use Set-Terraform to store them globally.
    APIToken can be generated at https://<TFE>/app/settings/tokens
 
    .DESCRIPTION
    List State Versions for a Workspace
        GET /state-versions
 
    Fetch the Current State Version for a Workspace
        GET /workspaces/:workspace_id/current-state-version
 
    Show a State Version
        GET /state-versions/:state_version_id
 
    .EXAMPLE
    Get-TFState -Name workspace
 
    List all state versions
 
    .EXAMPLE
    Get-TFWorkspace workspace | Get-TFState -Current
 
    Get the current state for a workspace
 
    .EXAMPLE
    Get-TFState workspace | select stateid
 
    List all state ids for a workspace
     
    .EXAMPLE
    Get-TFState -id sv-8oekDRUuXhUBmG3W
 
    Get detailed properties of a state
 
    .EXAMPLE
    Get-TFModule -Server tfe -APIToken string
 
    List using a specific Server and APIToken
 
    .LINK
    https://www.terraform.io/docs/cloud/api/state-versions.html
    #>


    [CmdletBinding()]
    Param
    (
        [Parameter(Position=0,Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName,ParameterSetName='Wks')]
        [string[]]$Name,

        [Parameter(ParameterSetName='Wks')]
        [switch]$Current,

        [Parameter(Position=1,Mandatory,ParameterSetName='State')]
        [Alias('id')]
        [string]$StateId,

        [string]$Server = $Terraform.Server,

        [string]$APIToken = $Terraform.Token,

        [string]$Org = $Terraform.Org
    )

    PROCESS {
        
        if (!$Server -or !$APIToken) {Write-Warning "Missing Server and APIToken, use Set-Terraform"; Continue}

        $Headers = @{
            Authorization = "Bearer $APIToken"
            'Content-Type' = 'application/vnd.api+json'
        }

        if ($PSCmdlet.ParameterSetName -eq 'Wks') {

            foreach ($Workspace in $Name) {

                try {
                    if ($Current) {
                        $WorkspaceId = (Get-TFWorkspace -Server $Server -APIToken $APIToken -Name $Workspace).id
                        Write-Verbose "Workspace $Workspace; WorkspaceId $WorkspaceId"
                        if (!$WorkspaceId) {Continue}

                        #Fetch the Current State Version for a Workspace
                        $Uri = "https://$Server/api/v2/workspaces/$WorkspaceId/current-state-version"
                    } else {
                        #List State Versions for a Workspace
                        #Currently supports 1 page of 20 States
                        $Uri = "https://$Server/api/v2/state-versions?filter%5Bworkspace%5D%5Bname%5D=$Workspace&filter%5Borganization%5D%5Bname%5D=$Org"
                    }    

                    $State = (Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Get).data
                    $State | Select-Object @{n='name';e={$Workspace}},@{n='stateid';e={$_.id}} -exp attributes

                } catch {
                    Write-Warning "Unable to get state for $Workspace : $($_.Exception.Message) : Line $($_.InvocationInfo.ScriptLineNumber)"
                    Continue
                }
            }
        }

        if ($PSCmdlet.ParameterSetName -eq 'State') {

            #Show a State Version
            $Uri = "https://$Server/api/v2/state-versions/$StateId"

            try {

                $State = (Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Get).data
                $State | Select-Object @{n='stateid';e={$_.id}} -exp attributes

            } catch {
                Write-Warning "Unable to get state for $StateId : $($_.Exception.Message) : Line $($_.InvocationInfo.ScriptLineNumber)"
                Continue
            }
        }
    }
}

Set-Alias gtfs Get-TFState