-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaws-cfn2dot
executable file
·167 lines (145 loc) · 4.12 KB
/
aws-cfn2dot
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
#!/usr/bin/env node
let _ = require('lodash-firecloud');
let {
stdin
} = process;
let chunks = [];
let isIntrinsicFun = function(Value) {
if (!_.isPlainObject(Value)) {
return false;
}
let keys = _.keys(Value);
if (keys.length !== 1) {
return false;
}
return /^(Fn::.*|Ref)$/.test(keys[0]);
};
let reduceToDeps = function(acc, statement) {
if (isIntrinsicFun(statement)) {
if (statement.Ref) {
acc = acc.concat(statement.Ref);
} else if (statement['Fn::GetAtt']) {
// {'Fn::GetAtt': ['Ref', 'Att']}
acc = acc.concat(statement['Fn::GetAtt'][0]);
} else if (_.isString(statement['Fn::Sub'])) {
// {'Fn::Sub': `${Ref}`}
let subRefs = statement['Fn::Sub'].match(/\$\{[^}]+\}/g);
subRefs = _.reject(subRefs, function(subRef) {
return /^AWS::.+/.test(subRef);
});
_.forEach(subRefs, function(subRef) {
subRef = subRef.match(/\$\{([^!][^.}]+)(\..+)?\}/)[1];
acc.push(subRef);
});
} else if (_.isArray(statement['Fn::Sub'])) {
// {'Fn::Sub': [`${Ref}${Var}`, {Var: Value}]}
let subRefs = statement['Fn::Sub'][0].match(/\$\{[^}]+\}/g);
subRefs = _.reject(subRefs, function(subRef) {
return /^AWS::.+/.test(subRef);
});
let subVars = _.keys(statement['Fn::Sub'][1]);
subRefs = _.without(subRefs, ...subVars);
_.forEach(subRefs, function(subRef) {
subRef = subRef.match(/\$\{([^!][^.}]+)(\..+)?\}/)[1];
acc.push(subRef);
});
}
} else if (_.isObjectLike(statement)) {
acc = acc.concat(_.reduce(statement, reduceToDeps, []));
}
return acc;
};
let findDeps = function(obj) {
let refs = _.reduce(obj, reduceToDeps, []);
return refs;
};
let cfn2graph = function(cfn) {
let graph = {
name: cfn.Description || '',
type: 'digraph',
rank: 'same',
shape: 'box',
style: 'filled',
nodes: [],
edges: [],
subgraphs: []
};
_.forEach(cfn.Resources, function(Resource, name) {
graph.nodes.push({name, type: Resource.Type});
let deps = findDeps(Resource.Properties);
deps = deps.concat(Resource.DependsOn || []);
deps = _.uniq(deps);
_.forEach(deps, function(from) {
graph.edges.push({from, to: name});
});
});
graph.nodes = _.uniqBy(graph.nodes, 'name');
let parametersSubgraph = {
name: 'Parameters',
type: 'subgraph',
rank: 'same', // 'source',
shape: 'ellipse',
style: 'filled',
nodes: [],
edges: []
};
graph.subgraphs.push(parametersSubgraph);
_.forEach(cfn.Parameters, function(Parameter, name) {
parametersSubgraph.nodes.push({name, type: 'AWS::CloudFormation::Parameter'});
_.forEach(findDeps(Parameter), function(from) {
parametersSubgraph.edges.push({from, to: name});
});
});
let outputsSubgraph = {
name: 'Outputs',
type: 'subgraph',
rank: 'same', // 'max',
shape: 'ellipse',
style: 'solid',
nodes: [],
edges: []
};
graph.subgraphs.push(outputsSubgraph);
_.forEach(cfn.Outputs, function(Output, name) {
name = `output${name}`;
outputsSubgraph.nodes.push({name, type: 'AWS::CloudFormation::Output'});
_.forEach(findDeps(Output), function(from) {
outputsSubgraph.edges.push({from, to: name});
});
});
// TODO handle pseudo parameters
return graph;
};
let render = function(graph) {
let shape = graph.shape ? `node [shape="${graph.shape}"];` : '';
let style = graph.style ? `node [style="${graph.style}"];` : '';
console.log(`
${graph.type} "${graph.name}" {
rank=${graph.rank};
labeljust=l;
${shape}
${style}
`);
_.forEach(graph.nodes, function({name, type}) {
console.log(`"${name}" [label="${name}\n${type}"]`);
});
_.forEach(graph.subgraphs, render);
_.forEach(graph.edges, function({from, to}) {
// console.log(`"${to}" -> "${from}";`);
console.log(`"${from}" -> "${to}";`);
});
console.log('}');
};
let read = function() {
stdin.resume();
stdin.setEncoding('utf-8');
stdin.on('data', function(chunk) {
chunks.push(chunk);
});
stdin.on('end', function() {
let cfn = JSON.parse(chunks.join(''));
let graph = cfn2graph(cfn);
render(graph);
});
};
read();