import json


def firewall_rule_callback(request):
    """
    Checks that firewall rule contains required fields.
    Returns the same body with 201 Created.
    """
    required_fields = [
        'position',
        'direction',
        'family',
        'protocol',
        'source_address_start',
        'source_address_end',
        'destination_port_start',
        'destination_port_end',
        'action',
    ]

    request_body = json.loads(request.body)

    def check_fields(body):
        """
        Helper for checking a firewall rule body against required_fields.
        """
        for field in required_fields:
            if field not in request_body['firewall_rule']:
                raise Exception(f'missing required field: {field}. Body was:{request_body}')

    if isinstance(request_body, list):
        for body in request_body:
            check_fields(body)
    else:
        check_fields(request_body)

    return (201, {}, json.dumps(request_body))