Improve repository API
* Replace tag.gz package format to zip * Add lazy=joined to get class_definitions automatically * Improve logging Change-Id: Idbbe9c913b02abcb1d273edf6a84cb14587bac2a
This commit is contained in:
parent
976b6813e9
commit
32b6902ba0
@ -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):
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user