def collect_inner_elements(el, d): """ This helper method collects all nodes in el and adds them to dictionary d. :param el: XML DOM element object with text only children :param d: Dictionary to add the values to """ for chel in vmxml.children(el): try: d[vmxml.tag(chel)] = int(vmxml.text(chel)) except (IndexError, ValueError): log.exception("Invalid value for %s", vmxml.tag(chel))
def collect_inner_elements(el, d): """ This helper method collects all nodes in el and adds them to dictionary d. :param el: XML DOM element object with text only children :param d: Dictionary to add the values to """ for chel in vmxml.children(el): try: d[vmxml.tag(chel)] = int(vmxml.text(chel)) except (IndexError, ValueError): log.exception("Invalid value for %s", vmxml.tag(chel))
def _update_unknown_device_info(vm): """ Obtain info about unknown devices from libvirt domain and update the corresponding device structures. Unknown device is a device that has an address but wasn't passed during VM creation request. :param vm: VM for which the device info should be updated :type vm: `class:Vm` instance """ def isKnownDevice(alias): for dev in vm.conf['devices']: if dev.get('alias') == alias: return True return False for x in vmxml.children(vm.domain.devices): # Ignore empty nodes and devices without address if vmxml.find_first(x, 'address', None) is None: continue alias = core.find_device_alias(x) if not isKnownDevice(alias): address = vmxml.device_address(x) # In general case we assume that device has attribute 'type', # if it hasn't dom_attribute returns ''. device = vmxml.attr(x, 'type') newDev = { 'type': vmxml.tag(x), 'alias': alias, 'device': device, 'address': address } vm.conf['devices'].append(newDev)
def _update_unknown_device_info(vm): """ Obtain info about unknown devices from libvirt domain and update the corresponding device structures. Unknown device is a device that has an address but wasn't passed during VM creation request. :param vm: VM for which the device info should be updated :type vm: `class:Vm` instance """ def isKnownDevice(alias): for dev in vm.conf['devices']: if dev.get('alias') == alias: return True return False for x in vmxml.children(vm.domain.devices): # Ignore empty nodes and devices without address if vmxml.find_first(x, 'address', None) is None: continue alias = core.find_device_alias(x) if not isKnownDevice(alias): address = vmxml.device_address(x) # In general case we assume that device has attribute 'type', # if it hasn't dom_attribute returns ''. device = vmxml.attr(x, 'type') newDev = {'type': vmxml.tag(x), 'alias': alias, 'device': device, 'address': address} vm.conf['devices'].append(newDev)
def test_find_all(self, start_tag, tag, number): dom = self._dom if start_tag is not None: dom = vmxml.find_first(self._dom, 'topelement') elements = vmxml.find_all(dom, tag) matches = [vmxml.tag(e) == tag for e in elements] self.assertTrue(all(matches)) self.assertEqual(len(matches), number)
def test_find_all(self, start_tag, tag, number): dom = self._dom if start_tag is not None: dom = vmxml.find_first(self._dom, 'topelement') elements = vmxml.find_all(dom, tag) matches = [vmxml.tag(e) == tag for e in elements] self.assertTrue(all(matches)) self.assertEqual(len(matches), number)
def get_bandwidth_xml(specParams, oldBandwidth=None): """Returns a valid libvirt xml dom element object.""" bandwidth = vmxml.Element('bandwidth') old = {} if oldBandwidth is None else dict( (vmxml.tag(elem), elem) for elem in vmxml.children(oldBandwidth)) for key in ('inbound', 'outbound'): elem = specParams.get(key) if elem is None: # Use the old setting if present if key in old: bandwidth.appendChild(etree_element=old[key]) elif elem: # Convert the values to string for adding them to the XML def attrs = dict((key, str(value)) for key, value in elem.items()) bandwidth.appendChildWithArgs(key, **attrs) return bandwidth