summaryrefslogtreecommitdiff
path: root/server/core/Compute.php
blob: 37fc8adfd824213aa379bf7dce0b3128c2396a40 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
//namespace istic-openstack\Server\core;
use OpenCloud\Common\Error;

class compute 
{
        /** @var App $app protected, contains the main app object */
        protected $app;

        /** @var OpenStack\Identity $libClass protected, contains the library Compute object */
        protected $libClass;


	    public function __construct($app)
    	{
       	         $this->app = $app;
        	 $this->libClass = $app->getLibClass("Compute");
    	}
	/**
	* Execute an action
	*
	* @param String $action name of another function of this class
	*
	* @return void
	*/
	public function action($action){
		
		 $this->{$action.""}();
		
	}
    	/**
     	* List servers.
     	* @return array
     	*/
        public function listServers()
        {
                $serverList = $this->libClass->listServers(true);
		$servers = Array();
		foreach($serverList as $server){
		$servers[$server->id] = Array();
		$server->flavor->retrieve();
		$server->image->retrieve();
		$server->retrieve();
		$servers[$server->id]["id"]  = $server->id;
		$servers[$server->id]["name"] = $server->name;
		$servers[$server->id]["image"] = $server->image; 
		$servers[$server->id]["ram"] = $server->flavor->ram;
		$servers[$server->id]["disk"] = $server->flavor->disk;
		$servers[$server->id]["flavor"] = $server->flavor; 
		$servers[$server->id]["status"] = $server->status;
		$servers[$server->id]["created"] = $server->created;
		$servers[$server->id]["updated"] = $server->updated;
		$servers[$server->id]["ipv4"] = $server->ipv4;
		$servers[$server->id]["ipv6"] = $server->ipv6;
		$servers[$server->id]["progress"] = $server->progress;
		$servers[$server->id]["hostId"] = $server->hostId;
		$servers[$server->id]["tenantId"] = $server->tenantId;
		$servers[$server->id]["userId"] = $server->userId;
		$servers[$server->id]["taskState"] = $server->taskState;
		$servers[$server->id]["addresses"] = $server->addresses;
		$servers[$server->id]["links"] = $server->links;
		$servers[$server->id]["metadata"] = $server->metadata;
		}
		$this->app->setOutput("Servers", $servers);
                return;
        }
        /**
         * List flavors.
         * @return array
         */
        public function listFlavors()
        {
                $flavorList = $this->libClass->listFlavors();
		$flavors = Array();
		foreach($flavorList as $flavor){
		$flavors[$flavor->id] = Array();
		$flavor->retrieve();
		$flavors[$flavor->id]["id"]  = $flavor->id;
		$flavors[$flavor->id]["name"] = $flavor->name;
		$flavors[$flavor->id]["ram"] = $flavor->ram;
		$flavors[$flavor->id]["disk"] = $flavor->disk;
		$flavors[$flavor->id]["vcpus"] = $flavor->vcpus;
		$flavors[$flavor->id]["links"] = $flavor->links;
		}
		$this->app->setOutput("Flavors", $flavors);
                return;
        }
        /**
         * List images.
         * @return array
         */
        public function listImages()
        {
                $imageList = $this->libClass->listImages();
		$images = Array();
		foreach($imageList as $image){
		$images[$image->id] = Array();
		$image->retrieve();
		$images[$image->id]["id"]  = $image->id;
		$images[$image->id]["name"] = $image->name;
		$images[$image->id]["status"] = $image->status;
		$images[$image->id]["created"] = $image->created;
		$images[$image->id]["updated"] = $image->updated;
		$images[$image->id]["minDisk"] = $image->minDisk;
		$images[$image->id]["minRam"] = $image->minRam;
		$images[$image->id]["progress"] = $image->progress;
		$images[$image->id]["links"] = $image->links;
		$images[$image->id]["metadata"] = $image->metadata;
		}
		$this->app->setOutput("Images", $images);
                return;
        }
       /**
         * Get server details.
         * @return array
         */
        public function getServer()
        {
		$serverId = $this->app->getPostParam("serverId");
		if(!isset($serverId)){
			$this->app->setOutput("Error", "Server ID is missing, son!");
			return;
		}
		$opt = array('id' => $serverId);
                $server = $this->libClass->getServer($opt);
		$server->retrieve();
		$this->app->setOutput("MyServer", $server);
                return;
        }
        /**
         * Get flavor details.
         * @return array
         */
        public function getFlavor()
        {
		$flavorId = $this->app->getPostParam("flavorId");
		if(!isset($serverId)){
			$this->app->setOutput("Error", "Flavor ID is missing, son!");
			return;
		}
		$opt = array('id' => $flavorId);
                $flavor = $this->libClass->getFlavor($opt);
		$flavor->retrieve();
		$this->app->setOutput("MyFlavor", $flavor);
                return;
        }
        /**
         * Get image details.
         * @return array
         */
        public function getImage()
        {
		$imageId = $this->app->getPostParam("imageId");
		if(!isset($serverId)){
			$this->app->setOutput("Error", "Image ID is missing, son!");
			return;
		}
		$opt = array('id' => $imageId);
                $image = $this->libClass->getImage($opt);
		$image->retrieve();
		$this->app->setOutput("MyImage", $image);
                return;
        }
        /**
         * Create server.
         * @return array
         */ 
        public function createServer()
        {
		$name = $this->app->getPostParam("name");
		$imageId = $this->app->getPostParam("imageId");
		$flavorId = $this->app->getPostParam("flavorId");
		if(!isset($name) || !isset($imageId) || !isset($flavorId)){
			$this->app->setOutput("Error", "No, we don't let you create a server without a name OR image ID OR flavor ID.");
			return;
		}
		$opt = array('name' => $name, 'imageId' => $imageId, 'flavorId' => $flavorId);
                $server = $this->libClass->createServer($opt);
        }
 
