Skip to content

Commit caec5c3

Browse files
committed
share: Add support for AZs
We won't be migrating the 'share availability zone list' command across, so do this instead. Depends-On: https://review.opendev.org/c/openstack/openstacksdk/+/990569 Change-Id: I6398375ba8435738073c4841e395034ad15f411b Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent 083aa57 commit caec5c3

2 files changed

Lines changed: 95 additions & 16 deletions

File tree

openstackclient/common/availability_zone.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ def _xform_compute_availability_zone(
6767
return result
6868

6969

70+
def _xform_share_availability_zone(az: Any) -> list[dict[str, str]]:
71+
result = []
72+
zone_info = {
73+
'zone_name': az.name,
74+
# manila doesn't have the concept of availability zone statuses so we
75+
# show zones as always "available"
76+
'zone_status': 'available',
77+
}
78+
result.append(zone_info)
79+
return result
80+
81+
7082
def _xform_network_availability_zone(az: Any) -> list[dict[str, str]]:
7183
result: list[dict[str, str]] = []
7284
zone_info = {}
@@ -114,6 +126,12 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
114126
default=False,
115127
help=_('List volume availability zones'),
116128
)
129+
parser.add_argument(
130+
'--share',
131+
action='store_true',
132+
default=False,
133+
help=_('List share availability zones'),
134+
)
117135
parser.add_argument(
118136
'--long',
119137
action='store_true',
@@ -162,6 +180,28 @@ def _get_network_availability_zones(
162180
result += _xform_network_availability_zone(zone)
163181
return result
164182

183+
def _get_share_availability_zone(
184+
self, parsed_args: argparse.Namespace
185+
) -> list[dict[str, str]]:
186+
data = []
187+
try:
188+
share_client = self.app.client_manager.sdk_connection.share
189+
data = list(share_client.availability_zones())
190+
except Exception as e:
191+
if parsed_args.share:
192+
message = _(
193+
"Availability zones list not supported by "
194+
"Shared File System API"
195+
)
196+
LOG.warning(message)
197+
else:
198+
LOG.debug('Share availability zone not available: %s', e)
199+
200+
result = []
201+
for zone in data:
202+
result += _xform_share_availability_zone(zone)
203+
return result
204+
165205
def _get_volume_availability_zones(
166206
self, parsed_args: argparse.Namespace
167207
) -> list[dict[str, str]]:
@@ -201,6 +241,7 @@ def take_action(
201241
show_all = (
202242
not parsed_args.compute
203243
and not parsed_args.network
244+
and not parsed_args.share
204245
and not parsed_args.volume
205246
)
206247

@@ -209,6 +250,8 @@ def take_action(
209250
result += self._get_compute_availability_zones(parsed_args)
210251
if parsed_args.network or show_all:
211252
result += self._get_network_availability_zones(parsed_args)
253+
if parsed_args.share or show_all:
254+
result += self._get_share_availability_zone(parsed_args)
212255
if parsed_args.volume or show_all:
213256
result += self._get_volume_availability_zones(parsed_args)
214257

openstackclient/tests/unit/common/test_availability_zone.py

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
import uuid
1414

1515
from openstack.block_storage.v3 import availability_zone as _volume_az
16+
from openstack.shared_file_system.v2 import availability_zone as _share_az
17+
from openstack.test import fakes
1618

1719
from openstackclient.common import availability_zone
1820
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
1921
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
22+
from openstackclient.tests.unit.share.v2 import fakes as share_fakes
2023
from openstackclient.tests.unit import utils
2124
from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes
2225

@@ -30,10 +33,7 @@ def _create_fake_volume_az():
3033
def _build_compute_az_datalist(compute_az, long_datalist=False):
3134
datalist = ()
3235
if not long_datalist:
33-
datalist = (
34-
compute_az.name,
35-
'available',
36-
)
36+
datalist = (compute_az.name, 'available')
3737
else:
3838
for host, services in compute_az.hosts.items():
3939
for service, state in services.items():
@@ -64,6 +64,15 @@ def _build_network_az_datalist(network_az, long_datalist=False):
6464
return (datalist,)
6565

6666

67+
def _build_share_az_datalist(share_az, long_datalist=False):
68+
datalist = ()
69+
if not long_datalist:
70+
datalist = (share_az.name, 'available')
71+
else:
72+
datalist = (share_az.name, 'available', '', '', '', '')
73+
return (datalist,)
74+
75+
6776
def _build_volume_az_datalist(volume_az, long_datalist=False):
6877
datalist = ()
6978
if not long_datalist:
@@ -77,6 +86,7 @@ class TestAvailabilityZoneList(
7786
network_fakes.FakeClientMixin,
7887
volume_fakes.FakeClientMixin,
7988
compute_fakes.FakeClientMixin,
89+
share_fakes.FakeClientMixin,
8090
utils.TestCommand,
8191
):
8292
short_columnslist = ('Zone Name', 'Zone Status')
@@ -94,8 +104,15 @@ def setUp(self):
94104

95105
self.compute_azs = compute_fakes.create_availability_zones()
96106
self.compute_client.availability_zones.return_value = self.compute_azs
107+
97108
self.network_azs = network_fakes.create_availability_zones()
98109
self.network_client.availability_zones.return_value = self.network_azs
110+
111+
self.share_azs = list(
112+
fakes.generate_fake_resources(_share_az.AvailabilityZone)
113+
)
114+
self.share_sdk_client.availability_zones.return_value = self.share_azs
115+
99116
self.volume_azs = [_create_fake_volume_az()]
100117
self.volume_client.availability_zones.return_value = self.volume_azs
101118

@@ -114,6 +131,7 @@ def test_availability_zone_list_no_options(self):
114131

115132
self.compute_client.availability_zones.assert_called_with(details=True)
116133
self.network_client.availability_zones.assert_called_with()
134+
self.share_sdk_client.availability_zones.assert_called_with()
117135
self.volume_client.availability_zones.assert_called_with()
118136

119137
self.assertEqual(self.short_columnslist, columns)
@@ -122,6 +140,8 @@ def test_availability_zone_list_no_options(self):
122140
datalist += _build_compute_az_datalist(compute_az)
123141
for network_az in self.network_azs:
124142
datalist += _build_network_az_datalist(network_az)
143+
for share_az in self.share_azs:
144+
datalist += _build_share_az_datalist(share_az)
125145
for volume_az in self.volume_azs:
126146
datalist += _build_volume_az_datalist(volume_az)
127147
self.assertEqual(datalist, tuple(data))
@@ -135,13 +155,11 @@ def test_availability_zone_list_long(self):
135155
]
136156
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
137157

138-
# In base command class Lister in cliff, abstract method take_action()
139-
# returns a tuple containing the column names and an iterable
140-
# containing the data to be listed.
141158
columns, data = self.cmd.take_action(parsed_args)
142159

143160
self.compute_client.availability_zones.assert_called_with(details=True)
144161
self.network_client.availability_zones.assert_called_with()
162+
self.share_sdk_client.availability_zones.assert_called_with()
145163
self.volume_client.availability_zones.assert_called_with()
146164

147165
self.assertEqual(self.long_columnslist, columns)
@@ -154,6 +172,8 @@ def test_availability_zone_list_long(self):
154172
datalist += _build_network_az_datalist(
155173
network_az, long_datalist=True
156174
)
175+
for share_az in self.share_azs:
176+
datalist += _build_share_az_datalist(share_az, long_datalist=True)
157177
for volume_az in self.volume_azs:
158178
datalist += _build_volume_az_datalist(
159179
volume_az, long_datalist=True
@@ -169,13 +189,11 @@ def test_availability_zone_list_compute(self):
169189
]
170190
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
171191

172-
# In base command class Lister in cliff, abstract method take_action()
173-
# returns a tuple containing the column names and an iterable
174-
# containing the data to be listed.
175192
columns, data = self.cmd.take_action(parsed_args)
176193

177194
self.compute_client.availability_zones.assert_called_with(details=True)
178195
self.network_client.availability_zones.assert_not_called()
196+
self.share_sdk_client.availability_zones.assert_not_called()
179197
self.volume_client.availability_zones.assert_not_called()
180198

181199
self.assertEqual(self.short_columnslist, columns)
@@ -193,13 +211,11 @@ def test_availability_zone_list_network(self):
193211
]
194212
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
195213

196-
# In base command class Lister in cliff, abstract method take_action()
197-
# returns a tuple containing the column names and an iterable
198-
# containing the data to be listed.
199214
columns, data = self.cmd.take_action(parsed_args)
200215

201216
self.compute_client.availability_zones.assert_not_called()
202217
self.network_client.availability_zones.assert_called_with()
218+
self.share_sdk_client.availability_zones.assert_not_called()
203219
self.volume_client.availability_zones.assert_not_called()
204220

205221
self.assertEqual(self.short_columnslist, columns)
@@ -208,6 +224,28 @@ def test_availability_zone_list_network(self):
208224
datalist += _build_network_az_datalist(network_az)
209225
self.assertEqual(datalist, tuple(data))
210226

227+
def test_availability_zone_list_share(self):
228+
arglist = [
229+
'--share',
230+
]
231+
verifylist = [
232+
('share', True),
233+
]
234+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
235+
236+
columns, data = self.cmd.take_action(parsed_args)
237+
238+
self.compute_client.availability_zones.assert_not_called()
239+
self.network_client.availability_zones.assert_not_called()
240+
self.share_sdk_client.availability_zones.assert_called_with()
241+
self.volume_client.availability_zones.assert_not_called()
242+
243+
self.assertEqual(self.short_columnslist, columns)
244+
datalist = ()
245+
for share_az in self.share_azs:
246+
datalist += _build_share_az_datalist(share_az)
247+
self.assertEqual(datalist, tuple(data))
248+
211249
def test_availability_zone_list_volume(self):
212250
arglist = [
213251
'--volume',
@@ -217,13 +255,11 @@ def test_availability_zone_list_volume(self):
217255
]
218256
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
219257

220-
# In base command class Lister in cliff, abstract method take_action()
221-
# returns a tuple containing the column names and an iterable
222-
# containing the data to be listed.
223258
columns, data = self.cmd.take_action(parsed_args)
224259

225260
self.compute_client.availability_zones.assert_not_called()
226261
self.network_client.availability_zones.assert_not_called()
262+
self.share_sdk_client.availability_zones.assert_not_called()
227263
self.volume_client.availability_zones.assert_called_with()
228264

229265
self.assertEqual(self.short_columnslist, columns)

0 commit comments

Comments
 (0)