There are some flake8 errors. Clear them before other work.
[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 encodeutils
21 from oslo_utils import strutils
22 import escalatorclient.v1.versions
23 from escalatorclient.common import utils
24 from escalatorclient import exc
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.version.VERSION_PARAMS
47     fields = dict(filter(lambda x: x[0] in VERSION_PARAMS, fields.items()))
48     version = dc.version.version(**fields)
49     _escalator_show(version)
50
51
52 @utils.arg('id', metavar='<ID>',
53            help='Filter version to those that have this id.')
54 def do_version_detail(dc, args):
55     """Get backend_types of escalator."""
56     version = utils.find_resource(dc.versions, args.id)
57     _escalator_show(version)
58
59
60 @utils.arg('name', metavar='<NAME>',
61            help='name of version.')
62 @utils.arg('type', metavar='<TYPE>',
63            help='version type.eg redhat7.0...')
64 @utils.arg('--size', metavar='<SIZE>',
65            help='size of the version file.')
66 @utils.arg('--checksum', metavar='<CHECKSUM>',
67            help='md5 of version file')
68 @utils.arg('--version', metavar='<VERSION>',
69            help='version number of version file')
70 @utils.arg('--description', metavar='<DESCRIPTION>',
71            help='description of version file')
72 @utils.arg('--status', metavar='<STATUS>',
73            help='version file status.default:init')
74 def do_version_add(dc, args):
75     """Add a version."""
76
77     fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
78
79     # Filter out values we can't use
80     CREATE_PARAMS = escalatorclient.v1.versions.CREATE_PARAMS
81     fields = dict(filter(lambda x: x[0] in CREATE_PARAMS, fields.items()))
82
83     version = dc.versions.add(**fields)
84     _escalator_show(version)
85
86
87 @utils.arg('id', metavar='<ID>',
88            help='ID of versions.')
89 @utils.arg('--name', metavar='<NAME>',
90            help='name of version.')
91 @utils.arg('--type', metavar='<TYPE>',
92            help='version type.eg redhat7.0...')
93 @utils.arg('--size', metavar='<SIZE>',
94            help='size of the version file.')
95 @utils.arg('--checksum', metavar='<CHECKSUM>',
96            help='md5 of version file')
97 @utils.arg('--version', metavar='<VERSION>',
98            help='version number of version file')
99 @utils.arg('--description', metavar='<DESCRIPTION>',
100            help='description of version file')
101 @utils.arg('--status', metavar='<STATUS>',
102            help='version file status.default:init')
103 def do_version_update(dc, args):
104     """Add a version."""
105
106     fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
107
108     # Filter out values we can't use
109     CREATE_PARAMS = escalatorclient.v1.versions.CREATE_PARAMS
110     fields = dict(filter(lambda x: x[0] in CREATE_PARAMS, fields.items()))
111     version_id = fields.get('id', None)
112     version = dc.versions.update(version_id, **fields)
113     _escalator_show(version)
114
115
116 @utils.arg('id', metavar='<ID>', nargs='+',
117            help='ID of versions.')
118 def do_version_delete(dc, args):
119     """Delete specified template(s)."""
120     fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
121     versions = fields.get('id', None)
122     for version in versions:
123         try:
124             if args.verbose:
125                 print('Requesting version delete for %s ...' %
126                       encodeutils.safe_decode(version), end=' ')
127             dc.versions.delete(version)
128             if args.verbose:
129                 print('[Done]')
130         except exc.HTTPException as e:
131             if args.verbose:
132                 print('[Fail]')
133             print('%s: Unable to delete version %s' % (e, version))
134
135
136 @utils.arg('--name', metavar='<NAME>',
137            help='Filter version to those that have this name.')
138 @utils.arg('--status', metavar='<STATUS>',
139            help='Filter version status.')
140 @utils.arg('--type', metavar='<type>',
141            help='Filter by type.')
142 @utils.arg('--version', metavar='<version>',
143            help='Filter by version number.')
144 @utils.arg('--page-size', metavar='<SIZE>', default=None, type=int,
145            help='Number to request in each paginated request.')
146 @utils.arg('--sort-key', default='name',
147            choices=escalatorclient.v1.versions.SORT_KEY_VALUES,
148            help='Sort version list by specified field.')
149 @utils.arg('--sort-dir', default='asc',
150            choices=escalatorclient.v1.versions.SORT_DIR_VALUES,
151            help='Sort version list in specified direction.')
152 def do_version_list(dc, args):
153     """List hosts you can access."""
154     filter_keys = ['name', 'type', 'status', 'version']
155     filter_items = [(key, getattr(args, key)) for key in filter_keys]
156     filters = dict([item for item in filter_items if item[1] is not None])
157
158     kwargs = {'filters': filters}
159     if args.page_size is not None:
160         kwargs['page_size'] = args.page_size
161
162     kwargs['sort_key'] = args.sort_key
163     kwargs['sort_dir'] = args.sort_dir
164
165     versions = dc.versions.list(**kwargs)
166
167     columns = ['ID', 'NAME', 'TYPE', 'VERSION', 'size',
168                'checksum', 'description', 'status', 'VERSION_PATCH']
169
170     utils.print_list(versions, columns)
171
172
173 @utils.arg('id', metavar='<ID>',
174            help='Filter version patch to those that have this id.')
175 def do_version_patch_detail(dc, args):
176     """Get version_patch of escalator."""
177     version = utils.find_resource(dc.version_patchs, args.id)
178     _escalator_show(version)