summaryrefslogtreecommitdiff
path: root/server/doc/files/core/App.php.txt
blob: fc6c01d2debb82e7b4d37d26626d63866ffea5da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* File containing the identity Class.
*
* @version 1.0 Initialisation of this file
* @since 1.0 Core application's file
* 
* @author Eole 'eoledev at outlook . fr'
* 
*/

/*
* Library token management override
*/
include_once("core/LibOverride/genTokenOptions.php");
include_once("core/ErrorManagement.php");

//Library loading
use OpenCloud\Common\Error\BadResponseError;
use OpenCloud\Common\Error\BaseError;
use OpenCloud\Common\Error\NotImplementedError;
use OpenCloud\Common\Error\UserInputError;

/**
* App Class of the back-end application
*
* This class allow the communication between the front-end application and
* the library which allow to send requests to an Openstack instance.
*
*/
class App{
	
	/** @var OpenStack\OpenStack $openstack protected, contains the main library object */
	protected $openstack;
	/** @var Array $postParams protected, contains the post parameters */
	protected $postParams;
	/** @var genTokenOptions $tokenClass protected, contains the class object for the authentication override of the library */
	protected $tokenClass;
	/** @var String $tokenPost protected, contains the token given in parameter */
	protected $tokenPost;
	/** @var errorManagement $errorClass protected, contains the errorManagement object */
	protected $errorClass;
	/** @var Array $output protected, contains the result for the API call */
	protected $output;

	/**
	* App constructor
	*
	* @param Array $args Args for the OpenStack Library
	*
	* @return App object
	*/
	public function __construct($args){

		$this->tokenPost = NULL;
		$this->tokenClass = new genTokenOptions($args);
		$this->openstack = new OpenStack\OpenStack(['authUrl' => $args["authUrl"]]);
		$this->errorClass = new errorManagement($this);
		$this->output = array();
		$this->postParams = $_POST;

	}
	
	/**
	* Set the class var $tokenPost and load the token
	* into the genTokenOptions Class
	*
	* @param String $token token to be set
	*
	*/
	public function setToken($token){
		
		$this->tokenPost = $token;
		$this->tokenClass->loadBackup($this->tokenPost);
		
	}
	
	/**
	* Check the expiration of the token
	*
	* @return Boolean if the token is not expired
	*/
	public function checkToken(){
		return $this->tokenClass->checkToken();
	}
	
	/**
	* Get the service Class given the name in parameter
	*
	* @param String $service Name of the service 
	*
	* @return Core object
	*/
	public function getLibClass($service){
		
		switch($service){
		case "Identity":
			if($this->tokenPost == NULL) $this->tokenClass->genIdentityToken();
			$opt = $this->tokenClass->getOptions($service);
			return $this->openstack->identityV3($opt);
			break;
		case "Image":
			if($this->tokenPost == NULL) $this->tokenClass->genImageToken();
			$opt = $this->tokenClass->getOptions($service);
			return $this->openstack->imagesV2($opt);
			break;
		case "Network":
			if($this->tokenPost == NULL) $this->tokenClass->genNetworkToken();
			$opt = $this->tokenClass->getOptions($service);
			return $this->openstack->networkingV2($opt);
			break;
		case "Compute":
			if($this->tokenPost == NULL) $this->tokenClass->genComputeToken();
			$opt = $this->tokenClass->getOptions($service);
			return $this->openstack->computeV2($opt);
			break;
		case "NetworkLayer3":
			if($this->tokenPost == NULL) $this->tokenClass->genNetworkToken();
			$opt = $this->tokenClass->getOptions('Network');
			return $this->openstack->networkingV2ExtLayer3($opt);
			break;
		}
		
	}
	
	/**
	* Generate the token for the different services in OpenStack
	*
	* @return void
	*/
	public function authenticate(){
		
		try{
			$this->tokenClass->genIdentityToken();
			$this->tokenClass->genComputeToken();
			$this->tokenClass->genImageToken();
			$this->tokenClass->genNetworkToken();
			
			$this->setOutput("token", $this->tokenClass->getBackup());
		}catch(BadResponseError $e){
			$this->errorClass->BadResponseHandler($e);
		}catch(UserInputError $e){
			$this->errorClass->UserInputHandler($e);
		}catch(BaseError $e){
			$this->errorClass->BaseErrorHandler($e);
		}catch(NotImplementedError $e){
			$this->errorClass->NotImplementedHandler($e);
		}
		
	}
	
	/**
	* Revoke the openstack services' token
	*
	* @return void
	*/
	public function deauthenticate(){
		
		try{
			
			$this->tokenClass->revokeComputeToken();
			$this->tokenClass->revokeImageToken();
			$this->tokenClass->revokeNetworkToken();
			$this->tokenClass->revokeIdentityToken();
			
			$this->setOutput("deauthenticate", "Ok");
		}catch(BadResponseError $e){
			$this->errorClass->BadResponseHandler($e);
		}catch(UserInputError $e){
			$this->errorClass->UserInputHandler($e);
		}catch(BaseError $e){
			$this->errorClass->BaseErrorHandler($e);
		}catch(NotImplementedError $e){
			$this->errorClass->NotImplementedHandler($e);
		}
		
	}
	
	/**
	* Retrieve a post parameter given its name
	*
	* @param String $name Expected post parameter's name
	*
	* @return Object Post value
	*/
	public function getPostParam($name){

		if(isset($this->postParams[$name])){
			return $this->postParams[$name];
		}else{
			$this->setOutput("Error", "Missing parameter ".$name);
		}	
		
	}

	/**
	* Set a post parameter for automating task
	*
	* @param String $param Name for the Post entry
	* @param Object $value Value for the Post entry
	*
	* @return void
	*/
	public function setPostParam($param, $value){
		
		$this->postParams[$param] = $value;
		
	}
	
	/**
	* Set a new output message
	*
	* @param String $key Array key for the message
	* @param Array $out Message's value
	*
	* @return void
	*/
	public function setOutput($key, $out){
		
		$this->output[$key] = $out;
		
	}
	
	/**
	* Retrieve the errorManagement instance Object
	*
	* @return errorManagement object
	*/
	public function getErrorInstance(){
		
		return $this->errorClass;
		
	}
	
	/**
	* Output the messages to be send to the client
	*
	* @return void
	*/
	public function show(){
		echo json_encode($this->output);
	}
	
}