Merge "Add client for get summary report"

This commit is contained in:
Jenkins 2017-01-18 14:32:18 +00:00 committed by Gerrit Code Review
commit a567600b77
6 changed files with 235 additions and 4 deletions

View File

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

View File

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

View File

@ -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 "<Report %s>" % self._info
return "<Summary %s" % self._info
class ReportManager(base.Manager):
class ReportManager(base.CrudManager):
base_url = "/v1/report"
@ -44,3 +52,25 @@ class ReportManager(base.Manager):
if filters:
url += "?%s" % ('&'.join(filters))
return self.client.get(url).json()
class ReportSummaryManager(ReportManager):
resource_class = ReportSummary
key = 'summary'
collection_key = "summary"
def get_summary(self, tenant_id=None, begin=None, end=None,
service=None, groupby=None):
kwargs = {}
if tenant_id:
kwargs['tenant_id'] = tenant_id
if begin:
kwargs['begin'] = begin.isoformat()
if end:
kwargs['end'] = end.isoformat()
if service:
kwargs['service'] = service
if groupby:
kwargs['groupby'] = groupby
return super(ReportManager, self).list(**kwargs)

View File

@ -48,3 +48,35 @@ def do_total_get(cc, args):
end=end,
service=args.service)
utils.print_dict({'Total': total or 0.0})
@utils.arg('-t', '--tenant-id',
help='Tenant id',
required=False,
dest='summary_tenant_id')
@utils.arg('-b', '--begin',
help='Begin timestamp',
required=False)
@utils.arg('-e', '--end',
help='End timestamp',
required=False)
@utils.arg('-s', '--service',
help='Service Type',
required=False)
@utils.arg('-g', '--groupby',
help=('Fields to groupby, separated by commas '
'if multiple, now support res_type,tenant_id'),
required=False)
def do_summary_get(cc, args):
"""Get summary report."""
begin = utils.ts2dt(args.begin) if args.begin else None
end = utils.ts2dt(args.end) if args.end else None
summarys = cc.reportsummary.get_summary(tenant_id=args.summary_tenant_id,
begin=begin,
end=end,
service=args.service,
groupby=args.groupby)
field_labels = ['Tenant ID', 'Resource Type', 'Rate',
'Begin Time', 'End Time']
fields = ['tenant_id', 'res_type', 'rate', 'begin', 'end']
utils.print_list(summarys, fields, field_labels, sortby=0)

View File

@ -46,3 +46,31 @@ class CliReportTenantList(command.Command):
def take_action(self, parsed_args):
ckclient = self.app.client_manager.rating
shell.do_report_tenant_list(ckclient, parsed_args)
class CliSummaryGet(command.Command):
def get_parser(self, prog_name):
parser = super(CliSummaryGet, self).get_parser(prog_name)
parser.add_argument('-t', '--tenant-id',
help='Tenant id',
required=False,
dest='summary_tenant_id')
parser.add_argument('-b', '--begin',
help='Begin timestamp',
required=False)
parser.add_argument('-e', '--end',
help='End timestamp',
required=False)
parser.add_argument('-s', '--service',
help='Service Type',
required=False)
parser.add_argument('-g', '--groupby',
help=('Fields to groupby, separated by '
'commas if multiple, now support '
'res_type,tenant_id'),
required=False)
return parser
def take_action(self, parsed_args):
ckclient = self.app.client_manager.rating
shell.do_summary_get(ckclient, parsed_args)

View File

@ -40,6 +40,7 @@ openstack.rating.v1 =
rating_module-set-priority = cloudkittyclient.v1.shell_cli:CliModuleSetPriority
rating_total-get = cloudkittyclient.v1.report.shell_cli:CliTotalGet
rating_summary-get = cloudkittyclient.v1.report.shell_cli:CliSummaryGet
rating_report-tenant-list = cloudkittyclient.v1.report.shell_cli:CliReportTenantList
rating_collector-mapping-list = cloudkittyclient.v1.collector.shell_cli:CliCollectorMappingList