# How to 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. This tutorial is tested with PHP version 7.0. 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('https://identity.api.fuga.io:5000/v2.0/tokens'); // Set the request type to POST. curl_setopt($ch,CURLOPT_POST,true);

// 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',
)
)
);

// Make sure we return instead of echo the resulting response.
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_CAINFO, "path\to\your\cacert.pem");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true); 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,"https://compute.api.fuga.io/v2/".$tenantId."/flavors/detail"); // Now, retrieve a list of flavors. curl_setopt($ch,CURLOPT_POST,false); // Again, GET request.
curl_setopt($ch,CURLOPT_HTTPHEADER, array( "X-Auth-Token:" .$token
)
); // Only need the token....

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

// Finally, retrieve the list of images.
curl_setopt($ch,CURLOPT_URL,"https://image.api.fuga.io/v2/images"); curl_setopt($ch,CURLOPT_POST,false);
curl_setopt($ch,CURLOPT_HTTPHEADER, array( "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 18.04 LTS - Bionic Beaver - 64-bit - Fuga Cloud Based Image') {
$imageid =$image->id;
}
}

if (!$flavorid) throw new exception("Could not find flavor"); if (!$imageid)
throw new exception("Could not find image");

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


// Finally - create the server.
curl_setopt($ch,CURLOPT_URL,"https://compute.api.fuga.io/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(
"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));
var_dump(curl_exec(\$ch));