From 32b6902ba0d85b2f72e0c6225fe52512a6042793 Mon Sep 17 00:00:00 2001 From: Ekaterina Fedorova Date: Thu, 3 Apr 2014 14:10:57 +0400 Subject: [PATCH] Improve repository API * Replace tag.gz package format to zip * Add lazy=joined to get class_definitions automatically * Improve logging Change-Id: Idbbe9c913b02abcb1d273edf6a84cb14587bac2a --- muranoapi/api/v1/catalog.py | 12 +++++++----- muranoapi/db/catalog/api.py | 9 +++++++-- muranoapi/db/models.py | 2 +- muranoapi/packages/application_package.py | 10 +++++----- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/muranoapi/api/v1/catalog.py b/muranoapi/api/v1/catalog.py index c44764441..c36f6f851 100644 --- a/muranoapi/api/v1/catalog.py +++ b/muranoapi/api/v1/catalog.py @@ -45,7 +45,7 @@ def _check_content_type(req, content_type): req.get_content_type((content_type,)) except exception.InvalidContentType: msg = _("Content-Type must be '{0}'").format(content_type) - LOG.debug(msg) + LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) @@ -74,8 +74,9 @@ def _get_filters(query_params): def _validate_body(body): if len(body.keys()) != 2: - msg = "multipart/form-data request should contain " \ - "two parts: json and tar.gz archive" + msg = "'multipart/form-data' request body should contain " \ + "2 parts: json string and zip archivel. Current body consist " \ + "of {0} parts".format(len(body.keys())) LOG.error(msg) raise exc.HTTPBadRequest(msg) file_obj = None @@ -149,6 +150,7 @@ class Controller(object): content = file_obj.file.read() if not content: msg = _("Uploading file can't be empty") + LOG.error(msg) raise exc.HTTPBadRequest(msg) tempf.write(content) package_meta['archive'] = content @@ -158,7 +160,7 @@ class Controller(object): drop_dir=True) except pkg_exc.PackageLoadError as e: LOG.exception(e) - raise exc.HTTPBadRequest(e.message) + raise exc.HTTPBadRequest(e) # extend dictionary for update db for k, v in PKG_PARAMS_MAP.iteritems(): @@ -189,7 +191,7 @@ class Controller(object): def show_categories(self, req): categories = db_api.categories_list() - return {"categories": [category.to_dict() for category in categories]} + return {'categories': [category.name for category in categories]} class PackageSerializer(wsgi.ResponseSerializer): diff --git a/muranoapi/db/catalog/api.py b/muranoapi/db/catalog/api.py index 84133cfe9..e0fec118b 100644 --- a/muranoapi/db/catalog/api.py +++ b/muranoapi/db/catalog/api.py @@ -41,6 +41,7 @@ def _package_get(package_id, session): msg = _("Package id '{0}' is not found".format(package_id)) LOG.error(msg) raise exc.HTTPNotFound(msg) + return package @@ -86,9 +87,11 @@ def _get_categories(category_names, session=None): ctg_obj = session.query(models.Category).filter_by( name=ctg_name).first() if not ctg_obj: + msg = _("Category '{name}' doesn't exist".format(name=ctg_name)) + LOG.error(msg) # it's not allowed to specify non-existent categories - raise exc.HTTPBadRequest( - "Category '{name}' doesn't exist".format(name=ctg_name)) + raise exc.HTTPBadRequest(msg) + categories.append(ctg_obj) return categories @@ -237,10 +240,12 @@ def package_search(filters, context): value = int(value) except ValueError: msg = _("limit param must be an integer") + LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) if value < 0: msg = _("limit param must be positive") + LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) return value diff --git a/muranoapi/db/models.py b/muranoapi/db/models.py index a4da1ea23..a0391a301 100644 --- a/muranoapi/db/models.py +++ b/muranoapi/db/models.py @@ -255,7 +255,7 @@ class Package(BASE, ModelBase): secondary=package_to_category, cascade='save-update, merge', lazy='joined') - class_definition = sa_orm.relationship("Class") + class_definition = sa_orm.relationship("Class", lazy='joined') def to_dict(self): d = self.__dict__.copy() diff --git a/muranoapi/packages/application_package.py b/muranoapi/packages/application_package.py index c1e974676..c309c843b 100644 --- a/muranoapi/packages/application_package.py +++ b/muranoapi/packages/application_package.py @@ -16,9 +16,9 @@ import imghdr import os import shutil import sys -import tarfile import tempfile import yaml +import zipfile import muranoapi.packages.exceptions as e import muranoapi.packages.versions.v1 @@ -206,10 +206,10 @@ def load_from_file(archive_path, target_dir=None, drop_dir=False): raise e.PackageLoadError('Target directory is not empty') try: - if not tarfile.is_tarfile(archive_path): - raise e.PackageFormatError("Uploading file should be a" - " 'tar.gz' archive") - package = tarfile.open(archive_path) + if not zipfile.is_zipfile(archive_path): + raise e.PackageFormatError("Uploading file should be a " + "zip' archive") + package = zipfile.ZipFile(archive_path) package.extractall(path=target_dir) return load_from_dir(target_dir, preload=True) finally: