forked from mweagle/Sparta
/
status.go
110 lines (100 loc) · 3.35 KB
/
status.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
110
// +build !lambdabinary
package sparta
import (
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/sts"
spartaAWS "github.com/mweagle/Sparta/aws"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// Status produces a status report for the given stack
func Status(serviceName string,
serviceDescription string,
redact bool,
logger *logrus.Logger) error {
awsSession := spartaAWS.NewSession(logger)
cfSvc := cloudformation.New(awsSession)
params := &cloudformation.DescribeStacksInput{
StackName: aws.String(serviceName),
}
describeStacksResponse, describeStacksResponseErr := cfSvc.DescribeStacks(params)
if describeStacksResponseErr != nil {
if strings.Contains(describeStacksResponseErr.Error(), "does not exist") {
logger.WithField("Region", *awsSession.Config.Region).Info("Stack does not exist")
return nil
}
return describeStacksResponseErr
}
if len(describeStacksResponse.Stacks) > 1 {
return errors.Errorf("More than 1 stack returned for %s. Count: %d",
serviceName,
len(describeStacksResponse.Stacks))
}
// What's the current accountID?
redactor := func(stringValue string) string {
return stringValue
}
if redact {
input := &sts.GetCallerIdentityInput{}
stsSvc := sts.New(awsSession)
identityResponse, identityResponseErr := stsSvc.GetCallerIdentity(input)
if identityResponseErr != nil {
return identityResponseErr
}
redactedValue := strings.Repeat("*", len(*identityResponse.Account))
redactor = func(stringValue string) string {
return strings.Replace(stringValue,
*identityResponse.Account,
redactedValue,
-1)
}
}
// Report on what's up with the stack...
logSectionHeader("Stack Summary", dividerLength, logger)
stackInfo := describeStacksResponse.Stacks[0]
logger.WithField("Id", redactor(*stackInfo.StackId)).Info("StackId")
logger.WithField("Description", redactor(*stackInfo.Description)).Info("Description")
logger.WithField("State", *stackInfo.StackStatus).Info("Status")
if stackInfo.StackStatusReason != nil {
logger.WithField("Reason", *stackInfo.StackStatusReason).Info("Reason")
}
logger.WithField("Time", stackInfo.CreationTime.UTC().String()).Info("Created")
if stackInfo.LastUpdatedTime != nil {
logger.WithField("Time", stackInfo.LastUpdatedTime.UTC().String()).Info("Last Update")
}
if stackInfo.DeletionTime != nil {
logger.WithField("Time", stackInfo.DeletionTime.UTC().String()).Info("Deleted")
}
logger.Info()
if len(stackInfo.Parameters) != 0 {
logSectionHeader("Parameters", dividerLength, logger)
for _, eachParam := range stackInfo.Parameters {
logger.WithField("Value",
redactor(*eachParam.ParameterValue)).Info(*eachParam.ParameterKey)
}
logger.Info()
}
if len(stackInfo.Tags) != 0 {
logSectionHeader("Tags", dividerLength, logger)
for _, eachTag := range stackInfo.Tags {
logger.WithField("Value",
redactor(*eachTag.Value)).Info(*eachTag.Key)
}
logger.Info()
}
if len(stackInfo.Outputs) != 0 {
logSectionHeader("Outputs", dividerLength, logger)
for _, eachOutput := range stackInfo.Outputs {
statement := logger.WithField("Value",
redactor(*eachOutput.OutputValue))
if eachOutput.ExportName != nil {
statement.WithField("ExportName", *eachOutput.ExportName)
}
statement.Info(*eachOutput.OutputKey)
}
logger.Info()
}
return nil
}