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
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
26 _bool_strict = functools.partial(strutils.bool_from_string, strict=True)
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:
35 utils.print_dict(info, max_column_width=max_column_width)
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()))
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)
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)
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):
77 fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
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()))
83 version = dc.versions.add(**fields)
84 _escalator_show(version)
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):
106 fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))
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)
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:
125 print('Requesting version delete for %s ...' %
126 encodeutils.safe_decode(version), end=' ')
127 dc.versions.delete(version)
130 except exc.HTTPException as e:
133 print('%s: Unable to delete version %s' % (e, version))
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_cluster_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])
158 kwargs = {'filters': filters}
159 if args.page_size is not None:
160 kwargs['page_size'] = args.page_size
162 kwargs['sort_key'] = args.sort_key
163 kwargs['sort_dir'] = args.sort_dir
165 versions = dc.versions.list(**kwargs)
167 columns = ['ID', 'NAME', 'TYPE', 'VERSION', 'size',
168 'checksum', 'description', 'status', 'VERSION_PATCH']
170 utils.print_list(versions, columns)
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)