Merge "Refactor gnocchi transformer"

This commit is contained in:
Jenkins 2016-06-14 13:19:48 +00:00 committed by Gerrit Code Review
commit da2e695c6e
2 changed files with 139 additions and 42 deletions

View File

@ -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)

View File

@ -17,8 +17,22 @@ from cloudkitty import transformer
class GnocchiTransformer(transformer.BaseTransformer): class GnocchiTransformer(transformer.BaseTransformer):
def __init__(self): compute_map = {
pass '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): def _generic_strip(self, data):
res_data = { res_data = {
@ -28,45 +42,15 @@ class GnocchiTransformer(transformer.BaseTransformer):
'metrics': data['metrics']} 'metrics': data['metrics']}
return res_data return res_data
def _strip_compute(self, data): @staticmethod
res_data = self._generic_strip(data) def get_image_id(data):
res_data.update({ image_ref = data.get('image_ref', '')
'instance_id': data['id'], return image_ref.rpartition('/')[-1] or image_ref
'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
def strip_resource_data(self, res_type, res_data): def strip_resource_data(self, res_type, res_data):
if res_type == 'compute': result = self._generic_strip(res_data)
return self._strip_compute(res_data) stripped_data = super(GnocchiTransformer, self).strip_resource_data(
elif res_type == 'image': res_type,
return self._strip_image(res_data) res_data)
elif res_type == 'volume': result.update(stripped_data)
return self._strip_volume(res_data) return result
elif res_type.startswith('network.'):
return self._strip_network(res_data)
else:
return self._generic_strip(res_data)