1 # Copyright 2012 OpenStack Foundation
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 from __future__ import print_function
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
30 _bool_strict = functools.partial(strutils.bool_from_string, strict=True)
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:
39 utils.print_dict(info, max_column_width=max_column_width)
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()))
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)
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)
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):
81 fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
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()))
87 version = dc.versions.add(**fields)
88 _escalator_show(version)
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):
110 fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
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)
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:
129 print('Requesting version delete for %s ...' %
130 encodeutils.safe_decode(version), end=' ')
131 dc.versions.delete(version)
134 except exc.HTTPException as e:
137 print('%s: Unable to delete version %s' % (e, version))
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])
162 kwargs = {'filters': filters}
163 if args.page_size is not None:
164 kwargs['page_size'] = args.page_size
166 kwargs['sort_key'] = args.sort_key
167 kwargs['sort_dir'] = args.sort_dir
169 versions = dc.versions.list(**kwargs)
171 columns = ['ID', 'NAME', 'TYPE', 'VERSION', 'size',
172 'checksum', 'description', 'status', 'VERSION_PATCH']
174 utils.print_list(versions, columns)
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)