import uuid


def create_runner(hc, type, datacenter, servertype, image, userdata):
    """
    The create_runner function takes a hetzner cloud handle (session) and some runner definitions to
    create a new cloud server to be used as a Gitlab CI runner.

    Parameters:
    datacenter:  The datacenter where the new Cloud VM should be created
    name:        The name of the new Cloud VM (default: type and a UUID, for ex. docker-2288393hd92dh9hd23d8h92d)
    server_type: The Cloud VM type. This defines the amount of CPU, RAM, Disk of the VM
    image:       The Operating System Base image to be used for spawning the new Cloud VM (e.g. Debian)
    ssh_keys:    A set of SSH Public keys to be installed into the new Cloud VM for remove management
    user_data:   Takes a cloud-init config for further configuration of the new Cloud VM after spawning (e.g. install additional tools)
    labels:      Adds a label depending on the "type" parameter that is true. This is used to easily find corresponding Cloud VMs easier for
                 later removal
    """
    response = hc.servers.create(
        datacenter=hc.datacenters.get_by_name(name=datacenter),
        name='{}-{}'.format(type, str(uuid.uuid4())),
        server_type=hc.server_types.get_by_name(name=servertype),
        image=hc.images.get_by_name(name=image),
        ssh_keys=hc.ssh_keys.get_all(),
        user_data=userdata,
        labels={type: "true"}
    )

    return(response.action.status)