From 60378e991ba04d8b33107bdd11b6c258ca5faee1 Mon Sep 17 00:00:00 2001 From: lju Date: Tue, 25 Jun 2019 03:48:26 -0700 Subject: [PATCH] Fix ec2.py dynamic inventory script when pulling down RDS cluster information (#48075) * Fixes #26481 * Pass lint tests --- contrib/inventory/ec2.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 3d0ba3a2454..feb31539870 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -160,6 +160,7 @@ import argparse import re from time import time from copy import deepcopy +from datetime import date, datetime import boto from boto import ec2 from boto import rds @@ -242,6 +243,13 @@ class Ec2Inventory(object): def _empty_inventory(self): return {"_meta": {"hostvars": {}}} + def _json_serial(self, obj): + """JSON serializer for objects not serializable by default json code""" + + if isinstance(obj, (datetime, date)): + return obj.isoformat() + raise TypeError("Type %s not serializable" % type(obj)) + def __init__(self): ''' Main execution path ''' @@ -728,13 +736,6 @@ class Ec2Inventory(object): account_id = boto.connect_iam().get_user().arn.split(':')[4] c_dict = {} for c in clusters: - # remove these datetime objects as there is no serialisation to json - # currently in place and we don't need the data yet - if 'EarliestRestorableTime' in c: - del c['EarliestRestorableTime'] - if 'LatestRestorableTime' in c: - del c['LatestRestorableTime'] - if not self.ec2_instance_filters: matches_filter = True else: @@ -1701,9 +1702,9 @@ class Ec2Inventory(object): string ''' if pretty: - return json.dumps(data, sort_keys=True, indent=2) + return json.dumps(data, sort_keys=True, indent=2, default=self._json_serial) else: - return json.dumps(data) + return json.dumps(data, default=self._json_serial) if __name__ == '__main__':