From 73c1e2a64d0ea7bbdbabe2251f93743b494b3b92 Mon Sep 17 00:00:00 2001 From: mountainwei Date: Wed, 9 Sep 2015 16:47:31 +0800 Subject: [PATCH] Add support for availability zone request Change-Id: Ic5109b5361de6b492cdb7fcb1c117d7109c55076 Co-Authored-By: xuhaiwei --- openstack/compute/v2/_proxy.py | 14 +++++++ openstack/compute/v2/availability_zone.py | 36 ++++++++++++++++ .../unit/compute/v2/test_availability_zone.py | 41 +++++++++++++++++++ openstack/tests/unit/compute/v2/test_proxy.py | 5 +++ 4 files changed, 96 insertions(+) create mode 100644 openstack/compute/v2/availability_zone.py create mode 100644 openstack/tests/unit/compute/v2/test_availability_zone.py diff --git a/openstack/compute/v2/_proxy.py b/openstack/compute/v2/_proxy.py index 636253b06..a54b93947 100644 --- a/openstack/compute/v2/_proxy.py +++ b/openstack/compute/v2/_proxy.py @@ -10,6 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. +from openstack.compute.v2 import availability_zone from openstack.compute.v2 import extension from openstack.compute.v2 import flavor as _flavor from openstack.compute.v2 import image as _image @@ -569,3 +570,16 @@ class Proxy(proxy.BaseProxy): server = _server.Server.from_id(server) return server.rebuild(self.session, name, image_ref, admin_password, **attrs) + + def availability_zones(self, **query): + """Return a generator of availability zones + + :param kwargs \*\*query: Optional query parameters to be sent + to limit the resources being returned. + + :returns: A generator of availability zone + :rtype: :class:`~openstack.compute.v2.availability_zone. + AvailabilityZone` + """ + return self._list(availability_zone.AvailabilityZone, + paginated=False, **query) diff --git a/openstack/compute/v2/availability_zone.py b/openstack/compute/v2/availability_zone.py new file mode 100644 index 000000000..e40136119 --- /dev/null +++ b/openstack/compute/v2/availability_zone.py @@ -0,0 +1,36 @@ +# 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 openstack.compute import compute_service +from openstack import resource + + +class AvailabilityZone(resource.Resource): + + resource_key = 'availability_zone' + resources_key = 'availabilityZoneInfo' + base_path = '/os-availability-zone' + + service = compute_service.ComputeService() + + # capabilities + allow_list = True + + # Properties + #: name of availability zone + name = resource.prop('zoneName') + + #: state of availability zone + state = resource.prop('zoneState') + + #: hosts of availability zone + hosts = resource.prop('hosts') diff --git a/openstack/tests/unit/compute/v2/test_availability_zone.py b/openstack/tests/unit/compute/v2/test_availability_zone.py new file mode 100644 index 000000000..07ff4c7b9 --- /dev/null +++ b/openstack/tests/unit/compute/v2/test_availability_zone.py @@ -0,0 +1,41 @@ +# 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 testtools + +from openstack.compute.v2 import availability_zone as az + +IDENTIFIER = 'IDENTIFIER' +BASIC_EXAMPLE = { + 'id': IDENTIFIER, + 'zoneState': 'available', + 'hosts': 'host1', + 'zoneName': 'zone1' +} + + +class TestAvailabilityZone(testtools.TestCase): + + def test_basic(self): + sot = az.AvailabilityZone() + self.assertEqual('availability_zone', sot.resource_key) + self.assertEqual('availabilityZoneInfo', sot.resources_key) + self.assertEqual('/os-availability-zone', sot.base_path) + self.assertTrue(sot.allow_list) + self.assertEqual('compute', sot.service.service_type) + + def test_make_basic(self): + sot = az.AvailabilityZone(BASIC_EXAMPLE) + self.assertEqual(BASIC_EXAMPLE['id'], sot.id) + self.assertEqual(BASIC_EXAMPLE['zoneState'], sot.state) + self.assertEqual(BASIC_EXAMPLE['hosts'], sot.hosts) + self.assertEqual(BASIC_EXAMPLE['zoneName'], sot.name) diff --git a/openstack/tests/unit/compute/v2/test_proxy.py b/openstack/tests/unit/compute/v2/test_proxy.py index 1e14d7202..8c8d7b5d9 100644 --- a/openstack/tests/unit/compute/v2/test_proxy.py +++ b/openstack/tests/unit/compute/v2/test_proxy.py @@ -13,6 +13,7 @@ import mock from openstack.compute.v2 import _proxy +from openstack.compute.v2 import availability_zone as az from openstack.compute.v2 import extension from openstack.compute.v2 import flavor from openstack.compute.v2 import image @@ -272,3 +273,7 @@ class TestComputeProxy(test_proxy_base.TestProxyBase): expected_args=["test_server", "test_image_url", "test_pass"], expected_kwargs={"metadata": {"k1": "v1"}}) + + def test_availability_zones(self): + self.verify_list(self.proxy.availability_zones, az.AvailabilityZone, + paginated=False)