-
Notifications
You must be signed in to change notification settings - Fork 48
/
cmd_describe.go
241 lines (222 loc) · 6.55 KB
/
cmd_describe.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//go:build !lambdabinary
// +build !lambdabinary
package sparta
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"text/template"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
// workflowHooksDescriptionNodes returns the set of []*DescriptionInfo
// entries that summarizes the WorkflowNodes
func workflowHooksDescriptionNodes(serviceName string, hooks *WorkflowHooks) ([]*DescriptionInfo, error) {
if hooks == nil {
return nil, nil
}
workflowDescInfo := make([]*DescriptionInfo, 0)
for _, eachServiceDecorator := range hooks.ServiceDecorators {
describable, isDescribable := eachServiceDecorator.(Describable)
if isDescribable {
descInfos, descInfosErr := describable.Describe(serviceName)
if descInfosErr != nil {
return nil, descInfosErr
}
workflowDescInfo = append(workflowDescInfo, descInfos)
}
}
return workflowDescInfo, nil
}
// Describe produces a graphical representation of a service's Lambda and data sources. Typically
// automatically called as part of a compiled golang binary via the `describe` command
// line option.
func Describe(serviceName string,
serviceDescription string,
lambdaAWSInfos []*LambdaAWSInfo,
api APIGateway,
site *S3Site,
s3BucketName string,
buildTags string,
linkerFlags string,
outputWriter io.Writer,
workflowHooks *WorkflowHooks,
logger *zerolog.Logger) error {
// Multiwriter
templateFile, templateFileErr := templateOutputFile(optionsProvision.OutputDir,
serviceName)
if templateFileErr != nil {
return templateFileErr
}
var cloudFormationTemplate bytes.Buffer
multiWriter := io.MultiWriter(templateFile, &cloudFormationTemplate)
buildErr := Build(context.Background(),
true,
serviceName,
serviceDescription,
lambdaAWSInfos,
api,
site,
false,
"BUILD_ID",
"",
ScratchDirectory,
buildTags,
linkerFlags,
multiWriter,
workflowHooks,
logger)
closeErr := templateFile.Close()
if closeErr != nil {
logger.Warn().
Err(closeErr).
Msg("Failed to close template file handle")
}
if buildErr != nil {
return buildErr
}
tmpl, err := template.New("description").Parse(embeddedMustString("resources/describe/template.html"))
if err != nil {
return errors.New(err.Error())
}
// Setup the describer
describer := descriptionWriter{
nodes: make([]*cytoscapeNode, 0),
logger: logger,
}
// Instead of inline mermaid stuff, we're going to stuff raw
// json through. We can also include AWS images in the icon
// using base64/encoded:
// Example: https://cytoscape.github.io/cytoscape.js-tutorial-demo/datasets/social.json
// Use the "fancy" CSS:
// https://github.com/cytoscape/cytoscape.js-tutorial-demo/blob/gh-pages/stylesheets/fancy.json
// Which is dynamically updated at: https://cytoscape.github.io/cytoscape.js-tutorial-demo/
fullIconPath := func(descriptionNode *DescriptionIcon) string {
// Use an empty PNG if we don't have an image
if descriptionNode == nil {
// Because the style uses data(image) we need to ensure that
// empty nodes have some sort of image, else the Cytoscape JS
// won't render
return "AWS-Architecture-Assets/Default/empty-image.png"
}
return fmt.Sprintf("AWS-Architecture-Assets/%s/%s",
descriptionNode.Category,
descriptionNode.Name)
}
// Setup the root object
writeErr := describer.writeNodeWithParent(serviceName,
nodeColorService,
fullIconPath(&DescriptionIcon{
Category: "Res_Management-Governance",
Name: "Res_48_Light/Res_AWS-CloudFormation_Stack_48_Light.png",
}),
"",
labelWeightBold)
if writeErr != nil {
return writeErr
}
parentMap := make(map[string]bool)
writeNodes := func(parent string, descriptionNodes []*DescriptionTriplet) error {
if parent != "" {
_, exists := parentMap[parent]
if !exists {
writeErr = describer.writeNodeWithParent(parent,
"#FF0000",
fullIconPath(nil),
"",
labelWeightBold)
if writeErr != nil {
return writeErr
}
parentMap[parent] = true
}
}
for _, eachDescNode := range descriptionNodes {
descDisplayInfo := eachDescNode.DisplayInfo
if descDisplayInfo == nil {
descDisplayInfo = &DescriptionDisplayInfo{}
}
writeErr = describer.writeNodeWithParent(eachDescNode.SourceNodeName,
descDisplayInfo.SourceNodeColor,
fullIconPath(descDisplayInfo.SourceIcon),
parent,
labelWeightNormal)
if writeErr != nil {
return writeErr
}
writeErr = describer.writeEdge(eachDescNode.SourceNodeName,
eachDescNode.TargetNodeName,
eachDescNode.ArcLabel)
if writeErr != nil {
return writeErr
}
}
return nil
}
for _, eachLambda := range lambdaAWSInfos {
descriptionNodes, descriptionNodesErr := eachLambda.Description(serviceName)
if descriptionNodesErr != nil {
return descriptionNodesErr
}
writeErr := writeNodes("Lambdas", descriptionNodes)
if writeErr != nil {
return writeErr
}
}
// The API needs to know how to describe itself. So for that it needs an object that
// encapsulates writing the nodes and links...so let's go ahead
// and make that object, then supply it to the Describe interface function
// API?
if nil != api {
descriptionInfo, descriptionInfoErr := api.Describe(serviceName)
if descriptionInfoErr != nil {
return descriptionInfoErr
}
writeErr := writeNodes(descriptionInfo.Name, descriptionInfo.Nodes)
if writeErr != nil {
return writeErr
}
}
// What about everything else...
workflowDescription, workflowDescriptionErr := workflowHooksDescriptionNodes(serviceName, workflowHooks)
if workflowDescriptionErr != nil {
return workflowDescriptionErr
}
for _, eachWorkflowDesc := range workflowDescription {
groupName := eachWorkflowDesc.Name
if groupName == "" {
groupName = "WorkflowHooks"
}
workflowDescriptionErr = writeNodes(groupName, eachWorkflowDesc.Nodes)
if workflowDescriptionErr != nil {
return workflowDescriptionErr
}
}
// Write it out...
cytoscapeBytes, cytoscapeBytesErr := json.MarshalIndent(describer.nodes, "", " ")
if cytoscapeBytesErr != nil {
return errors.Wrapf(cytoscapeBytesErr, "Failed to marshal cytoscape data")
}
params := struct {
SpartaVersion string
ServiceName string
ServiceDescription string
CloudFormationTemplate string
CSSFiles []*templateResource
JSFiles []*templateResource
ImageMap map[string]string
CytoscapeData interface{}
}{
SpartaGitHash[0:8],
serviceName,
serviceDescription,
cloudFormationTemplate.String(),
templateCSSFiles(logger),
templateJSFiles(logger),
templateImageMap(logger),
string(cytoscapeBytes),
}
return tmpl.Execute(outputWriter, params)
}