Skip to content

Commit 733b432

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add availability zone and host filters to aggregate list"
2 parents 9315bf4 + 810fa70 commit 733b432

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

openstackclient/compute/v2/aggregate.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
196196
default=False,
197197
help=_("List additional fields in output"),
198198
)
199+
parser.add_argument(
200+
'--availability-zone',
201+
metavar='<availability-zone>',
202+
default=None,
203+
help=_("Filter by availability zone name"),
204+
)
205+
parser.add_argument(
206+
'--host',
207+
metavar='<host>',
208+
default=None,
209+
help=_("Filter by aggregates containing this host"),
210+
)
199211
return parser
200212

201213
def take_action(
@@ -205,6 +217,23 @@ def take_action(
205217

206218
aggregates = list(compute_client.aggregates())
207219

220+
# NOTE: The compute API does not support server-side filtering of
221+
# aggregates, so we filter client-side. When more than one filter is
222+
# given they are combined with AND (results must match all filters).
223+
if parsed_args.availability_zone is not None:
224+
aggregates = [
225+
aggregate
226+
for aggregate in aggregates
227+
if aggregate.availability_zone == parsed_args.availability_zone
228+
]
229+
230+
if parsed_args.host is not None:
231+
aggregates = [
232+
aggregate
233+
for aggregate in aggregates
234+
if aggregate.hosts and parsed_args.host in aggregate.hosts
235+
]
236+
208237
if sdk_utils.supports_microversion(compute_client, '2.41'):
209238
column_headers: tuple[str, ...] = ("ID", "UUID")
210239
columns: tuple[str, ...] = ("id", "uuid")

openstackclient/tests/unit/compute/v2/test_aggregate.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,70 @@ def test_aggregate_list_with_long(self):
326326
self.assertEqual(expected_columns, columns)
327327
self.assertCountEqual(expected_data, tuple(data))
328328

329+
def test_aggregate_list_with_availability_zone(self):
330+
self.set_compute_api_version('2.41')
331+
332+
arglist = [
333+
'--availability-zone',
334+
self.fake_ag.availability_zone,
335+
]
336+
verifylist = [
337+
('availability_zone', self.fake_ag.availability_zone),
338+
]
339+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
340+
_, data = self.cmd.take_action(parsed_args)
341+
342+
# the matching aggregate is returned
343+
self.assertEqual(1, len(tuple(data)))
344+
345+
def test_aggregate_list_with_availability_zone_no_match(self):
346+
self.set_compute_api_version('2.41')
347+
348+
arglist = [
349+
'--availability-zone',
350+
'does-not-exist',
351+
]
352+
verifylist = [
353+
('availability_zone', 'does-not-exist'),
354+
]
355+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
356+
_, data = self.cmd.take_action(parsed_args)
357+
358+
# no aggregate matches the filter
359+
self.assertEqual(0, len(tuple(data)))
360+
361+
def test_aggregate_list_with_host(self):
362+
self.set_compute_api_version('2.41')
363+
364+
arglist = [
365+
'--host',
366+
self.fake_ag.hosts[0],
367+
]
368+
verifylist = [
369+
('host', self.fake_ag.hosts[0]),
370+
]
371+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
372+
_, data = self.cmd.take_action(parsed_args)
373+
374+
# the aggregate containing the host is returned
375+
self.assertEqual(1, len(tuple(data)))
376+
377+
def test_aggregate_list_with_host_no_match(self):
378+
self.set_compute_api_version('2.41')
379+
380+
arglist = [
381+
'--host',
382+
'does-not-exist',
383+
]
384+
verifylist = [
385+
('host', 'does-not-exist'),
386+
]
387+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
388+
_, data = self.cmd.take_action(parsed_args)
389+
390+
# no aggregate contains the host
391+
self.assertEqual(0, len(tuple(data)))
392+
329393

330394
class TestAggregateRemoveHost(TestAggregate):
331395
def setUp(self):
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
features:
3+
- |
4+
Added ``--availability-zone`` and ``--host`` options to the
5+
``aggregate list`` command. These allow the results to be filtered by
6+
availability zone name and by aggregates containing a given host. The
7+
compute API does not support server-side filtering of aggregates, so the
8+
filtering is performed client-side. When both options are supplied they
9+
are combined with AND, returning only aggregates that match every filter.

0 commit comments

Comments
 (0)