|
| 1 | +# Copyright 2019 Objectif Libre |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# |
| 15 | +from cliff import lister |
| 16 | +from oslo_utils import timeutils |
| 17 | + |
| 18 | +from cloudkittyclient import utils |
| 19 | + |
| 20 | + |
| 21 | +class CliSummaryGet(lister.Lister): |
| 22 | + """Get a summary for a given period.""" |
| 23 | + |
| 24 | + def get_parser(self, prog_name): |
| 25 | + parser = super(CliSummaryGet, self).get_parser(prog_name) |
| 26 | + |
| 27 | + def filter_(elem): |
| 28 | + if len(elem.split(':')) != 2: |
| 29 | + raise TypeError |
| 30 | + return str(elem) |
| 31 | + |
| 32 | + parser.add_argument('--offset', type=int, default=0, |
| 33 | + help='Index of the first element') |
| 34 | + parser.add_argument('--limit', type=int, default=100, |
| 35 | + help='Maximal number of elements') |
| 36 | + parser.add_argument('-g', '--groupby', type=str, action='append', |
| 37 | + help='Attribute to group the summary by. Can be ' |
| 38 | + 'specified several times') |
| 39 | + parser.add_argument('--filter', type=filter_, action='append', |
| 40 | + help="Optional filter, in 'key:value' format. Can " |
| 41 | + "be specified several times.") |
| 42 | + parser.add_argument('-b', '--begin', type=timeutils.parse_isotime, |
| 43 | + help="Start of the period to query, in iso8601 " |
| 44 | + "format. Example: 2019-05-01T00:00:00Z.") |
| 45 | + parser.add_argument('-e', '--end', type=timeutils.parse_isotime, |
| 46 | + help="End of the period to query, in iso8601 " |
| 47 | + "format. Example: 2019-06-01T00:00:00Z.") |
| 48 | + |
| 49 | + return parser |
| 50 | + |
| 51 | + def take_action(self, parsed_args): |
| 52 | + filters = dict(elem.split(':') for elem in (parsed_args.filter or [])) |
| 53 | + resp = utils.get_client_from_osc(self).summary.get_summary( |
| 54 | + offset=parsed_args.offset, |
| 55 | + limit=parsed_args.limit, |
| 56 | + begin=parsed_args.begin, |
| 57 | + end=parsed_args.end, |
| 58 | + filters=filters, |
| 59 | + groupby=parsed_args.groupby, |
| 60 | + ) |
| 61 | + columns = [c.replace('_', ' ').capitalize() for c in resp['columns']] |
| 62 | + return columns, resp['results'] |
0 commit comments