forked from awslabs/goformation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
200 lines (158 loc) · 4.81 KB
/
template.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package cloudformation
import (
"encoding/json"
"fmt"
"strings"
"github.com/awslabs/goformation/v4/intrinsics"
"github.com/sanathkr/yaml"
)
// Template represents an AWS CloudFormation template
// see: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-anatomy.html
type Template struct {
AWSTemplateFormatVersion string `json:"AWSTemplateFormatVersion,omitempty"`
Transform *Transform `json:"Transform,omitempty"`
Description string `json:"Description,omitempty"`
Metadata map[string]interface{} `json:"Metadata,omitempty"`
Parameters Parameters `json:"Parameters,omitempty"`
Mappings map[string]interface{} `json:"Mappings,omitempty"`
Conditions map[string]interface{} `json:"Conditions,omitempty"`
Resources Resources `json:"Resources,omitempty"`
Outputs Outputs `json:"Outputs,omitempty"`
}
type Parameter struct {
Type string `json:"Type"`
Description string `json:"Description,omitempty"`
Default interface{} `json:"Default,omitempty"`
AllowedPattern string `json:"AllowedPattern,omitempty"`
AllowedValues []string `json:"AllowedValues,omitempty"`
ConstraintDescription string `json:"ConstraintDescription,omitempty"`
MaxLength int `json:"MaxLength,omitempty"`
MinLength int `json:"MinLength,omitempty"`
MaxValue float64 `json:"MaxValue,omitempty"`
MinValue float64 `json:"MinValue,omitempty"`
NoEcho bool `json:"NoEcho,omitempty"`
}
type Output struct {
Value interface{} `json:"Value"`
Description string `json:"Description,omitempty"`
Export Export `json:"Export,omitempty"`
}
type Export struct {
Name string `json:"Name,omitempty"`
}
type Resource interface {
AWSCloudFormationType() string
}
type Parameters map[string]Parameter
type Resources map[string]Resource
type Outputs map[string]Output
func (resources *Resources) UnmarshalJSON(b []byte) error {
// Resources
var rawResources map[string]*json.RawMessage
err := json.Unmarshal(b, &rawResources)
if err != nil {
return err
}
newResources := Resources{}
for name, raw := range rawResources {
res, err := unmarshallResource(name, raw)
if err != nil {
return err
}
newResources[name] = res
}
*resources = newResources
return nil
}
func unmarshallResource(name string, raw_json *json.RawMessage) (Resource, error) {
var err error
type rType struct {
Type string
}
var rtype rType
if err = json.Unmarshal(*raw_json, &rtype); err != nil {
return nil, err
}
if rtype.Type == "" {
return nil, fmt.Errorf("Cannot find Type for %v", name)
}
// Custom Resource Handler
var resourceStruct Resource
if strings.HasPrefix(rtype.Type, "Custom::") {
resourceStruct = &CustomResource{Type: rtype.Type}
} else {
resourceStruct = AllResources()[rtype.Type]
}
err = json.Unmarshal(*raw_json, resourceStruct)
if err != nil {
return nil, err
}
return resourceStruct, nil
}
type Transform struct {
String *string
StringArray *[]string
}
func (t Transform) value() interface{} {
if t.String != nil {
return t.String
}
if t.StringArray != nil {
return t.StringArray
}
return nil
}
func (t *Transform) MarshalJSON() ([]byte, error) {
return json.Marshal(t.value())
}
func (t *Transform) UnmarshalJSON(b []byte) error {
var typecheck interface{}
if err := json.Unmarshal(b, &typecheck); err != nil {
return err
}
switch val := typecheck.(type) {
case string:
t.String = &val
case []string:
t.StringArray = &val
case []interface{}:
var strslice []string
for _, i := range val {
switch str := i.(type) {
case string:
strslice = append(strslice, str)
}
}
t.StringArray = &strslice
}
return nil
}
// NewTemplate creates a new AWS CloudFormation template struct
func NewTemplate() *Template {
return &Template{
AWSTemplateFormatVersion: "2010-09-09",
Description: "",
Metadata: map[string]interface{}{},
Parameters: Parameters{},
Mappings: map[string]interface{}{},
Conditions: map[string]interface{}{},
Resources: Resources{},
Outputs: Outputs{},
}
}
// JSON converts an AWS CloudFormation template object to JSON
func (t *Template) JSON() ([]byte, error) {
j, err := json.MarshalIndent(t, "", " ")
if err != nil {
return nil, err
}
return intrinsics.ProcessJSON(j, nil)
}
// YAML converts an AWS CloudFormation template object to YAML
func (t *Template) YAML() ([]byte, error) {
j, err := t.JSON()
if err != nil {
return nil, err
}
return yaml.JSONToYAML(j)
}