|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +"""Compute v2 Availability Zone action implementations""" |
| 15 | + |
| 16 | +import copy |
| 17 | +import logging |
| 18 | + |
| 19 | +from cliff import lister |
| 20 | +from novaclient import exceptions as nova_exceptions |
| 21 | +import six |
| 22 | + |
| 23 | +from openstackclient.common import utils |
| 24 | +from openstackclient.i18n import _ # noqa |
| 25 | + |
| 26 | + |
| 27 | +def _xform_availability_zone(az, include_extra): |
| 28 | + result = [] |
| 29 | + zone_info = {} |
| 30 | + if hasattr(az, 'zoneState'): |
| 31 | + zone_info['zone_status'] = ('available' if az.zoneState['available'] |
| 32 | + else 'not available') |
| 33 | + if hasattr(az, 'zoneName'): |
| 34 | + zone_info['zone_name'] = az.zoneName |
| 35 | + |
| 36 | + if not include_extra: |
| 37 | + result.append(zone_info) |
| 38 | + return result |
| 39 | + |
| 40 | + if hasattr(az, 'hosts') and az.hosts: |
| 41 | + for host, services in six.iteritems(az.hosts): |
| 42 | + host_info = copy.deepcopy(zone_info) |
| 43 | + host_info['host_name'] = host |
| 44 | + |
| 45 | + for svc, state in six.iteritems(services): |
| 46 | + info = copy.deepcopy(host_info) |
| 47 | + info['service_name'] = svc |
| 48 | + info['service_status'] = '%s %s %s' % ( |
| 49 | + 'enabled' if state['active'] else 'disabled', |
| 50 | + ':-)' if state['available'] else 'XXX', |
| 51 | + state['updated_at']) |
| 52 | + result.append(info) |
| 53 | + else: |
| 54 | + zone_info['host_name'] = '' |
| 55 | + zone_info['service_name'] = '' |
| 56 | + zone_info['service_status'] = '' |
| 57 | + result.append(zone_info) |
| 58 | + return result |
| 59 | + |
| 60 | + |
| 61 | +class ListAvailabilityZone(lister.Lister): |
| 62 | + """List Availability Zones and their status""" |
| 63 | + |
| 64 | + log = logging.getLogger(__name__ + '.ListAvailabilityZone') |
| 65 | + |
| 66 | + def get_parser(self, prog_name): |
| 67 | + parser = super(ListAvailabilityZone, self).get_parser(prog_name) |
| 68 | + parser.add_argument( |
| 69 | + '--long', |
| 70 | + action='store_true', |
| 71 | + default=False, |
| 72 | + help=_('List additional fields in output'), |
| 73 | + ) |
| 74 | + return parser |
| 75 | + |
| 76 | + def take_action(self, parsed_args): |
| 77 | + self.log.debug('take_action(%s)', parsed_args) |
| 78 | + |
| 79 | + if parsed_args.long: |
| 80 | + columns = ('Zone Name', 'Zone Status', |
| 81 | + 'Host Name', 'Service Name', 'Service Status') |
| 82 | + else: |
| 83 | + columns = ('Zone Name', 'Zone Status') |
| 84 | + |
| 85 | + compute_client = self.app.client_manager.compute |
| 86 | + try: |
| 87 | + data = compute_client.availability_zones.list() |
| 88 | + except nova_exceptions.Forbidden as e: # policy doesn't allow |
| 89 | + try: |
| 90 | + data = compute_client.availability_zones.list(detailed=False) |
| 91 | + except Exception: |
| 92 | + raise e |
| 93 | + |
| 94 | + # Argh, the availability zones are not iterable... |
| 95 | + result = [] |
| 96 | + for zone in data: |
| 97 | + result += _xform_availability_zone(zone, parsed_args.long) |
| 98 | + |
| 99 | + return (columns, |
| 100 | + (utils.get_dict_properties( |
| 101 | + s, columns |
| 102 | + ) for s in result)) |
0 commit comments