API openstack

Create a server with the OpenStack API

OpenStack has an API that can be used in a myriad of ways. Here we will demonstrate a simple but effective PHP script that logs into the API, retrieves and selects a flavor, retrieves and selects a image and continues to create a simple server. This tutorial assumes a small degree of familiarity with PHP and the availability of PHP with the Curl library. First, we will need to login and acquire an authentication token and retrieve the tenantId. To do this, we make the following request. Change YourTenant, YourUsername and YourPassword with the corresponding values.

// Initialize cURL
$ch = curl_init('http://identity.api.fuga.io:5000/v2.0/tokens');

// Set the request type to POST.

// Specify the HTTP headers.
curl_setopt($ch,CURLOPT_HTTPHEADER,array (
    "Accept: application/json",
        "Content-Type: application/json"


// Specify content of post request.
$data = array(
        "auth" => array(
            "tenantName" => 'YourTenant',
            "passwordCredentials" => array(
                    "username" => "YourUsername",
                    "password" => "YourPassword"

// Make sure we return instead of echo the resulting response.
curl_setopt($ch,CURLOPT_POSTFIELDS, "" . json_encode($data));

// Get and decode the response.
$response = json_decode(curl_exec($ch));
$token = $response->access->token->id;
$tenantId = $response->access->token->tenant->id;

We now have our token and tenant ids. Next, get a list of servers, flavors and images. The flavors and images are used to get the correct ID's that we need to create a new server. Getting the list of servers is just for fun! These steps are very similar and a lot of other lists can be retrieved in similar ways.

    curl_setopt($ch,CURLOPT_URL,"http://identity.api.fuga.io/v2/" . $tenantId . "/servers"); // Next, we get a list of servers.
    curl_setopt($ch,CURLOPT_POST,false); // This is a GET request.
        "X-Auth-Token:" . $token
    )); // Only the token we retrieved earlier is needed.

    curl_setopt($ch,CURLOPT_URL,"http://identity.api.fuga.io/v2/" . $tenantId . "/flavors"); // Now, retrieve a list of flavors.
    curl_setopt($ch,CURLOPT_POST,false); // Again, GET request.
        "X-Auth-Token:" . $token
    )); // Only need the token....

    $flavors = json_decode(curl_exec($ch));

    curl_setopt($ch,CURLOPT_URL,"http://identity.api.fuga.io/v2/" . $tenantId . "/images"); // Finally, retrieve the list of images.
        "X-Auth-Token:" . $token

    $images = json_decode(curl_exec($ch));

Now, we are going to see what the id's are that we need. We use the id's in the next step to create the actual server. We assume a simple c1.micro flavor with ubuntu cloud image - Change this to what you want!

    // Select the flavor we need, save the ID.
    foreach($flavors->flavors as $flavor)
        if ($flavor->name == 'c1.micro')
            $flavorid = $flavor->id;

  // Select the image we need. Save the ID.
    foreach($images->images as $image)
        if($image->name == 'Ubuntu 14.04 cloud image')
            $imageid = $image->id;

    if (!$flavorid)
        throw new exception("Could not find flavorn");
    if (!$imageid)
        throw new exception("Could not find imagen");

And now comes the final step! Create the actual server.

  // Finally - create the server.
    curl_setopt($ch,CURLOPT_URL,"http://identity.api.fuga.io:8774/v2/" . $tenantId . "/servers");
    curl_setopt($ch,CURLOPT_POST,TRUE); // This requires a post request.
  curl_setopt($ch,CURLOPT_HTTPHEADER,array (
    "Accept: application/json",
        "X-Auth-Token:" . $token,
    "Content-Type: application/json"
  )); // Specify the token and content type..

  $data =array('server' =>  array(
    "tenant_id" => $tenantId,
        "imageRef" => $imageid,
        "flavorRef" => $flavorid,
        "name" => "server" . rand(200,2000)
  )); // this is the information we need to create the server.

  curl_setopt($ch,CURLOPT_POSTFIELDS, "" . json_encode($data));
    $response = curl_exec($ch);

Was this article helpful?