<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 12/24/2013 04:19 AM, Mark Wu wrote:<br>
</div>
<blockquote cite="mid:52B92773.4010900@linux.vnet.ibm.com"
type="cite">On 12/24/2013 02:41 AM, Aline Manera wrote:
<br>
<blockquote type="cite">From: Aline Manera
<a class="moz-txt-link-rfc2396E" href="mailto:alinefm@br.ibm.com"><alinefm@br.ibm.com></a>
<br>
<br>
Resource(), Collection() and AsyncCollection classes are base
for all Kimchi
<br>
resources.
<br>
Move them to a separated file as they should not be changed with
high frequency
<br>
<br>
Signed-off-by: Aline Manera <a class="moz-txt-link-rfc2396E" href="mailto:alinefm@br.ibm.com"><alinefm@br.ibm.com></a>
<br>
---
<br>
Makefile.am | 1 +
<br>
src/kimchi/control/base.py | 290
++++++++++++++++++++++++++++++++++++++++++++
<br>
2 files changed, 291 insertions(+)
<br>
create mode 100644 src/kimchi/control/base.py
<br>
<br>
diff --git a/Makefile.am b/Makefile.am
<br>
index 65b39b8..997d4cc 100644
<br>
--- a/Makefile.am
<br>
+++ b/Makefile.am
<br>
@@ -45,6 +45,7 @@ PEP8_WHITELIST = \
<br>
src/kimchi/disks.py \
<br>
src/kimchi/root.py \
<br>
src/kimchi/server.py \
<br>
+ src/kimchi/control/base.py \
<br>
src/kimchi/control/utils.py \
<br>
plugins/__init__.py \
<br>
plugins/sample/__init__.py \
<br>
diff --git a/src/kimchi/control/base.py
b/src/kimchi/control/base.py
<br>
new file mode 100644
<br>
index 0000000..22452fc
<br>
--- /dev/null
<br>
+++ b/src/kimchi/control/base.py
<br>
@@ -0,0 +1,290 @@
<br>
+#
<br>
+# Project Kimchi
<br>
+#
<br>
+# Copyright IBM, Corp. 2013
<br>
+#
<br>
+# Authors:
<br>
+# Adam Litke <a class="moz-txt-link-rfc2396E" href="mailto:agl@linux.vnet.ibm.com"><agl@linux.vnet.ibm.com></a>
<br>
+# Aline Manera <a class="moz-txt-link-rfc2396E" href="mailto:alinefm@linix.vnet.ibm.com"><alinefm@linix.vnet.ibm.com></a>
<br>
+#
<br>
+# This library is free software; you can redistribute it and/or
<br>
+# modify it under the terms of the GNU Lesser General Public
<br>
+# License as published by the Free Software Foundation; either
<br>
+# version 2.1 of the License, or (at your option) any later
version.
<br>
+#
<br>
+# This library is distributed in the hope that it will be
useful,
<br>
+# but WITHOUT ANY WARRANTY; without even the implied warranty
of
<br>
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU
<br>
+# Lesser General Public License for more details.
<br>
+#
<br>
+# You should have received a copy of the GNU Lesser General
Public
<br>
+# License along with this library; if not, write to the Free
Software
<br>
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA
<br>
+
<br>
+import cherrypy
<br>
+import urllib2
<br>
+
<br>
+
<br>
+import kimchi.template
<br>
+from kimchi.control.utils import get_class_name,
internal_redirect, model_fn
<br>
+from kimchi.control.utils import parse_request, validate_method
<br>
+from kimchi.control.utils import validate_params
<br>
+from kimchi.exception import InvalidOperation, InvalidParameter
<br>
+from kimchi.exception import MissingParameter, NotFoundError,
OperationFailed
<br>
+
<br>
+
<br>
+class Resource(object):
<br>
+ """
<br>
+ A Resource represents a single entity in the API (such as a
Virtual
<br>
+ Machine)
<br>
+
<br>
+ To create new Resource types, subclass this and change the
following things
<br>
+ in the child class:
<br>
+
<br>
+ - If the Resource requires more than one identifier set
self.model_args as
<br>
+ appropriate. This should only be necessary if this
Resource is logically
<br>
+ nested. For example: A Storage Volume belongs to a
Storage Pool so the
<br>
+ Storage Volume would set model args to (pool_ident,
volume_ident).
<br>
+
<br>
+ - Implement the base operations of 'lookup' and 'delete' in
the model(s).
<br>
+
<br>
+ - Set the 'data' property to a JSON-serializable
representation of the
<br>
+ Resource.
<br>
+ """
<br>
+ def __init__(self, model, ident=None):
<br>
+ self.model = model
<br>
+ self.ident = ident
<br>
+ self.model_args = (ident,)
<br>
+ self.update_params = []
<br>
+
<br>
+ def generate_action_handler(self, instance, action_name,
action_args=None):
<br>
+ def wrapper(*args, **kwargs):
<br>
+ validate_method(('POST'))
<br>
+ try:
<br>
+ model_args = list(instance.model_args)
<br>
+ if action_args is not None:
<br>
+ model_args.extend(parse_request()[key]
<br>
+ for key in action_args)
<br>
+ fn = getattr(instance.model, model_fn(instance,
action_name))
<br>
+ fn(*model_args)
<br>
+ uri_params = tuple(instance.model_args)
<br>
+ raise internal_redirect(instance.uri_fmt %
uri_params)
<br>
+ except MissingParameter, param:
<br>
+ error = "Missing parameter: '%s'" % param
<br>
+ raise cherrypy.HTTPError(400, error)
<br>
+ except InvalidParameter, param:
<br>
+ error = "Invalid parameter: '%s'" % param
<br>
+ raise cherrypy.HTTPError(400, error)
<br>
+ except InvalidOperation, msg:
<br>
+ raise cherrypy.HTTPError(400, "Invalid
operation: '%s'" % msg)
<br>
+ except OperationFailed, msg:
<br>
+ raise cherrypy.HTTPError(500, "Operation
Failed: '%s'" % msg)
<br>
+ except NotFoundError, msg:
<br>
+ raise cherrypy.HTTPError(404, "Not found: '%s'"
% msg)
<br>
+
<br>
+ wrapper.__name__ = action_name
<br>
+ wrapper.exposed = True
<br>
+ return wrapper
<br>
+
<br>
+ def lookup(self):
<br>
+ try:
<br>
+ lookup = getattr(self.model, model_fn(self,
'lookup'))
<br>
+ self.info = lookup(*self.model_args)
<br>
+ except AttributeError:
<br>
+ self.info = {}
<br>
+
<br>
+ def delete(self):
<br>
+ try:
<br>
+ fn = getattr(self.model, model_fn(self, 'delete'))
<br>
+ fn(*self.model_args)
<br>
+ cherrypy.response.status = 204
<br>
+ except AttributeError:
<br>
+ error = "Delete is not allowed for %s" %
get_class_name(self)
<br>
+ raise cherrypy.HTTPError(405, error)
<br>
+ except OperationFailed, msg:
<br>
+ raise cherrypy.HTTPError(500, "Operation Failed:
'%s'" % msg)
<br>
+ except InvalidOperation, msg:
<br>
+ raise cherrypy.HTTPError(400, "Invalid operation:
'%s'" % msg)
<br>
+
<br>
+ @cherrypy.expose
<br>
+ def index(self):
<br>
+ method = validate_method(('GET', 'DELETE', 'PUT'))
<br>
+ if method == 'GET':
<br>
+ try:
<br>
+ return self.get()
<br>
+ except NotFoundError, msg:
<br>
+ raise cherrypy.HTTPError(404, "Not found: '%s'"
% msg)
<br>
+ except InvalidOperation, msg:
<br>
+ raise cherrypy.HTTPError(400, "Invalid
operation: '%s'" % msg)
<br>
+ except OperationFailed, msg:
<br>
+ raise cherrypy.HTTPError(406, "Operation
failed: '%s'" % msg)
<br>
+ elif method == 'DELETE':
<br>
+ try:
<br>
+ return self.delete()
<br>
+ except NotFoundError, msg:
<br>
+ raise cherrypy.HTTPError(404, "Not found: '%s'"
% msg)
<br>
+ elif method == 'PUT':
<br>
+ try:
<br>
+ return self.update()
<br>
+ except InvalidParameter, msg:
<br>
+ raise cherrypy.HTTPError(400, "Invalid
parameter: '%s'" % msg)
<br>
+ except InvalidOperation, msg:
<br>
+ raise cherrypy.HTTPError(400, "Invalid
operation: '%s'" % msg)
<br>
+ except NotFoundError, msg:
<br>
+ raise cherrypy.HTTPError(404, "Not found: '%s'"
% msg)
<br>
+
<br>
+ def update(self):
<br>
+ try:
<br>
+ update = getattr(self.model, model_fn(self,
'update'))
<br>
+ except AttributeError:
<br>
+ error = "%s does not implement update method"
<br>
+ raise cherrypy.HTTPError(405, error %
get_class_name(self))
<br>
+
<br>
+ params = parse_request()
<br>
+ validate_params(params, self, 'update')
<br>
+
<br>
+ if self.update_params is not None:
<br>
+ invalids = [v for v in params.keys() if
<br>
+ v not in self.update_params]
<br>
+ if invalids:
<br>
+ error = "%s are not allowed to be updated" %
invalids
<br>
+ raise cherrypy.HTTPError(405, error)
<br>
+
<br>
+ ident = update(self.ident, params)
<br>
+ if ident != self.ident:
<br>
+ uri_params = list(self.model_args[:-1])
<br>
+ uri_params += [urllib2.quote(ident.encode('utf8'))]
<br>
+ raise cherrypy.HTTPRedirect(self.uri_fmt %
tuple(uri_params), 303)
<br>
+
<br>
+ return self.get()
<br>
+
<br>
+ def get(self):
<br>
+ self.lookup()
<br>
+ return kimchi.template.render(get_class_name(self),
self.data)
<br>
+
<br>
+ @property
<br>
+ def data(self):
<br>
+ """
<br>
+ Override this in inherited classes to provide the
Resource
<br>
+ representation as a python dictionary.
<br>
+ """
<br>
+ return {}
<br>
+
<br>
+
<br>
+class Collection(object):
<br>
+ """
<br>
+ A Collection is a container for Resource objects. To
create a new
<br>
+ Collection type, subclass this and make the following
changes to the child
<br>
+ class:
<br>
+
<br>
+ - Set self.resource to the type of Resource that this
Collection contains
<br>
+
<br>
+ - Set self.resource_args. This can remain an empty list if
the Resources
<br>
+ can be initialized with only one identifier. Otherwise,
include
<br>
+ additional values as needed (eg. to identify a parent
resource).
<br>
+
<br>
+ - Set self.model_args. Similar to above, this is needed
only if the model
<br>
+ needs additional information to identify this Collection.
<br>
+
<br>
+ - Implement the base operations of 'create' and 'get_list'
in the model.
<br>
+ """
<br>
+ def __init__(self, model):
<br>
+ self.model = model
<br>
+ self.resource = Resource
<br>
+ self.resource_args = []
<br>
+ self.model_args = []
<br>
+
<br>
+ def create(self, *args):
<br>
+ try:
<br>
+ create = getattr(self.model, model_fn(self,
'create'))
<br>
+ except AttributeError:
<br>
+ error = 'Create is not allowed for %s' %
get_class_name(self)
<br>
+ raise cherrypy.HTTPError(405, error)
<br>
+
<br>
+ params = parse_request()
<br>
+ validate_params(params, self, 'create')
<br>
+ args = self.model_args + [params]
<br>
+ name = create(*args)
<br>
+ cherrypy.response.status = 201
<br>
+ args = self.resource_args + [name]
<br>
+ res = self.resource(self.model, *args)
<br>
+
<br>
+ return res.get()
<br>
+
<br>
+ def _get_resources(self):
<br>
+ try:
<br>
+ get_list = getattr(self.model, model_fn(self,
'get_list'))
<br>
+ idents = get_list(*self.model_args)
<br>
+ res_list = []
<br>
+ for ident in idents:
<br>
+ # internal text, get_list changes ident to
unicode for sorted
<br>
+ args = self.resource_args + [ident]
<br>
+ res = self.resource(self.model, *args)
<br>
+ res.lookup()
<br>
+ res_list.append(res)
<br>
+ return res_list
<br>
+ except AttributeError:
<br>
+ return []
<br>
+
<br>
+ def _cp_dispatch(self, vpath):
<br>
+ if vpath:
<br>
+ ident = vpath.pop(0)
<br>
+ # incoming text, from URL, is not unicode, need
decode
<br>
+ args = self.resource_args + [ident.decode("utf-8")]
<br>
+ return self.resource(self.model, *args)
<br>
+
<br>
+ def get(self):
<br>
+ resources = self._get_resources()
<br>
+ data = []
<br>
+ for res in resources:
<br>
+ data.append(res.data)
<br>
+ return kimchi.template.render(get_class_name(self),
data)
<br>
+
<br>
+ @cherrypy.expose
<br>
+ def index(self, *args):
<br>
+ method = validate_method(('GET', 'POST'))
<br>
+ if method == 'GET':
<br>
+ try:
<br>
+ return self.get()
<br>
+ except InvalidOperation, param:
<br>
+ error = "Invalid operation: '%s'" % param
<br>
+ raise cherrypy.HTTPError(400, error)
<br>
+ except NotFoundError, param:
<br>
+ raise cherrypy.HTTPError(404, "Not found: '%s'"
% param)
<br>
+
<br>
+ elif method == 'POST':
<br>
+ try:
<br>
+ return self.create(*args)
<br>
+ except MissingParameter, param:
<br>
+ error = "Missing parameter: '%s'" % param
<br>
+ raise cherrypy.HTTPError(400, error)
<br>
+ except InvalidParameter, param:
<br>
+ error = "Invalid parameter: '%s'" % param
<br>
+ raise cherrypy.HTTPError(400, error)
<br>
+ except OperationFailed, param:
<br>
+ raise cherrypy.HTTPError(500, "Operation
Failed: '%s'" % param)
<br>
+ except InvalidOperation, param:
<br>
+ error = "Invalid operation: '%s'" % param
<br>
+ raise cherrypy.HTTPError(400, error)
<br>
+ except NotFoundError, param:
<br>
+ raise cherrypy.HTTPError(404, "Not found: '%s'"
% param)
<br>
+
<br>
+
<br>
+class AsyncCollection(Collection):
<br>
+ """
<br>
+ A Collection to create it's resource by asynchronous task
<br>
+ """
<br>
+ def __init__(self, model):
<br>
+ super(AsyncCollection, self).__init__(model)
<br>
+
<br>
+ def create(self, *args):
<br>
+ try:
<br>
+ create = getattr(self.model, model_fn(self,
'create'))
<br>
+ except AttributeError:
<br>
+ raise cherrypy.HTTPError(405,
<br>
+ 'Create is not allowed for %s' %
get_class_name(self))
<br>
+ params = parse_request()
<br>
+ args = self.model_args + [params]
<br>
+ task = create(*args)
<br>
+ cherrypy.response.status = 202
<br>
+ return kimchi.template.render("Task", task)
<br>
</blockquote>
Pep8 error:
<br>
src/kimchi/control/base.py:285:17: E128 continuation line
under-indented for visual indent
<br>
<br>
</blockquote>
<font face="DejaVu Sans Mono"><br>
Mark, I didn't get this error. <br>
Maybe may pep8 version is different from yours.<br>
<br>
alinefm@alinefm:~/kimchi$ sudo make check-local<br>
/usr/bin/pep8 --version<br>
1.2<br>
/usr/bin/pep8 --filename '*.py,*.py.in' src/kimchi/asynctask.py
src/kimchi/auth.py src/kimchi/cachebust.py src/kimchi/config.py.in
src/kimchi/disks.py src/kimchi/root.py src/kimchi/server.py
src/kimchi/control/base.py src/kimchi/control/config.py
src/kimchi/control/debugreports.py src/kimchi/control/host.py
src/kimchi/control/interfaces.py src/kimchi/control/networks.py
src/kimchi/control/plugins.py src/kimchi/control/storagepools.py
src/kimchi/control/storagevolumes.py src/kimchi/control/tasks.py
src/kimchi/control/templates.py src/kimchi/control/utils.py
src/kimchi/control/vms.py plugins/__init__.py
plugins/sample/__init__.py plugins/sample/model.py
tests/test_plugin.py <br>
<br>
</font>
</body>
</html>