Skip to content

Instantly share code, notes, and snippets.

@gfa
Created September 28, 2016 10:05
Show Gist options
  • Save gfa/03e396f78fa5f4157e42e6e033b78e19 to your computer and use it in GitHub Desktop.
Save gfa/03e396f78fa5f4157e42e6e033b78e19 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
import boto3
import pprint
import json
import sys
from tempfile import mkstemp
import json_delta
from json_delta._util import read_bytestring
import argparse
import os
argparser = argparse.ArgumentParser(description='shows the difference between the template stored by cloudformation and the template stored on a s3 bucket, \n this script expect the AWS_* envvars to be present')
argparser.add_argument('-b', '--bucketname', nargs=1, default=None, required = True, help='Specify the bucket where the template is stored')
argparser.add_argument('-t', '--templatename', nargs=1, default=None, required = True, help='Template (file) name on the bucket')
argparser.add_argument('-s', '--stackname', nargs=1, default=None, help='Stack Name', required = True)
args = argparser.parse_args()
client = boto3.client('cloudformation')
response = client.get_template(
StackName = args.stackname[0]
)
# keys
# ResponseMetadata and TemplateBody
HTTPResponse = int(response['ResponseMetadata']['HTTPStatusCode'])
if HTTPResponse == 200:
templateBody = response['TemplateBody']
else:
print('http response != 200')
sys.exit(1)
# get the template from S3
fd, tempfile = mkstemp()
s3 = boto3.resource('s3')
s3.meta.client.download_file(args.bucketname[0],args.templatename[0],tempfile)
s3template = json.load(open(tempfile))
flags = {'common_key_threshold': 0.0, 'array_align': 'udiff',
'compare_lengths': False}
right_text = str(json.dumps(s3template))
left_text = str(json.dumps(templateBody))
both_text = None
diff = json_delta.load_and_diff(left_text, right_text,
verbose=False,
**flags)
for line in json_delta.load_and_udiff(left_text, right_text,
both_text, diff):
print(line.rstrip())
os.close(fd)
os.remove(tempfile)
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment