Change in vdsm[master]: Improve dom xml gereration

wudxw at linux.vnet.ibm.com wudxw at linux.vnet.ibm.com
Fri Dec 14 08:13:42 UTC 2012


Mark Wu has uploaded a new change for review.

Change subject: Improve dom xml gereration
......................................................................

Improve dom xml gereration

This patch adds a new class XMLElem, which allows to create a XML element
and set its attributes and text node with one call. Also it can add
a child element with the attibutes of child. Then dom xml generation
code can just focus on getting attributes from vm's defintion. It also
make it easier to replace minidom with elementtree if neccessary in future.

Change-Id: Ib936740fcbeeee551e13abfed7fd91f2a159e351
Signed-off-by: Mark Wu <wudxw at linux.vnet.ibm.com>
---
M vdsm/libvirtvm.py
1 file changed, 168 insertions(+), 241 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/54/10054/1

diff --git a/vdsm/libvirtvm.py b/vdsm/libvirtvm.py
index c089745..1797bfa 100644
--- a/vdsm/libvirtvm.py
+++ b/vdsm/libvirtvm.py
@@ -549,6 +549,32 @@
         return f
 
 
+class XMLElem(object):
+
+    def __init__(self, tagName, text=None, attrs={}):
+        self.doc = xml.dom.minidom.Document()
+        self.elem = self.doc.createElement(tagName)
+        self.setAttrs(attrs)
+        if text is not None:
+            self.addTextNode(text)
+
+    def __getattr__(self, name):
+        return getattr(self.elem, name)
+
+    def setAttrs(self, attrs):
+        for attrName, attrValue in attrs.iteritems():
+            self.elem.setAttribute(attrName, attrValue)
+
+    def addTextNode(self, text):
+        textNode = self.doc.createTextNode(text)
+        self.elem.appendChild(textNode)
+
+    def addChild(self, childName, text=None, attrs={}):
+        child = XMLElem(childName, text, attrs)
+        self.elem.appendChild(child)
+        return child
+
+
 class _DomXML:
     def __init__(self, conf, log):
         """
@@ -569,30 +595,20 @@
         self.log = log
 
         self.doc = xml.dom.minidom.Document()
-        self.dom = self.doc.createElement('domain')
-
-        if utils.tobool(self.conf.get('kvmEnable', 'true')):
-            self.dom.setAttribute('type', 'kvm')
-        else:
-            self.dom.setAttribute('type', 'qemu')
-
+        domType = ('kvm' if utils.tobool(self.conf.get('kvmEnable', 'true'))
+                   else 'qemu')
+        self.dom = XMLElem('domain', attrs={'type': domType})
         self.doc.appendChild(self.dom)
 
-        self.appendChildWithText('name', self.conf['vmName'])
-        self.appendChildWithText('uuid', self.conf['vmId'])
+        self.dom.addChild('name', text=self.conf['vmName'])
+        self.dom.addChild('uuid', text=self.conf['vmId'])
         memSizeKB = str(int(self.conf.get('memSize', '256')) * 1024)
-        self.appendChildWithText('memory', memSizeKB)
-        self.appendChildWithText('currentMemory', memSizeKB)
-        self.appendChildWithText('vcpu', self.conf['smp'])
+        self.dom.addChild('memory', text=memSizeKB)
+        self.dom.addChild('currentMemory', text=memSizeKB)
+        self.dom.addChild('vcpu', text=self.conf['smp'])
 
-        self._devices = self.doc.createElement('devices')
+        self._devices = XMLElem('devices')
         self.dom.appendChild(self._devices)
-
-    def appendChildWithText(self, childName, text):
-        childNode = self.doc.createElement(childName)
-        textNode = self.doc.createTextNode(text)
-        childNode.appendChild(textNode)
-        self.dom.appendChild(childNode)
 
     def appendClock(self):
         """
@@ -603,15 +619,13 @@
         </clock>
         """
 
-        m = self.doc.createElement('clock')
-        m.setAttribute('offset', 'variable')
-        m.setAttribute('adjustment', str(self.conf.get('timeOffset', 0)))
+        clockAttrs = {'offset': 'variable',
+                      'adjustment': str(self.conf.get('timeOffset', 0))}
+        m = XMLElem('clock', attrs=clockAttrs)
 
         if utils.tobool(self.conf.get('tdf', True)):
-            t = self.doc.createElement('timer')
-            t.setAttribute('name', 'rtc')
-            t.setAttribute('tickpolicy', 'catchup')
-            m.appendChild(t)
+            m.addChild('timer', attrs={'name': 'rtc',
+                                       'tickpolicy': 'catchup'})
 
         self.dom.appendChild(m)
 
@@ -629,39 +643,26 @@
         </os>
         """
 
-        oselem = self.doc.createElement('os')
+        oselem = XMLElem('os')
         self.dom.appendChild(oselem)
-        typeelem = self.doc.createElement('type')
-        oselem.appendChild(typeelem)
-        typeelem.setAttribute('arch', 'x86_64')
-        typeelem.setAttribute('machine',
-                              self.conf.get('emulatedMachine', 'pc'))
-        typeelem.appendChild(self.doc.createTextNode('hvm'))
+        oselem.addChild('type', text='hvm',
+                        attrs={'arch': 'x86_64', 'machine':
+                               self.conf.get('emulatedMachine', 'pc')})
 
         qemu2libvirtBoot = {'a': 'fd', 'c': 'hd', 'd': 'cdrom', 'n': 'network'}
         for c in self.conf.get('boot', ''):
-            m = self.doc.createElement('boot')
-            m.setAttribute('dev', qemu2libvirtBoot[c])
-            oselem.appendChild(m)
+            oselem.addChild('boot', attrs={'dev': qemu2libvirtBoot[c]})
 
         if self.conf.get('initrd'):
-            m = self.doc.createElement('initrd')
-            m.appendChild(self.doc.createTextNode(self.conf['initrd']))
-            oselem.appendChild(m)
+            oselem.addChild('initrd', text=self.conf['initrd'])
 
         if self.conf.get('kernel'):
-            m = self.doc.createElement('kernel')
-            m.appendChild(self.doc.createTextNode(self.conf['kernel']))
-            oselem.appendChild(m)
+            oselem.addChild('kernel', text=self.conf['kernel'])
 
         if self.conf.get('kernelArgs'):
-            m = self.doc.createElement('cmdline')
-            m.appendChild(self.doc.createTextNode(self.conf['kernelArgs']))
-            oselem.appendChild(m)
+            oselem.addChild('cmdline', text=self.conf['kernelArgs'])
 
-        m = self.doc.createElement('smbios')
-        m.setAttribute('mode', 'sysinfo')
-        oselem.appendChild(m)
+        oselem.addChild('smbios', attrs={'mode': 'sysinfo'})
 
     def appendSysinfo(self, osname, osversion, hostUUID):
         """
@@ -682,18 +683,14 @@
         </sysinfo>
         """
 
-        sysinfoelem = self.doc.createElement('sysinfo')
-        sysinfoelem.setAttribute('type', 'smbios')
+        sysinfoelem = XMLElem('sysinfo', attrs={'type': 'smbios'})
         self.dom.appendChild(sysinfoelem)
 
-        syselem = self.doc.createElement('system')
+        syselem = XMLElem('system')
         sysinfoelem.appendChild(syselem)
 
         def appendEntry(k, v):
-            m = self.doc.createElement('entry')
-            m.setAttribute('name', k)
-            m.appendChild(self.doc.createTextNode(v))
-            syselem.appendChild(m)
+            syselem.addChild('entry', text=v, attrs={'name': k})
 
         appendEntry('manufacturer', constants.SMBIOS_MANUFACTURER)
         appendEntry('product', osname)
