<?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("CoreInterface.php");
include("Image.php");
include("Network.php");
include("Compute.php");

class automating implements Core{

	/** @var App $app protected, contains the main app object */
	protected $appCompute;
	protected $appImage;
	protected $appNetwork;
	protected $appIdentity;
	protected $appFloatingIp;
	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){
		if(!isset($app)){
			$this->app->setOutput("Error", "Parameter app missing.");
		}
		$this->appCompute = $appCompute;
		$this->appImage = $appImage;
		$this->appNetwork = $appNetwork;
		$this->appIdentity = $appIdentity;
		$this->appFloatingIp = $appFloatingIp;
		$this->app = $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 $imageName name of the new image
	* @param String $serverName name ofthe new server
	* @param String $flavor kind of server
	*	
	* @return void
	*/
	private function createServer()
	{
		$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;
			$this->appImage->setPostParam('opt' $opt);	
			$this->appImage->createImage();
			$image = json_decode($this->app->show(), true)["Images"];

			// Création server
			$this->appCompute->setPostParam('name', $serverName);
			$this->appCompute->setPostParam('imageId', $image['id']);
			$this->appCompute->setPostParam('flavorId', $flavor);
			$this->appCompute->createServer();
			$server = json_decode($this->app->show(), true)["Compute"];

			// Ajout adresse IP public
			$optIp = array();
			$opt['floatingip'] = null; //new floatingip(); ???
			$opt['floating_network_id'] = $server['id'];
			$this->appFloatingIp->setPostParam('opt', $optIp);	
			$this->appFloatingIp->create(); 

		}
	}

}

?>