Merge "Add validation to package import"
This commit is contained in:
commit
8e0aba70b5
@ -17,6 +17,9 @@ from __future__ import print_function
|
||||
|
||||
import collections
|
||||
import json
|
||||
from muranopkgcheck import manager as check_manager
|
||||
from muranopkgcheck import pkg_loader as check_pkg_loader
|
||||
from muranopkgcheck import validators as check_validators
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
@ -307,7 +310,11 @@ class Package(FileWrapperMixin):
|
||||
def from_file(file_obj):
|
||||
if not isinstance(file_obj, File):
|
||||
file_obj = File(file_obj)
|
||||
return Package(file_obj)
|
||||
pkg = Package(file_obj)
|
||||
errs = pkg.validate()
|
||||
if errs:
|
||||
raise exceptions.HTTPBadRequest(details=errs)
|
||||
return pkg
|
||||
|
||||
@staticmethod
|
||||
def fromFile(file_obj):
|
||||
@ -342,6 +349,16 @@ class Package(FileWrapperMixin):
|
||||
extension='.zip')
|
||||
)
|
||||
|
||||
def validate(self):
|
||||
m = check_manager.Manager(self._file,
|
||||
loader=check_pkg_loader.ZipLoader)
|
||||
errors = m.validate(
|
||||
validators=[check_validators.manifest.ManifestValidator],
|
||||
only_errors=True)
|
||||
if errors:
|
||||
fmt = check_manager.PlainTextFormatter().format
|
||||
return 'Invalid Murano package\n{}\n'.format(fmt(errors))
|
||||
|
||||
@property
|
||||
def contents(self):
|
||||
"""Contents of a package."""
|
||||
|
@ -70,13 +70,14 @@ def make_pkg(manifest_override, image_dicts=None):
|
||||
'Classes': {'foo': 'foo.yaml'},
|
||||
'Description': '',
|
||||
'Format': 1.0,
|
||||
'FullName': '',
|
||||
'FullName': 'org.foo',
|
||||
'Name': 'Apache HTTP Server',
|
||||
'Type': 'Application'}
|
||||
manifest.update(manifest_override)
|
||||
file_obj = six.BytesIO()
|
||||
zfile = zipfile.ZipFile(file_obj, "a")
|
||||
zfile.writestr('manifest.yaml', yaml.dump(manifest))
|
||||
zfile.writestr('Classes/foo.yaml', yaml.dump({}))
|
||||
if image_dicts:
|
||||
images_list = []
|
||||
default_image_spec = {
|
||||
@ -414,10 +415,12 @@ class PackageTest(testtools.TestCase):
|
||||
self.assertRaises(ValueError, utils.Package.from_location,
|
||||
name='foo.bar.baz', base_url='')
|
||||
|
||||
def test_file_object_repo(self):
|
||||
@mock.patch.object(utils.Package, 'validate')
|
||||
def test_file_object_repo(self, m_validate):
|
||||
resp = requests.Response()
|
||||
resp.raw = six.BytesIO(six.b("123"))
|
||||
resp.status_code = 200
|
||||
m_validate.return_value = None
|
||||
with mock.patch(
|
||||
'requests.get',
|
||||
mock.Mock(side_effect=lambda k, *args, **kwargs: resp)):
|
||||
|
@ -19,6 +19,8 @@ import yaml
|
||||
|
||||
from muranoclient.common import base
|
||||
from muranoclient.common import exceptions
|
||||
from muranoclient.common import utils
|
||||
|
||||
|
||||
DEFAULT_PAGE_SIZE = 20
|
||||
|
||||
@ -51,6 +53,10 @@ class PackageManager(base.Manager):
|
||||
response_key='categories', obj_class=Category)
|
||||
|
||||
def create(self, data, files):
|
||||
for pkg_file in files.values():
|
||||
utils.Package.from_file(pkg_file)
|
||||
pkg_file.seek(0)
|
||||
|
||||
response = self.api.request(
|
||||
'/v1/catalog/packages',
|
||||
'POST',
|
||||
|
@ -13,6 +13,7 @@ requests>=2.10.0 # Apache-2.0
|
||||
PyYAML>=3.10.0 # MIT
|
||||
yaql>=1.1.0 # Apache 2.0 License
|
||||
osc-lib>=1.2.0 # Apache-2.0
|
||||
murano-pkg-check>=0.2.0 # Apache-2.0
|
||||
|
||||
oslo.serialization>=1.10.0 # Apache-2.0
|
||||
oslo.utils>=3.18.0 # Apache-2.0
|
||||
|
Loading…
Reference in New Issue
Block a user