@@ -711,8 +708,7 @@
         <features/>
         """
         if utils.tobool(self.conf.get('acpiEnable', 'true')):
-            self.dom.appendChild(self.doc.createElement('features')) \
-                .appendChild(self.doc.createElement('acpi'))
+            self.dom.addChild('features').addChild('acpi')
 
     def appendCpu(self):
         """
@@ -728,17 +724,14 @@
 
         features = self.conf.get('cpuType', 'qemu64').split(',')
         model = features[0]
-        cpu = self.doc.createElement('cpu')
-        cpu.setAttribute('match', 'exact')
+        cpu = XMLElem('cpu', attrs={'match': 'exact'})
 
         if model == 'hostPassthrough':
             cpu.setAttribute('mode', 'host-passthrough')
         elif model == 'hostModel':
             cpu.setAttribute('mode', 'host-model')
         else:
-            m = self.doc.createElement('model')
-            m.appendChild(self.doc.createTextNode(model))
-            cpu.appendChild(m)
+            cpu.appendChild(XMLElem('model', text=model))
 
             # This hack is for backward compatibility as the libvirt
             # does not allow 'qemu64' guest on intel hardware
@@ -750,24 +743,21 @@
                 if feature[1:6] == 'sse4_':
                     feature = feature[0] + 'sse4.' + feature[6:]
 
-                f = self.doc.createElement('feature')
+                featureAttrs = {'name': feature[1:]}
                 if feature[0] == '+':
-                    f.setAttribute('policy', 'require')
-                    f.setAttribute('name', feature[1:])
+                    featureAttrs['policy'] = 'require'
                 elif feature[0] == '-':
-                    f.setAttribute('policy', 'disable')
-                    f.setAttribute('name', feature[1:])
-                cpu.appendChild(f)
+                    featureAttrs['policy'] = 'disable'
+                cpu.addChild('feature', attrs=featureAttrs)
 
         if ('smpCoresPerSocket' in self.conf or
                 'smpThreadsPerCore' in self.conf):
-            topo = self.doc.createElement('topology')
             vcpus = int(self.conf.get('smp', '1'))
             cores = int(self.conf.get('smpCoresPerSocket', '1'))
             threads = int(self.conf.get('smpThreadsPerCore', '1'))
-            topo.setAttribute('sockets', str(vcpus / cores / threads))
-            topo.setAttribute('cores', str(cores))
-            topo.setAttribute('threads', str(threads))
+            topoAttrs = {'sockets': str(vcpus / cores / threads),
+                         'cores': str(cores), 'threads': str(threads)}
+            topo = XMLElem('topology', attrs=topoAttrs)
             cpu.appendChild(topo)
 
         #CPU-pinning support
@@ -776,9 +766,9 @@
             cputune = self.doc.createElement('cputune')
             cpuPinning = self.conf.get('cpuPinning')
             for cpuPin in cpuPinning.keys():
-                vcpupin = self.doc.createElement('vcpupin')
-                vcpupin.setAttribute('vcpu', cpuPin)
-                vcpupin.setAttribute('cpuset', cpuPinning[cpuPin])
+                vcpupin = XMLElem(
+                    'vcpupin', attrs={'vcpu': cpuPin,
+                                      'cpuset': cpuPinning[cpuPin]})
                 cputune.appendChild(vcpupin)
             self.dom.appendChild(cputune)
 
@@ -791,16 +781,9 @@
              <source mode='bind' path='/tmp/socket'/>
           </channel>
         """
-        channel = self.doc.createElement('channel')
-        channel.setAttribute('type', 'unix')
-        target = xml.dom.minidom.Element('target')
-        target.setAttribute('type', 'virtio')
-        target.setAttribute('name', name)
-        source = xml.dom.minidom.Element('source')
-        source.setAttribute('mode', 'bind')
-        source.setAttribute('path', path)
-        channel.appendChild(target)
-        channel.appendChild(source)
+        channel = XMLElem('channel', attrs={'type': 'unix'})
+        channel.addChild('target', attrs={'type': 'virtio', 'name': name})
+        channel.addChild('source', attrs={'mode': 'bind', 'path': path})
         self._devices.appendChild(channel)
 
     def appendInput(self):
@@ -809,14 +792,11 @@
 
         <input bus="ps2" type="mouse"/>
         """
