Skip to content

Instantly share code, notes, and snippets.

@ibejohn818
Last active January 3, 2020 07:14
Show Gist options
  • Select an option

  • Save ibejohn818/b5b4d0417175ad0d8b8c742d529ebe8d to your computer and use it in GitHub Desktop.

Select an option

Save ibejohn818/b5b4d0417175ad0d8b8c742d529ebe8d to your computer and use it in GitHub Desktop.
Cloudformation resource spec json (py3.6)
def test():
from pprint import pprint
from collections import OrderedDict
import zlib
import json
import urllib.request as request
url = 'https://d201a2mn26r7lk.cloudfront.net/latest/gzip/CloudFormationResourceSpecification.json'
req = request.Request(url)
result = request.urlopen(req)
data = zlib.decompress(result.read(), 16+zlib.MAX_WBITS)
encoding = result.info().get_content_charset('utf-8')
spec = json.loads(data.decode(encoding))
types = spec.get('PropertyTypes')
res = OrderedDict(sorted(spec.get('ResourceTypes').items()))
# global types
global_types = []
# namespaced types
ns_types = {}
for k, v in types.items():
# dot split
sp = k.split('.')
if len(sp) == 1:
global_types.append(k)
continue
ns = sp[0]
key = sp[1]
if not ns_types.get(ns):
ns_types[ns] = []
if v.get("Properties"):
props = list(v.get("Properties").keys())
else:
props = "N/A"
prop = {key: props}
ns_types[ns].append(prop)
print("GLOBAL TYPES: ({})".format(len(global_types)))
pprint(global_types)
print("NAMESPACED TYPES: ({})".format(len(ns_types)))
pprint(ns_types)
print("RESOURCES : ({})".format(len(res.keys())))
for k, v in res.items():
print(k)
if v.get('Properties'):
print("PROPERTIES: ")
for kk, vv in v.get("Properties").items():
p = kk
if vv.get("Type"):
t = vv.get("Type")
elif vv.get("PrimitiveType"):
t = vv.get("PrimitiveType")
else:
t = "N/A"
if t == 'List':
t = "{} ({}) ".format(t, vv.get("ItemType"))
print("{} ({})".format(p, t))
print("____________________")
if __name__ == "__main__":
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment