123 lines
3.1 KiB
PHP
Executable file
123 lines
3.1 KiB
PHP
Executable file
<?php
|
|
/**
|
|
* File containing the Automating Class.
|
|
*
|
|
* @version 1.0 Initialisation of this file
|
|
* @since 1.0 Core application's file
|
|
*
|
|
* @author Evan Pisani 'yogg at epsina . com' et bhupi
|
|
*
|
|
* @todo Complete the functions with errors detection and finish the descriptions
|
|
*/
|
|
|
|
include("Image.php");
|
|
include("Network.php");
|
|
include("Compute.php");
|
|
include("NetworkLayer3.php");
|
|
|
|
class automating{
|
|
|
|
/** @var App $app protected, contains the main app object */
|
|
protected $compute;
|
|
protected $image;
|
|
protected $network;
|
|
protected $networkLayer3;
|
|
protected $app;
|
|
|
|
/**
|
|
* Our library's app constructor for all server app objects
|
|
*
|
|
* @param App $app the main app object, e.g. compute, image, network, etc.
|
|
*
|
|
* @return
|
|
*/
|
|
public function __construct($app){
|
|
$this->app = $app;
|
|
$compute = new Compute($app);
|
|
$image = new Image($app);
|
|
$network = new Network($app);
|
|
$networkLayer3 = new NetworkLayer3($app);
|
|
}
|
|
|
|
/**
|
|
* Execute an action
|
|
*
|
|
* @param String $action name of another function of this class
|
|
*
|
|
* @return void
|
|
*/
|
|
public function action($action){
|
|
$this->{$action.""}();
|
|
}
|
|
|
|
/**
|
|
* create a new server and associate a public ip
|
|
*
|
|
* @param String $networkId the id of the network where the server will be created
|
|
* @param String $imageName name of the new image
|
|
* @param String $serverName name ofthe new server
|
|
* @param String $flavor kind of server
|
|
*
|
|
* @return void
|
|
*/
|
|
private function createPublicServer()
|
|
{
|
|
$networkId = $this->app->getPostParam('networkId');
|
|
$imageName = $this->app->getPostParam('imageName');
|
|
$serverName = $this->app->getPostParam('serverName');
|
|
$flavor = $this->app->getPostParam('flavor');
|
|
|
|
if(!isset($imageName)){
|
|
$this->app->setOutput("Error", "Incorrect imageName parameter");
|
|
}
|
|
else if(!isset($serverName)){
|
|
$this->app->setOutput("Error", "Incorrect serverName parameter");
|
|
}
|
|
else if(!isset($flavor)){
|
|
$this->app->setOutput("Error", "Incorrect flavor parameter");
|
|
}
|
|
else{
|
|
// Création image
|
|
$opt = array();
|
|
$opt['name'] = $imageName;
|
|
$image->setPostParam('opt', $opt);
|
|
$image->action("createImage");
|
|
$image = json_decode($this->app->show(), true)["Images"];
|
|
|
|
// Création server
|
|
$compute->setPostParam('name', $serverName);
|
|
$compute->setPostParam('imageId', $image['id']);
|
|
$compute->setPostParam('flavorId', $flavor);
|
|
$compute->action("createServer");
|
|
$server = json_decode($this->app->show(), true)["Compute"];
|
|
|
|
// liste des adresses ip publiques diponibles
|
|
$networkLayer3->action("listFloatingIp");
|
|
$listFloatingIp = json_decode($App->show(), true)["NetworkLayer3"];
|
|
$ip = null;
|
|
foreach ($listFloatingIp as $f) {
|
|
if(strcmp($f['status'], "DOWN")){
|
|
$ip = $f;
|
|
}
|
|
}
|
|
|
|
// Si pas d'ip publique disponible on en créé une
|
|
if(!isset($ip)){
|
|
// Ajout adresse IP public
|
|
$optIp = array();
|
|
$opt['floatingNetworkId'] = $networkId;
|
|
$floatingIp->setPostParam('opt', $optIp);
|
|
$networkLayer3->action("createFloatingIp");
|
|
$ip = json_decode($App->show(), true)["NetworkLayer3"];
|
|
}
|
|
|
|
// Association de l'ip publique au serveur
|
|
/*
|
|
* API non diponible pour le moment
|
|
*/
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|