From 3e2c66427f94e477a7af0cd528418fa079c1ec5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Albert?= Date: Tue, 10 May 2016 18:18:24 +0200 Subject: [PATCH] Refactor gnocchi transformer Use of the new generic transformer class. Fixed a bug with image_id extraction from image_ref. Change-Id: I1e2fdf8c19b7d7414dc6d9ed2fcb229d5453aa53 --- cloudkitty/tests/transformers/test_gnocchi.py | 113 ++++++++++++++++++ cloudkitty/transformer/gnocchi.py | 68 ++++------- 2 files changed, 139 insertions(+), 42 deletions(-) create mode 100644 cloudkitty/tests/transformers/test_gnocchi.py diff --git a/cloudkitty/tests/transformers/test_gnocchi.py b/cloudkitty/tests/transformers/test_gnocchi.py new file mode 100644 index 00000000..fed17636 --- /dev/null +++ b/cloudkitty/tests/transformers/test_gnocchi.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Objectif Libre +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# @author: Stéphane Albert +# +import copy + +from cloudkitty import tests +from cloudkitty.transformer import gnocchi + +GNOCCHI_COMPUTE = { + 'id': '2f58a438-3169-11e6-b36c-bfe1fa3241fe', + 'project_id': '4480c638-3169-11e6-91de-a3bd3a7d3afb', + 'user_id': '576808d8-3169-11e6-992b-5f931fc671df', + 'display_name': 'test', + 'flavor_id': '6aa7b1ce-317c-11e6-92d2-835668472674', + 'image_ref': 'http://fakeglance/c8ae2e38-316d-11e6-b19a-dbee663ddaee', + 'metrics': {}} + +TRANS_COMPUTE = { + 'instance_id': '2f58a438-3169-11e6-b36c-bfe1fa3241fe', + 'resource_id': '2f58a438-3169-11e6-b36c-bfe1fa3241fe', + 'project_id': '4480c638-3169-11e6-91de-a3bd3a7d3afb', + 'user_id': '576808d8-3169-11e6-992b-5f931fc671df', + 'name': 'test', + 'flavor_id': '6aa7b1ce-317c-11e6-92d2-835668472674', + 'image_id': 'c8ae2e38-316d-11e6-b19a-dbee663ddaee', + 'metrics': {}} + +GNOCCHI_IMAGE = { + 'id': '2f58a438-3169-11e6-b36c-bfe1fa3241fe', + 'project_id': '4480c638-3169-11e6-91de-a3bd3a7d3afb', + 'user_id': '576808d8-3169-11e6-992b-5f931fc671df', + 'container_format': 'bare', + 'disk_format': 'raw', + 'metrics': {}} + +TRANS_IMAGE = { + 'resource_id': '2f58a438-3169-11e6-b36c-bfe1fa3241fe', + 'project_id': '4480c638-3169-11e6-91de-a3bd3a7d3afb', + 'user_id': '576808d8-3169-11e6-992b-5f931fc671df', + 'container_format': 'bare', + 'disk_format': 'raw', + 'metrics': {}} + +GNOCCHI_VOLUME = { + 'id': '17992d58-316f-11e6-9528-1379eed8ebe4', + 'project_id': '4480c638-3169-11e6-91de-a3bd3a7d3afb', + 'user_id': '576808d8-3169-11e6-992b-5f931fc671df', + 'display_name': 'vol1', + 'metrics': {}} + +TRANS_VOLUME = { + 'resource_id': '17992d58-316f-11e6-9528-1379eed8ebe4', + 'project_id': '4480c638-3169-11e6-91de-a3bd3a7d3afb', + 'user_id': '576808d8-3169-11e6-992b-5f931fc671df', + 'name': 'vol1', + 'metrics': {}} + +GNOCCHI_NETWORK = { + 'id': '02f8e84e-317d-11e6-ad23-af0423cd2a97', + 'project_id': '4480c638-3169-11e6-91de-a3bd3a7d3afb', + 'user_id': '576808d8-3169-11e6-992b-5f931fc671df', + 'name': 'network1', + 'metrics': {}} + +TRANS_NETWORK = { + 'resource_id': '02f8e84e-317d-11e6-ad23-af0423cd2a97', + 'project_id': '4480c638-3169-11e6-91de-a3bd3a7d3afb', + 'user_id': '576808d8-3169-11e6-992b-5f931fc671df', + 'name': 'network1', + 'metrics': {}} + + +class GnocchiTransformerTest(tests.TestCase): + def setUp(self): + super(GnocchiTransformerTest, self).setUp() + + def test_strip_gnocchi_compute(self): + resource = copy.deepcopy(GNOCCHI_COMPUTE) + t_test = gnocchi.GnocchiTransformer() + result = t_test.strip_resource_data('compute', resource) + self.assertEqual(TRANS_COMPUTE, result) + + def test_strip_gnocchi_image(self): + resource = copy.deepcopy(GNOCCHI_IMAGE) + t_test = gnocchi.GnocchiTransformer() + result = t_test.strip_resource_data('image', resource) + self.assertEqual(TRANS_IMAGE, result) + + def test_strip_gnocchi_volume(self): + resource = copy.deepcopy(GNOCCHI_VOLUME) + t_test = gnocchi.GnocchiTransformer() + result = t_test.strip_resource_data('volume', resource) + self.assertEqual(TRANS_VOLUME, result) + + def test_strip_gnocchi_network(self): + resource = copy.deepcopy(GNOCCHI_NETWORK) + t_test = gnocchi.GnocchiTransformer() + result = t_test.strip_resource_data('network', resource) + self.assertEqual(TRANS_NETWORK, result) diff --git a/cloudkitty/transformer/gnocchi.py b/cloudkitty/transformer/gnocchi.py index 3445db52..87350123 100644 --- a/cloudkitty/transformer/gnocchi.py +++ b/cloudkitty/transformer/gnocchi.py @@ -17,8 +17,22 @@ from cloudkitty import transformer class GnocchiTransformer(transformer.BaseTransformer): - def __init__(self): - pass + compute_map = { + 'instance_id': ['id'], + 'name': ['display_name'], + 'flavor_id': ['flavor_id'], + 'image_id': lambda x, y: x.get_image_id(y), + } + image_map = { + 'container_format': ['container_format'], + 'disk_format': ['disk_format'], + } + volume_map = { + 'name': ['display_name'], + } + network_map = { + 'name': ['name'], + } def _generic_strip(self, data): res_data = { @@ -28,45 +42,15 @@ class GnocchiTransformer(transformer.BaseTransformer): 'metrics': data['metrics']} return res_data - def _strip_compute(self, data): - res_data = self._generic_strip(data) - res_data.update({ - 'instance_id': data['id'], - 'project_id': data['project_id'], - 'user_id': data['user_id'], - 'name': data['display_name'], - 'flavor_id': data['flavor_id']}) - if 'image_ref' in data: - res_data['image_id'] = data.rpartition['image_ref'][-1] - return res_data - - def _strip_image(self, data): - res_data = self._generic_strip(data) - res_data.update({ - 'container_format': data['container_format'], - 'disk_format': data['disk_format']}) - return res_data - - def _strip_volume(self, data): - res_data = self._generic_strip(data) - res_data.update({ - 'name': data['display_name']}) - return res_data - - def _strip_network(self, data): - res_data = self._generic_strip(data) - res_data.update({ - 'name': data['name']}) - return res_data + @staticmethod + def get_image_id(data): + image_ref = data.get('image_ref', '') + return image_ref.rpartition('/')[-1] or image_ref def strip_resource_data(self, res_type, res_data): - if res_type == 'compute': - return self._strip_compute(res_data) - elif res_type == 'image': - return self._strip_image(res_data) - elif res_type == 'volume': - return self._strip_volume(res_data) - elif res_type.startswith('network.'): - return self._strip_network(res_data) - else: - return self._generic_strip(res_data) + result = self._generic_strip(res_data) + stripped_data = super(GnocchiTransformer, self).strip_resource_data( + res_type, + res_data) + result.update(stripped_data) + return result