|
| 1 | +# Copyright 2012 OpenStack Foundation |
| 2 | +# Copyright 2013 Nebula Inc. |
| 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 | + |
| 17 | +"""Compute v2 Aggregate action implementations""" |
| 18 | + |
| 19 | +import logging |
| 20 | +import six |
| 21 | + |
| 22 | +from cliff import command |
| 23 | +from cliff import lister |
| 24 | +from cliff import show |
| 25 | + |
| 26 | +from openstackclient.common import parseractions |
| 27 | +from openstackclient.common import utils |
| 28 | + |
| 29 | + |
| 30 | +class AddAggregateHost(show.ShowOne): |
| 31 | + """Add host to aggregate""" |
| 32 | + |
| 33 | + log = logging.getLogger(__name__ + '.AddAggregateHost') |
| 34 | + |
| 35 | + def get_parser(self, prog_name): |
| 36 | + parser = super(AddAggregateHost, self).get_parser(prog_name) |
| 37 | + parser.add_argument( |
| 38 | + 'aggregate', |
| 39 | + metavar='<aggregate>', |
| 40 | + help='Name or ID of aggregate', |
| 41 | + ) |
| 42 | + parser.add_argument( |
| 43 | + 'host', |
| 44 | + metavar='<host>', |
| 45 | + help='Host to add to aggregate', |
| 46 | + ) |
| 47 | + return parser |
| 48 | + |
| 49 | + def take_action(self, parsed_args): |
| 50 | + self.log.debug("take_action(%s)" % parsed_args) |
| 51 | + |
| 52 | + compute_client = self.app.client_manager.compute |
| 53 | + |
| 54 | + aggregate = utils.find_resource( |
| 55 | + compute_client.aggregates, |
| 56 | + parsed_args.aggregate, |
| 57 | + ) |
| 58 | + data = compute_client.aggregates.add_host(aggregate, parsed_args.host) |
| 59 | + |
| 60 | + info = {} |
| 61 | + info.update(data._info) |
| 62 | + return zip(*sorted(six.iteritems(info))) |
| 63 | + |
| 64 | + |
| 65 | +class CreateAggregate(show.ShowOne): |
| 66 | + """Create a new aggregate""" |
| 67 | + |
| 68 | + log = logging.getLogger(__name__ + ".CreateAggregate") |
| 69 | + |
| 70 | + def get_parser(self, prog_name): |
| 71 | + parser = super(CreateAggregate, self).get_parser(prog_name) |
| 72 | + parser.add_argument( |
| 73 | + "name", |
| 74 | + metavar="<name>", |
| 75 | + help="New aggregate name", |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + "--zone", |
| 79 | + metavar="<availability-zone>", |
| 80 | + help="Availability zone name", |
| 81 | + ) |
| 82 | + parser.add_argument( |
| 83 | + "--property", |
| 84 | + metavar="<key=value>", |
| 85 | + action=parseractions.KeyValueAction, |
| 86 | + help='Property to add to this aggregate ' |
| 87 | + '(repeat option to set multiple properties)', |
| 88 | + ) |
| 89 | + return parser |
| 90 | + |
| 91 | + def take_action(self, parsed_args): |
| 92 | + self.log.debug("take_action(%s)" % parsed_args) |
| 93 | + |
| 94 | + compute_client = self.app.client_manager.compute |
| 95 | + |
| 96 | + info = {} |
| 97 | + data = compute_client.aggregates.create( |
| 98 | + parsed_args.name, |
| 99 | + parsed_args.zone, |
| 100 | + ) |
| 101 | + info.update(data._info) |
| 102 | + |
| 103 | + if parsed_args.property: |
| 104 | + info.update(compute_client.aggregates.set_metadata( |
| 105 | + data, |
| 106 | + parsed_args.property, |
| 107 | + )._info) |
| 108 | + |
| 109 | + return zip(*sorted(six.iteritems(info))) |
| 110 | + |
| 111 | + |
| 112 | +class DeleteAggregate(command.Command): |
| 113 | + """Delete an existing aggregate""" |
| 114 | + |
| 115 | + log = logging.getLogger(__name__ + '.DeleteAggregate') |
| 116 | + |
| 117 | + def get_parser(self, prog_name): |
| 118 | + parser = super(DeleteAggregate, self).get_parser(prog_name) |
| 119 | + parser.add_argument( |
| 120 | + 'aggregate', |
| 121 | + metavar='<aggregate>', |
| 122 | + help='Name or ID of aggregate to delete', |
| 123 | + ) |
| 124 | + return parser |
| 125 | + |
| 126 | + def take_action(self, parsed_args): |
| 127 | + self.log.debug('take_action(%s)' % parsed_args) |
| 128 | + |
| 129 | + compute_client = self.app.client_manager.compute |
| 130 | + data = utils.find_resource( |
| 131 | + compute_client.aggregates, |
| 132 | + parsed_args.aggregate, |
| 133 | + ) |
| 134 | + compute_client.aggregates.delete(data.id) |
| 135 | + return |
| 136 | + |
| 137 | + |
| 138 | +class ListAggregate(lister.Lister): |
| 139 | + """List all aggregates""" |
| 140 | + |
| 141 | + log = logging.getLogger(__name__ + ".ListAggregate") |
| 142 | + |
| 143 | + def get_parser(self, prog_name): |
| 144 | + parser = super(ListAggregate, self).get_parser(prog_name) |
| 145 | + parser.add_argument( |
| 146 | + '--long', |
| 147 | + action='store_true', |
| 148 | + default=False, |
| 149 | + help='List additional fields in output') |
| 150 | + return parser |
| 151 | + |
| 152 | + def take_action(self, parsed_args): |
| 153 | + self.log.debug("take_action(%s)" % parsed_args) |
| 154 | + |
| 155 | + compute_client = self.app.client_manager.compute |
| 156 | + |
| 157 | + data = compute_client.aggregates.list() |
| 158 | + |
| 159 | + if parsed_args.long: |
| 160 | + # Remove availability_zone from metadata because Nova doesn't |
| 161 | + for d in data: |
| 162 | + if 'availability_zone' in d.metadata: |
| 163 | + d.metadata.pop('availability_zone') |
| 164 | + # This is the easiest way to change column headers |
| 165 | + column_headers = ( |
| 166 | + "ID", |
| 167 | + "Name", |
| 168 | + "Availability Zone", |
| 169 | + "Properties", |
| 170 | + ) |
| 171 | + columns = ( |
| 172 | + "ID", |
| 173 | + "Name", |
| 174 | + "Availability Zone", |
| 175 | + "Metadata", |
| 176 | + ) |
| 177 | + else: |
| 178 | + column_headers = columns = ( |
| 179 | + "ID", |
| 180 | + "Name", |
| 181 | + "Availability Zone", |
| 182 | + ) |
| 183 | + |
| 184 | + return (column_headers, |
| 185 | + (utils.get_item_properties( |
| 186 | + s, columns, |
| 187 | + ) for s in data)) |
| 188 | + |
| 189 | + |
| 190 | +class RemoveAggregateHost(show.ShowOne): |
| 191 | + """Remove host from aggregate""" |
| 192 | + |
| 193 | + log = logging.getLogger(__name__ + '.RemoveAggregateHost') |
| 194 | + |
| 195 | + def get_parser(self, prog_name): |
| 196 | + parser = super(RemoveAggregateHost, self).get_parser(prog_name) |
| 197 | + parser.add_argument( |
| 198 | + 'aggregate', |
| 199 | + metavar='<aggregate>', |
| 200 | + help='Name or ID of aggregate', |
| 201 | + ) |
| 202 | + parser.add_argument( |
| 203 | + 'host', |
| 204 | + metavar='<host>', |
| 205 | + help='Host to remove from aggregate', |
| 206 | + ) |
| 207 | + return parser |
| 208 | + |
| 209 | + def take_action(self, parsed_args): |
| 210 | + self.log.debug("take_action(%s)" % parsed_args) |
| 211 | + |
| 212 | + compute_client = self.app.client_manager.compute |
| 213 | + |
| 214 | + aggregate = utils.find_resource( |
| 215 | + compute_client.aggregates, |
| 216 | + parsed_args.aggregate, |
| 217 | + ) |
| 218 | + data = compute_client.aggregates.remove_host( |
| 219 | + aggregate, |
| 220 | + parsed_args.host, |
| 221 | + ) |
| 222 | + |
| 223 | + info = {} |
| 224 | + info.update(data._info) |
| 225 | + return zip(*sorted(six.iteritems(info))) |
| 226 | + |
| 227 | + |
| 228 | +class SetAggregate(show.ShowOne): |
| 229 | + """Set aggregate properties""" |
| 230 | + |
| 231 | + log = logging.getLogger(__name__ + '.SetAggregate') |
| 232 | + |
| 233 | + def get_parser(self, prog_name): |
| 234 | + parser = super(SetAggregate, self).get_parser(prog_name) |
| 235 | + parser.add_argument( |
| 236 | + 'aggregate', |
| 237 | + metavar='<aggregate>', |
| 238 | + help='Name or ID of aggregate to display', |
| 239 | + ) |
| 240 | + parser.add_argument( |
| 241 | + '--name', |
| 242 | + metavar='<new-name>', |
| 243 | + help='New aggregate name', |
| 244 | + ) |
| 245 | + parser.add_argument( |
| 246 | + "--zone", |
| 247 | + metavar="<availability-zone>", |
| 248 | + help="Availability zone name", |
| 249 | + ) |
| 250 | + parser.add_argument( |
| 251 | + "--property", |
| 252 | + metavar="<key=value>", |
| 253 | + action=parseractions.KeyValueAction, |
| 254 | + help='Property to add/change for this aggregate ' |
| 255 | + '(repeat option to set multiple properties)', |
| 256 | + ) |
| 257 | + return parser |
| 258 | + |
| 259 | + def take_action(self, parsed_args): |
| 260 | + self.log.debug('take_action(%s)' % parsed_args) |
| 261 | + |
| 262 | + compute_client = self.app.client_manager.compute |
| 263 | + aggregate = utils.find_resource( |
| 264 | + compute_client.aggregates, |
| 265 | + parsed_args.aggregate, |
| 266 | + ) |
| 267 | + |
| 268 | + info = {} |
| 269 | + kwargs = {} |
| 270 | + if parsed_args.name: |
| 271 | + kwargs['name'] = parsed_args.name |
| 272 | + if parsed_args.zone: |
| 273 | + kwargs['availability_zone'] = parsed_args.zone |
| 274 | + if kwargs: |
| 275 | + info.update(compute_client.aggregates.update( |
| 276 | + aggregate, |
| 277 | + kwargs |
| 278 | + )._info) |
| 279 | + |
| 280 | + if parsed_args.property: |
| 281 | + info.update(compute_client.aggregates.set_metadata( |
| 282 | + aggregate, |
| 283 | + parsed_args.property, |
| 284 | + )._info) |
| 285 | + |
| 286 | + if info: |
| 287 | + return zip(*sorted(six.iteritems(info))) |
| 288 | + else: |
| 289 | + return ({}, {}) |
| 290 | + |
| 291 | + |
| 292 | +class ShowAggregate(show.ShowOne): |
| 293 | + """Show a specific aggregate""" |
| 294 | + |
| 295 | + log = logging.getLogger(__name__ + '.ShowAggregate') |
| 296 | + |
| 297 | + def get_parser(self, prog_name): |
| 298 | + parser = super(ShowAggregate, self).get_parser(prog_name) |
| 299 | + parser.add_argument( |
| 300 | + 'aggregate', |
| 301 | + metavar='<aggregate>', |
| 302 | + help='Name or ID of aggregate to display', |
| 303 | + ) |
| 304 | + return parser |
| 305 | + |
| 306 | + def take_action(self, parsed_args): |
| 307 | + self.log.debug('take_action(%s)' % parsed_args) |
| 308 | + |
| 309 | + compute_client = self.app.client_manager.compute |
| 310 | + data = utils.find_resource( |
| 311 | + compute_client.aggregates, |
| 312 | + parsed_args.aggregate, |
| 313 | + ) |
| 314 | + # Remove availability_zone from metadata because Nova doesn't |
| 315 | + if 'availability_zone' in data.metadata: |
| 316 | + data.metadata.pop('availability_zone') |
| 317 | + # Map 'metadata' column to 'properties' |
| 318 | + data._info.update({'properties': data._info.pop('metadata')}) |
| 319 | + |
| 320 | + info = {} |
| 321 | + info.update(data._info) |
| 322 | + return zip(*sorted(six.iteritems(info))) |
0 commit comments