From 53275bec677e2b82768ee7254f45f52bbeb9b423 Mon Sep 17 00:00:00 2001 From: Kafilat Adeleke Date: Mon, 16 Aug 2021 17:25:58 +0000 Subject: [PATCH] [OSC] Implement Availability Zones Command This commit implements the following openstack share command: openstack share availability zone list Change-Id: I75d3c5441b82fb8f16896719544a81dd032cdd15 --- doc/source/cli/osc/v2/index.rst | 7 +++ manilaclient/osc/v2/availability_zones.py | 40 ++++++++++++++ manilaclient/tests/unit/osc/v2/fakes.py | 48 +++++++++++++++++ .../unit/osc/v2/test_availability_zones.py | 54 +++++++++++++++++++ setup.cfg | 1 + 5 files changed, 150 insertions(+) create mode 100644 manilaclient/osc/v2/availability_zones.py create mode 100644 manilaclient/tests/unit/osc/v2/test_availability_zones.py diff --git a/doc/source/cli/osc/v2/index.rst b/doc/source/cli/osc/v2/index.rst index 8eec1167c..c4c80c34c 100644 --- a/doc/source/cli/osc/v2/index.rst +++ b/doc/source/cli/osc/v2/index.rst @@ -88,3 +88,10 @@ share replicas .. autoprogram-cliff:: openstack.share.v2 :command: share replica * + +======================== +share availability zones +======================== + +.. autoprogram-cliff:: openstack.share.v2 + :command: share availability zone list diff --git a/manilaclient/osc/v2/availability_zones.py b/manilaclient/osc/v2/availability_zones.py new file mode 100644 index 000000000..4df9a12e3 --- /dev/null +++ b/manilaclient/osc/v2/availability_zones.py @@ -0,0 +1,40 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import logging + +from osc_lib.command import command +from osc_lib import utils as oscutils + +from manilaclient.common._i18n import _ + +LOG = logging.getLogger(__name__) + + +class ShareAvailabilityZoneList(command.Lister): + """List all availability zones.""" + + _description = _("List all availability zones") + + def get_parser(self, prog_name): + parser = super(ShareAvailabilityZoneList, self).get_parser( + prog_name) + return parser + + def take_action(self, parsed_args): + share_client = self.app.client_manager.share + availability_zones = share_client.availability_zones.list() + + fields = ("Id", "Name", "Created At", "Updated At") + + return (fields, (oscutils.get_item_properties + (s, fields) for s in availability_zones)) diff --git a/manilaclient/tests/unit/osc/v2/fakes.py b/manilaclient/tests/unit/osc/v2/fakes.py index c532661b5..d9f30692d 100644 --- a/manilaclient/tests/unit/osc/v2/fakes.py +++ b/manilaclient/tests/unit/osc/v2/fakes.py @@ -42,6 +42,7 @@ class FakeShareClient(object): self.share_export_locations.resource_class = ( osc_fakes.FakeResource(None, {})) self.messages = mock.Mock() + self.availability_zones = mock.Mock() class ManilaParseException(Exception): @@ -649,3 +650,50 @@ class FakeShareReplica(object): share_replicas.append( FakeShareReplica.create_one_replica(attrs)) return share_replicas + + +class FakeShareAvailabilityZones(object): + """Fake one or more availability zones""" + + @staticmethod + def create_one_availability_zone(attrs=None): + """Create a fake share availability zone + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeResource object, with project_id, resource and so on + """ + + attrs = attrs or {} + + availability_zone = { + "id": 'id-' + uuid.uuid4().hex, + "name": 'name-' + uuid.uuid4().hex, + "created_at": 'time-' + uuid.uuid4().hex, + "updated_at": 'time-' + uuid.uuid4().hex, + } + + availability_zone.update(attrs) + availability_zone = osc_fakes.FakeResource(info=copy.deepcopy( + availability_zone), + loaded=True) + return availability_zone + + @staticmethod + def create_share_availability_zones(attrs=None, count=2): + """Create multiple availability zones. + + :param Dictionary attrs: + A dictionary with all attributes + :param Integer count: + The number of share types to be faked + :return: + A list of FakeResource objects + """ + + availability_zones = [] + for n in range(0, count): + availability_zones.append( + FakeShareAvailabilityZones.create_one_availability_zone(attrs)) + return availability_zones diff --git a/manilaclient/tests/unit/osc/v2/test_availability_zones.py b/manilaclient/tests/unit/osc/v2/test_availability_zones.py new file mode 100644 index 000000000..02b5cebfe --- /dev/null +++ b/manilaclient/tests/unit/osc/v2/test_availability_zones.py @@ -0,0 +1,54 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +from osc_lib import utils as oscutils + +from manilaclient.osc.v2 import availability_zones as osc_availability_zones +from manilaclient.tests.unit.osc.v2 import fakes as manila_fakes + + +class TestAvailabilityZones(manila_fakes.TestShare): + + def setUp(self): + super(TestAvailabilityZones, self).setUp() + + self.zones_mock = self.app.client_manager.share.availability_zones + self.zones_mock.reset_mock() + + +class TestShareAvailabilityZoneList(TestAvailabilityZones): + + availability_zones = manila_fakes.FakeShareAvailabilityZones.\ + create_share_availability_zones() + COLUMNS = ("Id", "Name", "Created At", "Updated At") + + def setUp(self): + super(TestShareAvailabilityZoneList, self).setUp() + + self.zones_mock.list.return_value = self.availability_zones + + # Get the command object to test + self.cmd = osc_availability_zones.ShareAvailabilityZoneList( + self.app, None) + + self.values = (oscutils.get_dict_properties( + s._info, self.COLUMNS) for s in self.availability_zones) + + def test_share_list_availability_zone(self): + arglist = [] + verifylist = [] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(self.COLUMNS, columns) + self.assertCountEqual(list(self.values), list(data)) diff --git a/setup.cfg b/setup.cfg index 552bba96e..6cf16c152 100644 --- a/setup.cfg +++ b/setup.cfg @@ -89,6 +89,7 @@ openstack.share.v2 = share_replica_set = manilaclient.osc.v2.share_replicas:SetShareReplica share_replica_promote = manilaclient.osc.v2.share_replicas:PromoteShareReplica share_replica_resync = manilaclient.osc.v2.share_replicas:ResyncShareReplica + share_availability_zone_list = manilaclient.osc.v2.availability_zones:ShareAvailabilityZoneList [coverage:run] omit = manilaclient/tests/*