From 1a00eb21b773217af8cd9a0421879c2db920bfe7 Mon Sep 17 00:00:00 2001 From: Cindia-blue Date: Wed, 2 Mar 2016 22:13:09 +0800 Subject: [PATCH] Add Hypervisor support to Compute Service This patch is to support basic hypervisor operations: Find, List, and Get. Co-Author: bran and Cindia-blue Change-Id: I0ea45d2be9a8b06ff1fcaa6df1bd0ed7389a3067 --- openstack/compute/v2/_proxy.py | 37 +++++++++++++++++ openstack/compute/v2/hypervisor.py | 39 ++++++++++++++++++ .../tests/unit/compute/v2/test_hypervisor.py | 41 +++++++++++++++++++ openstack/tests/unit/compute/v2/test_proxy.py | 13 ++++++ 4 files changed, 130 insertions(+) create mode 100644 openstack/compute/v2/hypervisor.py create mode 100644 openstack/tests/unit/compute/v2/test_hypervisor.py diff --git a/openstack/compute/v2/_proxy.py b/openstack/compute/v2/_proxy.py index 9b8970db6..94dfe6739 100644 --- a/openstack/compute/v2/_proxy.py +++ b/openstack/compute/v2/_proxy.py @@ -13,6 +13,7 @@ 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 hypervisor as _hypervisor from openstack.compute.v2 import image as _image from openstack.compute.v2 import keypair as _keypair from openstack.compute.v2 import limits @@ -837,3 +838,39 @@ class Proxy(proxy.BaseProxy): :rtype: :class:`~openstack.compute.v2.server_group.ServerGroup` """ return self._list(_server_group.ServerGroup, paginated=False, **query) + + def hypervisors(self, **query): + """Return a generator of hypervisor + + :returns: A generator of hypervisor + :rtype: class: `~openstack.compute.v2.hypervisor.Hypervisor` + """ + + return self._list(_hypervisor.Hypervisor, paginated=False, **query) + + def find_hypervisor(self, name_or_id, ignore_missing=True): + """Find a hypervisor from name or id to get the corresponding info + + :param name_or_id: The name or id of a hypervisor + + :returns: + One: class:`~openstack.compute.v2.hypervisor.Hypervisor` object + or None + """ + + return self._find(_hypervisor.Hypervisor, name_or_id, + ignore_missing=ignore_missing) + + def get_hypervisor(self, hypervisor): + """Get a single hypervisor + + :param hypervisor: The value can be the ID of a hypervisor or a + :class:`~openstack.compute.v2.hypervisor.Hypervisor` + instance. + + :returns: + A :class:`~openstack.compute.v2.hypervisor.Hypervisor` object. + :raises: :class:`~openstack.exceptions.ResourceNotFound` + when no resource can be found. + """ + return self._get(_hypervisor.Hypervisor, hypervisor) diff --git a/openstack/compute/v2/hypervisor.py b/openstack/compute/v2/hypervisor.py new file mode 100644 index 000000000..971e9bdfe --- /dev/null +++ b/openstack/compute/v2/hypervisor.py @@ -0,0 +1,39 @@ +# 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 Hypervisor(resource.Resource): + + name_attribute = 'hypervisor_hostname' + resource_key = 'hypervisor' + resources_key = 'hypervisors' + base_path = '/os-hypervisors' + + service = compute_service.ComputeService() + + # capabilities + allow_retrieve = True + allow_list = True + + # Properties + #: status of hypervisor + status = resource.prop('status') + + #: state of hypervisor + state = resource.prop('state') + + #: name of hypervisor + hypervisor_hostname = resource.prop('hypervisor_hostname') diff --git a/openstack/tests/unit/compute/v2/test_hypervisor.py b/openstack/tests/unit/compute/v2/test_hypervisor.py new file mode 100644 index 000000000..0a6ab696d --- /dev/null +++ b/openstack/tests/unit/compute/v2/test_hypervisor.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 hypervisor + +EXAMPLE = { + 'id': 'IDENTIFIER', + 'name': 'hypervisor_hostname', + 'state': 'up', + 'status': 'enabled', +} + + +class TestHypervisor(testtools.TestCase): + + def test_basic(self): + sot = hypervisor.Hypervisor() + self.assertEqual('hypervisor', sot.resource_key) + self.assertEqual('hypervisors', sot.resources_key) + self.assertEqual('/os-hypervisors', sot.base_path) + self.assertEqual('compute', sot.service.service_type) + self.assertTrue(sot.allow_retrieve) + self.assertTrue(sot.allow_list) + + def test_make_it(self): + sot = hypervisor.Hypervisor(EXAMPLE) + self.assertEqual(EXAMPLE['id'], sot.id) + self.assertEqual(EXAMPLE['name'], sot.name) + self.assertEqual(EXAMPLE['state'], sot.state) + self.assertEqual(EXAMPLE['status'], sot.status) diff --git a/openstack/tests/unit/compute/v2/test_proxy.py b/openstack/tests/unit/compute/v2/test_proxy.py index 08d0638aa..837c82244 100644 --- a/openstack/tests/unit/compute/v2/test_proxy.py +++ b/openstack/tests/unit/compute/v2/test_proxy.py @@ -16,6 +16,7 @@ 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 hypervisor from openstack.compute.v2 import image from openstack.compute.v2 import keypair from openstack.compute.v2 import limits @@ -361,3 +362,15 @@ class TestComputeProxy(test_proxy_base.TestProxyBase): def test_server_groups(self): self.verify_list(self.proxy.server_groups, server_group.ServerGroup, paginated=False) + + def test_hypervisors(self): + self.verify_list(self.proxy.hypervisors, hypervisor.Hypervisor, + paginated=False) + + def test_find_hypervisor(self): + self.verify_find(self.proxy.find_hypervisor, + hypervisor.Hypervisor) + + def test_get_hypervisor(self): + self.verify_get(self.proxy.get_hypervisor, + hypervisor.Hypervisor)