Skip to content

Commit 86ef206

Browse files
authored
Merge pull request #627 from ericzbeard/analytics
Add analytics to template Metadata
2 parents 6c651fd + a785154 commit 86ef206

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed

cft/cft.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (t Template) GetSection(section Section) (*yaml.Node, error) {
143143
m := t.Node.Content[0]
144144
_, s, _ := s11n.GetMapValue(m, string(section))
145145
if s == nil {
146-
return nil, fmt.Errorf("unable to locate the %s node", section)
146+
return nil, fmt.Errorf("unable to locate the %s section", section)
147147
}
148148
return s, nil
149149
}

cft/pkg/module.go

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"gopkg.in/yaml.v3"
1717
)
1818

19+
var HasModules bool
20+
1921
const (
2022
Rain = "Rain"
2123
Metadata = "Metadata"
@@ -795,6 +797,8 @@ func module(ctx *directiveContext) (bool, error) {
795797
return false, errors.New("expected !Rain::Module <URI>")
796798
}
797799

800+
HasModules = true
801+
798802
uri := n.Content[1].Value
799803
var content []byte
800804
var err error

cft/pkg/module_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,7 @@ func TestCsvToSequence(t *testing.T) {
128128
t.Errorf("Unexpected sequence")
129129
}
130130
}
131+
132+
func init() {
133+
pkg.NoAnalytics = true
134+
}

cft/pkg/pkg.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package pkg
2626

2727
import (
2828
"embed"
29+
"encoding/json"
2930
"errors"
3031
"fmt"
3132
"path/filepath"
@@ -44,6 +45,22 @@ import (
4445

4546
// Experimental must be set to true to enable !Rain::Module
4647
var Experimental bool
48+
var NoAnalytics bool
49+
var HasRainSection bool
50+
51+
type analytics struct {
52+
// Current Rain version
53+
Version string
54+
55+
// Are we using experimental features?
56+
Experimental bool
57+
58+
// Did we use any Rain modules?
59+
HasModules bool
60+
61+
// Did the template have a Rain section?
62+
HasRainSection bool
63+
}
4764

4865
type transformContext struct {
4966
nodeToTransform *yaml.Node
@@ -154,6 +171,8 @@ func processRainSection(t *cft.Template) bool {
154171
// Now remove the Rain node from the template
155172
t.RemoveSection(cft.Rain)
156173

174+
HasRainSection = true
175+
157176
return true
158177
}
159178

@@ -265,7 +284,27 @@ func Template(t cft.Template, rootDir string, fs *embed.FS) (cft.Template, error
265284
retval.Node.Content = append(retval.Node.Content, templateNode)
266285
}
267286

268-
return retval, err
287+
// Add analytics to Metadata
288+
if !NoAnalytics {
289+
metadata, err := retval.GetSection(cft.Metadata)
290+
if err != nil || metadata == nil {
291+
metadata = node.AddMap(retval.Node.Content[0], string(cft.Metadata))
292+
}
293+
awsToolsMetrics := node.AddMap(metadata, "AWSToolsMetrics")
294+
a := analytics{
295+
Version: config.VERSION,
296+
HasModules: HasModules,
297+
Experimental: Experimental,
298+
HasRainSection: HasRainSection,
299+
}
300+
s, _ := json.Marshal(&a)
301+
awsToolsMetrics.Content = append(awsToolsMetrics.Content,
302+
&yaml.Node{Kind: yaml.ScalarNode, Value: "Rain"})
303+
awsToolsMetrics.Content = append(awsToolsMetrics.Content,
304+
&yaml.Node{Kind: yaml.ScalarNode, Value: string(s)})
305+
}
306+
307+
return retval, nil
269308
}
270309

271310
// File opens path as a CloudFormation template and returns a cft.Template

internal/cmd/deploy/deploy.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ To list and delete changesets, use the ls and rm commands.
153153
panic(err)
154154
}
155155

156-
// Figure out how long we thing the stack will take to execute
156+
// Figure out how long we think the stack will take to execute
157157
//totalSeconds := forecast.PredictTotalEstimate(template, stackExists)
158158
// TODO - Wait until the forecast command is GA and add this to output
159159

@@ -339,4 +339,5 @@ func init() {
339339
Cmd.Flags().StringVar(&format.NodeStyle, "node-style", "original", format.NodeStyleDocs)
340340
Cmd.Flags().BoolVar(&experimental, "experimental", false, "Acknowledge that you want to deploy with an experimental feature")
341341
Cmd.Flags().BoolVar(&includeNested, "nested-change-set", true, "Whether or not to include nested stacks in the change set")
342+
Cmd.Flags().BoolVar(&cftpkg.NoAnalytics, "no-analytics", false, "Do not write analytics to Metadata")
342343
}

internal/cmd/pkg/pkg.go

+1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,5 @@ func init() {
9393
Cmd.Flags().BoolVar(&config.Debug, "debug", false, "Output debugging information")
9494
Cmd.Flags().BoolVar(&dataModel, "datamodel", false, "Output the go yaml data model")
9595
Cmd.Flags().StringVar(&format.NodeStyle, "node-style", "", format.NodeStyleDocs)
96+
Cmd.Flags().BoolVar(&cftpkg.NoAnalytics, "no-analytics", false, "Do not include analytics in Metadata")
9697
}

test/templates/analytics.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Metadata:
2+
Comment: abc
3+
Resources:
4+
Bucket:
5+
Type: AWS::S3::Bucket

0 commit comments

Comments
 (0)