-        input = self.doc.createElement('input')
         if utils.tobool(self.conf.get('tabletEnable')):
-            input.setAttribute('type', 'tablet')
-            input.setAttribute('bus', 'usb')
+            inputAttrs = {'type': 'tablet', 'bus': 'usb'}
         else:
-            input.setAttribute('type', 'mouse')
-            input.setAttribute('bus', 'ps2')
-        self._devices.appendChild(input)
+            inputAttrs = {'type': 'mouse', 'bus': 'ps2'}
+        self._devices.appendChild(XMLElem('input', attrs=inputAttrs))
 
     def appendGraphics(self):
         """
@@ -833,45 +813,41 @@
            <target type='virtio' name='com.redhat.spice.0'/>
         </channel>
         """
-        graphics = self.doc.createElement('graphics')
+        graphicsAttrs = {'port': self.conf['displayPort'], 'autoport': 'yes'}
         if self.conf['display'] == 'vnc':
-            graphics.setAttribute('type', 'vnc')
-            graphics.setAttribute('port', self.conf['displayPort'])
-            graphics.setAttribute('autoport', 'yes')
+            graphicsAttrs['type'] = 'vnc'
         elif 'qxl' in self.conf['display']:
-            graphics.setAttribute('type', 'spice')
-            graphics.setAttribute('port', self.conf['displayPort'])
-            graphics.setAttribute('tlsPort', self.conf['displaySecurePort'])
-            graphics.setAttribute('autoport', 'yes')
+            graphicsAttrs['type'] = 'spice'
+            graphicsAttrs['tlsPort'] = self.conf['displaySecurePort']
+
+        if self.conf.get('keyboardLayout'):
+            graphicsAttrs['keymap'] = self.conf['keyboardLayout']
+        if not 'spiceDisableTicketing' in self.conf:
+            graphicsAttrs['passwd'] = '*****'
+            graphicsAttrs['passwdValidTo'] = '1970-01-01T00:00:01'
+
+        graphics = XMLElem('graphics', attrs=graphicsAttrs)
+
+        if 'qxl' in self.conf['display']:
             if self.conf.get('spiceSecureChannels'):
                 for channel in self.conf['spiceSecureChannels'].split(','):
-                    m = self.doc.createElement('channel')
-                    m.setAttribute('name', channel[1:])
-                    m.setAttribute('mode', 'secure')
-                    graphics.appendChild(m)
+                    graphics.addChild('channel', attrs={'name': channel[1:],
+                                                        'mode': 'secure'})
 
-            vmc = self.doc.createElement('channel')
-            vmc.setAttribute('type', 'spicevmc')
-            m = self.doc.createElement('target')
-            m.setAttribute('type', 'virtio')
-            m.setAttribute('name', 'com.redhat.spice.0')
-            vmc.appendChild(m)
+            vmc = XMLElem('channel', attrs={'type': 'spicevmc'})
+            vmc.addChild('target', attrs={'type': 'virtio',
+                                          'name': 'com.redhat.spice.0'})
+
             self._devices.appendChild(vmc)
 
         if self.conf.get('displayNetwork'):
-            listen = self.doc.createElement('listen')
-            listen.setAttribute('type', 'network')
-            listen.setAttribute('network', netinfo.LIBVIRT_NET_PREFIX +
-                                self.conf.get('displayNetwork'))
-            graphics.appendChild(listen)
+            listenAttrs = {'type': 'network',
+                           'network': netinfo.LIBVIRT_NET_PREFIX +
+                           self.conf.get('displayNetwork')}
+            graphics.appendChild(XMLElem('listen', attrs=listenAttrs))
         else:
             graphics.setAttribute('listen', '0')
 
