[PATCHv8 0/8] storage server and target support

From: Royce Lv <lvroyce@linux.vnet.ibm.com> v7>v8, address model break, change xml construction to libxml v6>v7, adopt lxml to parse xml, move parse params to get() to avoid duplicate code, fix bugs when one server support multiple target type. v5>v6, change GET param support to cover more scenario of filter collected results. v4>v5, remove storage server list reload function, merge storage server and targets v3>v4, fix inconsistency between doc and json schema v1>v3, fix racing problem, fix style. Royce Lv (8): Support params for GET method Add testcase for GET param Storage server: Update API.md storage server: update controller.py storage server: Update model and mockmodel storage target: Update API.md storage target: Update controller and json schema storage target: Add model support docs/API.md | 22 +++++++++++ src/kimchi/API.json | 20 ++++++++++ src/kimchi/control/base.py | 24 ++++++++---- src/kimchi/control/storagepools.py | 4 +- src/kimchi/control/storageserver.py | 61 +++++++++++++++++++++++++++++ src/kimchi/control/storagevolumes.py | 2 +- src/kimchi/control/utils.py | 7 ++++ src/kimchi/mockmodel.py | 30 +++++++++++++++ src/kimchi/model.py | 75 ++++++++++++++++++++++++++++++++++++ src/kimchi/root.py | 2 + tests/test_rest.py | 36 +++++++++++++++++ 11 files changed, 272 insertions(+), 11 deletions(-) create mode 100644 src/kimchi/control/storageserver.py -- 1.8.1.2

From: Royce Lv <lvroyce@linux.vnet.ibm.com> GET filter parameter will take effect in two places: 1. process of query resources 2. final resource infomation filtering If you are adding some collection of get param, pls explicity add its param to API.json and wrap its model to accept filter param. Then we can call it like: GET /collection?filter_field=value Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- src/kimchi/control/base.py | 24 ++++++++++++++++-------- src/kimchi/control/storagepools.py | 4 ++-- src/kimchi/control/storagevolumes.py | 2 +- src/kimchi/control/utils.py | 7 +++++++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py index 185c8d8..ce3101d 100644 --- a/src/kimchi/control/base.py +++ b/src/kimchi/control/base.py @@ -28,7 +28,7 @@ import urllib2 import kimchi.template from kimchi.control.utils import get_class_name, internal_redirect, model_fn -from kimchi.control.utils import parse_request, validate_method +from kimchi.control.utils import parse_request, get_query_params, validate_method from kimchi.control.utils import validate_params from kimchi.exception import InvalidOperation, InvalidParameter from kimchi.exception import MissingParameter, NotFoundError, OperationFailed @@ -212,10 +212,10 @@ class Collection(object): return res.get() - def _get_resources(self): + def _get_resources(self, filter_params): try: get_list = getattr(self.model, model_fn(self, 'get_list')) - idents = get_list(*self.model_args) + idents = get_list(*self.model_args, **filter_params) res_list = [] for ident in idents: # internal text, get_list changes ident to unicode for sorted @@ -234,19 +234,27 @@ class Collection(object): args = self.resource_args + [ident.decode("utf-8")] return self.resource(self.model, *args) - def get(self): - resources = self._get_resources() + def filter_data(self, resources, filter_params): data = [] for res in resources: - data.append(res.data) + if all(key not in res.data or res.data[key] == val \ + for key, val in filter_params.iteritems()): + data.append(res.data) + return data + + def get(self, filter_params): + resources = self._get_resources(filter_params) + data = self.filter_data(resources, filter_params) return kimchi.template.render(get_class_name(self), data) @cherrypy.expose - def index(self, *args): + def index(self, *args, **kwargs): method = validate_method(('GET', 'POST')) if method == 'GET': try: - return self.get() + filter_params = get_query_params() + validate_params(filter_params, self, 'get_list') + return self.get(filter_params) except InvalidOperation, param: error = "Invalid operation: '%s'" % param raise cherrypy.HTTPError(400, error) diff --git a/src/kimchi/control/storagepools.py b/src/kimchi/control/storagepools.py index 782f5a6..e3236a7 100644 --- a/src/kimchi/control/storagepools.py +++ b/src/kimchi/control/storagepools.py @@ -63,9 +63,9 @@ class StoragePools(Collection): return resp - def _get_resources(self): + def _get_resources(self, filter_params): try: - res_list = super(StoragePools, self)._get_resources() + res_list = super(StoragePools, self)._get_resources(filter_params) # Append reserved pools isos = getattr(self, ISO_POOL_NAME) isos.lookup() diff --git a/src/kimchi/control/storagevolumes.py b/src/kimchi/control/storagevolumes.py index d541807..cd15bcc 100644 --- a/src/kimchi/control/storagevolumes.py +++ b/src/kimchi/control/storagevolumes.py @@ -70,7 +70,7 @@ class IsoVolumes(Collection): super(IsoVolumes, self).__init__(model) self.pool = pool - def get(self): + def get(self, filter_params): res_list = [] try: get_list = getattr(self.model, model_fn(self, 'get_list')) diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index 814ba20..28733e2 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -81,6 +81,8 @@ def parse_request(): raise cherrypy.HTTPError(415, "This API only supports" " 'application/json'") +def get_query_params(): + return cherrypy.request.params def internal_redirect(url): raise cherrypy.InternalRedirect(url.encode("utf-8")) @@ -98,6 +100,11 @@ def validate_params(params, instance, action): validator = Draft3Validator(api_schema, format_checker=FormatChecker()) request = {operation: params} + if (params and action in ['get_list'] and + operation not in validator.schema['properties']): + # get_list method does not allow parameter by default + raise InvalidParameter("%s does not support param %s" % (operation, params)) + try: validator.validate(request) except ValidationError: -- 1.8.1.2

