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):
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()
def _init_netdev(self):

View File

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

View File

@ -467,24 +467,19 @@ class VirtualDisk(VirtualDevice):
return
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 = []
for vm in vms:
xml = vm.get_xml(refresh_if_nec=False)
if util.get_xml_path(xml, func=count_cb):
names.append(vm.get_backend().name())
found = False
for disk in vm.get_devices("disk"):
if disk.path != path:
continue
if check_conflict:
if disk.shareable:
continue
found = True
break
if found:
names.append(vm.name)
return names

View File

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

View File

@ -19,13 +19,15 @@
import logging
import os
import re
import weakref
import libvirt
from virtinst import Guest
from virtinst import CapabilitiesParser
from virtinst import pollhelpers
from virtinst import support
from virtinst import util
from virtinst import CapabilitiesParser
from virtinst.cli import parse_optstr
_virtinst_uri_magic = "__virtinst_test__"
@ -183,7 +185,8 @@ class VirtualConnection(object):
ignore, ignore, ret = pollhelpers.fetch_vms(self, {},
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:
self._fetch_cache[key] = ret
return ret