Public/SDK Wrappers/CloudFormation/Get-VSStack.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
function Get-VSStack {
    <#
    .SYNOPSIS
    Gets info about a stack or list of stacks
    
    .DESCRIPTION
    Gets the description, policy, template, event list, resource info, resource list, exports info, imports info, summary or even estimated cost of a particular stack. This can also be used to list your stacks.
    
    .PARAMETER Description
    Returns the description for the specified stack; if no stack name was specified, then it returns the description for all the stacks created. If the stack does not exist, an AmazonCloudFormationException is returned.
    
    .PARAMETER List
    Returns the summary information for stacks whose status matches the specified StackStatusFilter. Summary information for stacks that have been deleted is kept for 90 days after the stack is deleted. If no StackStatusFilter is specified, summary information for all stacks is returned (including existing stacks and stacks that have been deleted).
    
    .PARAMETER Policy
    Returns the stack policy for a specified stack. If a stack doesn't have a policy, a null value is returned.
    
    .PARAMETER Template
    Returns the template body for a specified stack. You can get the template for running or deleted stacks.
    
    .PARAMETER Events
    Returns all stack related events for a specified stack in reverse chronological order.
    
    .PARAMETER Resource
    Returns a description of the specified resource in the specified stack.
    
    .PARAMETER Resources
    Returns AWS resource descriptions for running and deleted stacks. If StackName is specified, all the associated resources that are part of the stack are returned. If PhysicalResourceId is specified, the associated resources of the stack that the resource belongs to are returned.
    
    .PARAMETER ResourceList
    Returns descriptions of all resources of the specified stack.
    
    .PARAMETER ExportList
    Lists all exported output values in the account and region in which you call this action. Use this action to see the exported output values that you can import into other stacks. To import values, use the Fn::ImportValue function.
    
    .PARAMETER ImportList
    Lists all stacks that are importing an exported output value. To modify or remove an exported output value, first use this action to see which stacks are using it. To see the exported output values in your account, see ListExports.
    
    .PARAMETER Summary
    Returns information about a new or existing template. The GetTemplateSummary action is useful for viewing parameter information, such as default parameter values and parameter types, before you create or update a stack or stack set. You can use the GetTemplateSummary action when you submit a template, or you can get template information for a stack set, or a running or deleted stack. For deleted stacks, GetTemplateSummary returns the template information for up to 90 days after the stack has been deleted. If the template does not exist, a ValidationError is returned.
    
    .PARAMETER EstimatedCost
    Returns the estimated monthly cost of a template. The return value is an AWS Simple Monthly Calculator URL with a query string that describes the resources required to run the template.
    
    .PARAMETER StackId
    The Stack ID or Stack Name of the stack you are trying to get info for.
    
    .PARAMETER LogicalResourceId
    The logical name of the resource as specified in the template.
    
    .PARAMETER PhysicalResourceId
    The name or unique identifier that corresponds to a physical instance ID of a resource supported by AWS CloudFormation. For example, for an Amazon Elastic Compute Cloud (EC2) instance, PhysicalResourceId corresponds to the InstanceId. You can pass the EC2 InstanceId to DescribeStackResources to find which stack the instance belongs to and what other resources are part of the stack.
    
    .PARAMETER StackSetName
    The name or unique ID of the stack set from which the stack was created.
    
    .PARAMETER ExportName
    The name of the exported output value. AWS CloudFormation returns the stack names that are importing this value.
    
    .PARAMETER ChangeSetName
    The name or Amazon Resource Name (ARN) of a change set for which AWS CloudFormation returns the associated template. If you specify a name, you must also specify the StackName.
    
    .PARAMETER TemplateStage
    For templates that include transforms, the stage of the template that AWS CloudFormation returns. To get the user-submitted template, specify Original. To get the template after AWS CloudFormation has processed all transforms, specify Processed. If the template doesn't include transforms, Original and Processed return the same template. By default, AWS CloudFormation specifies Original.
    
    .PARAMETER TemplateBody
    Structure containing the template body with a minimum length of 1 byte and a maximum length of 51,200 bytes.
    
    .PARAMETER TemplateUrl
    Location of file containing the template body. The URL must point to a template that is located in an Amazon S3 bucket.
    
    .PARAMETER Path
    Absolute or relative file path to the template file you would like to summarize.
    
    .PARAMETER Parameters
    A list of Parameter structures that specify input parameters.
    
    .PARAMETER StackStatusFilter
    Stack status to use as a filter. Specify one or more stack status codes to list only stacks with the specified status codes. For a complete list of stack status codes, see the StackStatus parameter of the Stack data type.
    
    .PARAMETER Colorize
    Used with -Events to output colorized events.
    
    .PARAMETER ProfileName
    The name of the configuration profile to deploy the stack with. Defaults to $env:AWS_PROFILE, if set.
    
    .EXAMPLE
    # This gets the list of stacks.
    Get-VSStack -List
    
    .EXAMPLE
    # This gets the list of stacks and shows the events for each in color.
    Get-VSStack -List | Get-VSStack -Events -ColorizeEvents
    
    .EXAMPLE
    # This gets the description for a stack named 'testSAMdeployment'
    Get-VSStack -StackId testSAMdeployment
    
    .FUNCTIONALITY
    Vaporshell
    #>

    [cmdletbinding(DefaultParameterSetName = "Description")]
    Param
    (
        [parameter(ParameterSetName = "Description")]
        [Switch]
        $Description,
        [parameter(ParameterSetName = "ListStacks")]
        [Switch]
        $List,
        [parameter(ParameterSetName = "Policy")]
        [Switch]
        $Policy,
        [parameter(ParameterSetName = "Template")]
        [Switch]
        $Template,
        [parameter(ParameterSetName = "Events")]
        [Switch]
        $Events,
        [parameter(ParameterSetName = "Resource")]
        [Switch]
        $Resource,
        [parameter(ParameterSetName = "ResourcesPhysicalId")]
        [parameter(ParameterSetName = "ResourcesStackId")]
        [Switch]
        $Resources,
        [parameter(ParameterSetName = "ResourceList")]
        [Switch]
        $ResourceList,
        [parameter(ParameterSetName = "ExportList")]
        [Switch]
        $ExportList,
        [parameter(ParameterSetName = "ImportList")]
        [Switch]
        $ImportList,
        [parameter(ParameterSetName = "SummaryPath")]
        [parameter(ParameterSetName = "SummaryBody")]
        [parameter(ParameterSetName = "SummaryUrl")]
        [Switch]
        $Summary,
        [parameter(ParameterSetName = "EstimatedCostBody")]
        [parameter(ParameterSetName = "EstimatedCostUrl")]
        [parameter(ParameterSetName = "EstimatedCostPath")]
        [Switch]
        $EstimatedCost,
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "Events")]
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "Description")]
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "Policy")]
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "Template")]
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "SummaryPath")]
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "SummaryBody")]
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "SummaryUrl")]
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "Resource")]
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "ResourcesStackId")]
        [parameter(ValueFromPipelineByPropertyName = $true,ParameterSetName = "ResourceList")]
        [Alias("StackName")]
        [String]
        $StackId,
        [parameter(ParameterSetName = "Resource")]
        [parameter(ParameterSetName = "ResourcesPhysicalId")]
        [parameter(ParameterSetName = "ResourcesStackId")]
        [String]
        $LogicalResourceId,
        [parameter(ParameterSetName = "ResourcesPhysicalId")]
        [String]
        $PhysicalResourceId,
        [parameter(ParameterSetName = "SummaryPath")]
        [parameter(ParameterSetName = "SummaryBody")]
        [parameter(ParameterSetName = "SummaryUrl")]
        [String]
        $StackSetName,
        [parameter(ParameterSetName = "ImportList")]
        [String]
        $ExportName,
        [parameter(ParameterSetName = "Template")]
        [String]
        $ChangeSetName,
        [parameter(ParameterSetName = "Template")]
        [ValidateSet("Original","Processed")]
        [String]
        $TemplateStage,
        [parameter(ValueFromPipeline = $true,ParameterSetName = "EstimatedCostBody")]
        [parameter(ParameterSetName = "SummaryBody")]
        [String]
        $TemplateBody,
        [parameter(ParameterSetName = "EstimatedCostUrl")]
        [parameter(ParameterSetName = "SummaryUrl")]
        [String]
        $TemplateUrl,
        [parameter(ParameterSetName = "EstimatedCostPath")]
        [parameter(ParameterSetName = "SummaryPath")]
        [ValidateScript( {Test-Path $_})]
        [String]
        $Path,
        [parameter(ParameterSetName = "EstimatedCostBody")]
        [parameter(ParameterSetName = "EstimatedCostUrl")]
        [parameter(ParameterSetName = "EstimatedCostPath")]
        [Amazon.CloudFormation.Model.Parameter[]]
        $Parameters,
        [parameter(ParameterSetName = "ListStacks")]
        [ValidateSet("CREATE_IN_PROGRESS","CREATE_FAILED","CREATE_COMPLETE","ROLLBACK_IN_PROGRESS","ROLLBACK_FAILED","ROLLBACK_COMPLETE","DELETE_IN_PROGRESS","DELETE_FAILED","DELETE_COMPLETE","UPDATE_IN_PROGRESS","UPDATE_COMPLETE_CLEANUP_IN_PROGRESS","UPDATE_COMPLETE","UPDATE_ROLLBACK_IN_PROGRESS","UPDATE_ROLLBACK_FAILED","UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS","UPDATE_ROLLBACK_COMPLETE","REVIEW_IN_PROGRESS")]
        [String]
        $StackStatusFilter,
        [parameter(ParameterSetName = "Events")]
        [Switch]
        $Colorize,
        [parameter(Mandatory = $false)]
        [String]
        $ProfileName = $env:AWS_PROFILE
    )
    Process {
        $method = switch ($PSCmdlet.ParameterSetName) {
            Description {
                'DescribeStacks'
                $expand = 'Stacks'
            }
            ListStacks {
                'ListStacks'
                $expand = 'StackSummaries'
            }
            Policy {
                'GetStackPolicy'
                $expand = 'StackPolicyBody'
            }
            Events {
                'DescribeStackEvents'
                $expand = 'StackEvents'
            }
            ResourceList {
                'ListStackResources'
            }
            ExportList {
                'ListExports'
            }
            ImportList {
                'ListImports'
            }
            Resource {
                'DescribeStackResource'
            }
            ResourcesPhysicalId {
                'DescribeStackResources'
            }
            ResourcesStackId {
                'DescribeStackResources'
            }
            EstimatedCostBody {
                'EstimateTemplateCost'
                $expand = 'Url'
            }
            EstimatedCostUrl {
                'EstimateTemplateCost'
                $expand = 'Url'
            }
            EstimatedCostPath {
                'EstimateTemplateCost'
                $expand = 'Url'
            }
            SummaryPath {
                'GetTemplateSummary'
            }
            SummaryBody {
                'GetTemplateSummary'
            }
            SummaryUrl {
                'GetTemplateSummary'
            }
            Template {
                'GetTemplate'
            }
        }
        $requestType = "Amazon.CloudFormation.Model.$($method)Request"
        $request = New-Object $requestType
        foreach ($key in $PSBoundParameters.Keys) {
            switch ($key) {
                StackId {
                    $request.StackName = $StackId
                }
                TemplateStage {
                    $request.TemplateStage = [Amazon.CloudFormation.TemplateStage]::$TemplateStage
                }
                Path {
                    $request.TemplateBody = [System.IO.File]::ReadAllText((Resolve-Path $Path))
                }
                Default {
                    if ($request.PSObject.Properties.Name -contains $key) {
                        $request.$key = $PSBoundParameters[$key]
                    }
                }
            }
        }
        $results = ProcessRequest $PSCmdlet.ParameterSetName $ProfileName $method $request $expand
        if (!$results) {
            return
        }
        elseif ($results -is 'System.Management.Automation.ErrorRecord') {
            $PSCmdlet.ThrowTerminatingError($results)
        }
        elseif ($results) {
            if ($Colorize) {
                $strings = @()
                $strings += "`nSTACK ID : $($results.StackId[0])`n"
                $stack = $results | Sort-Object timestamp | Select-Object Timestamp,ResourceStatus,StackName,ResourceType,ResourceStatusReason | Format-Table -AutoSize
                $strings += ($stack | Out-String) -split "`n" | Where-Object {$_}
                Colorize $strings
            }
            else {
                return $results
            }
        }
    }
}