-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudformations.go
92 lines (83 loc) · 2.04 KB
/
cloudformations.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
package migrate
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
const DynamoDB = "AWS::DynamoDB::Table"
type Template struct {
Version string `json:"AWSTemplateFormatVersion" yaml:"AWSTemplateFormatVersion"`
Resources `json:"Resources" yaml:"Resources"`
}
type Resources map[string]Resource
type Resource struct {
Type string `json:"Type" yaml:"Type"`
Properties TableSchema `json:"Properties" yaml:"Properties"`
}
func (r *Resource) Checksum() (h [32]byte, err error) {
b, err := json.Marshal(r)
if err != nil {
return h, err
}
return sha256.Sum256(b), nil
}
func ParseJson(name string, body []byte) (schemas []Schema, err error) {
t := Template{}
if err = json.Unmarshal(body, &t); err != nil {
return nil, errors.WithStack(err)
}
if t.Version == "" {
s := &Schema{name: name}
if err = json.Unmarshal(body, s); err != nil {
return nil, errors.WithStack(err)
}
schemas = []Schema{
*s,
}
} else {
schemas = make([]Schema, 0, len(t.Resources))
for _, r := range t.Resources {
if r.Type == DynamoDB {
h, err := r.Checksum()
if err != nil {
return schemas, err
}
key := fmt.Sprintf("%s_%s:%x", name, r.Properties.TableName, h)
schemas = append(schemas, Schema{name: key, Table: r.Properties})
}
}
}
return
}
func ParseYaml(name string, body []byte) (schemas []Schema, err error) {
t := Template{}
if err = yaml.Unmarshal(body, &t); err != nil {
return nil, errors.WithStack(err)
}
if t.Version == "" {
s := &Schema{name: name}
if err = yaml.Unmarshal(body, s); err != nil {
return nil, errors.WithStack(err)
}
if s.Table.TableName != "" {
schemas = []Schema{
*s,
}
}
} else {
schemas = make([]Schema, 0, len(t.Resources))
for _, r := range t.Resources {
if r.Type == DynamoDB {
h, err := r.Checksum()
if err != nil {
return schemas, err
}
key := fmt.Sprintf("%s_%s:%x", name, r.Properties.TableName, h)
schemas = append(schemas, Schema{name: key, Table: r.Properties})
}
}
}
return
}