From: ShaoHe Feng <shaohef(a)linux.vnet.ibm.com>
When clone template, if no name in parameters, kimchi will set a
default name.
Then copy the info from existing template and create a new one.
Signed-off-by: ShaoHe Feng <shaohef(a)linux.vnet.ibm.com>
---
src/kimchi/mockmodel.py | 19 +++++++++++++++++++
src/kimchi/model/templates.py | 20 ++++++++++++++++++++
2 files changed, 39 insertions(+)
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index 4e276eb..f66de3a 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -44,6 +44,7 @@ except ImportError:
from kimchi import config
from kimchi.asynctask import AsyncTask
from kimchi.config import config as kconfig
+from kimchi.control.utils import parse_request
from kimchi.distroloader import DistroLoader
from kimchi.exception import InvalidOperation, InvalidParameter
from kimchi.exception import MissingParameter, NotFoundError, OperationFailed
@@ -202,6 +203,24 @@ class MockModel(object):
self._mock_templates[name] = t
return name
+ def template_clone(self, name):
+ params = parse_request()
+ # set default name
+ if not params.get("name"):
+ subfixs = [v[len(name):] for v in self.templates_get_list()
+ if v.startswith(name)]
+ indexs = [int(v.lstrip("-clone")) for v in subfixs
+ if v.startswith("-clone") and
+ v.lstrip("-clone").isdigit()]
+ indexs.sort()
+ index = "1" if not indexs else str(indexs[-1] + 1)
+ params["name"] = name + "-clone" + index
+
+ temp = self.template_lookup(name)
+ temp.update(params)
+ ident = self.templates_create(temp)
+ raise cherrypy.HTTPRedirect("/templates/%s" % ident, 303)
+
def template_update(self, name, params):
old_t = self.template_lookup(name)
new_t = copy.copy(old_t)
diff --git a/src/kimchi/model/templates.py b/src/kimchi/model/templates.py
index 03632a6..e8eb396 100644
--- a/src/kimchi/model/templates.py
+++ b/src/kimchi/model/templates.py
@@ -22,9 +22,11 @@
import copy
+import cherrypy
import libvirt
from kimchi import xmlutils
+from kimchi.control.utils import parse_request
from kimchi.exception import InvalidOperation, InvalidParameter, NotFoundError
from kimchi.utils import pool_name_from_uri
from kimchi.vmtemplate import VMTemplate
@@ -85,6 +87,24 @@ class TemplateModel(object):
t = self.get_template(name, self.objstore, self.conn)
return t.info
+ def clone(self, name):
+ params = parse_request()
+ # set default name
+ if not params.get("name"):
+ subfixs = [v[len(name):] for v in self.templates.get_list()
+ if v.startswith(name)]
+ indexs = [int(v.lstrip("-clone")) for v in subfixs
+ if v.startswith("-clone") and
+ v.lstrip("-clone").isdigit()]
+ indexs.sort()
+ index = "1" if not indexs else str(indexs[-1] + 1)
+ params["name"] = name + "-clone" + index
+
+ temp = self.lookup(name)
+ temp.update(params)
+ ident = self.templates.create(temp)
+ raise cherrypy.HTTPRedirect("/templates/%s" % ident, 303)
+
def delete(self, name):
with self.objstore as session:
session.delete('template', name)
--
1.8.4.2