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