-        if self.conf.get('keyboardLayout'):
-            graphics.setAttribute('keymap', self.conf['keyboardLayout'])
-        if not 'spiceDisableTicketing' in self.conf:
-            graphics.setAttribute('passwd', '*****')
-            graphics.setAttribute('passwdValidTo', '1970-01-01T00:00:01')
         self._devices.appendChild(graphics)
 
     def toxml(self):
@@ -883,11 +859,11 @@
         """
         Create domxml device element according to passed in params
         """
-        doc = xml.dom.minidom.Document()
-        element = doc.createElement(elemType)
+        childs = []
+        elemAttrs = {}
 
         if deviceType:
-            element.setAttribute('type', deviceType)
+            elemAttrs['type'] = deviceType
 
         for attrName in attributes:
             if not hasattr(self, attrName):
@@ -895,13 +871,13 @@
 
             attr = getattr(self, attrName)
             if isinstance(attr, dict):
-                child = doc.createElement(attrName)
-                for key, value in attr.iteritems():
-                    child.setAttribute(key, value)
-                element.appendChild(child)
+                childs.append(XMLElem(attrName, attrs=attr))
             else:
-                element.setAttribute(attrName, attr)
+                elemAttrs[attrName] = attr
 
+        element = XMLElem(elemType, attrs=elemAttrs)
+        for child in childs:
+            element.appendChild(child)
         return element
 
 
@@ -935,13 +911,10 @@
         """
         Create domxml for video device
         """
-        doc = xml.dom.minidom.Document()
         video = self.createXmlElem('video', None, ['address'])
-        m = doc.createElement('model')
-        m.setAttribute('type', self.device)
-        m.setAttribute('vram', self.specParams['vram'])
-        m.setAttribute('heads', '1')
-        video.appendChild(m)
+        video.addChild('model', attrs={'type': self.device,
+                                       'vram': self.specParams['vram'],
+                                       'heads': '1'})
 
         return video
 
@@ -1009,40 +982,26 @@
             [<link state='up|down'/>]
         </interface>
         """
-        doc = xml.dom.minidom.Document()
         iface = self.createXmlElem('interface', self.device, ['address'])
-        m = doc.createElement('mac')
-        m.setAttribute('address', self.macAddr)
-        iface.appendChild(m)
-        m = doc.createElement('model')
-        m.setAttribute('type', self.nicModel)
-        iface.appendChild(m)
-        m = doc.createElement('source')
-        m.setAttribute('bridge', self.network)
-        iface.appendChild(m)
+        iface.addChild('mac', attrs={'address': self.macAddr})
+        iface.addChild('model', attrs={'type': self.nicModel})
+        iface.addChild('source', attrs={'bridge': self.network})
         if hasattr(self, 'filter'):
-            m = doc.createElement('filterref')
-            m.setAttribute('filter', self.filter)
-            iface.appendChild(m)
+            iface.addChild('filterref', attrs={'filter': self.filter})
+
         if hasattr(self, 'linkActive'):
-            m = doc.createElement('link')
-            m.setAttribute('state',
-                           'up' if utils.tobool(self.linkActive) else 'down')
-            iface.appendChild(m)
+            iface.addChild('link', attrs={'state': 'up'
+                                          if utils.tobool(self.linkActive)
+                                          else 'down'})
+
         if hasattr(self, 'bootOrder'):
-            bootOrder = doc.createElement('boot')
-            bootOrder.setAttribute('order', self.bootOrder)
-            iface.appendChild(bootOrder)
+            iface.addChild('boot', attrs={'order': self.bootOrder})
+
         if self.driver:
-            m = doc.createElement('driver')
-            m.setAttribute('name', self.driver)
-            iface.appendChild(m)
+            iface.addChild('driver', attrs={'name': self.driver})
+
         if self.sndbufParam:
-            tune = doc.createElement('tune')
-            sndbuf = doc.createElement('sndbuf')
-            sndbuf.appendChild(doc.createTextNode(self.sndbufParam))
-            tune.appendChild(sndbuf)
-            iface.appendChild(tune)
+            iface.addChild('tune').addChild('sndbuf', text=self.sndbufParam)
 
         return iface
 
@@ -1144,65 +1103,58 @@
           <serial>54-a672-23e5b495a9ea</serial>
         </disk>
         """
