401ad76adcfeef68ac840692b0d927a595520603
[escalator.git] / client / escalatorclient / v1 / shell.py
1 # Copyright 2012 OpenStack Foundation
2 # All Rights Reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15
16 from __future__ import print_function
17
18 import copy
19 import functools
20 from oslo_utils import strutils
21 import escalatorclient.v1.versions
22 from escalatorclient.common import utils
23
24 _bool_strict = functools.partial(strutils.bool_from_string, strict=True)
25
26
27 def _escalator_show(escalator, max_column_width=80):
28     info = copy.deepcopy(escalator._info)
29     exclusive_field = ('deleted', 'deleted_at')
30     for field in exclusive_field:
31         if field in info:
32             info.pop(field)
33     utils.print_dict(info, max_column_width=max_column_width)
34
35
36 @utils.arg('--type', metavar='<TYPE>',
37            help='Type of escalator version, supported type are "internal": '
38                 'the internal version of escalator.')
39 def do_version(dc, args):
40     """Get version of escalator."""
41     fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
42
43     # Filter out values we can't use
44     VERSION_PARAMS = escalatorclient.v1.versions.VERSION_PARAMS
45     fields = dict(filter(lambda x: x[0] in VERSION_PARAMS, fields.items()))
46     version = dc.versions.version(**fields)
47     _escalator_show(version)
48
49
50 @utils.arg('--name', metavar='<NAME>',
51            help='Filter version to those that have this name.')
52 @utils.arg('--status', metavar='<STATUS>',
53            help='Filter version status.')
54 @utils.arg('--type', metavar='<type>',
55            help='Filter by type.')
56 @utils.arg('--version', metavar='<version>',
57            help='Filter by version number.')
58 @utils.arg('--page-size', metavar='<SIZE>', default=None, type=int,
59            help='Number to request in each paginated request.')
60 @utils.arg('--sort-key', default='name',
61            choices=escalatorclient.v1.versions.SORT_KEY_VALUES,
62            help='Sort version list by specified field.')
63 @utils.arg('--sort-dir', default='asc',
64            choices=escalatorclient.v1.versions.SORT_DIR_VALUES,
65            help='Sort version list in specified direction.')
66 def do_cluster_version_list(dc, args):
67     """List hosts you can access."""
68     filter_keys = ['name', 'type', 'status', 'version']
69     filter_items = [(key, getattr(args, key)) for key in filter_keys]
70     filters = dict([item for item in filter_items if item[1] is not None])
71
72     kwargs = {'filters': filters}
73     if args.page_size is not None:
74         kwargs['page_size'] = args.page_size
75
76     kwargs['sort_key'] = args.sort_key
77     kwargs['sort_dir'] = args.sort_dir
78
79     versions = dc.versions.list(**kwargs)
80
81     columns = ['ID', 'NAME', 'TYPE', 'VERSION', 'size',
82                'checksum', 'description', 'status', 'VERSION_PATCH']
83
84     utils.print_list(versions, columns)