Am 19-01-2014 13:28, schrieb lvroyce0210@gmail.com:
diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index 814ba20..28733e2 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -81,6 +81,8 @@ def parse_request(): raise cherrypy.HTTPError(415, "This API only supports" " 'application/json'")
+def get_query_params(): + return cherrypy.request.params
def internal_redirect(url): raise cherrypy.InternalRedirect(url.encode("utf-8")) Please use two blank lines between two top-level functions in order to be consistent with our code guidelines.

From: Royce Lv <lvroyce@linux.vnet.ibm.com> Add a testcase to test GET param passing and demo how GET param work with current model implementation,which means: 1. change the API.json 2. wrap its model implementation to accept parameters Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- tests/test_rest.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_rest.py b/tests/test_rest.py index a8e5842..437baca 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -21,6 +21,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA import base64 +import cherrypy import json import os import time @@ -1240,6 +1241,41 @@ class RestTests(unittest.TestCase): self.assertIn('net_recv_rate', stats) self.assertIn('net_sent_rate', stats) + def test_get_param(self): + def hack_model(func): + def _get_param_func(*args, **kwargs): + res = func() + return res + return _get_param_func + + global model + cherrypy.request.app.root.api_schema['properties']['vms_get_list'] = {} + old_handler = model.vms_get_list + model.vms_get_list = hack_model(model.vms_get_list) + + req = json.dumps({'name': 'test', 'cdrom': '/nonexistent.iso'}) + self.request('/templates', req, 'POST') + + # Create a VM + req = json.dumps({'name': 'test-vm1', 'template': '/templates/test'}) + resp = self.request('/vms', req, 'POST') + self.assertEquals(201, resp.status) + req = json.dumps({'name': 'test-vm2', 'template': '/templates/test'}) + resp = self.request('/vms', req, 'POST') + self.assertEquals(201, resp.status) + + resp = request(host, port, '/vms') + self.assertEquals(200, resp.status) + res = json.loads(resp.read()) + self.assertEquals(2, len(res)) + + resp = request(host, port, '/vms?name=test-vm1') + self.assertEquals(200, resp.status) + res = json.loads(resp.read()) + self.assertEquals(1, len(res)) + self.assertEquals('test-vm1', res[0]['name']) + + model.vms_get_list = old_handler class HttpsRestTests(RestTests): """ -- 1.8.1.2

