forked from awslabs/goformation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref.go
123 lines (106 loc) · 4.29 KB
/
ref.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
package intrinsics
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/drmmarsunited/goformation/v7/cloudformation/utils"
"strings"
)
var pph = newPseudoParamHelper()
// Ref resolves the 'Ref' AWS CloudFormation intrinsic function.
// Currently, this only resolves against CloudFormation Parameter default values
// See: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html
func Ref(name string, input interface{}, template interface{}) interface{} {
// Dang son, this has got more nest than a bald eagle
// Check the input is a string
if name, ok := input.(string); ok {
switch name {
case "AWS::AccountId":
pph.parseAwsAccountId()
return pph.accountId
case "AWS::NotificationARNs":
// This data is not knowable outside the context of the AWS CloudFormation service, using a dummy value
return []string{"arn:aws:sns:us-east-1:123456789012:MyTopic"}
case "AWS::NoValue":
return nil
case "AWS::Partition":
pph.parseAwsPartition()
return pph.partition
case "AWS::Region":
pph.parseAwsRegion()
return pph.region
case "AWS::StackId":
// This data is not knowable outside the context of the AWS CloudFormation service, using a dummy value
return "arn:aws:cloudformation:us-east-1:123456789012:stack/MyStack/1c2fa620-982a-11e3-aff7-50e2416294e0"
case "AWS::StackName":
// This data is not knowable outside the context of the AWS CloudFormation service, using a dummy value
return "goformation-stack"
default:
// This isn't a pseudo 'Ref' parameter, so we need to look inside the CloudFormation template
// to see if we can resolve the reference. This implementation just looks at the Parameters section
// to see if there is a parameter matching the name, and if so, return the default value.
// This is where we handle dynamic references to other resources in the same template with a dummy value
// Check the template is a map
if template, ok := template.(map[string]interface{}); ok {
// Check there is a resources section
if uresources, ok := template["Resources"]; ok {
// Check the resources section is a map
if resources, ok := uresources.(map[string]interface{}); ok {
// Check there is a resource with the same name as the Ref
if uresource, ok := resources[name]; ok {
// Check the resource is a map
if _, ok := uresource.(map[string]interface{}); ok {
return "dummyvalue"
}
}
}
}
// Check there is a parameters section
if uparameters, ok := template["Parameters"]; ok {
// Check the parameters section is a map
if parameters, ok := uparameters.(map[string]interface{}); ok {
// Check there is a parameter with the same name as the Ref
if uparameter, ok := parameters[name]; ok {
// Check the parameter is a map
if parameter, ok := uparameter.(map[string]interface{}); ok {
// Check the parameter has a default
if def, ok := parameter["Default"]; ok {
// This is where we handle a CommaDelimitedList parameter type
if strings.Contains(parameter["Type"].(string), "CommaDelimitedList") {
return strings.Split(def.(string), ",")
}
// This is where we handle SSM parameter resolution
if strings.Contains(parameter["Type"].(string), "AWS::SSM::Parameter::Value") {
// Set up AWS helper
awsHelper, err := utils.NewAwsHelper()
if err != nil {
fmt.Printf("Could not setup AWS Helper: %s\n", err.Error())
return "dummyvalue"
}
// Set up SSM client
svc := ssm.NewFromConfig(awsHelper.Cfg)
// Get the SSM parameter value
pn := def.(string)
res, err := svc.GetParameter(context.TODO(), &ssm.GetParameterInput{Name: &pn})
if err != nil {
fmt.Printf("Could not get SSM parameter %s: %s\n", pn, err.Error())
return "dummyvalue"
}
return *res.Parameter.Value
}
return def
} else {
// This is where we handle parameters that do not have default values
if strings.HasPrefix(parameter["Type"].(string), "AWS::EC2::Subnet::Id") {
return "subnet-xxxxxx"
}
}
}
}
}
}
}
}
}
return nil
}