Skip to content

Commit 381b47f

Browse files
author
Steve Martinelli
committed
list availability zones for compute
Adds the command `os availability zone list` Change-Id: I77bf52a9b84a62c3771a4838c9ea0c3af03eedb2 Closes-Bug: #1400795
1 parent c7a5ead commit 381b47f

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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))

‎setup.cfg‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ openstack.common =
4949
quota_show = openstackclient.common.quota:ShowQuota
5050

5151
openstack.compute.v2 =
52+
availability_zone_list = openstackclient.compute.v2.availability_zone:ListAvailabilityZone
53+
5254
compute_agent_create = openstackclient.compute.v2.agent:CreateAgent
5355
compute_agent_delete = openstackclient.compute.v2.agent:DeleteAgent
5456
compute_agent_list = openstackclient.compute.v2.agent:ListAgent

0 commit comments

Comments
 (0)