Skip to content

Commit 866dccd

Browse files
committed
Add list hosts
1 parent 7ec3704 commit 866dccd

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

libcloud/compute/drivers/openstack.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4414,6 +4414,47 @@ def _to_lease(self, obj):
44144414
driver=self.reservation_connection.driver,
44154415
)
44164416

4417+
def ex_list_hosts(self):
4418+
"""
4419+
List leases
4420+
4421+
:rtype: ``list`` of :class:`OpenStack_2_Host`
4422+
"""
4423+
return self._to_hosts(self.reservation_connection.request("/os-hosts").object)
4424+
4425+
def _to_hosts(self, obj):
4426+
host_elements = obj["hosts"]
4427+
return [self._to_host(host) for host in host_elements]
4428+
4429+
def _to_host(self, obj):
4430+
return OpenStack_2_Host(
4431+
id=obj["id"],
4432+
hypervisor_hostname=obj["hypervisor_hostname"],
4433+
vcpus=obj["vcpus"],
4434+
memory_mb=obj["memory_mb"],
4435+
local_gb=obj["local_gb"],
4436+
service_name=obj["service_name"],
4437+
)
4438+
4439+
class OpenStack_2_Host:
4440+
"""
4441+
Host info.
4442+
"""
4443+
4444+
def __init__(self, id, hypervisor_hostname, vcpus, memory_mb, local_gb, service_name):
4445+
self.id = id
4446+
self.hypervisor_hostname = hypervisor_hostname
4447+
self.vcpus = vcpus
4448+
self.memory_mb = memory_mb
4449+
self.local_gb = local_gb
4450+
self.service_name = service_name
4451+
4452+
def __repr__(self):
4453+
return "<OpenStack_2_Host: id=%s, hypervisor_hostname=%s>" % (
4454+
self.id,
4455+
self.hypervisor_hostname,
4456+
)
4457+
44174458

44184459
class OpenStack_2_Lease:
44194460
"""

libcloud/test/compute/test_openstack.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,13 @@ def test_ex_list_leases(self):
25712571
self.assertEqual(leases[0].end, "2017-12-27T12:00:00.000000")
25722572
self.assertEqual(leases[0].status, "PENDING")
25732573

2574+
def test_ex_list_hosts(self):
2575+
hosts = self.driver.ex_list_hosts()
2576+
self.assertEqual(len(hosts), 1)
2577+
self.assertEqual(hosts[0].id, "1")
2578+
self.assertEqual(hosts[0].hypervisor_hostname, "compute-1")
2579+
self.assertEqual(hosts[0].vcpus, 4)
2580+
25742581

25752582
class OpenStack_1_1_FactoryMethodTests(OpenStack_1_1_Tests):
25762583
should_list_locations = False
@@ -4029,6 +4036,17 @@ def _v1_leases(self, method, url, body, headers):
40294036
httplib.responses[httplib.OK],
40304037
)
40314038

4039+
def _v1_os_hosts(self, method, url, body, headers):
4040+
if method == "GET":
4041+
body = self.fixtures.load("_os_hosts.json")
4042+
4043+
return (
4044+
httplib.OK,
4045+
body,
4046+
self.json_content_headers,
4047+
httplib.responses[httplib.OK],
4048+
)
4049+
40324050
# This exists because the nova compute url in devstack has v2 in there but the v1.1 fixtures
40334051
# work fine.
40344052

0 commit comments

Comments
 (0)