def instances_delete_by_id(instance_uuid): try: response = _get_instances_or_problem(instance_uuid) if isinstance(response, Response): return response lm.deregister_test_instance(response) return connexion.NoContent, 204 except OAuthIdentityNotFoundException as e: return lm_exceptions.report_problem(401, "Unauthorized", extra_info={"exception": str(e)}) except lm_exceptions.EntityNotFoundException as e: return lm_exceptions.report_problem( 404, "Not Found", extra_info={"exception": str(e.detail)}, detail=messages.instance_not_found.format(instance_uuid)) except lm_exceptions.NotAuthorizedException as e: return lm_exceptions.report_problem(403, "Forbidden", extra_info={"exception": str(e)}) except Exception as e: raise lm_exceptions.LifeMonitorException(title="Internal Error", detail=str(e))
def _get_instances_or_problem(instance_uuid): try: instance = lm.get_test_instance(instance_uuid) if not instance: return lm_exceptions.report_problem( 404, "Not Found", detail=messages.instance_not_found.format(instance_uuid)) response = _get_suite_or_problem(instance.test_suite.uuid) if isinstance(response, Response): logger.debug("Data: %r", response.get_json()) if response.status_code == 404: return lm_exceptions.report_problem( 500, "Internal Error", extra_info={"reason": response.get_json()['detail']}) details_message = "" if current_user and not current_user.is_anonymous: details_message = messages.unauthorized_user_instance_access\ .format(current_user.username, instance_uuid) elif current_registry: details_message = messages.unauthorized_registry_instance_access\ .format(current_registry.name, instance_uuid) return lm_exceptions.report_problem( 403, "Forbidden", detail=details_message, extra_info={"reason": response.get_json()}) return instance except lm_exceptions.EntityNotFoundException: return lm_exceptions.report_problem( 404, "Not Found", detail=messages.instance_not_found.format(instance_uuid))
def instances_builds_get_logs(instance_uuid, build_id, offset_bytes=0, limit_bytes=131072): if not isinstance(offset_bytes, int) or offset_bytes < 0: return lm_exceptions.report_problem(400, "Bad Request", detail=messages.invalid_log_offset) if not isinstance(limit_bytes, int) or limit_bytes < 0: return lm_exceptions.report_problem(400, "Bad Request", detail=messages.invalid_log_limit) response = _get_instances_or_problem(instance_uuid) if isinstance(response, Response): return response try: build = response.get_test_build(build_id) logger.debug("offset = %r, limit = %r", offset_bytes, limit_bytes) if build: return build.get_output(offset_bytes=offset_bytes, limit_bytes=limit_bytes) return lm_exceptions\ .report_problem(404, "Not Found", detail=messages.instance_build_not_found.format(build_id, instance_uuid)) except lm_exceptions.EntityNotFoundException: return lm_exceptions\ .report_problem(404, "Not Found", detail=messages.instance_build_not_found.format(build_id, instance_uuid)) except ValueError as e: return lm_exceptions.report_problem(400, "Bad Request", detail=str(e)) except Exception as e: logger.exception(e) return lm_exceptions.report_problem(500, "Internal Error", extra_info={"exception": str(e)})
def suites_post_instance(suite_uuid): try: response = _get_suite_or_problem(suite_uuid) if isinstance(response, Response): return response # data as JSON data = request.get_json() # notify that 'managed' are not supported if data['managed'] is True: return lm_exceptions.report_problem( 501, "Not implemented yet", detail="Only unmanaged test instances are supported!") submitter = current_user if current_user and not current_user.is_anonymous else None test_instance = lm.register_test_instance( response, submitter, data['managed'], data['name'], data['service']['type'], data['service']['url'], data['resource']) return {'test_instance_uuid': str(test_instance.uuid)}, 201 except KeyError as e: return lm_exceptions.report_problem(400, "Bad Request", extra_info={"exception": str(e)}, detail=messages.input_data_missing) except lm_exceptions.EntityNotFoundException: return "Invalid ID", 400
def _get_workflow_or_problem(wf_uuid, wf_version): try: wf = None if current_user and not current_user.is_anonymous: wf = lm.get_user_workflow_version(current_user, wf_uuid, wf_version) elif current_registry: wf = lm.get_registry_workflow_version(current_registry, wf_uuid, wf_version) else: return lm_exceptions.report_problem( 403, "Forbidden", detail=messages.no_user_in_session) if wf is None: return lm_exceptions.report_problem( 404, "Not Found", detail=messages.workflow_not_found.format(wf_uuid, wf_version)) return wf except lm_exceptions.EntityNotFoundException as e: return lm_exceptions.report_problem( 404, "Not Found", extra_info={"exception": str(e)}, detail=messages.workflow_not_found.format(wf_uuid, wf_version)) except lm_exceptions.NotAuthorizedException as e: return lm_exceptions.report_problem( 403, "Forbidden", extra_info={"exception": str(e)}, detail=messages.unauthorized_workflow_access.format(wf_uuid))
def workflows_delete(wf_uuid, wf_version): try: if current_user and not current_user.is_anonymous: lm.deregister_user_workflow(wf_uuid, wf_version, current_user) elif current_registry: lm.deregister_registry_workflow(wf_uuid, wf_version, current_registry) else: return lm_exceptions.report_problem( 403, "Forbidden", detail=messages.no_user_in_session) return connexion.NoContent, 204 except lm_exceptions.EntityNotFoundException as e: return lm_exceptions.report_problem( 404, "Not Found", extra_info={"exception": str(e.detail)}, detail=messages.workflow_not_found.format(wf_uuid, wf_version)) except OAuthIdentityNotFoundException as e: return lm_exceptions.report_problem(401, "Unauthorized", extra_info={"exception": str(e)}) except lm_exceptions.NotAuthorizedException as e: return lm_exceptions.report_problem(403, "Forbidden", extra_info={"exception": str(e)}) except Exception as e: raise lm_exceptions.LifeMonitorException(title="Internal Error", detail=str(e))
def user_registry_workflows_post(registry_uuid, body): if not current_user or current_user.is_anonymous: return lm_exceptions.report_problem(401, "Unauthorized", detail=messages.no_user_in_session) try: registry = lm.get_workflow_registry_by_uuid(registry_uuid) return workflows_post(body, _registry=registry) except lm_exceptions.EntityNotFoundException: return lm_exceptions.report_problem( 404, "Not Found", detail=messages.no_registry_found.format(registry_uuid))
def suites_delete(suite_uuid): try: response = _get_suite_or_problem(suite_uuid) if isinstance(response, Response): return response if lm.deregister_test_suite(response) == suite_uuid: return connexion.NoContent, 204 return lm_exceptions.report_problem( 500, "Internal Error", detail=messages.unable_to_delete_suite.format(suite_uuid)) except Exception as e: return lm_exceptions.report_problem( 500, "Internal Error", extra_info={"exception": str(e)}, detail=messages.unable_to_delete_suite.format(suite_uuid))
def user_registry_workflows_get(registry_uuid): if not current_user or current_user.is_anonymous: return lm_exceptions.report_problem(401, "Unauthorized", detail=messages.no_user_in_session) logger.debug("Registry UUID: %r", registry_uuid) try: registry = lm.get_workflow_registry_by_uuid(registry_uuid) workflows = lm.get_user_registry_workflows(current_user, registry) logger.debug("workflows_get. Got %s workflows (user: %s)", len(workflows), current_user) workflow_status = request.args.get('status', 'true').lower() == 'true' return serializers.ListOfWorkflows( workflow_status=workflow_status).dump(workflows) except lm_exceptions.EntityNotFoundException: return lm_exceptions.report_problem( 404, "Not Found", detail=messages.no_registry_found.format(registry_uuid))
def user_workflows_get(): if not current_user or current_user.is_anonymous: return lm_exceptions.report_problem(401, "Unauthorized", detail=messages.no_user_in_session) workflows = lm.get_user_workflows(current_user) logger.debug("user_workflows_get. Got %s workflows (user: %s)", len(workflows), current_user) workflow_status = request.args.get('status', 'true').lower() == 'true' return serializers.ListOfWorkflows( workflow_status=workflow_status).dump(workflows)
def workflows_get(): workflows = [] if current_user and not current_user.is_anonymous: workflows.extend(lm.get_user_workflows(current_user)) elif current_registry: workflows.extend(lm.get_registry_workflows(current_registry)) else: return lm_exceptions.report_problem(401, "Unauthorized", detail=messages.no_user_in_session) logger.debug("workflows_get. Got %s workflows (user: %s)", len(workflows), current_user) return serializers.WorkflowSchema().dump(workflows, many=True)
def registry_user_workflows_get(user_id): if not current_registry: return lm_exceptions.report_problem(401, "Unauthorized", detail=messages.no_registry_found) try: identity = lm.find_registry_user_identity(current_registry, external_id=user_id) workflows = lm.get_user_registry_workflows(identity.user, current_registry) logger.debug( "registry_user_workflows_get. Got %s workflows (user: %s)", len(workflows), current_user) workflow_status = request.args.get('status', 'true').lower() == 'true' return serializers.ListOfWorkflows( workflow_status=workflow_status).dump(workflows) except OAuthIdentityNotFoundException: return lm_exceptions.report_problem( 401, "Unauthorized", detail=messages.no_user_oauth_identity_on_registry.format( user_id, current_registry.name))
def workflows_get(): workflows = [] if current_user and not current_user.is_anonymous: workflows.extend(lm.get_user_workflows(current_user)) elif current_registry: workflows.extend(lm.get_registry_workflows(current_registry)) else: return lm_exceptions.report_problem(401, "Unauthorized", detail=messages.no_user_in_session) logger.debug("workflows_get. Got %s workflows (user: %s)", len(workflows), current_user) workflow_status = request.args.get('status', 'true').lower() == 'true' return serializers.ListOfWorkflows( workflow_status=workflow_status).dump(workflows)
def _get_suite_or_problem(suite_uuid): try: suite = lm.get_suite(suite_uuid) if not suite: return lm_exceptions.report_problem( 404, "Not Found", detail=messages.suite_not_found.format(suite_uuid)) response = _get_workflow_or_problem( suite.workflow_version.workflow.uuid, suite.workflow_version.version) if isinstance(response, Response): if response.status_code == 404: return lm_exceptions.report_problem( 500, "Internal Error", extra_info={"reason": response.get_json()['detail']}) details_message = "" if current_user and not current_user.is_anonymous: details_message = messages.unauthorized_user_suite_access\ .format(current_user.username, suite_uuid) elif current_registry: details_message = messages.unauthorized_registry_suite_access\ .format(current_registry.name, suite_uuid) return lm_exceptions.report_problem( 403, "Forbidden", detail=details_message, extra_info={"reason": response.get_json()['detail']}) return suite except lm_exceptions.EntityNotFoundException: return lm_exceptions.report_problem( 404, "Not Found", detail=messages.suite_not_found.format(suite_uuid))
def instances_builds_get_by_id(instance_uuid, build_id): response = _get_instances_or_problem(instance_uuid) if isinstance(response, Response): return response try: build = response.get_test_build(build_id) logger.debug("The test build: %r", build) if build: return serializers.BuildSummarySchema().dump(build) else: return lm_exceptions\ .report_problem(404, "Not Found", detail=messages.instance_build_not_found.format(build_id, instance_uuid)) except lm_exceptions.EntityNotFoundException: return lm_exceptions\ .report_problem(404, "Not Found", detail=messages.instance_build_not_found.format(build_id, instance_uuid)) except Exception as e: return lm_exceptions.report_problem(500, "Internal Error", extra_info={"exception": str(e)})
def workflows_post(body): registry = current_registry._get_current_object() if registry and 'registry' in body: return lm_exceptions.report_problem( 400, "Bad request", detail=messages.unexpected_registry_uri) if not registry and 'registry' in body: registry_ref = body.get('registry', None) try: registry = lm.get_workflow_registry_by_generic_reference( registry_ref) except lm_exceptions.EntityNotFoundException: return lm_exceptions.report_problem( 404, "Not Found", detail=messages.no_registry_found.format(registry_ref)) submitter = current_user if not current_user or current_user.is_anonymous: # the client is a registry try: submitter_id = body['submitter_id'] # Try to find the identity of the submitter identity = lm.find_registry_user_identity( registry, internal_id=current_user.id, external_id=submitter_id) submitter = identity.user except KeyError: return lm_exceptions.report_problem( 400, "Bad request", detail=messages.no_submitter_id_provided) except OAuthIdentityNotFoundException: return lm_exceptions.report_problem( 401, "Unauthorized", detail=messages.no_user_oauth_identity_on_registry.format( submitter_id or current_user.id, registry.name)) roc_link = body.get('roc_link', None) if not registry and not roc_link: return lm_exceptions.report_problem( 400, "Bad Request", extra_info={"missing input": "roc_link"}, detail=messages.input_data_missing) try: w = lm.register_workflow(roc_link=roc_link, workflow_submitter=submitter, workflow_version=body['version'], workflow_uuid=body.get('uuid', None), workflow_identifier=body.get( 'identifier', None), workflow_registry=registry, name=body.get('name', None), authorization=body.get('authorization', None)) logger.debug("workflows_post. Created workflow '%s' (ver.%s)", w.uuid, w.version) return {'wf_uuid': str(w.workflow.uuid), 'wf_version': w.version}, 201 except KeyError as e: return lm_exceptions.report_problem(400, "Bad Request", extra_info={"exception": str(e)}, detail=messages.input_data_missing) except lm_exceptions.NotValidROCrateException as e: return lm_exceptions.report_problem(400, "Bad Request", extra_info={"exception": str(e)}, detail=messages.invalid_ro_crate) except lm_exceptions.NotAuthorizedException as e: return lm_exceptions.report_problem( 403, "Forbidden", extra_info={"exception": str(e)}, detail=messages.not_authorized_registry_access.format( registry.name) if registry else messages.not_authorized_workflow_access) except lm_exceptions.WorkflowVersionConflictException: return lm_exceptions.report_problem( 409, "Workflow version conflict", detail=messages.workflow_version_conflict.format( body.get('uuid', None) or body.get('identifier', None), body['version'])) except Exception as e: logger.exception(e) raise lm_exceptions.LifeMonitorException(title="Internal Error", detail=str(e))
def workflow_registries_get_current(): if current_registry: registry = current_registry logger.debug("registries_get. Got %s registry", registry) return serializers.WorkflowRegistrySchema().dump(registry) return lm_exceptions.report_problem(401, "Unauthorized")
def user_workflows_post(body): if not current_user or current_user.is_anonymous: return lm_exceptions.report_problem(401, "Unauthorized", detail=messages.no_user_in_session) return workflows_post(body)
def registry_user_workflows_post(user_id, body): if not current_registry: return lm_exceptions.report_problem(401, "Unauthorized", detail=messages.no_registry_found) return workflows_post(body, _submitter_id=user_id)