-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcfn-show
78 lines (56 loc) · 2.43 KB
/
cfn-show
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
#!/usr/bin/python
import simplejson as json
import sys
from argparse import ArgumentParser
from os import path
script_name = path.basename(__file__)
parser = ArgumentParser()
parser.add_argument('file_name',
metavar='FILE', help='CloudFormation json file')
parser.add_argument('sections', metavar='SECTIONS', default='Resources',
nargs='?', help='A csv list of sections to display in the output')
parser.add_argument('resource', metavar='RESOURCE', nargs='?',
help='A single resource to display')
parser.add_argument('-n', '--names', '--show-names', action="store_true",
help='Show the name of an individual resource')
parser.add_argument('-i', '--indent', metavar='INDENT', type=int,
default=4, help='Indent size of the json output')
args = parser.parse_args()
with open(args.file_name) as stack_json:
try:
stack = json.load(stack_json)
except json.JSONDecodeError:
sys.stderr.write("%s: %s is not valid json. Exiting\n" % (script_name, args.file_name))
sys.exit(1)
# these meta keys do not contain resources so we drop them
unneeded_keys = ('AWSTemplateFormatVersion', 'Description')
[stack.pop(k, None) for k in unneeded_keys]
if args.sections == 'sections':
for section in sorted(stack.keys()):
print section
sys.exit(0)
if args.sections:
sections = args.sections.split(',')
sections = [s.capitalize() for s in sections]
# verify all the section names exist in the json
for section in sections:
if section not in stack.keys():
sys.stderr.write("%s: section '%s' is not in the template\n" % (script_name, section))
sys.exit(1)
else:
sections = stack.keys()
# if we had a single argument then display all the entries for that section
if not args.resource:
for section in sorted(sections):
for resource in sorted(stack[section].keys()):
resource_type = stack[section][resource].get("Type", section)
print("%s\n%s\n" % (resource, resource_type))
sys.exit(0)
# if we had two arguments then expand a single resource
if args.resource in stack[section].keys():
if args.names:
print '"%s" : ' % args.resource,
print(json.dumps(stack[section].get(args.resource), stack, indent=args.indent * ' '))
else:
sys.stderr.write("Resource [%s] was not found in the [%s] section\n" % (args.resource, section))
sys.exit(1)