Added vms_by_host

This commit is contained in:
Anton Beloglazov
2012-09-21 17:57:05 +10:00
parent 8006b32dfc
commit d5b1cb8e95
3 changed files with 60 additions and 5 deletions

View File

@@ -18,6 +18,7 @@ import collections
import libvirt
import sqlalchemy
import neat.db
# import novaclient
new_contract('deque', collections.deque)
@@ -26,3 +27,4 @@ new_contract('virConnect', libvirt.virConnect)
new_contract('virDomain', libvirt.virDomain)
new_contract('Table', sqlalchemy.Table)
new_contract('Database', neat.db.Database)
# new_contract('Client', novaclient.v1_1.client.Client)

View File

@@ -225,14 +225,40 @@ def execute_underload(config, state, host):
:return: The updated state dictionary.
:rtype: dict(str: *)
"""
nt = client.Client("admin", "adminpassword", "admin", "http://controller:5000/v2.0/", service_type="compute")
vms = nt.servers.list(detailed=True)
vms[0].id # uuid
vms[0].__getattr__('OS-EXT-SRV-ATTR:host') # host name
vms = vms_by_host(state['nova'], host)
return state
@contract
def vms_by_host(nova, host):
""" Get VMs from the specified host using the Nova API.
:param nova: A Nova client.
:type nova: *
:param host: A host name.
:type host: str
:return: A list of VM UUIDs from the specified host.
:rtype: list(str)
"""
return [vm.id for vm in nova.servers.list()
if vm_hostname(vm) == host]
@contract
def vm_hostname(vm):
""" Get the name of the host where VM is running.
:param vm: A Nova VM object.
:type vm: *
:return: The hostname.
:rtype: str
"""
return vm.__getattr__('OS-EXT-SRV-ATTR:host')
@contract
def execute_overload(config, state, vm_uuids):
""" Execute an iteration of the global manager

View File

@@ -164,3 +164,30 @@ class GlobalManager(TestCase):
expect(manager).validate_params(config, params).and_return(True).once()
expect(manager).execute_overload(config, state, 'vm_uuids').once()
manager.service()
@qc(20)
def vms_by_host(
x=dict_(
keys=str_(of='abc123-', min_length=36, max_length=36),
values=str_(of='abc123-', min_length=10, max_length=10),
min_length=0, max_length=3
),
y=list_(str_(of='abc123-', min_length=36, max_length=36),
min_length=0, max_length=3),
host=str_(of='abc123-', min_length=5, max_length=5)
):
with MockTransaction:
extra_vms = {}
for vm in y:
extra_vms[vm] = host
x.update(extra_vms)
vms = []
for vm_uuid, h in x.items():
vm = mock(vm_uuid)
vm.id = vm_uuid
expect(manager).vm_hostname(vm).and_return(h).once()
vms.append(vm)
nova = mock('nova')
nova.servers = mock('servers')
expect(nova.servers).list().and_return(vms).once()
assert set(manager.vms_by_host(nova, host)) == set(y)