Jira ESCALATOR-41:get cluster list from installer
[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 import escalatorclient.v1.clusters
23 from escalatorclient.common import utils
24
25 _bool_strict = functools.partial(strutils.bool_from_string, strict=True)
26
27
28 def _escalator_show(escalator, max_column_width=80):
29     info = copy.deepcopy(escalator._info)
30     exclusive_field = ('deleted', 'deleted_at')
31     for field in exclusive_field:
32         if field in info:
33             info.pop(field)
34     utils.print_dict(info, max_column_width=max_column_width)
35
36
37 @utils.arg('--type', metavar='<TYPE>',
38            help='Type of escalator version, supported type are "internal": '
39                 'the internal version of escalator.')
40 def do_version(dc, args):
41     """Get version of escalator."""
42     fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
43
44     # Filter out values we can't use
45     VERSION_PARAMS = escalatorclient.v1.versions.VERSION_PARAMS
46     fields = dict(filter(lambda x: x[0] in VERSION_PARAMS, fields.items()))
47     version = dc.versions.version(**fields)
48     _escalator_show(version)
49
50
51 @utils.arg('--name', metavar='<NAME>',
52            help='Filter version to those that have this name.')
53 @utils.arg('--status', metavar='<STATUS>',
54            help='Filter version status.')
55 @utils.arg('--type', metavar='<type>',
56            help='Filter by type.')
57 @utils.arg('--version', metavar='<version>',
58            help='Filter by version number.')
59 @utils.arg('--page-size', metavar='<SIZE>', default=None, type=int,
60            help='Number to request in each paginated request.')
61 @utils.arg('--sort-key', default='name',
62            choices=escalatorclient.v1.versions.SORT_KEY_VALUES,
63            help='Sort version list by specified field.')
64 @utils.arg('--sort-dir', default='asc',
65            choices=escalatorclient.v1.versions.SORT_DIR_VALUES,
66            help='Sort version list in specified direction.')
67 def do_cluster_version_list(dc, args):
68     """List hosts you can access."""
69     filter_keys = ['name', 'type', 'status', 'version']
70     filter_items = [(key, getattr(args, key)) for key in filter_keys]
71     filters = dict([item for item in filter_items if item[1] is not None])
72
73     kwargs = {'filters': filters}
74     if args.page_size is not None:
75         kwargs['page_size'] = args.page_size
76
77     kwargs['sort_key'] = args.sort_key
78     kwargs['sort_dir'] = args.sort_dir
79
80     versions = dc.versions.list(**kwargs)
81
82     columns = ['ID', 'NAME', 'TYPE', 'VERSION', 'size',
83                'checksum', 'description', 'status', 'VERSION_PATCH']
84
85     utils.print_list(versions, columns)
86
87
88 @utils.arg('--name', metavar='<NAME>',
89            help='Filter version to those that have this name.')
90 @utils.arg('--status', metavar='<STATUS>',
91            help='Filter version status.')
92 @utils.arg('--type', metavar='<type>',
93            help='Filter by type.')
94 @utils.arg('--version', metavar='<version>',
95            help='Filter by version number.')
96 @utils.arg('--page-size', metavar='<SIZE>', default=None, type=int,
97            help='Number to request in each paginated request.')
98 @utils.arg('--sort-key', default='name',
99            choices=escalatorclient.v1.versions.SORT_KEY_VALUES,
100            help='Sort version list by specified field.')
101 @utils.arg('--sort-dir', default='asc',
102            choices=escalatorclient.v1.versions.SORT_DIR_VALUES,
103            help='Sort version list in specified direction.')
104 def do_cluster_list(gc, args):
105     """List clusters you can access."""
106     filter_keys = ['name']
107     filter_items = [(key, getattr(args, key)) for key in filter_keys]
108     filters = dict([item for item in filter_items if item[1] is not None])
109
110     kwargs = {'filters': filters}
111     if args.page_size is not None:
112         kwargs['page_size'] = args.page_size
113
114     kwargs['sort_key'] = args.sort_key
115     kwargs['sort_dir'] = args.sort_dir
116
117     clusters = gc.clusters.list(**kwargs)
118
119     columns = ['ID', 'Name', 'Description', 'Nodes', 'Networks',
120                'Auto_scale', 'Use_dns', 'Status']
121     utils.print_list(clusters, columns)
122
123
124 @utils.arg('id', metavar='<ID>',
125            help='Filter cluster to those that have this id.')
126 def do_cluster_detail(gc, args):
127     """List cluster you can access."""
128     filter_keys = ['id']
129     filter_items = [(key, getattr(args, key)) for key in filter_keys]
130     filters = dict([item for item in filter_items if item[1] is not None])
131     fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
132     kwargs = {'filters': filters}
133     if filters:
134         cluster = utils.find_resource(gc.clusters, fields.pop('id'))
135         _escalator_show(cluster)
136     else:
137         cluster = gc.clusters.list(**kwargs)
138         columns = ['ID', 'Name', 'Description', 'Nodes',
139                    'Networks', 'Auto_scale', 'Use_dns']
140         utils.print_list(cluster, columns)