libvirt: add helper methods for getting guest devices/disks

Add get_all_devices and get_all_disks methods to the libvirt
Guest object.

Related-bug: #1429220
Change-Id: I97ee786c5cc603aec1695929f58aa127063db439
This commit is contained in:
Daniel P. Berrange 2015-07-28 17:39:33 +01:00 committed by Michael Still
parent 36198e4c44
commit 2f6cf7cba8
2 changed files with 117 additions and 0 deletions

View File

@ -257,6 +257,93 @@ class GuestTestCase(test.NoDBTestCase):
self.guest.set_user_password("foo", "123")
self.domain.setUserPassword.assert_called_once_with("foo", "123", 0)
def test_get_devices(self):
xml = """
<domain type='qemu'>
<name>QEMUGuest1</name>
<uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
<memory unit='KiB'>219136</memory>
<currentMemory unit='KiB'>219136</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='i686' machine='pc'>hvm</type>
<boot dev='hd'/>
</os>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/qemu</emulator>
<disk type='block' device='disk'>
<driver name='qemu' type='raw'/>
<source dev='/dev/HostVG/QEMUGuest2'/>
<target dev='hda' bus='ide'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<disk type='network' device='disk'>
<driver name='qemu' type='raw'/>
<auth username='myname'>
<secret type='iscsi' usage='mycluster_myname'/>
</auth>
<source protocol='iscsi' name='iqn.1992-01.com.example'>
<host name='example.org' port='6000'/>
</source>
<target dev='vda' bus='virtio'/>
</disk>
<disk type='network' device='disk'>
<driver name='qemu' type='raw'/>
<source protocol='iscsi' name='iqn.1992-01.com.example/1'>
<host name='example.org' port='6000'/>
</source>
<target dev='vdb' bus='virtio'/>
</disk>
<hostdev mode='subsystem' type='pci' managed='yes'>
<source>
<address domain='0x0000' bus='0x06' slot='0x12' function='0x5'/>
</source>
</hostdev>
<hostdev mode='subsystem' type='pci' managed='yes'>
<source>
<address domain='0x0000' bus='0x06' slot='0x12' function='0x6'/>
</source>
</hostdev>
<controller type='usb' index='0'/>
<controller type='pci' index='0' model='pci-root'/>
<memballoon model='none'/>
</devices>
</domain>
"""
self.domain.XMLDesc.return_value = xml
devs = self.guest.get_all_devices()
# Only currently parse <disk> and <hostdev> elements
# hence we're not counting the controller/memballoon
self.assertEqual(5, len(devs))
self.assertIsInstance(devs[0], vconfig.LibvirtConfigGuestDisk)
self.assertIsInstance(devs[1], vconfig.LibvirtConfigGuestDisk)
self.assertIsInstance(devs[2], vconfig.LibvirtConfigGuestDisk)
self.assertIsInstance(devs[3], vconfig.LibvirtConfigGuestHostdev)
self.assertIsInstance(devs[4], vconfig.LibvirtConfigGuestHostdev)
devs = self.guest.get_all_devices(vconfig.LibvirtConfigGuestDisk)
self.assertEqual(3, len(devs))
self.assertIsInstance(devs[0], vconfig.LibvirtConfigGuestDisk)
self.assertIsInstance(devs[1], vconfig.LibvirtConfigGuestDisk)
self.assertIsInstance(devs[2], vconfig.LibvirtConfigGuestDisk)
devs = self.guest.get_all_disks()
self.assertEqual(3, len(devs))
self.assertIsInstance(devs[0], vconfig.LibvirtConfigGuestDisk)
self.assertIsInstance(devs[1], vconfig.LibvirtConfigGuestDisk)
self.assertIsInstance(devs[2], vconfig.LibvirtConfigGuestDisk)
devs = self.guest.get_all_devices(vconfig.LibvirtConfigGuestHostdev)
self.assertEqual(2, len(devs))
self.assertIsInstance(devs[0], vconfig.LibvirtConfigGuestHostdev)
self.assertIsInstance(devs[1], vconfig.LibvirtConfigGuestHostdev)
class GuestBlockTestCase(test.NoDBTestCase):

View File

@ -217,6 +217,36 @@ class Guest(object):
conf.parse_dom(node)
return conf
def get_all_disks(self):
"""Returns all the disks for a guest
:returns: a list of LibvirtConfigGuestDisk instances
"""
return self.get_all_devices(vconfig.LibvirtConfigGuestDisk)
def get_all_devices(self, devtype=None):
"""Returns all devices for a guest
:param devtype: a LibvirtConfigGuestDevice subclass class
:returns: a list of LibvirtConfigGuestDevice instances
"""
try:
config = vconfig.LibvirtConfigGuest()
config.parse_str(
self._domain.XMLDesc(0))
except Exception:
return []
devs = []
for dev in config.devices:
if (devtype is None or
isinstance(dev, devtype)):
devs.append(dev)
return devs
def detach_device(self, conf, persistent=False, live=False):
"""Detaches device to the guest.