-
Notifications
You must be signed in to change notification settings - Fork 48
/
cloudformation_resources.go
143 lines (126 loc) · 4.48 KB
/
cloudformation_resources.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
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
package sparta
import (
"errors"
"fmt"
"reflect"
// Also included in lambda_permissions.go, but doubly included
// here as the package's init() function handles registering
// the resources we look up in this package.
_ "github.com/mweagle/cloudformationresources"
"github.com/Sirupsen/logrus"
gocf "github.com/crewjam/go-cloudformation"
)
// See http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html
const (
// TagLogicalResourceID is the current logical resource name
TagLogicalResourceID = "aws:cloudformation:logical-id"
// TagResourceType is the type of the referred resource type
TagResourceType = "sparta:cloudformation:restype"
// TagStackRegion is the current stack's logical id
TagStackRegion = "sparta:cloudformation:region"
// TagStackID is the current stack's ID
TagStackID = "aws:cloudformation:stack-id"
// TagStackName is the current stack name
TagStackName = "aws:cloudformation:stack-name"
)
var cloudformationTypeMapDiscoveryOutputs = map[string][]string{
"AWS::DynamoDB::Table": {"StreamArn"},
"AWS::Kinesis::Stream": {"Arn"},
"AWS::Route53::RecordSet": {""},
"AWS::S3::Bucket": {"DomainName", "WebsiteURL"},
"AWS::SNS::Topic": {"TopicName"},
"AWS::SQS::Queue": {"Arn", "QueueName"},
}
func newCloudFormationResource(resourceType string, logger *logrus.Logger) (gocf.ResourceProperties, error) {
resProps := gocf.NewResourceByType(resourceType)
if nil == resProps {
logger.WithFields(logrus.Fields{
"Type": resourceType,
}).Fatal("Failed to create CloudFormation CustomResource!")
return nil, fmt.Errorf("Unsupported CustomResourceType: %s", resourceType)
}
return resProps, nil
}
func outputsForResource(template *gocf.Template,
logicalResourceName string,
logger *logrus.Logger) (map[string]interface{}, error) {
item, ok := template.Resources[logicalResourceName]
if !ok {
return nil, nil
}
outputs := make(map[string]interface{}, 0)
attrs, exists := cloudformationTypeMapDiscoveryOutputs[item.Properties.CfnResourceType()]
if exists {
outputs["Ref"] = gocf.Ref(logicalResourceName).String()
outputs[TagResourceType] = item.Properties.CfnResourceType()
for _, eachAttr := range attrs {
outputs[eachAttr] = gocf.GetAtt(logicalResourceName, eachAttr)
}
// Any tags?
r := reflect.ValueOf(item.Properties)
tagsField := reflect.Indirect(r).FieldByName("Tags")
if tagsField.IsValid() && !tagsField.IsNil() {
outputs["Tags"] = tagsField.Interface()
}
}
if len(outputs) != 0 {
logger.WithFields(logrus.Fields{
"ResourceName": logicalResourceName,
"Outputs": outputs,
}).Debug("Resource Outputs")
}
return outputs, nil
}
func safeAppendDependency(resource *gocf.Resource, dependencyName string) {
if nil == resource.DependsOn {
resource.DependsOn = []string{}
}
resource.DependsOn = append(resource.DependsOn, dependencyName)
}
func safeMetadataInsert(resource *gocf.Resource, key string, value interface{}) {
if nil == resource.Metadata {
resource.Metadata = make(map[string]interface{}, 0)
}
resource.Metadata[key] = value
}
func safeMergeTemplates(sourceTemplate *gocf.Template, destTemplate *gocf.Template, logger *logrus.Logger) error {
var mergeErrors []string
// Append the custom resources
for eachKey, eachLambdaResource := range sourceTemplate.Resources {
_, exists := destTemplate.Resources[eachKey]
if exists {
errorMsg := fmt.Sprintf("Duplicate CloudFormation resource name: %s", eachKey)
mergeErrors = append(mergeErrors, errorMsg)
} else {
destTemplate.Resources[eachKey] = eachLambdaResource
}
}
// Append the custom Mappings
for eachKey, eachMapping := range sourceTemplate.Mappings {
_, exists := destTemplate.Mappings[eachKey]
if exists {
errorMsg := fmt.Sprintf("Duplicate CloudFormation Mapping name: %s", eachKey)
mergeErrors = append(mergeErrors, errorMsg)
} else {
destTemplate.Mappings[eachKey] = eachMapping
}
}
// Append the custom outputs
for eachKey, eachLambdaOutput := range sourceTemplate.Outputs {
_, exists := destTemplate.Outputs[eachKey]
if exists {
errorMsg := fmt.Sprintf("Duplicate CloudFormation output key name: %s", eachKey)
mergeErrors = append(mergeErrors, errorMsg)
} else {
destTemplate.Outputs[eachKey] = eachLambdaOutput
}
}
if len(mergeErrors) > 0 {
logger.Error("Failed to update template. The following collisions were found:")
for _, eachError := range mergeErrors {
logger.Error("\t" + eachError)
}
return errors.New("Template merge failed")
}
return nil
}