From 56e6f689bb6f8ebc415623034ad83bc3e799f520 Mon Sep 17 00:00:00 2001 From: Aaron-DH Date: Mon, 23 Jan 2017 19:01:39 +0800 Subject: [PATCH] Add `--all-tenants` when get total/summary Use `cloudkitty total-get --all-tenants` to get total rate of all_tenants. Same with summary-get Depends-On: 8cf7332162ad30bcdb2c8dfd10a3d348601c2870 Change-Id: I1efcbb8eff77c5f8d358a02178b1f99204b6cba7 --- cloudkittyclient/v1/report/__init__.py | 9 +++++++-- cloudkittyclient/v1/report/shell.py | 18 ++++++++++++++++-- cloudkittyclient/v1/report/shell_cli.py | 12 ++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/cloudkittyclient/v1/report/__init__.py b/cloudkittyclient/v1/report/__init__.py index 550692a..0dbba8f 100644 --- a/cloudkittyclient/v1/report/__init__.py +++ b/cloudkittyclient/v1/report/__init__.py @@ -38,7 +38,8 @@ class ReportManager(base.CrudManager): def list_tenants(self): return self.client.get(self.base_url + "/tenants").json() - def get_total(self, tenant_id=None, begin=None, end=None, service=None): + def get_total(self, tenant_id=None, begin=None, end=None, + service=None, all_tenants=False): url = self.base_url + "/total" filters = list() if tenant_id: @@ -49,6 +50,8 @@ class ReportManager(base.CrudManager): filters.append("end=%s" % end.isoformat()) if service: filters.append("service=%s" % service) + if all_tenants: + filters.append("all_tenants=%s" % all_tenants) if filters: url += "?%s" % ('&'.join(filters)) return self.client.get(url).json() @@ -61,7 +64,7 @@ class ReportSummaryManager(ReportManager): collection_key = "summary" def get_summary(self, tenant_id=None, begin=None, end=None, - service=None, groupby=None): + service=None, groupby=None, all_tenants=False): kwargs = {} if tenant_id: kwargs['tenant_id'] = tenant_id @@ -73,4 +76,6 @@ class ReportSummaryManager(ReportManager): kwargs['service'] = service if groupby: kwargs['groupby'] = groupby + if all_tenants: + kwargs['all_tenants'] = all_tenants return super(ReportManager, self).list(**kwargs) diff --git a/cloudkittyclient/v1/report/shell.py b/cloudkittyclient/v1/report/shell.py index e02adb6..19e8146 100644 --- a/cloudkittyclient/v1/report/shell.py +++ b/cloudkittyclient/v1/report/shell.py @@ -39,6 +39,12 @@ def do_report_tenant_list(cc, args): @utils.arg('-s', '--service', help='Service Type', required=False) +@utils.arg('-a', '--all-tenants', + default=False, + action="store_true", + dest='all_tenants', + help='Allows to get total from all tenants' + ' (admin only).') def do_total_get(cc, args): """Get total reports.""" begin = utils.iso2dt(args.begin) if args.begin else None @@ -46,7 +52,8 @@ def do_total_get(cc, args): total = cc.reports.get_total(tenant_id=args.total_tenant_id, begin=begin, end=end, - service=args.service) + service=args.service, + all_tenants=args.all_tenants) utils.print_dict({'Total': total or 0.0}) @@ -67,6 +74,12 @@ def do_total_get(cc, args): help=('Fields to groupby, separated by commas ' 'if multiple, now support res_type,tenant_id'), required=False) +@utils.arg('-a', '--all-tenants', + default=False, + action="store_true", + dest='all_tenants', + help='Allows to get summary from all tenants' + ' (admin only).') def do_summary_get(cc, args): """Get summary report.""" begin = utils.ts2dt(args.begin) if args.begin else None @@ -75,7 +88,8 @@ def do_summary_get(cc, args): begin=begin, end=end, service=args.service, - groupby=args.groupby) + groupby=args.groupby, + all_tenants=args.all_tenants) field_labels = ['Tenant ID', 'Resource Type', 'Rate', 'Begin Time', 'End Time'] fields = ['tenant_id', 'res_type', 'rate', 'begin', 'end'] diff --git a/cloudkittyclient/v1/report/shell_cli.py b/cloudkittyclient/v1/report/shell_cli.py index 8f5da0f..9f4649c 100644 --- a/cloudkittyclient/v1/report/shell_cli.py +++ b/cloudkittyclient/v1/report/shell_cli.py @@ -35,6 +35,12 @@ class CliTotalGet(command.Command): parser.add_argument('-s', '--service', help='Service Type', required=False) + parser.add_argument('-a', '--all-tenants', + default=False, + action="store_true", + dest='all_tenants', + help='Allows to get total from all tenants' + ' (admin only).') return parser def take_action(self, parsed_args): @@ -69,6 +75,12 @@ class CliSummaryGet(command.Command): 'commas if multiple, now support ' 'res_type,tenant_id'), required=False) + parser.add_argument('-a', '--all-tenants', + default=False, + action="store_true", + dest='all_tenants', + help='Allows to get summary from all tenants' + ' (admin only).') return parser def take_action(self, parsed_args):