-        doc = xml.dom.minidom.Document()
         self.device = getattr(self, 'device', 'disk')
-        source = doc.createElement('source')
         if self.blockDev:
             deviceType = 'block'
-            source.setAttribute('dev', self.path)
+            sourceAttrs = {'dev': self.path}
         else:
             deviceType = 'file'
-            source.setAttribute('file', self.path)
+            sourceAttrs = {'file': self.path}
             if self.device == 'cdrom' or self.device == 'floppy':
-                source.setAttribute('startupPolicy', 'optional')
+                sourceAttrs['startupPolicy'] = 'optional'
         diskelem = self.createXmlElem('disk', deviceType,
                                       ['device', 'address'])
         diskelem.setAttribute('snapshot', 'no')
-        diskelem.appendChild(source)
-        target = doc.createElement('target')
-        target.setAttribute('dev', self.name)
-        if self.iface:
-            target.setAttribute('bus', self.iface)
-        diskelem.appendChild(target)
-        if hasattr(self, 'shared') and utils.tobool(self.shared):
-            shareable = doc.createElement('shareable')
-            diskelem.appendChild(shareable)
-        if hasattr(self, 'readonly') and utils.tobool(self.readonly):
-            readonly = doc.createElement('readonly')
-            diskelem.appendChild(readonly)
-        if hasattr(self, 'serial'):
-            serial = doc.createElement('serial')
-            serial.appendChild(doc.createTextNode(self.serial))
-            diskelem.appendChild(serial)
-        if hasattr(self, 'bootOrder'):
-            bootOrder = doc.createElement('boot')
-            bootOrder.setAttribute('order', self.bootOrder)
-            diskelem.appendChild(bootOrder)
-        if self.device == 'disk':
-            driver = doc.createElement('driver')
-            driver.setAttribute('name', 'qemu')
-            if self.blockDev:
-                driver.setAttribute('io', 'native')
-            else:
-                driver.setAttribute('io', 'threads')
-            if self.format == 'cow':
-                driver.setAttribute('type', 'qcow2')
-            elif self.format:
-                driver.setAttribute('type', 'raw')
+        diskelem.addChild('source', attrs=sourceAttrs)
 
-            driver.setAttribute('cache', self.cache)
+        targetAttrs = {'dev': self.name}
+        if self.iface:
+            targetAttrs['bus'] = self.iface
+        diskelem.addChild('target', attrs=targetAttrs)
+
+        if hasattr(self, 'shared') and utils.tobool(self.shared):
+            diskelem.addChild('shareable')
+        if hasattr(self, 'readonly') and utils.tobool(self.readonly):
+            diskelem.addChild('readonly')
+        if hasattr(self, 'serial'):
+            diskelem.addChild('serial', text=self.serial)
+        if hasattr(self, 'bootOrder'):
+            diskelem.addChild('boot', attrs={'order': self.bootOrder})
+
+        if self.device == 'disk':
+            driverAttrs = {'name': 'qemu'}
+            if self.blockDev:
+                driverAttrs['io'] = 'native'
+            else:
+                driverAttrs['io'] = 'threads'
+            if self.format == 'cow':
+                driverAttrs['type'] = 'qcow2'
+            elif self.format:
+                driverAttrs['type'] = 'raw'
+
+            driverAttrs['cache'] = self.cache
 
             if (self.propagateErrors == 'on' or
                     utils.tobool(self.propagateErrors)):
-                driver.setAttribute('error_policy', 'enospace')
+                driverAttrs['error_policy'] = 'enospace'
             else:
