|
| 1 | +# Copyright 2013 OpenStack, LLC. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# |
| 15 | + |
| 16 | +"""Hypervisor action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +from cliff import lister |
| 21 | +from cliff import show |
| 22 | + |
| 23 | +from openstackclient.common import utils |
| 24 | + |
| 25 | + |
| 26 | +class ListHypervisor(lister.Lister): |
| 27 | + """List hypervisor command""" |
| 28 | + |
| 29 | + api = "compute" |
| 30 | + log = logging.getLogger(__name__ + ".ListHypervisor") |
| 31 | + |
| 32 | + def get_parser(self, prog_name): |
| 33 | + parser = super(ListHypervisor, self).get_parser(prog_name) |
| 34 | + parser.add_argument( |
| 35 | + "--matching", |
| 36 | + metavar="<hostname>", |
| 37 | + help="List hypervisors with hostnames matching the given" |
| 38 | + " substring") |
| 39 | + return parser |
| 40 | + |
| 41 | + def take_action(self, parsed_args): |
| 42 | + self.log.debug("take_action(%s)" % parsed_args) |
| 43 | + compute_client = self.app.client_manager.compute |
| 44 | + columns = ( |
| 45 | + "ID", |
| 46 | + "Hypervisor Hostname" |
| 47 | + ) |
| 48 | + |
| 49 | + if parsed_args.matching: |
| 50 | + data = compute_client.hypervisors.search(parsed_args.matching) |
| 51 | + else: |
| 52 | + data = compute_client.hypervisors.list() |
| 53 | + |
| 54 | + return (columns, |
| 55 | + (utils.get_item_properties( |
| 56 | + s, columns, |
| 57 | + ) for s in data)) |
| 58 | + |
| 59 | + |
| 60 | +class ShowHypervisor(show.ShowOne): |
| 61 | + """Show hypervisor command""" |
| 62 | + |
| 63 | + api = "compute" |
| 64 | + log = logging.getLogger(__name__ + ".ShowHypervisor") |
| 65 | + |
| 66 | + def get_parser(self, prog_name): |
| 67 | + parser = super(ShowHypervisor, self).get_parser(prog_name) |
| 68 | + parser.add_argument( |
| 69 | + "id", |
| 70 | + metavar="<id>", |
| 71 | + help="ID of the hypervisor to display") |
| 72 | + return parser |
| 73 | + |
| 74 | + def take_action(self, parsed_args): |
| 75 | + self.log.debug("take_action(%s)" % parsed_args) |
| 76 | + compute_client = self.app.client_manager.compute |
| 77 | + hypervisor = utils.find_resource(compute_client.hypervisors, |
| 78 | + parsed_args.id)._info.copy() |
| 79 | + |
| 80 | + hypervisor["service_id"] = hypervisor["service"]["id"] |
| 81 | + hypervisor["service_host"] = hypervisor["service"]["host"] |
| 82 | + del hypervisor["service"] |
| 83 | + |
| 84 | + return zip(*sorted(hypervisor.iteritems())) |
0 commit comments