Add hypervisor details to hypervisors list if requested

Allow optionally generating a list of hypervisors that includes
hypervisor details to reduce both openstacksdk and Nova API calls when
details are wanted for all hypervisors and to better mirror the Nova
API which provides such a feature.

The "details" argument defaults to "False" to maintain API backwards
compatibility.

Change-Id: Iefa32570a43a7e1fd6f05ab519882424272ae1c1
Signed-off-by: Corey Wright <corey.wright@rackspace.com>
This commit is contained in:
Corey Wright 2018-06-15 16:58:05 -05:00
parent 07d3828860
commit f7da0170ae
4 changed files with 37 additions and 6 deletions

View File

@ -937,14 +937,23 @@ class Proxy(proxy.Proxy):
"""
return self._list(_server_group.ServerGroup, paginated=False, **query)
def hypervisors(self):
def hypervisors(self, details=False):
"""Return a generator of hypervisor
:param bool details: When set to the default, ``False``,
:class:`~openstack.compute.v2.hypervisor.Hypervisor`
instances will be returned. ``True`` will cause
:class:`~openstack.compute.v2.hypervisor.HypervisorDetail`
instances to be returned.
:returns: A generator of hypervisor
:rtype: class: `~openstack.compute.v2.hypervisor.Hypervisor`
"""
if details:
hypervisor = _hypervisor.HypervisorDetail
else:
hypervisor = _hypervisor.Hypervisor
return self._list(_hypervisor.Hypervisor, paginated=False)
return self._list(hypervisor, paginated=False)
def find_hypervisor(self, name_or_id, ignore_missing=True):
"""Find a hypervisor from name or id to get the corresponding info

View File

@ -65,3 +65,11 @@ class Hypervisor(resource.Resource):
host_ip = resource.Body('host_ip')
#: Disk space available to the scheduler
disk_available = resource.Body("disk_available_least")
class HypervisorDetail(Hypervisor):
base_path = '/os-hypervisors/detail'
# capabilities
allow_get = False
allow_list = True

View File

@ -76,3 +76,12 @@ class TestHypervisor(base.TestCase):
self.assertEqual(EXAMPLE['disk_available_least'], sot.disk_available)
self.assertEqual(EXAMPLE['local_gb'], sot.local_disk_size)
self.assertEqual(EXAMPLE['free_ram_mb'], sot.memory_free)
def test_detail(self):
sot = hypervisor.HypervisorDetail()
self.assertEqual('hypervisor', sot.resource_key)
self.assertEqual('hypervisors', sot.resources_key)
self.assertEqual('/os-hypervisors/detail', sot.base_path)
self.assertEqual('compute', sot.service.service_type)
self.assertFalse(sot.allow_get)
self.assertTrue(sot.allow_list)

View File

@ -475,10 +475,15 @@ class TestComputeProxy(test_proxy_base.TestProxyBase):
self.verify_list(self.proxy.server_groups, server_group.ServerGroup,
paginated=False)
def test_hypervisors(self):
self.verify_list_no_kwargs(self.proxy.hypervisors,
hypervisor.Hypervisor,
paginated=False)
def test_hypervisors_not_detailed(self):
self.verify_list(self.proxy.hypervisors, hypervisor.Hypervisor,
paginated=False,
method_kwargs={"details": False})
def test_hypervisors_detailed(self):
self.verify_list(self.proxy.hypervisors, hypervisor.HypervisorDetail,
paginated=False,
method_kwargs={"details": True})
def test_find_hypervisor(self):
self.verify_find(self.proxy.find_hypervisor,