[Kimchi-devel] [PATCH][Kimchi 1/3] Issue #857: Support VM description

Ramon Medeiros ramonn at linux.vnet.ibm.com
Wed Aug 17 04:07:20 UTC 2016


Able Kimchi to add description and title while creating and updating vms

Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
---
 API.json      | 20 ++++++++++++++++++++
 i18n.py       |  2 ++
 model/vms.py  | 34 +++++++++++++++++++++++++++++++---
 vmtemplate.py |  5 ++++-
 4 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/API.json b/API.json
index 4fdd522..a3af02d 100644
--- a/API.json
+++ b/API.json
@@ -268,6 +268,16 @@
                     "pattern": "^[^/]*$",
                     "error": "KCHVM0011E"
                 },
+                "title": {
+                    "description": "Title of VM",
+                    "type": "string",
+                    "error": "KCHVM0085E"
+                },
+                "description": {
+                    "description": "Description of VM",
+                    "type": "string",
+                    "error": "KCHVM0086E"
+                },
                 "template": {
                     "description": "The URI of a template to use when building a VM",
                     "type": "string",
@@ -294,6 +304,16 @@
                     "minLength": 1,
                     "error": "KCHVM0011E"
                 },
+                "title": {
+                    "description": "Title of VM",
+                    "type": "string",
+                    "error": "KCHVM0085E"
+                },
+                "description": {
+                    "description": "Description of VM",
+                    "type": "string",
+                    "error": "KCHVM0086E"
+                },
                 "bootorder": {
                         "description": "Boot order",
                         "type": "array",
diff --git a/i18n.py b/i18n.py
index 7625469..e8d9c05 100644
--- a/i18n.py
+++ b/i18n.py
@@ -135,6 +135,8 @@ messages = {
     "KCHVM0082E": _("Either the guest %(name)s did not start to listen to the serial or it is not configured to use the serial console."),
     "KCHVM0083E": _("Unable to retrieve Virt Viewer file for stopped virtual machine %(name)s"),
     "KCHVM0084E": _("Error occured while retrieving the Virt Viewer file for virtual machine %(name)s : %(err)s"),
+    "KCHVM0085E": _("Virtual machine title must be a string"),
+    "KCHVM0086E": _("Virtual machine description must be a string"),
 
     "KCHVMHDEV0001E": _("VM %(vmid)s does not contain directly assigned host device %(dev_name)s."),
     "KCHVMHDEV0002E": _("The host device %(dev_name)s is not allowed to directly assign to VM."),
diff --git a/model/vms.py b/model/vms.py
index 433770a..3380278 100644
--- a/model/vms.py
+++ b/model/vms.py
@@ -83,7 +83,8 @@ VM_ONLINE_UPDATE_PARAMS = ['graphics', 'groups', 'memory', 'users']
 
 # update parameters which are updatable when the VM is offline
 VM_OFFLINE_UPDATE_PARAMS = ['cpu_info', 'graphics', 'groups', 'memory',
-                            'name', 'users', 'bootorder', 'bootmenu']
+                            'name', 'users', 'bootorder', 'bootmenu',
+                            'description', 'title']
 
 XPATH_DOMAIN_DISK = "/domain/devices/disk[@device='disk']/source/@file"
 XPATH_DOMAIN_DISK_BY_FILE = "./devices/disk[@device='disk']/source[@file='%s']"
@@ -98,10 +99,12 @@ XPATH_DOMAIN_DEV_CPU_ID = '/domain/devices/spapr-cpu-socket/@id'
 XPATH_BOOT = 'os/boot/@dev'
 XPATH_BOOTMENU = 'os/bootmenu/@enable'
 XPATH_CPU = './cpu'
+XPATH_DESCRIPTION = './description'
 XPATH_NAME = './name'
 XPATH_NUMA_CELL = './cpu/numa/cell'
 XPATH_SNAP_VM_NAME = './domain/name'
 XPATH_SNAP_VM_UUID = './domain/uuid'
+XPATH_TITLE = './title'
 XPATH_TOPOLOGY = './cpu/topology'
 XPATH_VCPU = './vcpu'
 XPATH_MAX_MEMORY = './maxMemory'
@@ -138,7 +141,9 @@ class VMsModel(object):
 
         t.validate()
         data = {'name': name, 'template': t,
-                'graphics': params.get('graphics', {})}
+                'graphics': params.get('graphics', {}),
+                "title": params.get("title", ""),
+                "description": params.get("description", "")}
         taskid = add_task(u'/plugins/kimchi/vms/%s' % name, self._create_task,
                           self.objstore, data)
 
