forked from virtuozzo/OnApp-PHP-Wrapper-External
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disk.php
395 lines (368 loc) · 13 KB
/
Disk.php
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Managing Disks
*
* @category API wrapper
* @package OnApp
* @author Vitaliy Kondratyuk
* @copyright © 2011 OnApp
* @link http://www.onapp.com/
* @see OnApp
*/
/**
*
*/
define( 'ONAPP_GETRESOURCE_AUTOBACKUP_ENABLE', 'autobackup_enable' );
/**
*
*/
define( 'ONAPP_GETRESOURCE_AUTOBACKUP_DISABLE', 'autobackup_disable' );
/**
*
*/
define( 'ONAPP_GETRESOURCE_TAKE_BACKUP', 'backups' );
/**
* Managing Disks
*
* The OnApp_Disk class uses the following basic methods:
* {@link load}, {@link save}, {@link delete}, {@link getList}.
*
* For full fields reference and curl request details visit: ( http://help.onapp.com/manual.php?m=2 )
*/
class OnApp_Disk extends OnApp {
/**
* root tag used in the API request
*
* @var string
*/
var $_tagRoot = 'disk';
/**
* alias processing the object data
*
* @var string
*/
var $_resource = 'settings/disks';
/**
* API Fields description
*
* @param string|float $version OnApp API version
* @param string $className current class' name
*
* @return array
*/
public function initFields( $version = null, $className = '' ) {
switch( $version ) {
case '2.0':
case '2.1':
case 2.2:
case 2.3:
$this->fields = array(
'id' => array(
ONAPP_FIELD_MAP => '_id',
ONAPP_FIELD_TYPE => 'integer',
ONAPP_FIELD_READ_ONLY => true,
),
'created_at' => array(
ONAPP_FIELD_MAP => '_created_at',
ONAPP_FIELD_TYPE => 'datetime',
ONAPP_FIELD_READ_ONLY => true,
),
'updated_at' => array(
ONAPP_FIELD_MAP => '_updated_at',
ONAPP_FIELD_TYPE => 'datetime',
ONAPP_FIELD_READ_ONLY => true,
),
'add_to_linux_fstab' => array(
ONAPP_FIELD_MAP => '_add_to_linux_fstab',
ONAPP_FIELD_TYPE => 'boolean',
),
'disk_size' => array(
ONAPP_FIELD_MAP => '_disk_size',
ONAPP_FIELD_TYPE => 'integer',
ONAPP_FIELD_REQUIRED => true,
),
'primary' => array(
ONAPP_FIELD_MAP => '_primary',
ONAPP_FIELD_TYPE => 'boolean',
ONAPP_FIELD_READ_ONLY => true,
),
'data_store_id' => array(
ONAPP_FIELD_MAP => '_data_store_id',
ONAPP_FIELD_TYPE => 'integer',
ONAPP_FIELD_REQUIRED => true,
),
'disk_vm_number' => array(
ONAPP_FIELD_MAP => '_disk_vm_number',
ONAPP_FIELD_TYPE => 'integer',
ONAPP_FIELD_READ_ONLY => true,
),
'is_swap' => array(
ONAPP_FIELD_MAP => '_is_swap',
ONAPP_FIELD_TYPE => 'boolean',
),
'mount_point' => array(
ONAPP_FIELD_MAP => '_mount_point',
),
'identifier' => array(
ONAPP_FIELD_MAP => '_identifier',
),
'file_system' => array(
ONAPP_FIELD_MAP => '_file_system',
ONAPP_FIELD_READ_ONLY => true,
),
'virtual_machine_id' => array(
ONAPP_FIELD_MAP => '_virtual_machine_id',
ONAPP_FIELD_TYPE => 'integer',
ONAPP_FIELD_READ_ONLY => true,
),
'require_format_disk' => array(
ONAPP_FIELD_MAP => '_require_format_disk',
ONAPP_FIELD_TYPE => 'integer',
ONAPP_FIELD_READ_ONLY => true,
),
'built' => array(
ONAPP_FIELD_MAP => '_built',
ONAPP_FIELD_TYPE => 'boolean',
ONAPP_FIELD_READ_ONLY => true,
),
'locked' => array(
ONAPP_FIELD_MAP => '_locked',
ONAPP_FIELD_TYPE => 'boolean',
ONAPP_FIELD_READ_ONLY => true,
),
'has_autobackups' => array(
ONAPP_FIELD_MAP => '_has_autobackups',
ONAPP_FIELD_TYPE => 'boolean',
ONAPP_FIELD_READ_ONLY => true,
ONAPP_FIELD_SKIP_FROM_REQUEST => true,
),
);
break;
case 3.0:
case 3.1:
case 3.2:
case 3.3:
$this->fields = $this->initFields( 2.3 );
break;
}
parent::initFields( $version, __CLASS__ );
return $this->fields;
}
/**
* Returns the URL Alias of the API Class that inherits the OnApp class
*
* @param string $action action name
*
* @return string API resource
* @access public
*/
function getResource( $action = ONAPP_GETRESOURCE_DEFAULT ) {
switch( $action ) {
case ONAPP_GETRESOURCE_LIST:
/**
* ROUTE :
*
* @name virtual_machine_disks
* @method GET
* @alias /virtual_machines/:virtual_machine_id/disks(.:format)
* @format {:controller=>"disks", :action=>"index"}
*/
$resource = $this->_virtual_machine_id ?
'virtual_machines/' . $this->_virtual_machine_id . '/disks' :
$this->getResource();
break;
case ONAPP_GETRESOURCE_ADD:
/**
* ROUTE :
*
* @name
* @method POST
* @alias /virtual_machines/:virtual_machine_id/disks(.:format)
* @format {:controller=>"disks", :action=>"create"}
*/
if( is_null( $this->_virtual_machine_id ) ) {
$this->logger->error(
'getResource( ' . $action . ' ): argument _virtual_machine_id not set.',
__FILE__,
__LINE__
);
}
else {
$resource = 'virtual_machines/' . $this->_virtual_machine_id . '/disks';
}
break;
case ONAPP_GETRESOURCE_AUTOBACKUP_ENABLE:
/**
* ROUTE :
*
* @name autobackup_enable_disk
* @method POST
* @alias /settings/disks/:id/autobackup_enable(.:format)
* @format {:controller=>"disks", :action=>"autobackup_enable"}
*/
$resource = $this->getResource( ONAPP_GETRESOURCE_LOAD ) . '/autobackup_enable';
break;
case ONAPP_GETRESOURCE_AUTOBACKUP_DISABLE:
/**
* ROUTE :
*
* @name autobackup_disable_disk
* @method POST
* @alias /settings/disks/:id/autobackup_disable(.:format)
* @format {:controller=>"disks", :action=>"autobackup_disable"}
*/
$resource = $this->getResource( ONAPP_GETRESOURCE_LOAD ) . '/autobackup_disable';
break;
case ONAPP_GETRESOURCE_TAKE_BACKUP:
/**
* ROUTE :
*
* @name
* @method POST
* @alias /settings/disks/:disk_id/backups(.:format)
* @format {:controller=>"backups", :action=>"create"}
*/
$resource = $this->getResource( ONAPP_GETRESOURCE_LOAD ) . '/backups';
break;
default:
/**
* ROUTE :
*
* @name disks
* @method GET
* @alias /settings/disks(.:format)
* @format {:controller=>"disks", :action=>"index"}
*/
/**
* ROUTE :
*
* @name disk
* @method GET
* @alias /settings/disks/:id(.:format)
* @format {:controller=>"disks", :action=>"show"}
*/
/**
* ROUTE :
*
* @name
* @method POST
* @alias /settings/disks(.:format)
* @format {:controller=>"disks", :action=>"create"}
*/
/**
* ROUTE :
*
* @name
* @method PUT
* @alias /settings/disks/:id(.:format)
* @format {:controller=>"disks", :action=>"update"}
*/
/**
* ROUTE :
*
* @name
* @method DELETE
* @alias /settings/disks/:id(.:format)
* @format {:controller=>"disks", :action=>"destroy"}
*/
$resource = parent::getResource( $action );
break;
}
$actions = array(
ONAPP_GETRESOURCE_LIST,
ONAPP_GETRESOURCE_ADD,
ONAPP_GETRESOURCE_AUTOBACKUP_ENABLE,
ONAPP_GETRESOURCE_AUTOBACKUP_DISABLE,
);
if( in_array( $action, $actions ) ) {
$this->logger->debug( 'getResource( ' . $action . ' ): return ' . $resource );
}
return $resource;
}
/**
* Enable autobackup
*
* @param null $id
*
* @return void
*/
function enableAutobackup( $id = null ) {
if( $id ) {
$this->_id = $id;
}
$this->sendPost( ONAPP_GETRESOURCE_AUTOBACKUP_ENABLE, '' );
}
/**
* Disable autobackup
*
* @param null $id
*
* @return void
*/
function disableAutobackup( $id = null ) {
if( $id ) {
$this->_id = $id;
}
$this->sendPost( ONAPP_GETRESOURCE_AUTOBACKUP_DISABLE, '' );
}
/**
* Sends an API request to get the Objects. After requesting,
* unserializes the received response into the array of Objects
*
* @param integer $vm_id VM ID
*
* @return mixed an array of Object instances on success. Otherwise false
* @access public
*/
function getList( $vm_id = null, $url_args = null ) {
if( $vm_id ) {
$this->_virtual_machine_id = $vm_id;
}
return parent::getList();
}
/**
* The method saves an Object to your account
*
* @param integer $vm_id VM ID
*
* @return mixed Serialized API Response
* @access private
*/
function save() {
if( $this->_virtual_machine_id ) {
$this->fields[ 'require_format_disk' ] = array(
ONAPP_FIELD_MAP => '_require_format_disk',
ONAPP_FIELD_TYPE => 'integer',
ONAPP_FIELD_REQUIRED => true,
ONAPP_FIELD_DEFAULT_VALUE => false,
);
}
if( $this->_id ) {
$this->fields[ 'add_to_linux_fstab' ][ ONAPP_FIELD_REQUIRED ] = false;
$this->fields[ 'data_store_id' ][ ONAPP_FIELD_REQUIRED ] = false;
$this->fields[ 'is_swap' ][ ONAPP_FIELD_REQUIRED ] = false;
$this->fields[ 'mount_point' ][ ONAPP_FIELD_REQUIRED ] = false;
}
return parent::save();
}
/**
* Takes Disk Backup
*
* @param integer $disk_id Disk Id
*
* @return void
*/
function takeBackup( $disk_id ) {
if( $disk_id ) {
$this->_id = $disk_id;
}
// workaround because we get backup data in response
$this->_tagRoot = 'backup';
$this->className = 'OnApp_VirtualMachine_Backup';
$backup = new OnApp_VirtualMachine_Backup();
$backup->initFields( $this->getAPIVersion() );
$this->fields = $backup->getClassFields();
$this->sendPost( ONAPP_GETRESOURCE_TAKE_BACKUP );
}
}