github.com/mweagle/Sparta@v1.15.0/status.go (about)

     1  // +build !lambdabinary
     2  
     3  package sparta
     4  
     5  import (
     6  	"strings"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/cloudformation"
    10  	"github.com/aws/aws-sdk-go/service/sts"
    11  	spartaAWS "github.com/mweagle/Sparta/aws"
    12  	"github.com/pkg/errors"
    13  	"github.com/sirupsen/logrus"
    14  )
    15  
    16  // Status produces a status report for the given stack
    17  func Status(serviceName string,
    18  	serviceDescription string,
    19  	redact bool,
    20  	logger *logrus.Logger) error {
    21  
    22  	awsSession := spartaAWS.NewSession(logger)
    23  	cfSvc := cloudformation.New(awsSession)
    24  
    25  	params := &cloudformation.DescribeStacksInput{
    26  		StackName: aws.String(serviceName),
    27  	}
    28  	describeStacksResponse, describeStacksResponseErr := cfSvc.DescribeStacks(params)
    29  
    30  	if describeStacksResponseErr != nil {
    31  		if strings.Contains(describeStacksResponseErr.Error(), "does not exist") {
    32  			logger.WithField("Region", *awsSession.Config.Region).Info("Stack does not exist")
    33  			return nil
    34  		}
    35  		return describeStacksResponseErr
    36  	}
    37  	if len(describeStacksResponse.Stacks) > 1 {
    38  		return errors.Errorf("More than 1 stack returned for %s. Count: %d",
    39  			serviceName,
    40  			len(describeStacksResponse.Stacks))
    41  	}
    42  
    43  	// What's the current accountID?
    44  	redactor := func(stringValue string) string {
    45  		return stringValue
    46  	}
    47  	if redact {
    48  		input := &sts.GetCallerIdentityInput{}
    49  		stsSvc := sts.New(awsSession)
    50  		identityResponse, identityResponseErr := stsSvc.GetCallerIdentity(input)
    51  		if identityResponseErr != nil {
    52  			return identityResponseErr
    53  		}
    54  		redactedValue := strings.Repeat("*", len(*identityResponse.Account))
    55  		redactor = func(stringValue string) string {
    56  			return strings.Replace(stringValue,
    57  				*identityResponse.Account,
    58  				redactedValue,
    59  				-1)
    60  		}
    61  	}
    62  
    63  	// Report on what's up with the stack...
    64  	logSectionHeader("Stack Summary", dividerLength, logger)
    65  	stackInfo := describeStacksResponse.Stacks[0]
    66  	logger.WithField("Id", redactor(*stackInfo.StackId)).Info("StackId")
    67  	logger.WithField("Description", redactor(*stackInfo.Description)).Info("Description")
    68  	logger.WithField("State", *stackInfo.StackStatus).Info("Status")
    69  	if stackInfo.StackStatusReason != nil {
    70  		logger.WithField("Reason", *stackInfo.StackStatusReason).Info("Reason")
    71  	}
    72  	logger.WithField("Time", stackInfo.CreationTime.UTC().String()).Info("Created")
    73  	if stackInfo.LastUpdatedTime != nil {
    74  		logger.WithField("Time", stackInfo.LastUpdatedTime.UTC().String()).Info("Last Update")
    75  	}
    76  	if stackInfo.DeletionTime != nil {
    77  		logger.WithField("Time", stackInfo.DeletionTime.UTC().String()).Info("Deleted")
    78  	}
    79  
    80  	logger.Info()
    81  	if len(stackInfo.Parameters) != 0 {
    82  		logSectionHeader("Parameters", dividerLength, logger)
    83  		for _, eachParam := range stackInfo.Parameters {
    84  			logger.WithField("Value",
    85  				redactor(*eachParam.ParameterValue)).Info(*eachParam.ParameterKey)
    86  		}
    87  		logger.Info()
    88  	}
    89  	if len(stackInfo.Tags) != 0 {
    90  		logSectionHeader("Tags", dividerLength, logger)
    91  		for _, eachTag := range stackInfo.Tags {
    92  			logger.WithField("Value",
    93  				redactor(*eachTag.Value)).Info(*eachTag.Key)
    94  		}
    95  		logger.Info()
    96  	}
    97  	if len(stackInfo.Outputs) != 0 {
    98  		logSectionHeader("Outputs", dividerLength, logger)
    99  		for _, eachOutput := range stackInfo.Outputs {
   100  			statement := logger.WithField("Value",
   101  				redactor(*eachOutput.OutputValue))
   102  			if eachOutput.ExportName != nil {
   103  				statement.WithField("ExportName", *eachOutput.ExportName)
   104  			}
   105  			statement.Info(*eachOutput.OutputKey)
   106  		}
   107  		logger.Info()
   108  	}
   109  	return nil
   110  }