/
resources.go
54 lines (45 loc) · 1.49 KB
/
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
//go:build !lambdabinary
package cloudformation
import (
_ "embed"
"encoding/json"
"fmt"
gof "github.com/awslabs/goformation/v5/cloudformation"
"github.com/jmespath/go-jmespath"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
//go:embed cloudformation-schema.json
var cloudformationSchema string
// ResourceOutputs is responsible for returning the
// set of CloudFormation outputs for a given resource type. These are
// produced from the schema that has been previously downloaded and
// embedded into the binary.
func ResourceOutputs(resourceName string,
resource gof.Resource,
logger *zerolog.Logger) ([]string, error) {
var rawData interface{}
unmarshalErr := json.Unmarshal([]byte(cloudformationSchema), &rawData)
if unmarshalErr != nil {
return nil, unmarshalErr
}
// Issue the JMES query to find this resource in the schema...
jmesQuery := fmt.Sprintf("ResourceTypes.\"%s\".Attributes", resource.AWSCloudFormationType())
result, resultErr := jmespath.Search(jmesQuery, rawData)
if resultErr != nil {
return nil, resultErr
}
resultMap, resultMapOk := result.(map[string]interface{})
if !resultMapOk {
// If this a custom resource, there are no outputs...
if resource.AWSCloudFormationType() == "AWS::CloudFormation::CustomResource" {
return nil, nil
}
return nil, errors.Errorf("Failed to extract outputs for resource type: %s", resource.AWSCloudFormationType())
}
vals := []string{}
for eachKey := range resultMap {
vals = append(vals, eachKey)
}
return vals, nil
}