[Kimchi-devel] [PATCH V2 2/2] use libvirt api to set user and group metadata by virDomain.setMetadata API
shaohef at linux.vnet.ibm.com
shaohef at linux.vnet.ibm.com
Mon Apr 21 09:15:38 UTC 2014
From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
add a method to wrap virDomain.setMetadata.
Use this method to set user and group metadata
Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
---
src/kimchi/model/vms.py | 28 ++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)
diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index 4997478..f0b36ed 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -253,7 +253,18 @@ class VMModel(object):
for group in groups:
access.append(E.group(group))
- return E.metadata(E.kimchi(access))
+ return access
+
+ def _update_metadata(self, dom, meta_xml, element, mode="persistent"):
+ flag = {"live": libvirt.VIR_DOMAIN_AFFECT_LIVE,
+ "persistent": libvirt.VIR_DOMAIN_AFFECT_CONFIG,
+ "current": libvirt.VIR_DOMAIN_AFFECT_CURRENT,
+ "all": libvirt.VIR_DOMAIN_AFFECT_CURRENT +
+ libvirt.VIR_DOMAIN_AFFECT_CONFIG if dom.isPersistent() else
+ libvirt.VIR_DOMAIN_AFFECT_CURRENT}
+ dom.setMetadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT, meta_xml,
+ "kimchi", KIMCHI_META_URL + element,
+ flags=flag[mode])
def _get_metadata_element(self, dom, element, mode="persistent"):
flag = {"live": libvirt.VIR_DOMAIN_AFFECT_LIVE,
@@ -275,9 +286,10 @@ class VMModel(object):
old_xml = new_xml = dom.XMLDesc(0)
- metadata_xpath = "/domain/metadata/kimchi/access/%s"
- users = xpath_get_text(old_xml, metadata_xpath % "user")
- groups = xpath_get_text(old_xml, metadata_xpath % "group")
+ access_xml = (self._get_metadata_element(dom, "access") or
+ """<access></access>""")
+ users = xpath_get_text(access_xml, "/access/user")
+ groups = xpath_get_text(access_xml, "/access/group")
for key, val in params.items():
if key == 'users':
@@ -311,12 +323,12 @@ class VMModel(object):
root = ET.fromstring(new_xml)
current_metadata = root.find('metadata')
new_metadata = self._get_metadata_node(users, groups)
- if current_metadata is not None:
- root.replace(current_metadata, new_metadata)
- else:
- root.append(new_metadata)
dom = conn.defineXML(ET.tostring(root))
+ meta = (new_metadata if current_metadata is not None
+ else current_metadata)
+ self._update_metadata(dom, ET.tostring(meta), 'access')
+
except libvirt.libvirtError as e:
dom = conn.defineXML(old_xml)
raise OperationFailed("KCHVM0008E", {'name': dom.name(),
--
1.9.0
More information about the Kimchi-devel
mailing list