def handled_exceptions(request): """Handles all murano-api specific exceptions.""" try: yield except exc.CommunicationError: msg = _('Unable to communicate to murano-api server.') LOG.exception(msg) _handle_message(request, msg) except exc.HTTPUnauthorized: msg = _('Check Keystone configuration of murano-api server.') LOG.exception(msg) _handle_message(request, msg) except exc.HTTPForbidden: msg = _('Operation is forbidden by murano-api server.') LOG.exception(msg) _handle_message(request, msg) except exc.HTTPNotFound: msg = _('Requested object is not found on murano server.') LOG.exception(msg) _handle_message(request, msg) except exc.HTTPConflict: msg = _('Requested operation conflicts with an existing object.') LOG.exception(msg) _handle_message(request, msg) except exc.BadRequest as e: msg = _('The request data is not acceptable by the server') LOG.exception(msg) reason = muranodashboard_utils.parse_api_error( getattr(e, 'details', '')) if not reason: reason = msg _handle_message(request, reason)
def process_step(self, form): @catalog_views.update_latest_apps def _update_latest_apps(request, app_id): LOG.info('Adding {0} application to the' ' latest apps list'.format(app_id)) step_data = self.get_form_step_data(form) if self.steps.current == 'upload': import_type = form.cleaned_data['import_type'] data = {} f = None base_url = packages_consts.MURANO_REPO_URL if import_type == 'upload': pkg = form.cleaned_data['package'] f = pkg.file elif import_type == 'by_url': f = form.cleaned_data['url'] elif import_type == 'by_name': name = form.cleaned_data['repo_name'] version = form.cleaned_data['repo_version'] f = muranoclient_utils.to_url( name, version=version, path='/apps/', extension='.zip', base_url=base_url, ) try: package = muranoclient_utils.Package.from_file(f) name = package.manifest['FullName'] except Exception as e: msg = _("Package creation failed" "Reason: {0}").format(e) LOG.exception(msg) messages.error(self.request, msg) raise exceptions.Http302( reverse('horizon:murano:packages:index')) def _ensure_images(name, package): try: imgs = muranoclient_utils.ensure_images( glance_client=glance_client, image_specs=package.images(), base_url=base_url) for img in imgs: msg = _("Added {0}, {1} image to glance").format( img['name'], img['id'], ) messages.success(self.request, msg) LOG.info(msg) except Exception as e: msg = _("Error {0} occurred while installing " "images for {1}").format(e, name) messages.error(self.request, msg) LOG.exception(msg) reqs = package.requirements(base_url=base_url) glance_client = glance.glanceclient(self.request, version='1') original_package = reqs.pop(name) step_data['dependencies'] = [] for dep_name, dep_package in reqs.iteritems(): _ensure_images(dep_name, dep_package) try: files = {dep_name: dep_package.file()} package = api.muranoclient(self.request).packages.create( data, files) messages.success( self.request, _('Package {0} uploaded').format(dep_name)) _update_latest_apps(request=self.request, app_id=package.id) step_data['dependencies'].append(package) except exc.HTTPConflict: msg = _("Package {0} already registered.").format(dep_name) messages.warning(self.request, msg) LOG.exception(msg) except Exception as e: msg = _("Error {0} occurred while " "installing package {1}").format(e, dep_name) messages.error(self.request, msg) LOG.exception(msg) continue # add main packages images _ensure_images(name, original_package) # import main package itself try: files = {name: original_package.file()} package = api.muranoclient(self.request).packages.create( data, files) messages.success(self.request, _('Package {0} uploaded').format(name)) _update_latest_apps(request=self.request, app_id=package.id) step_data['package'] = package except exc.HTTPConflict: msg = _("Package with specified name already exists") LOG.exception(msg) exceptions.handle( self.request, msg, redirect=reverse('horizon:murano:packages:index')) except exc.HTTPInternalServerError as e: self._handle_exception(e) except exc.HTTPException as e: reason = muranodashboard_utils.parse_api_error( getattr(e, 'details', '')) if not reason: raise LOG.exception(reason) exceptions.handle( self.request, reason, redirect=reverse('horizon:murano:packages:index')) except Exception as original_e: self._handle_exception(original_e) return step_data
def process_step(self, form): @catalog_views.update_latest_apps def _update_latest_apps(request, app_id): LOG.info('Adding {0} application to the' ' latest apps list'.format(app_id)) step_data = self.get_form_step_data(form) if self.steps.current == 'upload': import_type = form.cleaned_data['import_type'] data = {} f = None base_url = packages_consts.MURANO_REPO_URL if import_type == 'by_url': f = form.cleaned_data['url'] elif import_type == 'by_name': f = muranoclient_utils.to_url( form.cleaned_data['name'], path='/bundles/', base_url=base_url, extension='.bundle', ) try: bundle = muranoclient_utils.Bundle.from_file(f) except Exception as e: msg = _("Bundle creation failed" "Reason: {0}").format(e) LOG.exception(msg) messages.error(self.request, msg) raise exceptions.Http302( reverse('horizon:murano:packages:index')) glance_client = glance.glanceclient(self.request, version='1') for package_spec in bundle.package_specs(): try: package = muranoclient_utils.Package.from_location( package_spec['Name'], version=package_spec.get('Version'), url=package_spec.get('Url'), base_url=base_url, path=None, ) except Exception as e: msg = _("Error {0} occurred while parsing package {1}")\ .format(e, package_spec.get('Name')) messages.error(self.request, msg) LOG.exception(msg) continue reqs = package.requirements(base_url=base_url) for dep_name, dep_package in reqs.iteritems(): try: imgs = muranoclient_utils.ensure_images( glance_client=glance_client, image_specs=dep_package.images(), base_url=base_url) for img in imgs: msg = _("Added {0}, {1} image to glance").format( img['name'], img['id'], ) messages.success(self.request, msg) LOG.info(msg) except Exception as e: msg = _("Error {0} occurred while installing " "images for {1}").format(e, dep_name) messages.error(self.request, msg) LOG.exception(msg) try: files = {dep_name: dep_package.file()} package = api.muranoclient( self.request).packages.create(data, files) messages.success( self.request, _('Package {0} uploaded').format(dep_name)) _update_latest_apps(request=self.request, app_id=package.id) except exc.HTTPConflict: msg = _("Package {0} already registered.").format( dep_name) messages.warning(self.request, msg) LOG.exception(msg) except exc.HTTPException as e: reason = muranodashboard_utils.parse_api_error( getattr(e, 'details', '')) if not reason: raise msg = _("Package {0} upload failed. {1}").format( dep_name, reason) messages.warning(self.request, msg) LOG.exception(msg) except Exception as e: msg = _("Importing package {0} failed. " "Reason: {1}").format(dep_name, e) messages.warning(self.request, msg) LOG.exception(msg) continue return step_data
def process_step(self, form): @catalog_views.update_latest_apps def _update_latest_apps(request, app_id): LOG.info('Adding {0} application to the' ' latest apps list'.format(app_id)) step_data = self.get_form_step_data(form) if self.steps.current == 'upload': import_type = form.cleaned_data['import_type'] data = {} f = None base_url = packages_consts.MURANO_REPO_URL if import_type == 'upload': pkg = form.cleaned_data['package'] f = pkg.file elif import_type == 'by_url': f = form.cleaned_data['url'] elif import_type == 'by_name': name = form.cleaned_data['repo_name'] version = form.cleaned_data['repo_version'] f = muranoclient_utils.to_url( name, version=version, path='apps/', extension='.zip', base_url=base_url, ) try: package = muranoclient_utils.Package.from_file(f) name = package.manifest['FullName'] except Exception as e: if '(404)' in e.message: msg = _("Package creation failed." "Reason: Can't find Package name from repository.") else: msg = _("Package creation failed." "Reason: {0}").format(e) LOG.exception(msg) messages.error(self.request, msg) raise exceptions.Http302( reverse('horizon:murano:packages:index')) def _ensure_images(name, package): try: imgs = muranoclient_utils.ensure_images( glance_client=glance_client, image_specs=package.images(), base_url=base_url) for img in imgs: msg = _("Trying to add {0} image to glance. " "Image will be ready for deployment after " "successful upload").format(img['name'],) messages.warning(self.request, msg) log_msg = _("Trying to add {0}, {1} image to " "glance. Image will be ready for " "deployment after successful upload")\ .format(img['name'], img['id'],) LOG.info(log_msg) step_data['images'].append(img) except Exception as e: msg = _("Error {0} occurred while installing " "images for {1}").format(e, name) messages.error(self.request, msg) LOG.exception(msg) reqs = package.requirements(base_url=base_url) glance_client = glance.glanceclient(self.request, version='1') original_package = reqs.pop(name) step_data['dependencies'] = [] step_data['images'] = [] for dep_name, dep_package in six.iteritems(reqs): _ensure_images(dep_name, dep_package) try: files = {dep_name: dep_package.file()} package = api.muranoclient(self.request).packages.create( data, files) messages.success( self.request, _('Package {0} uploaded').format(dep_name) ) _update_latest_apps( request=self.request, app_id=package.id) step_data['dependencies'].append(package) except exc.HTTPConflict: msg = _("Package {0} already registered.").format( dep_name) messages.warning(self.request, msg) LOG.exception(msg) except Exception as e: msg = _("Error {0} occurred while " "installing package {1}").format(e, dep_name) messages.error(self.request, msg) LOG.exception(msg) continue # add main packages images _ensure_images(name, original_package) # import main package itself try: files = {name: original_package.file()} package = api.muranoclient(self.request).packages.create( data, files) messages.success(self.request, _('Package {0} uploaded').format(name)) _update_latest_apps(request=self.request, app_id=package.id) step_data['package'] = package except exc.HTTPConflict: msg = _("Package with specified name already exists") LOG.exception(msg) exceptions.handle( self.request, msg, redirect=reverse('horizon:murano:packages:index')) except exc.HTTPInternalServerError as e: self._handle_exception(e) except exc.HTTPException as e: reason = muranodashboard_utils.parse_api_error( getattr(e, 'details', '')) if not reason: raise LOG.exception(reason) exceptions.handle( self.request, reason, redirect=reverse('horizon:murano:packages:index')) except Exception as original_e: self._handle_exception(original_e) return step_data
def test_parse_api_error_without_body(self): test_html = '<html></html>' self.assertIsNone(utils.parse_api_error(test_html))
def process_step(self, form): @catalog_views.update_latest_apps def _update_latest_apps(request, app_id): LOG.info('Adding {0} application to the' ' latest apps list'.format(app_id)) step_data = self.get_form_step_data(form) if self.steps.current == 'upload': import_type = form.cleaned_data['import_type'] data = {} f = None base_url = packages_consts.MURANO_REPO_URL if import_type == 'by_url': f = form.cleaned_data['url'] elif import_type == 'by_name': f = muranoclient_utils.to_url( form.cleaned_data['name'], path='bundles/', base_url=base_url, extension='.bundle', ) try: bundle = muranoclient_utils.Bundle.from_file(f) except Exception as e: if '(404)' in e.message: msg = _("Bundle creation failed." "Reason: Can't find Bundle name from repository.") else: msg = _("Bundle creation failed." "Reason: {0}").format(e) LOG.exception(msg) messages.error(self.request, msg) raise exceptions.Http302( reverse('horizon:murano:packages:index')) glance_client = glance.glanceclient(self.request, version='1') for package_spec in bundle.package_specs(): try: package = muranoclient_utils.Package.from_location( package_spec['Name'], version=package_spec.get('Version'), url=package_spec.get('Url'), base_url=base_url, path=None, ) except Exception as e: msg = _("Error {0} occurred while parsing package {1}")\ .format(e, package_spec.get('Name')) messages.error(self.request, msg) LOG.exception(msg) continue reqs = package.requirements(base_url=base_url) for dep_name, dep_package in six.iteritems(reqs): try: imgs = muranoclient_utils.ensure_images( glance_client=glance_client, image_specs=dep_package.images(), base_url=base_url) for img in imgs: msg = _("Trying to add {0} image to glance. " "Image will be ready for deployment after" " successful upload").format(img['name'],) messages.warning(self.request, msg) log_msg = _("Trying to add {0}, {1} image to " "glance. Image will be ready for " "deployment after successful upload")\ .format(img['name'], img['id'],) LOG.info(log_msg) except Exception as e: msg = _("Error {0} occurred while installing " "images for {1}").format(e, dep_name) messages.error(self.request, msg) LOG.exception(msg) try: files = {dep_name: dep_package.file()} package = api.muranoclient( self.request).packages.create(data, files) messages.success( self.request, _('Package {0} uploaded').format(dep_name) ) _update_latest_apps( request=self.request, app_id=package.id) except exc.HTTPConflict: msg = _("Package {0} already registered.").format( dep_name) messages.warning(self.request, msg) LOG.exception(msg) except exc.HTTPException as e: reason = muranodashboard_utils.parse_api_error( getattr(e, 'details', '')) if not reason: raise msg = _("Package {0} upload failed. {1}").format( dep_name, reason) messages.warning(self.request, msg) LOG.exception(msg) except Exception as e: msg = _("Importing package {0} failed. " "Reason: {1}").format(dep_name, e) messages.warning(self.request, msg) LOG.exception(msg) continue return step_data
def test_parse_api_error(self): test_html = '<html><body><h1>Foo Header</h1>Foo Error </body></html>' self.assertEqual('Foo Error', utils.parse_api_error(test_html))
def process_step(self, form): @catalog_views.update_latest_apps def _update_latest_apps(request, app_id): LOG.info("Adding {0} application to the" " latest apps list".format(app_id)) step_data = self.get_form_step_data(form) if self.steps.current == "upload": import_type = form.cleaned_data["import_type"] data = {} f = None base_url = packages_consts.MURANO_REPO_URL if import_type == "upload": pkg = form.cleaned_data["package"] f = pkg.file elif import_type == "by_url": f = form.cleaned_data["url"] elif import_type == "by_name": name = form.cleaned_data["repo_name"] version = form.cleaned_data["repo_version"] f = muranoclient_utils.to_url(name, version=version, path="/apps/", extension=".zip", base_url=base_url) try: package = muranoclient_utils.Package.from_file(f) name = package.manifest["FullName"] except Exception as e: msg = _("Package creation failed" "Reason: {0}").format(e) LOG.exception(msg) messages.error(self.request, msg) raise exceptions.Http302(reverse("horizon:murano:packages:index")) def _ensure_images(name, package): try: imgs = muranoclient_utils.ensure_images( glance_client=glance_client, image_specs=package.images(), base_url=base_url ) for img in imgs: msg = _("Added {0}, {1} image to glance").format(img["name"], img["id"]) messages.success(self.request, msg) LOG.info(msg) except Exception as e: msg = _("Error {0} occurred while installing " "images for {1}").format(e, name) messages.error(self.request, msg) LOG.exception(msg) reqs = package.requirements(base_url=base_url) glance_client = glance.glanceclient(self.request, version="1") original_package = reqs.pop(name) step_data["dependencies"] = [] for dep_name, dep_package in reqs.iteritems(): _ensure_images(dep_name, dep_package) try: files = {dep_name: dep_package.file()} package = api.muranoclient(self.request).packages.create(data, files) messages.success(self.request, _("Package {0} uploaded").format(dep_name)) _update_latest_apps(request=self.request, app_id=package.id) step_data["dependencies"].append(package) except exc.HTTPConflict: msg = _("Package {0} already registered.").format(dep_name) messages.warning(self.request, msg) LOG.exception(msg) except Exception as e: msg = _("Error {0} occurred while " "installing package {1}").format(e, dep_name) messages.error(self.request, msg) LOG.exception(msg) continue # add main packages images _ensure_images(name, original_package) # import main package itself try: files = {name: original_package.file()} package = api.muranoclient(self.request).packages.create(data, files) messages.success(self.request, _("Package {0} uploaded").format(name)) _update_latest_apps(request=self.request, app_id=package.id) step_data["package"] = package except exc.HTTPConflict: msg = _("Package with specified name already exists") LOG.exception(msg) exceptions.handle(self.request, msg, redirect=reverse("horizon:murano:packages:index")) except exc.HTTPInternalServerError as e: self._handle_exception(e) except exc.HTTPException as e: reason = muranodashboard_utils.parse_api_error(getattr(e, "details", "")) if not reason: raise LOG.exception(reason) exceptions.handle(self.request, reason, redirect=reverse("horizon:murano:packages:index")) except Exception as original_e: self._handle_exception(original_e) return step_data
def process_step(self, form): @catalog_views.update_latest_apps def _update_latest_apps(request, app_id): LOG.info("Adding {0} application to the" " latest apps list".format(app_id)) step_data = self.get_form_step_data(form) if self.steps.current == "upload": import_type = form.cleaned_data["import_type"] data = {} f = None base_url = packages_consts.MURANO_REPO_URL if import_type == "by_url": f = form.cleaned_data["url"] elif import_type == "by_name": f = muranoclient_utils.to_url( form.cleaned_data["name"], path="/bundles/", base_url=base_url, extension=".bundle" ) try: bundle = muranoclient_utils.Bundle.from_file(f) except Exception as e: msg = _("Bundle creation failed" "Reason: {0}").format(e) LOG.exception(msg) messages.error(self.request, msg) raise exceptions.Http302(reverse("horizon:murano:packages:index")) glance_client = glance.glanceclient(self.request, version="1") for package_spec in bundle.package_specs(): try: package = muranoclient_utils.Package.from_location( package_spec["Name"], version=package_spec.get("Version"), url=package_spec.get("Url"), base_url=base_url, path=None, ) except Exception as e: msg = _("Error {0} occurred while parsing package {1}").format(e, package_spec.get("Name")) messages.error(self.request, msg) LOG.exception(msg) continue reqs = package.requirements(base_url=base_url) for dep_name, dep_package in reqs.iteritems(): try: imgs = muranoclient_utils.ensure_images( glance_client=glance_client, image_specs=dep_package.images(), base_url=base_url ) for img in imgs: msg = _("Added {0}, {1} image to glance").format(img["name"], img["id"]) messages.success(self.request, msg) LOG.info(msg) except Exception as e: msg = _("Error {0} occurred while installing " "images for {1}").format(e, dep_name) messages.error(self.request, msg) LOG.exception(msg) try: files = {dep_name: dep_package.file()} package = api.muranoclient(self.request).packages.create(data, files) messages.success(self.request, _("Package {0} uploaded").format(dep_name)) _update_latest_apps(request=self.request, app_id=package.id) except exc.HTTPConflict: msg = _("Package {0} already registered.").format(dep_name) messages.warning(self.request, msg) LOG.exception(msg) except exc.HTTPException as e: reason = muranodashboard_utils.parse_api_error(getattr(e, "details", "")) if not reason: raise msg = _("Package {0} upload failed. {1}").format(dep_name, reason) messages.warning(self.request, msg) LOG.exception(msg) except Exception as e: msg = _("Importing package {0} failed. " "Reason: {1}").format(dep_name, e) messages.warning(self.request, msg) LOG.exception(msg) continue return step_data