-
Notifications
You must be signed in to change notification settings - Fork 22
/
cloud_formation.go
109 lines (89 loc) · 2.84 KB
/
cloud_formation.go
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
package cloudformation
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/giantswarm/microerror"
)
type Config struct {
Client CF
}
type CloudFormation struct {
client CF
}
func New(config Config) (*CloudFormation, error) {
if config.Client == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Client must not be empty", config)
}
c := &CloudFormation{
client: config.Client,
}
return c, nil
}
// DescribeOutputsAndStatus returns stack outputs, stack status and error. The
// stack status is returned when the error is nil or the error is matched by
// IsOutputsNotAccessible.
func (c *CloudFormation) DescribeOutputsAndStatus(stackName string) ([]Output, string, error) {
// At first we fetch the CF stack state by describing it via the AWS golang
// SDK. We are interested in the stack outputs and the stack status, since
// this tells us if we are able to access outputs at all.
var stackOutputs []Output
var stackStatus string
{
i := &cloudformation.DescribeStacksInput{
StackName: aws.String(stackName),
}
o, err := c.client.DescribeStacks(i)
if IsStackNotFound(err) {
return nil, "", microerror.Maskf(stackNotFoundError, "stack name '%s'", stackName)
} else if err != nil {
return nil, "", microerror.Mask(err)
}
if len(o.Stacks) != 1 {
return nil, "", microerror.Maskf(tooManyStacksError, "expected 1 stack, got %d", len(o.Stacks))
}
stackOutputs = toOutputs(o.Stacks[0].Outputs)
stackStatus = *o.Stacks[0].StackStatus
}
// We call DescribeOutputsAndStatus in certain GetCurrentState
// implementations. If the stack creation failed, the outputs can be
// unaccessible. This can lead to a stack that cannot be deleted. it can also
// be called during creation, while the outputs are still not accessible.
var outputsAccessible bool
{
okStatuses := []string{
cloudformation.StackStatusCreateComplete,
cloudformation.StackStatusRollbackComplete,
cloudformation.StackStatusUpdateComplete,
cloudformation.StackStatusUpdateRollbackComplete,
}
for _, s := range okStatuses {
if stackStatus == s {
outputsAccessible = true
break
}
}
}
if !outputsAccessible {
return nil, stackStatus, microerror.Maskf(outputsNotAccessibleError, "stack state '%s'", stackStatus)
}
return stackOutputs, stackStatus, nil
}
func (c *CloudFormation) GetOutputValue(outputs []Output, key string) (string, error) {
for _, o := range outputs {
if o.OutputKey == key {
return o.OutputValue, nil
}
}
return "", microerror.Maskf(outputNotFoundError, "stack output value for key '%s'", key)
}
func toOutputs(outputs []*cloudformation.Output) []Output {
var newOutputs []Output
for _, o := range outputs {
n := Output{
OutputKey: *o.OutputKey,
OutputValue: *o.OutputValue,
}
newOutputs = append(newOutputs, n)
}
return newOutputs
}