Add Hypervisor support to Compute Service

This patch is to support basic hypervisor operations: Find, List, and
Get.

Co-Author: bran<branw@vmware.com> and Cindia-blue<lxinhui@vmware.com>

Change-Id: I0ea45d2be9a8b06ff1fcaa6df1bd0ed7389a3067
This commit is contained in:
Cindia-blue
2016-03-02 22:13:09 +08:00
parent e798debe0f
commit 1a00eb21b7
4 changed files with 130 additions and 0 deletions

View File

@@ -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)

View File

@@ -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')

View File

@@ -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)

View File

@@ -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)