github.com/mweagle/Sparta@v1.15.0/decorator/outputs.go (about) 1 package decorator 2 3 import ( 4 "fmt" 5 "regexp" 6 7 "github.com/aws/aws-sdk-go/aws/session" 8 sparta "github.com/mweagle/Sparta" 9 gocf "github.com/mweagle/go-cloudformation" 10 "github.com/sirupsen/logrus" 11 ) 12 13 var reInvalidOutputChars = regexp.MustCompile("[^A-Za-z0-9]+") 14 15 func sanitizedKeyName(userValue string) string { 16 return reInvalidOutputChars.ReplaceAllString(userValue, "") 17 } 18 19 // PublishAllResourceOutputs is a utility function to include all Ref and Att 20 // outputs associated with the given (cfResourceName, cfResource) pair. 21 func PublishAllResourceOutputs(cfResourceName string, 22 cfResource gocf.ResourceProperties) sparta.ServiceDecoratorHookFunc { 23 return func(context map[string]interface{}, 24 serviceName string, 25 cfTemplate *gocf.Template, 26 S3Bucket string, 27 S3Key string, 28 buildID string, 29 awsSession *session.Session, 30 noop bool, 31 logger *logrus.Logger) error { 32 33 // Add the Ref 34 cfTemplate.Outputs[sanitizedKeyName(fmt.Sprintf("%s_Ref", cfResourceName))] = &gocf.Output{ 35 Description: fmt.Sprintf("%s (%s) Ref", 36 cfResourceName, 37 cfResource.CfnResourceType()), 38 Value: gocf.Ref(cfResourceName), 39 } 40 // Get the resource attributes 41 for _, eachAttr := range cfResource.CfnResourceAttributes() { 42 // Add the function ARN as a stack output 43 cfTemplate.Outputs[sanitizedKeyName(fmt.Sprintf("%s_Attr_%s", cfResourceName, eachAttr))] = &gocf.Output{ 44 Description: fmt.Sprintf("%s (%s) Attribute: %s", 45 cfResourceName, 46 cfResource.CfnResourceType(), 47 eachAttr), 48 Value: gocf.GetAtt(cfResourceName, eachAttr), 49 } 50 } 51 return nil 52 } 53 } 54 55 // PublishAttOutputDecorator returns a TemplateDecoratorHookFunc 56 // that publishes an Att value for a given Lambda 57 func PublishAttOutputDecorator(keyName string, description string, fieldName string) sparta.TemplateDecoratorHookFunc { 58 attrDecorator := func(serviceName string, 59 lambdaResourceName string, 60 lambdaResource gocf.LambdaFunction, 61 resourceMetadata map[string]interface{}, 62 S3Bucket string, 63 S3Key string, 64 buildID string, 65 template *gocf.Template, 66 context map[string]interface{}, 67 logger *logrus.Logger) error { 68 69 // Add the function ARN as a stack output 70 template.Outputs[sanitizedKeyName(keyName)] = &gocf.Output{ 71 Description: description, 72 Value: gocf.GetAtt(lambdaResourceName, fieldName), 73 } 74 return nil 75 } 76 return sparta.TemplateDecoratorHookFunc(attrDecorator) 77 } 78 79 // PublishRefOutputDecorator returns an TemplateDecoratorHookFunc 80 // that publishes the Ref value for a given lambda 81 func PublishRefOutputDecorator(keyName string, description string) sparta.TemplateDecoratorHookFunc { 82 attrDecorator := func(serviceName string, 83 lambdaResourceName string, 84 lambdaResource gocf.LambdaFunction, 85 resourceMetadata map[string]interface{}, 86 S3Bucket string, 87 S3Key string, 88 buildID string, 89 template *gocf.Template, 90 context map[string]interface{}, 91 logger *logrus.Logger) error { 92 93 // Add the function ARN as a stack output 94 template.Outputs[sanitizedKeyName(keyName)] = &gocf.Output{ 95 Description: description, 96 Value: gocf.Ref(lambdaResourceName), 97 } 98 return nil 99 } 100 101 return sparta.TemplateDecoratorHookFunc(attrDecorator) 102 }