예제 #1
0
def delete_template(template_id):
    logger.info(f'Deleting template id {template_id}')
    resp = templates_dynamodb.delete_template(template_id)
    if resp:
        templates_s3.delete_file(Template.from_dict(resp))
        return '', http.HTTPStatus.NO_CONTENT
    else:
        return jsonify({'error': 'The template does not exists'
                        }), http.HTTPStatus.NOT_FOUND
예제 #2
0
def get_template(template_id):
    logger.info(f'Getting template for template id {template_id}')
    resp = templates_dynamodb.get_template(template_id)
    if not resp:
        logger.error(f'Template with id {template_id} does not exist')
        return jsonify({'error':
                        'Template does not exist'}), http.HTTPStatus.NOT_FOUND

    return jsonify(Template.from_json(resp))
예제 #3
0
def delete_template_file(template_id):
    logger.info(
        f'Deleting template file from s3 for template id {template_id}')
    resp = templates_dynamodb.get_template(template_id)
    if not resp:
        logger.error(f'Template with id {template_id} does not exist')
        return jsonify({'error':
                        'Template does not exist'}), http.HTTPStatus.NOT_FOUND

    templates_s3.delete_file(Template.from_json(resp))
    return '', http.HTTPStatus.NO_CONTENT
예제 #4
0
def save_template_file(template_id):
    logger.info(f'Adding template file to s3 for template id {template_id}')
    resp = templates_dynamodb.get_template(template_id)
    if not resp:
        logger.error(f'Template with id {template_id} does not exist')
        return jsonify({'error':
                        'Template does not exist'}), http.HTTPStatus.NOT_FOUND

    template = Template.from_json(resp)
    templates_s3.save_file(template, request.json)
    template.parameters = mock_service.parse_template_parameters(request.json)
    templates_dynamodb.update_template(template)
    return jsonify(template)
예제 #5
0
def create_template():
    try:
        logger.info(f'Creating a new template for request {request.json}')
        request.json['id'] = str(uuid4())[:8]
        template = Template.from_dict(request.json)
    except (KeyError, TypeError):
        logger.exception('Error creating template')
        return jsonify({'error': 'Please provide at least a valid id'
                        }), http.HTTPStatus.BAD_REQUEST
    if templates_dynamodb.add_template(template) is None:
        return jsonify({'error': 'The template path already exists'
                        }), http.HTTPStatus.BAD_REQUEST
    return jsonify(template)
예제 #6
0
def get_template_file(template_id):
    logger.info(f'Getting template file from s3 for template id {template_id}')
    resp = templates_dynamodb.get_template(template_id)
    if not resp:
        logger.error(f'Template with id {template_id} does not exist')
        return jsonify({'error':
                        'Template does not exist'}), http.HTTPStatus.NOT_FOUND

    file = templates_s3.get_file(Template.from_json(resp))
    if not file:
        logger.error(f'There is no file loaded for template_id {template_id}')
        return jsonify({'error':
                        'File does not exist'}), http.HTTPStatus.NOT_FOUND
    return jsonify(file)
예제 #7
0
def mock():
    try:
        logger.info(
            f'Trying to return mock response for request {request.json}')
        mock_request = MockRequest.from_dict(request.json)
    except (KeyError, TypeError):
        logger.exception('Error creating mock request')
        return jsonify({'error':
                        'Invalid request'}), http.HTTPStatus.BAD_REQUEST

    user_json = users_dynamodb.get_user(mock_request.userId)
    if not user_json:
        logger.error(f'User id not found for request {mock_request.userId}')
        return jsonify({'error': f'User {mock_request.userId} not found'
                        }), http.HTTPStatus.NOT_FOUND

    user = User.from_json(user_json)
    if not user.isMock:
        logger.warning(f'User {mock_request.userId} is not mock available')
        return '', http.HTTPStatus.NO_CONTENT

    if not mock_request.httpStatus:
        mock_request.httpStatus = user.defaultResponse

    template_reduced = mock_service.search_template(mock_request)
    if not template_reduced:
        logger.error(
            f'Template id not found for path {mock_request.path} and http status response '
            f'{mock_request.httpStatus}')
        return jsonify({'error':
                        'Template id not found'}), http.HTTPStatus.NOT_FOUND

    template = Template.from_json(
        templates_dynamodb.get_template(template_reduced['id']))
    file = templates_s3.get_file(template)
    if not file:
        logger.error(
            f"File not found for path {mock_request.path} and template id {template_reduced['id']}"
        )
        return jsonify({'error': 'File not found'}), http.HTTPStatus.NOT_FOUND

    if template.responseType == 'json':
        return jsonify(
            mock_service.parse_file(mock_request, file, template,
                                    user.parameters))

    return '', http.HTTPStatus.NO_CONTENT
예제 #8
0
def update_template(template_id):
    logger.info(f'Updating template id {template_id} with {request.json}')
    resp = templates_dynamodb.get_template(template_id)
    if not resp:
        logger.error(f'Template with id {template_id} does not exist')
        return jsonify({'error':
                        'Template does not exist'}), http.HTTPStatus.NOT_FOUND

    template = Template.from_json(resp)
    default_parameters = request.json['defaultParameters']
    if not default_parameters or not isinstance(default_parameters, dict):
        logger.error(f'Bad request to update template with id {template_id}')
        return jsonify({
            'error':
            'To update template defaultParameters are required'
        }), http.HTTPStatus.BAD_REQUEST

    template.defaultParameters = default_parameters
    templates_dynamodb.update_template(template)
    return jsonify(template)