def instance_create(request):

    # Get multipart-form data: image_id, realm_id, hwp_name, name
    try:
        image_id = request.POST["image_id"]
        realm_id = request.POST["realm_id"]
    except KeyError:
        return HttpResponseBadRequest("Both an image_id and a realm_id must " \
                "be specified.")

    # Get the libcloud driver
    try:
        policy = Policy.objects.get(id=realm_id)
    except ObjectDoesNotExist:
        return HttpResponseBadRequest("The requested realm_id was not found.")
    provider = policy.get_next_provider(image_id)
    driver = provider.get_client()

    # Get the RealImage object
    try:
        aggregate_image = AggregateImage.objects.get(id=image_id)
    except ObjectDoesNotExist:
        return HttpResponseBadRequest("The requested image_id was not found.")
    try:
        real_image = RealImage.objects.get(
            aggregate_image=aggregate_image,
            provider=provider,
        )
    except ObjectDoesNotExist:
        return HttpResponseBadRequest("There is no aggregate image image " \
                "matching this provider.")

    # Get the libcloud node object
    image = None
    for node in driver.list_images():
        if node.id == real_image.image_id:
            image = node
            break
    if not image:
        #TODO: This should probably return an HTTP error code instead of
        #      raising an exception
        raise ValueError("Image was not found in the provider: %s" % \
            real_image
        )

    # Get an instance size
    size = driver.list_sizes()[0]

    # Add instance to database
    # We do this before the actual creating of the image so we can get the
    # instance_id
    database_instance = Instance(
        image = aggregate_image,
        owner_id = "",
        name = "",
        provider = provider,
        instance_id = -1,
        policy = policy,
    )
    database_instance.save()

    # Start the instance!
    instance = driver.create_node(
        image=image,
        name="dcmux-%s" % database_instance.id,
        size=size,
    )

    # Update instance_id in database
    database_instance.instance_id = instance.id
    database_instance.save()

    return render_to_response(
        'dcmux/instances.xml',
        {
            "images_uri": uri_lookup(request, "images"),
            "instances_uri": uri_lookup(request, "instances"),
            "instances": [instance],
        },
        mimetype="application/xml",
    )
Creates an instance with the policy given by realm_id.
dataset/ETHPy150Open bmbouter/Opus/opus/project/dcmux/views.py/instance_create