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 c14f5a484d).

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
This commit is contained in:
Luka Peschke 2017-12-06 11:14:07 +01:00
parent 43e1999e9d
commit cfc53b4883
1 changed files with 19 additions and 6 deletions

View File

@ -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 = {}