	/**
	* update a server
	* @return void
	*/
        public function updateServer()
        {
               	$serverId = $this->app->getPostParam("serverId");
               	$newName = $this->app->getPostParam("newName");
               	$newIpv4 = $this->app->getPostParam("newIpv4");
               	$newIpv6 = $this->app->getPostParam("newIpv6");
		if(!isset($serverId)|| !(isset($newName) || isset($newIpv4) || isset($newIpv6)) ){
			$this->app->setOutput("Error", "You'll have to provide server ID and the new attribute(IP(v4/v6)/Name) you desire to update!");
			return;
		}
		$opt = array('id' => $serverId);
                $server = $this->libClass->getServer($opt);
		if (isset($newName)){
			if(isset($newIpv4)){
				if(isset($newIpv6)){
					$attr = array('name' => $newName, 'accessIPv4' => $newIPv4, 'accessIPv6' => $newIpv6); 
				}
				else $attr = array('name' => $newName, 'accessIPv4' => $newIPv4); 
			}
			else $attr = array('name' => $newName); 
		}			
		$server->update($attr);
		$this->app->setOutput("Success", $serverId." has been updated successfully.");
                return;
        }
	/**
	* Delete a server
	* @return void
	*/
        public function deleteServer()
        {
               	$serverId = $this->app->getPostParam("serverId");
		if(!isset($serverId)){
			$this->app->setOutput("Error", "Server ID is missing, son!");
			return;
		}
		$opt = array('id' => $serverId);
                $server = $this->libClass->getServer($opt);
		$server->delete();
		$this->app->setOutput("Success", $serverId." has been deleted successfully.");
                return;
        }
	/**
	* Change the password of a server
	* @return void
	*/
        public function changePassword()
        {
		$serverId = $this->app->getPostParam("serverId");
		$password = $this->app->getPostParam("newPassword");
		if(!isset($serverId) || !isset($password)){
			$this->app->setOutput("Error", "Server ID or new password missing.");
			return;
		}
		$opt = array('id' => $serverId);
                $server = $this->libClass->getServer($opt);
		$server->changePassword($password);	
		$this->app->setOutput("Success", "Password for ".$serverId." has been updated successfully.");
		return;
        }
        /**
	* Reboot a server
	* @return void
	*/
        public function reboot()
        {
               	$serverId = $this->app->getPostParam("serverId");
		if(!isset($serverId)){
			$this->app->setOutput("Error", "Server ID is missing, son!");
			return;
		}
		$opt = array('id' => $serverId);
                $server = $this->libClass->getServer($opt);
		$server->reboot();
		$this->app->setOutput("Success", $serverId." has been deleted successfully.");
                return;
        }
       	/**
	* Rebuild a server
	* @return void
	*/
        public function rebuild()
        {
               	$serverId = $this->app->getPostParam("serverId");
               	$imageId = $this->app->getPostParam("imageId");
               	$newName = $this->app->getPostParam("newName");
               	$adminPass = $this->app->getPostParam("adminPass");
		if(!isset($serverId)|| !isset($imageId) || isset($newName) || isset($adminPass)) ){
			$this->app->setOutput("Error", "You'll have to provide server ID and the new image, name and admin password!");
			return;
		}
		$opt = array('id' => $serverId);
                $server = $this->libClass->getServer($opt);
		$attr = array('imageId' => $imageId, 'name' => $newName, 'adminPass' => $adminPass); 
		$server->rebuild($attr);
		$this->app->setOutput("Success", $serverId." has been rebuilt successfully with the new image.");
                return;
        }
       	/**
	* Resize a server
	* A call to this method has to be followed by either confirmResize or revertResize
	* @return void
	*/
        public function resize()
        {
               	$serverId = $this->app->getPostParam("serverId");
               	$newFlavorId = $this->app->getPostParam("newFlavorId");
		if(!isset($serverId)|| !isset($flavorId)){
			$this->app->setOutput("Error", "You'll have to provide server ID and the new flavor ID!");
			return;
		}
		$opt = array('id' => $serverId);
                $server = $this->libClass->getServer($opt);
		$server->resize($newFlavorId);
                return;
        }
        /**
	* Confirm resize operation on a server
	* @return void
	*/
        public function confirmResize()
        {
               	$serverId = $this->app->getPostParam("serverId");
		if(!isset($serverId)){
			$this->app->setOutput("Error", "Server ID is missing!");
			return;
		}
		$opt = array('id' => $serverId);
                $server = $this->libClass->getServer($opt);
		$server->confirmResize();
		$this->app->setOutput("Success", $serverId." has been resized successfully as the new flavor.");
                return;
        }
        /**
	* Revert resize operation on a server
	* @return void
	*/
        public function revertResize()
        {
               	$serverId = $this->app->getPostParam("serverId");
		if(!isset($serverId)){
			$this->app->setOutput("Error", "Server ID is missing!");
			return;
		}
		$opt = array('id' => $serverId);
                $server = $this->libClass->getServer($opt);
		$server->revertResize();
		$this->app->setOutput("Success", $serverId." : resize operation has been reverted to the old flavor.");
                return;
        }
	/*
        public function createImage(array $options)
        {
                //TODO
        }
        public function listAddresses(array $options = [])
        {
                //TODO
        }
        public function getMetadata()
        {
                //TODO
        }
        public function resetMetadata(array $metadata)
        {
                //TODO
        }
        public function mergeMetadata(array $metadata)
        {
                //TODO
        }
        public function getMetadataItem($key)
        {
                //TODO
        }
        public function deleteMetadataItem($key)
        {
                //TODO
        }
*/
}