"""Interface to the ows-project-manager microservice.""" from flask import g from oto import response from owsrequest import request from product_digital.connectors.sentry import send_to_sentry from product_digital.constants import error from product_digital.constants import services def get_project_by_id(project_id, include_deletions=False): """Get project by its id. Fetch project information by performing a GET call to ows-project-manager Args: project_id (int): uid of a project. include_deletions (bool): Optional flag to include deleted projects Returns: response.Response: the result of getting project by id. """ resource = '/project/{project_id}'.format(project_id=project_id) if include_deletions: resource += '?include_deletions=True' ows_project_response = request.get(services.OWS_PROJECT_MANAGER, resource) message = ows_project_response.json() if ows_project_response.status_code == 200: return response.Response(message=message) if ows_project_response.status_code == 404: return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, status=400, message=error.ERROR_MESSAGE_INVALID_PROJECT) error_message = '{} returns {} status'.format( services.OWS_PROJECT_MANAGER, ows_project_response.status_code) if message: error_message += ' with body {}'.format(message.get('message')) g.ows.log.error(error_message) send_to_sentry('get_project_by_id_error', {}, ows_project_response.status_code, error_message) return response.create_fatal_response() def check_project_ownership(project_id, account_type, account_id): """Check project ownership by making call to ows-project. Args: project_id (int): uid of project. account_type (str): the type of account e.g. 'vendor' account_id (int): uid of the account Returns: response.Response: the result of the ownership check """ resource = \ '/ownership/{account_type}/{account_id}/project/{project_id}'.format( account_type=account_type, account_id=account_id, project_id=project_id) ows_project_response = request.head(services.OWS_PROJECT_MANAGER, resource) if ows_project_response.status_code == 200: return response.Response() if ows_project_response.status_code == 403: return response.create_error_response( code=error.ERROR_CODE_AUTHORIZATION, message=error.ERROR_MESSAGE_NOT_OWNER, status=403) if ows_project_response.status_code == 404: return response.create_error_response( code=error.ERROR_CODE_NOT_FOUND, message=error.ERROR_MESSAGE_INVALID_PROJECT, status=400) error_message = '{} returns {} status'.format( services.OWS_PROJECT_MANAGER, ows_project_response.status_code) g.ows.log.error(error_message) send_to_sentry('check_project_ownership_error', {}, ows_project_response.status_code, error_message) return response.create_fatal_response()