@@ -152,6 +157,8 @@ class VMsModel(object):
             - name: The name for the new VM
         """
         vm_uuid = str(uuid.uuid4())
+        title = params.get('title', '')
+        description = params.get('description', '')
         t = params['template']
         name, nonascii_name = get_ascii_nonascii_name(params['name'])
         conn = self.conn.get()
@@ -178,7 +185,8 @@ class VMsModel(object):
         xml = t.to_vm_xml(name, vm_uuid,
                           libvirt_stream_protocols=stream_protocols,
                           graphics=graphics,
-                          mem_hotplug_support=self.caps.mem_hotplug_support)
+                          mem_hotplug_support=self.caps.mem_hotplug_support,
+                          title=title, description=description)
 
         cb('Defining new VM')
         try:
@@ -786,6 +794,24 @@ class VMModel(object):
             name, nonascii_name = get_ascii_nonascii_name(name)
             new_xml = xml_item_update(new_xml, XPATH_NAME, name, None)
 
+        if 'title' in params:
+            if len(xpath_get_text(new_xml, XPATH_TITLE)) > 0:
+                new_xml = xml_item_update(new_xml, XPATH_TITLE,
+                                          params['title'], None)
+            else:
+                et = ET.fromstring(new_xml)
+                et.append(E.title(params["title"]))
+                new_xml = ET.tostring(et)
+
+        if 'description' in params:
+            if len(xpath_get_text(new_xml, XPATH_DESCRIPTION)) > 0:
+                new_xml = xml_item_update(new_xml, XPATH_DESCRIPTION,
+                                          params['description'], None)
+            else:
+                et = ET.fromstring(new_xml)
+                et.append(E.description(params["description"]))
+                new_xml = ET.tostring(et)
+
         # Update CPU info
         cpu_info = params.get('cpu_info', {})
         cpu_info = self._update_cpu_info(new_xml, dom, cpu_info)
@@ -1267,6 +1293,8 @@ class VMModel(object):
             else "no"
 
         return {'name': name,
+                'title': "".join(xpath_get_text(xml, XPATH_TITLE)),
+                'description': "".join(xpath_get_text(xml, XPATH_DESCRIPTION)),
                 'state': state,
                 'stats': res,
                 'uuid': dom.UUIDString(),
diff --git a/vmtemplate.py b/vmtemplate.py
index dc81fe2..79730cf 100644
--- a/vmtemplate.py
+++ b/vmtemplate.py
@@ -347,7 +347,8 @@ class VMTemplate(object):
         params['qemu-stream-cmdline'] = ''
         params['disks'] = self._get_disks_xml(vm_uuid)
         params['serial'] = get_serial_xml(params)
-
+        params['title'] = kwargs.get('title', '')
+        params['description'] = kwargs.get('description', '')
         graphics = dict(self.info['graphics'])
         graphics.update(kwargs.get('graphics', {}))
         # Graphics is not supported on s390x, this check will
@@ -404,6 +405,8 @@ class VMTemplate(object):
         <domain type='%(domain)s'>
           %(qemu-stream-cmdline)s
           <name>%(name)s</name>
+          <title>%(title)s</title>
+          <description>%(description)s</description>
           <uuid>%(uuid)s</uuid>
           <memtune>
             <hard_limit unit='MiB'>%(hard_limit)s</hard_limit>
-- 
2.5.5




More information about the Kimchi-devel mailing list