From e402ce676c24a32dbc4e2721759e97e905cf8129 Mon Sep 17 00:00:00 2001 From: Aaron-DH Date: Sun, 15 Jan 2017 11:07:12 +0800 Subject: [PATCH] Add client for get summary report Use commands as follows to get summary: -- cloudkitty summary-get -- openstack rating summary-get Change-Id: I07da26cb31a03104493ab749efffd73ba8d17d62 Implements: blueprint price-groupby-fields --- cloudkittyclient/tests/v1/test_report.py | 139 +++++++++++++++++++++++ cloudkittyclient/v1/client.py | 1 + cloudkittyclient/v1/report/__init__.py | 38 ++++++- cloudkittyclient/v1/report/shell.py | 32 ++++++ cloudkittyclient/v1/report/shell_cli.py | 28 +++++ setup.cfg | 1 + 6 files changed, 235 insertions(+), 4 deletions(-) create mode 100644 cloudkittyclient/tests/v1/test_report.py diff --git a/cloudkittyclient/tests/v1/test_report.py b/cloudkittyclient/tests/v1/test_report.py new file mode 100644 index 0000000..a0eccee --- /dev/null +++ b/cloudkittyclient/tests/v1/test_report.py @@ -0,0 +1,139 @@ +# Copyright 2015 Objectif Libre +# All Rights Reserved. +# +# 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. +from cloudkittyclient.apiclient import client +from cloudkittyclient.apiclient import fake_client +from cloudkittyclient.tests import utils +import cloudkittyclient.v1.report + + +fixtures = { + '/v1/report/summary': { + 'GET': ( + {}, + {'summary': [ + { + 'tenant_id': 'ALL', + 'res_type': 'ALL', + 'begin': '2017-01-01T00:00:00', + 'end': '2017-02-01T00:00:00', + 'rate': '2325.29992' + }, + ]}, + ), + }, + '/v1/report/summary?tenant_id=649de47ad78a44bd8562b0aa84389b2b': { + 'GET': ( + {}, + {'summary': [ + { + 'tenant_id': '649de47ad78a44bd8562b0aa84389b2b', + 'res_type': 'ALL', + 'begin': '2017-01-01T00:00:00', + 'end': '2017-02-01T00:00:00', + 'rate': '990.14996' + }, + ]}, + ), + }, + '/v1/report/summary?service=compute': { + 'GET': ( + {}, + {'summary': [ + { + 'tenant_id': 'ALL', + 'res_type': 'compute', + 'begin': '2017-01-01T00:00:00', + 'end': '2017-02-01T00:00:00', + 'rate': '690.0' + }, + ]}, + ), + }, + '/v1/report/summary?groupby=res_type%2Ctenant_id': { + 'GET': ( + {}, + {'summary': [ + { + 'tenant_id': '3747afc360b64702a53bdd64dc1b8976', + 'res_type': 'compute', + 'begin': '2017-01-01T00:00:00', + 'end': '2017-02-01T00:00:00', + 'rate': '517.5' + }, + { + 'tenant_id': '3747afc360b64702a53bdd64dc1b8976', + 'res_type': 'volume', + 'begin': '2017-01-01T00:00:00', + 'end': '2017-02-01T00:00:00', + 'rate': '817.64996' + }, + { + 'tenant_id': '649de47ad78a44bd8562b0aa84389b2b', + 'res_type': 'compute', + 'begin': '2017-01-01T00:00:00', + 'end': '2017-02-01T00:00:00', + 'rate': '172.5' + }, + { + 'tenant_id': '649de47ad78a44bd8562b0aa84389b2b', + 'res_type': 'volume', + 'begin': '2017-01-01T00:00:00', + 'end': '2017-02-01T00:00:00', + 'rate': '817.64996' + }, + ]}, + ), + }, +} + + +class ReportSummaryManagerTest(utils.BaseTestCase): + + def setUp(self): + super(ReportSummaryManagerTest, self).setUp() + self.http_client = fake_client.FakeHTTPClient(fixtures=fixtures) + self.api = client.BaseClient(self.http_client) + self.mgr = cloudkittyclient.v1.report.ReportSummaryManager(self.api) + + def test_get_summary(self): + self.mgr.get_summary() + expect = [ + 'GET', '/v1/report/summary' + ] + self.http_client.assert_called(*expect) + + def test_get_summary_with_tenant(self): + self.mgr.get_summary(tenant_id='649de47ad78a44bd8562b0aa84389b2b') + expect = [ + 'GET', + '/v1/report/summary?tenant_id=649de47ad78a44bd8562b0aa84389b2b' + ] + self.http_client.assert_called(*expect) + + def test_get_summary_with_service(self): + self.mgr.get_summary(service='compute') + expect = [ + 'GET', + '/v1/report/summary?service=compute' + ] + self.http_client.assert_called(*expect) + + def test_get_summary_with_groupby(self): + self.mgr.get_summary(groupby='res_type,tenant_id') + expect = [ + 'GET', + '/v1/report/summary?groupby=res_type%2Ctenant_id' + ] + self.http_client.assert_called(*expect) diff --git a/cloudkittyclient/v1/client.py b/cloudkittyclient/v1/client.py index d1e4e64..2126e29 100644 --- a/cloudkittyclient/v1/client.py +++ b/cloudkittyclient/v1/client.py @@ -57,6 +57,7 @@ class Client(object): self.modules = core.CloudkittyModuleManager(self.http_client) self.collector = collector.CollectorManager(self.http_client) self.reports = report.ReportManager(self.http_client) + self.reportsummary = report.ReportSummaryManager(self.http_client) self.quotations = core.QuotationManager(self.http_client) self.storage = storage.StorageManager(self.http_client) self._expose_submodules() diff --git a/cloudkittyclient/v1/report/__init__.py b/cloudkittyclient/v1/report/__init__.py index 50c86d8..550692a 100644 --- a/cloudkittyclient/v1/report/__init__.py +++ b/cloudkittyclient/v1/report/__init__.py @@ -15,15 +15,23 @@ from cloudkittyclient.common import base -class ReportResult(base.Resource): +class ReportSummary(base.Resource): - key = 'report' + key = 'summary' + + def __init(self, tenant_id=None, res_type=None, begin=None, + end=None, rate=None): + self.tenant_id = tenant_id + self.res_type = res_type + self.begin = begin + self.end = end + self.rate = rate def __repr__(self): - return "" % self._info + return "