From cfc53b4883a83e9d8f68ec3ea514fdf02034e900 Mon Sep 17 00:00:00 2001 From: Luka Peschke Date: Wed, 6 Dec 2017 11:14:07 +0100 Subject: [PATCH] Ensure compatibility with all versions of gnocchiclient. This makes sure that the gnocchi storage backend is compatible with all versions of gnocchiclient. Starting with version 5.0.0 (which is not stable yet), it returns python datetime objects instead of timestamps (see https://github.com/gnocchixyz/python-gnocchiclient/commit/c14f5a484dd00c14854f56150fca5eee5c42a8b7). As soon as a gnocchiclient version > to 4.0 is considered stable, the try/except block can be removed and a constraint python-gnocchiclient>=5.0 may be set in requirements.txt. Change-Id: If1816b6926e8048ff05908a15fc0870050a5718a --- cloudkitty/storage/gnocchi/__init__.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/cloudkitty/storage/gnocchi/__init__.py b/cloudkitty/storage/gnocchi/__init__.py index 229db296..511c345d 100644 --- a/cloudkitty/storage/gnocchi/__init__.py +++ b/cloudkitty/storage/gnocchi/__init__.py @@ -254,9 +254,15 @@ class GnocchiStorage(storage.BaseStorage): except gexceptions.MetricNotFound: return if len(r) > 0: - # (aolwas) According http://gnocchi.xyz/rest.html#metrics, - # gnocchi always returns measures ordered by timestamp - return ck_utils.dt2ts(dateutil.parser.parse(r[-1][0])) + # NOTE(lukapeschke) Since version 5.0.0, gnocchiclient returns a + # datetime object instead of a timestamp. This fixture is made + # to ensure compatibility with all versions + try: + # (aolwas) According http://gnocchi.xyz/rest.html#metrics, + # gnocchi always returns measures ordered by timestamp + return ck_utils.dt2ts(dateutil.parser.parse(r[-1][0])) + except TypeError: + return ck_utils.dt2ts(r[-1][0]) def get_total(self, begin=None, end=None, tenant_id=None, service=None, groupby=None): @@ -316,9 +322,16 @@ class GnocchiStorage(storage.BaseStorage): project_id=None) def _to_cloudkitty(self, res_type, resource_data, measure): - begin = dateutil.parser.parse(measure[0]) - end = (dateutil.parser.parse(measure[0]) + - datetime.timedelta(seconds=self._period)) + # NOTE(lukapeschke) Since version 5.0.0, gnocchiclient returns a + # datetime object instead of a timestamp. This fixture is made + # to ensure compatibility with all versions + try: + begin = dateutil.parser.parse(measure[0]) + end = (dateutil.parser.parse(measure[0]) + + datetime.timedelta(seconds=self._period)) + except TypeError: + begin = measure[0] + end = begin + datetime.timedelta(seconds=self._period) cost = decimal.Decimal(measure[2]) # Rating informations rating_dict = {}