VirtualConnection: Deal with Guest objects, not raw XML

We already do XML parsing, no need to open code it elsewhere
This commit is contained in:
Cole Robinson 2013-07-09 19:50:49 -04:00
parent 97264a3dfe
commit 318ba7e474
5 changed files with 27 additions and 31 deletions

View File

@ -135,7 +135,9 @@ class vmmConnection(vmmGObject):
################# #################
def _init_virtconn(self): def _init_virtconn(self):
self._backend.cb_fetch_all_guests = lambda: self.vms.values() self._backend.cb_fetch_all_guests = (
lambda: [vm.get_guest_for_virtinst_func(refresh_if_nec=False)
for vm in self.vms.values()])
self._backend.cb_fetch_all_pools = lambda: self.pools.values() self._backend.cb_fetch_all_pools = lambda: self.pools.values()
def _init_netdev(self): def _init_netdev(self):

View File

@ -404,6 +404,7 @@ class vmmDomain(vmmLibvirtObject):
return self._build_guest(xml) return self._build_guest(xml)
return self._guest return self._guest
get_guest_for_virtinst_func = _get_guest
def _build_guest(self, xml): def _build_guest(self, xml):
return virtinst.Guest(self.conn.get_backend(), return virtinst.Guest(self.conn.get_backend(),

View File

@ -467,24 +467,19 @@ class VirtualDisk(VirtualDevice):
return return
vms = conn.fetch_all_guests() vms = conn.fetch_all_guests()
def count_cb(ctx):
template = "count(/domain/devices/disk["
if check_conflict:
template += "not(shareable) and "
template += "source/@%s='%s'])"
for dtype in VirtualDisk._target_props:
xpath = template % (dtype, util.xml_escape(path))
if ctx.xpathEval(xpath):
return True
return False
names = [] names = []
for vm in vms: for vm in vms:
xml = vm.get_xml(refresh_if_nec=False) found = False
if util.get_xml_path(xml, func=count_cb): for disk in vm.get_devices("disk"):
names.append(vm.get_backend().name()) if disk.path != path:
continue
if check_conflict:
if disk.shareable:
continue
found = True
break
if found:
names.append(vm.name)
return names return names

View File

@ -316,18 +316,13 @@ class VirtualNetworkInterface(VirtualDevice):
if searchmac is None: if searchmac is None:
return (False, None) return (False, None)
def count_cb(ctx): vms = self.conn.fetch_all_guests()
for mac in ctx.xpathEval("/domain/devices/interface/mac"): for vm in vms:
macaddr = mac.xpathEval("attribute::address")[0].content for nic in vm.get_devices("interface"):
if macaddr and _compareMAC(searchmac, macaddr) == 0: nicmac = nic.macaddr or ""
return True if nicmac.lower() == searchmac.lower():
return False return (True, _("The MAC address '%s' is in use "
"by another virtual machine.") % searchmac)
for vm in self.conn.fetch_all_guests():
xml = vm.get_xml(refresh_if_nec=False)
if util.get_xml_path(xml, func=count_cb):
return (True, _("The MAC address '%s' is in use "
"by another virtual machine.") % searchmac)
return (False, None) return (False, None)
def setup(self, meter=None): def setup(self, meter=None):

View File

@ -19,13 +19,15 @@
import logging import logging
import os import os
import re import re
import weakref
import libvirt import libvirt
from virtinst import Guest
from virtinst import CapabilitiesParser
from virtinst import pollhelpers from virtinst import pollhelpers
from virtinst import support from virtinst import support
from virtinst import util from virtinst import util
from virtinst import CapabilitiesParser
from virtinst.cli import parse_optstr from virtinst.cli import parse_optstr
_virtinst_uri_magic = "__virtinst_test__" _virtinst_uri_magic = "__virtinst_test__"
@ -183,7 +185,8 @@ class VirtualConnection(object):
ignore, ignore, ret = pollhelpers.fetch_vms(self, {}, ignore, ignore, ret = pollhelpers.fetch_vms(self, {},
lambda obj, ignore: obj) lambda obj, ignore: obj)
ret = [_FetchObjWrapper(obj) for obj in ret.values()] ret = [Guest(weakref.ref(self), parsexml=obj.XMLDesc(0))
for obj in ret.values()]
if self.cache_object_fetch: if self.cache_object_fetch:
self._fetch_cache[key] = ret self._fetch_cache[key] = ret
return ret return ret