import requests
import json


def create_role(auth, name, owners, admins, members, host):
    """Creates a role in the system.

       Args:
           auth: The bearer token to be used in the Authorization header
           name: The name of the role
           owners: Owners of the role (add/remove members)
           admins: Administrators of the role (modify the role itself)
           members: Members of the role (inherit the privileges assigned to the role)


       Returns the response payload of the role creation rest call as a json object.
    """

    payload_create_role = {
        "name": name,
        "owners": owners,
        "administrators": admins,
        "members": members,
        "metadata": ""
    }

    response_create_role_envelope = requests.post('http://' + host + ':8000/api/roles/', json=payload_create_role,
                                                  headers={"Content-Type": "application/json",
                                                           "Authorization": auth})

    if response_create_role_envelope.status_code != 200:
        raise RuntimeError('Failed to create role. Response: ' + response_create_role_envelope.text)

    response_create_role_payload = json.loads(response_create_role_envelope.text)
    return response_create_role_payload