From: Royce Lv <lvroyce@linux.vnet.ibm.com> Update API.md to specify storage server api. Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- docs/API.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/API.md b/docs/API.md index f872eab..8e8e008 100644 --- a/docs/API.md +++ b/docs/API.md @@ -495,6 +495,19 @@ creation. not tested yet * **POST**: *See Configuration Actions* +**Actions (POST):** + +*No actions defined* + +### Collection: Storage Servers + +**URI:** /storageservers + +**Methods:** + +* **GET**: Retrieve a summarized list of used storage servers. + * target_type: Filter server list with given type, currently support 'netfs'. + ### Collection: Distros **URI:** /config/distros -- 1.8.1.2

From: Royce Lv <lvroyce@linux.vnet.ibm.com> Add storage server collection and resource to report used storage server. Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- src/kimchi/API.json | 10 ++++++++++ src/kimchi/control/storageserver.py | 38 +++++++++++++++++++++++++++++++++++++ src/kimchi/root.py | 2 ++ 3 files changed, 50 insertions(+) create mode 100644 src/kimchi/control/storageserver.py diff --git a/src/kimchi/API.json b/src/kimchi/API.json index 46818d4..398936e 100644 --- a/src/kimchi/API.json +++ b/src/kimchi/API.json @@ -237,6 +237,16 @@ }, "additionalProperties": false }, + "storageservers_get_list": { + "type": "object", + "properties": { + "target_type": { + "description": "List storage servers of given type", + "type": "string", + "pattern": "^netfs$" + } + } + }, "template_update": { "type": "object", "properties": { diff --git a/src/kimchi/control/storageserver.py b/src/kimchi/control/storageserver.py new file mode 100644 index 0000000..0d4cb05 --- /dev/null +++ b/src/kimchi/control/storageserver.py @@ -0,0 +1,38 @@ +# +# Project Kimchi +# +# Copyright IBM, Corp. 2014 +# +# Authors: +# Royce Lv <lvroyce@linux.vnet.ibm.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +from kimchi.control.base import Collection, Resource + + +class StorageServers(Collection): + def __init__(self, model): + super(StorageServers, self).__init__(model) + self.resource = StorageServer + + +class StorageServer(Resource): + def __init__(self, model, ident): + super(StorageServer, self).__init__(model, ident) + + @property + def data(self): + return self.info diff --git a/src/kimchi/root.py b/src/kimchi/root.py index 3cc6321..ec531c0 100644 --- a/src/kimchi/root.py +++ b/src/kimchi/root.py @@ -36,6 +36,7 @@ from kimchi.control.interfaces import Interfaces from kimchi.control.networks import Networks from kimchi.control.plugins import Plugins from kimchi.control.storagepools import StoragePools +from kimchi.control.storageserver import StorageServers from kimchi.control.tasks import Tasks from kimchi.control.templates import Templates from kimchi.control.utils import parse_request @@ -60,6 +61,7 @@ class Root(Resource): self.vms = VMs(model) self.templates = Templates(model) self.storagepools = StoragePools(model) + self.storageservers = StorageServers(model) self.interfaces = Interfaces(model) self.networks = Networks(model) self.tasks = Tasks(model) -- 1.8.1.2

From: Royce Lv <lvroyce@linux.vnet.ibm.com> Query all storage pool to retrieve storage server we used. If no query param is given, all supported type will be listed. With param given, only specified type of server is listed. Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- src/kimchi/mockmodel.py | 30 ++++++++++++++++++++++++++++++ src/kimchi/model.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py index 4ef3fa6..b34dbb7 100644 --- a/src/kimchi/mockmodel.py +++ b/src/kimchi/mockmodel.py @@ -408,6 +408,36 @@ class MockModel(object): iso_volumes.append(res) return iso_volumes + def storageservers_get_list(self, target_type=None): + # FIXME: When added new storage server support, this needs to be updated + target_type = kimchi.model.STORAGE_SOURCES.keys() \ + if not target_type else [target_type] + pools = self.storagepools_get_list() + server_list = [] + for pool in pools: + try: + pool_info = self.storagepool_lookup(pool) + if (pool_info['type'] in target_type and + pool_info['source']['addr'] not in server_list): + server_list.append(pool_info['source']['addr']) + except NotFoundError: + pass + + return server_list + + def storageserver_lookup(self, server): + pools = self.storagepools_get_list() + for pool in pools: + try: + pool_info = self.storagepool_lookup(pool) + if pool_info['source'] and pool_info['source']['addr'] == server: + return dict(addr=server) + except NotFoundError: + # Avoid inconsistent pool result because of lease between list and lookup + pass + + raise NotFoundError + def dummy_interfaces(self): interfaces = {} ifaces = {"eth1": "nic", "bond0": "bonding", diff --git a/src/kimchi/model.py b/src/kimchi/model.py index 2c6d3a1..04051cf 100644 --- a/src/kimchi/model.py +++ b/src/kimchi/model.py @@ -1300,6 +1300,36 @@ class Model(object): else: raise + def storageservers_get_list(self, target_type=None): + target_type = STORAGE_SOURCES.keys() if not target_type else [target_type] + pools = self.storagepools_get_list() + server_list = [] + for pool in pools: + try: + pool_info = self.storagepool_lookup(pool) + if (pool_info['type'] in target_type and + pool_info['source']['addr'] not in server_list): + # Avoid to add same server for multiple times + # if it hosts more than one storage type + server_list.append(pool_info['source']['addr']) + except NotFoundError: + pass + + return server_list + + def storageserver_lookup(self, server): + pools = self.storagepools_get_list() + for pool in pools: + try: + pool_info = self.storagepool_lookup(pool) + if pool_info['source'] and pool_info['source']['addr'] == server: + return dict(addr=server) + except NotFoundError: + # Avoid inconsistent pool result because of lease between list and lookup + pass + + raise NotFoundError + def _get_screenshot(self, vm_uuid): with self.objstore as session: try: -- 1.8.1.2

From: Royce Lv <lvroyce@linux.vnet.ibm.com> Add colleciton of storage targets to API.md. Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- docs/API.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/API.md b/docs/API.md index 8e8e008..c8c3c1e 100644 --- a/docs/API.md +++ b/docs/API.md @@ -508,6 +508,15 @@ creation. * **GET**: Retrieve a summarized list of used storage servers. * target_type: Filter server list with given type, currently support 'netfs'. +### Collection: Storage Targets + +**URI:** /storageservers/*:name*/storagetargets + +**Methods:** + +* **GET**: Retrieve a list of available storage targets. + * target_type: Filter target list with given type, currently support 'netfs'. + ### Collection: Distros **URI:** /config/distros -- 1.8.1.2

From: Royce Lv <lvroyce@linux.vnet.ibm.com> Add json schema to validate mandatory param of target_type, also update controller.py. Reload the get_list function because we don't need to query each target. Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- src/kimchi/API.json | 10 ++++++++++ src/kimchi/control/storageserver.py | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/kimchi/API.json b/src/kimchi/API.json index 398936e..f737958 100644 --- a/src/kimchi/API.json +++ b/src/kimchi/API.json @@ -247,6 +247,16 @@ } } }, + "storagetargets_get_list": { + "type": "object", + "properties": { + "target_type": { + "description": "List storage servers of given type", + "type": "string", + "pattern": "^netfs$" + } + } + }, "template_update": { "type": "object", "properties": { diff --git a/src/kimchi/control/storageserver.py b/src/kimchi/control/storageserver.py index 0d4cb05..297d071 100644 --- a/src/kimchi/control/storageserver.py +++ b/src/kimchi/control/storageserver.py @@ -21,6 +21,8 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA from kimchi.control.base import Collection, Resource +from kimchi.control.utils import get_class_name, model_fn +import kimchi.template class StorageServers(Collection): @@ -36,3 +38,24 @@ class StorageServer(Resource): @property def data(self): return self.info + + def _cp_dispatch(self, vpath): + if vpath: + subcollection = vpath.pop(0) + if subcollection == 'storagetargets': + # incoming text, from URL, is not unicode, need decode + return StorageTargets(self.model, self.ident.decode("utf-8")) + + +class StorageTargets(Collection): + def __init__(self, model, server): + super(StorageTargets, self).__init__(model) + self.server = server + self.resource_args = [self.server, ] + self.model_args = [self.server, ] + + def get(self, filter_params): + res_list = [] + get_list = getattr(self.model, model_fn(self, 'get_list')) + res_list = get_list(*self.model_args, **filter_params) + return kimchi.template.render(get_class_name(self), res_list) -- 1.8.1.2

From: Royce Lv <lvroyce@linux.vnet.ibm.com> Construct xml to query storage targets information from storage server. Use lxml to parse result instead of etree. Use lxml to parse target query result. Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- src/kimchi/model.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/kimchi/model.py b/src/kimchi/model.py index 04051cf..06950f4 100644 --- a/src/kimchi/model.py +++ b/src/kimchi/model.py @@ -30,6 +30,7 @@ import ipaddr import json import libvirt import logging +import lxml.etree as ET import os import platform import psutil @@ -45,6 +46,8 @@ import uuid from cherrypy.process.plugins import BackgroundTask from cherrypy.process.plugins import SimplePlugin from collections import defaultdict +from lxml import objectify +from lxml.builder import E from xml.etree import ElementTree @@ -1330,6 +1333,24 @@ class Model(object): raise NotFoundError + def storagetargets_get_list(self, storage_server, target_type=None): + target_types = STORAGE_SOURCES.keys() if not target_type else [target_type] + target_list = list() + + for target_type in target_types: + xml = _get_storage_server_spec(server=storage_server, target_type=target_type) + conn = self.conn.get() + + try: + ret = conn.findStoragePoolSources(target_type, xml, 0) + except libvirt.libvirtError as e: + kimchi_log.warning("Query storage pool source fails because of %s", + e.get_error_message()) + continue + + target_list.extend(_parse_target_source_result(target_type, ret)) + return target_list + def _get_screenshot(self, vm_uuid): with self.objstore as session: try: @@ -1540,6 +1561,30 @@ class LibvirtVMScreenshot(VMScreenshot): os.close(fd) +def _parse_target_source_result(target_type, xml_str): + root = objectify.fromstring(xml_str) + ret = [] + for source in root.getchildren(): + if target_type == 'netfs': + host_name = source.host.get('name') + target_path = source.dir.get('path') + type = source.format.get('type') + ret.append(dict(host=host_name, target_type=type, target=target_path)) + return ret + + +def _get_storage_server_spec(**kwargs): + # Required parameters: + # server: + # target_type: + extra_args = [] + if kwargs['target_type'] == 'nefs': + extra_args.append(E.format(type='nfs')) + obj = E.source(E.host(name=kwargs['server']), *extra_args) + xml = ET.tostring(obj) + return xml + + class StoragePoolDef(object): @classmethod def create(cls, poolArgs): -- 1.8.1.2

On 01/19/2014 11:28 PM, lvroyce0210@gmail.com wrote:
From: Royce Lv <lvroyce@linux.vnet.ibm.com>
Construct xml to query storage targets information from storage server. Use lxml to parse result instead of etree. Use lxml to parse target query result.
Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- src/kimchi/model.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
diff --git a/src/kimchi/model.py b/src/kimchi/model.py index 04051cf..06950f4 100644 --- a/src/kimchi/model.py +++ b/src/kimchi/model.py @@ -30,6 +30,7 @@ import ipaddr import json import libvirt import logging +import lxml.etree as ET import os import platform import psutil @@ -45,6 +46,8 @@ import uuid from cherrypy.process.plugins import BackgroundTask from cherrypy.process.plugins import SimplePlugin from collections import defaultdict +from lxml import objectify +from lxml.builder import E from xml.etree import ElementTree
@@ -1330,6 +1333,24 @@ class Model(object):
raise NotFoundError
+ def storagetargets_get_list(self, storage_server, target_type=None): + target_types = STORAGE_SOURCES.keys() if not target_type else [target_type] + target_list = list() + + for target_type in target_types: + xml = _get_storage_server_spec(server=storage_server, target_type=target_type) + conn = self.conn.get() + + try: + ret = conn.findStoragePoolSources(target_type, xml, 0) + except libvirt.libvirtError as e: + kimchi_log.warning("Query storage pool source fails because of %s", + e.get_error_message()) + continue + + target_list.extend(_parse_target_source_result(target_type, ret)) + return target_list + def _get_screenshot(self, vm_uuid): with self.objstore as session: try: @@ -1540,6 +1561,30 @@ class LibvirtVMScreenshot(VMScreenshot): os.close(fd)
+def _parse_target_source_result(target_type, xml_str): + root = objectify.fromstring(xml_str) + ret = [] + for source in root.getchildren(): + if target_type == 'netfs': + host_name = source.host.get('name') + target_path = source.dir.get('path') + type = source.format.get('type') + ret.append(dict(host=host_name, target_type=type, target=target_path)) + return ret + + +def _get_storage_server_spec(**kwargs): + # Required parameters: + # server: + # target_type: + extra_args = [] + if kwargs['target_type'] == 'nefs': netfs? + extra_args.append(E.format(type='nfs')) + obj = E.source(E.host(name=kwargs['server']), *extra_args) + xml = ET.tostring(obj) + return xml + + class StoragePoolDef(object): @classmethod def create(cls, poolArgs):

On 2014年01月20日 10:48, zhoumeina wrote:
On 01/19/2014 11:28 PM, lvroyce0210@gmail.com wrote:
From: Royce Lv <lvroyce@linux.vnet.ibm.com>
Construct xml to query storage targets information from storage server. Use lxml to parse result instead of etree. Use lxml to parse target query result.
Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> --- src/kimchi/model.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
diff --git a/src/kimchi/model.py b/src/kimchi/model.py index 04051cf..06950f4 100644 --- a/src/kimchi/model.py +++ b/src/kimchi/model.py @@ -30,6 +30,7 @@ import ipaddr import json import libvirt import logging +import lxml.etree as ET import os import platform import psutil @@ -45,6 +46,8 @@ import uuid from cherrypy.process.plugins import BackgroundTask from cherrypy.process.plugins import SimplePlugin from collections import defaultdict +from lxml import objectify +from lxml.builder import E from xml.etree import ElementTree
@@ -1330,6 +1333,24 @@ class Model(object):
raise NotFoundError
+ def storagetargets_get_list(self, storage_server, target_type=None): + target_types = STORAGE_SOURCES.keys() if not target_type else [target_type] + target_list = list() + + for target_type in target_types: + xml = _get_storage_server_spec(server=storage_server, target_type=target_type) + conn = self.conn.get() + + try: + ret = conn.findStoragePoolSources(target_type, xml, 0) + except libvirt.libvirtError as e: + kimchi_log.warning("Query storage pool source fails because of %s", + e.get_error_message()) + continue + + target_list.extend(_parse_target_source_result(target_type, ret)) + return target_list + def _get_screenshot(self, vm_uuid): with self.objstore as session: try: @@ -1540,6 +1561,30 @@ class LibvirtVMScreenshot(VMScreenshot): os.close(fd)
+def _parse_target_source_result(target_type, xml_str): + root = objectify.fromstring(xml_str) + ret = [] + for source in root.getchildren(): + if target_type == 'netfs': + host_name = source.host.get('name') + target_path = source.dir.get('path') + type = source.format.get('type') + ret.append(dict(host=host_name, target_type=type, target=target_path)) + return ret + + +def _get_storage_server_spec(**kwargs): + # Required parameters: + # server: + # target_type: + extra_args = [] + if kwargs['target_type'] == 'nefs': netfs? Wrong rebase, thanks, meina + extra_args.append(E.format(type='nfs')) + obj = E.source(E.host(name=kwargs['server']), *extra_args) + xml = ET.tostring(obj) + return xml + + class StoragePoolDef(object): @classmethod def create(cls, poolArgs):
_______________________________________________ Kimchi-devel mailing list Kimchi-devel@ovirt.org http://lists.ovirt.org/mailman/listinfo/kimchi-devel

+ try: + ret = conn.findStoragePoolSources(target_type, xml, 0) + except libvirt.libvirtError as e: + kimchi_log.warning("Query storage pool source fails because of %s", + e.get_error_message()) + continue Please align the second line of "kimchi.log.warning" to the beginning of
Am 19-01-2014 13:28, schrieb lvroyce0210@gmail.com: the parameter list in order to be consistent with our code guidelines (i.e. "e.get_error_message" should start right below "Query ...").
participants (4)
-
Crístian Viana
-
lvroyce0210@gmail.com
-
Royce Lv
-
zhoumeina