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