-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
template.go
36 lines (29 loc) · 998 Bytes
/
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
package manager
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/pkg/errors"
"github.com/weaveworks/goformation/v4"
)
// GetStackTemplate gets the Cloudformation template for a stack
// and returns a json string representation
func (c *StackCollection) GetStackTemplate(ctx context.Context, stackName string) (string, error) {
input := &cloudformation.GetTemplateInput{
StackName: aws.String(stackName),
}
output, err := c.cloudformationAPI.GetTemplate(ctx, input)
if err != nil {
return "", err
}
return ensureJSONResponse([]byte(*output.TemplateBody))
}
func ensureJSONResponse(templateBody []byte) (string, error) {
//since json is valid yaml we just need to check the response is valid yaml
template, err := goformation.ParseYAML(templateBody)
if err != nil {
return "", errors.Wrap(err, "failed to parse GetStackTemplate response")
}
bytes, err := template.JSON()
return string(bytes), err
}