-                driver.setAttribute('error_policy', 'stop')
-            diskelem.appendChild(driver)
+                driverAttrs['error_policy'] = 'stop'
+            diskelem.addChild('driver', attrs=driverAttrs)
         elif self.device == 'floppy':
             if (self.path and
                 not utils.getUserPermissions(constants.QEMU_PROCESS_USER,
                                              self.path)['write']):
-                diskelem.appendChild(doc.createElement('readonly'))
+                diskelem.addChild('readonly')
 
         return diskelem
 
@@ -1234,8 +1186,8 @@
         </watchdog>
         """
         m = self.createXmlElem(self.type, None, ['address'])
-        m.setAttribute('model', self.specParams['model'])
-        m.setAttribute('action', self.specParams['action'])
+        m.setAttrs({'model': self.specParams['model'],
+                    'action': self.specParams['action']})
         return m
 
 
@@ -1260,10 +1212,8 @@
           <target type='virtio' port='0'/>
         </console>
         """
-        target = self.createXmlElem('target', 'virtio')
-        target.setAttribute('port', '0')
         m = self.createXmlElem('console', 'pty')
-        m.appendChild(target)
+        m.addChild('target', attrs={'type': 'virtio', 'port': '0'})
         return m
 
 
@@ -1300,23 +1250,11 @@
         </lease>
         """
 
-        doc = xml.dom.minidom.Document()
-
-        keyElem = doc.createElement('key')
-        keyElem.appendChild(doc.createTextNode(volumeID))
-
-        lksElem = doc.createElement('lockspace')
-        lksElem.appendChild(doc.createTextNode(domainID))
-
-        tgtElem = doc.createElement('target')
-        tgtElem.setAttribute('path', leasePath)
-        tgtElem.setAttribute('offset', str(leaseOffset))
-
-        leaseElem = doc.createElement('lease')
-        leaseElem.appendChild(keyElem)
-        leaseElem.appendChild(lksElem)
-        leaseElem.appendChild(tgtElem)
-
+        leaseElem = XMLElem('lease')
+        leaseElem.addChild('key', text=volumeID)
+        leaseElem.addChild('lockspace', text=domainID)
+        leaseElem.addChild('target', attrs={'path': leasePath,
+                                            'offset': str(leaseOffset)})
         return leaseElem
 
     def _buildCmdLine(self):
@@ -1979,14 +1917,9 @@
         def _diskSnapshot(vmDev, newPath):
             """Libvirt snapshot XML"""
 
-            disk = xml.dom.minidom.Element('disk')
-            disk.setAttribute('name', vmDev)
-            disk.setAttribute('snapshot', 'external')
-
-            source = xml.dom.minidom.Element('source')
-            source.setAttribute('file', newPath)
-
-            disk.appendChild(source)
+            disk = XMLElem('disk', attrs={'name': vmDev,
+                                          'snapshot': 'external'})
+            disk.addChild('source', attrs={'file': newPath})
             return disk
 
         def _normSnapDriveParams(drive):
@@ -2421,15 +2354,9 @@
             return {'status': {'code': errCode['imageErr']['status']['code'],
                                'message': errCode['imageErr']['status']
                                                  ['message'] % str(e)}}
-        diskelem = xml.dom.minidom.Element('disk')
-        diskelem.setAttribute('type', 'file')
-        diskelem.setAttribute('device', vmDev)
-        source = xml.dom.minidom.Element('source')
-        source.setAttribute('file', path)
-        diskelem.appendChild(source)
-        target = xml.dom.minidom.Element('target')
-        target.setAttribute('dev', blockdev)
-        diskelem.appendChild(target)
+        diskelem = XMLElem('disk', attrs={'type': 'file', 'device': vmDev})
+        diskelem.addChild('source', attrs={'file': path})
+        diskelem.addChild('target', attrs={'dev': blockdev})
 
         try:
             self._dom.updateDeviceFlags(


--
To view, visit http://gerrit.ovirt.org/10054
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib936740fcbeeee551e13abfed7fd91f2a159e351
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Mark Wu <wudxw at linux.vnet.ibm.com>


More information about the vdsm-patches mailing list