forked from blesta/module-vultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vultr.php
2648 lines (2285 loc) · 99.8 KB
/
vultr.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Blesta\Core\Util\Validate\Server;
/**
* Vultr Module.
*
* @package blesta
* @subpackage blesta.components.modules.vultr
* @copyright Copyright (c) 2018, Phillips Data, Inc.
* @license http://www.blesta.com/license/ The Blesta License Agreement
* @link http://www.blesta.com/ Blesta
*/
class Vultr extends Module
{
/**
* Initializes the module.
*/
public function __construct()
{
// Load components required by this module
Loader::loadComponents($this, ['Input']);
// Load the language required by this module
Language::loadLang('vultr', null, dirname(__FILE__) . DS . 'language' . DS);
// Load config
Configure::load('vultr', dirname(__FILE__) . DS . 'config' . DS);
// Load module config
$this->loadConfig(dirname(__FILE__) . DS . 'config.json');
}
/**
* Returns all tabs to display to an admin when managing a service whose
* package uses this module.
*
* @param stdClass $package A stdClass object representing the selected package
* @return array An array of tabs in the format of method => title.
* Example: array('methodName' => "Title", 'methodName2' => "Title2")
*/
public function getAdminTabs($package)
{
$tabs = [
'tabActions' => Language::_('Vultr.tab_actions', true),
'tabStats' => Language::_('Vultr.tab_stats', true),
'tabSnapshots' => Language::_('Vultr.tab_snapshots', true),
'tabBackups' => Language::_('Vultr.tab_backups', true)
];
if ($package->meta->server_type == 'baremetal') {
unset($tabs['tabSnapshots']);
unset($tabs['tabBackups']);
}
return $tabs;
}
/**
* Returns all tabs to display to a client when managing a service whose
* package uses this module.
*
* @param stdClass $package A stdClass object representing the selected package
* @return array An array of tabs in the format of method => title.
* Example: array('methodName' => "Title", 'methodName2' => "Title2")
*/
public function getClientTabs($package)
{
$tabs = [
'tabClientActions' => Language::_('Vultr.tab_client_actions', true),
'tabClientStats' => Language::_('Vultr.tab_client_stats', true),
'tabClientSnapshots' => Language::_('Vultr.tab_client_snapshots', true),
'tabClientBackups' => Language::_('Vultr.tab_client_backups', true),
];
if ($package->meta->server_type == 'baremetal') {
unset($tabs['tabSnapshots']);
unset($tabs['tabBackups']);
}
return $tabs;
}
/**
* Returns all fields used when adding/editing a package, including any
* javascript to execute when the page is rendered with these fields.
*
* @param $vars stdClass A stdClass object representing a set of post fields
* @return ModuleFields A ModuleFields object, containing the fields to
* render as well as any additional HTML markup to include
*/
public function getPackageFields($vars = null)
{
Loader::loadHelpers($this, ['Html']);
$fields = new ModuleFields();
$fields->setHtml("
<script type=\"text/javascript\">
$(document).ready(function() {
// Set whether to show or hide the plans option, depending on the server type
$('#vultr_baremetal_plan').closest('li').hide();
$('#vultr_server_plan').closest('li').hide();
if ($('#vultr_server_type').val() == 'server') {
$('#vultr_server_plan').closest('li').show();
$('#vultr_baremetal_plan').closest('li').hide();
} else {
$('#vultr_baremetal_plan').closest('li').show();
$('#vultr_server_plan').closest('li').hide();
}
$('#vultr_server_type').change(function() {
if ($(this).val() == 'server') {
$('#vultr_server_plan').closest('li').show();
$('#vultr_baremetal_plan').closest('li').hide();
} else {
$('#vultr_baremetal_plan').closest('li').show();
$('#vultr_server_plan').closest('li').hide();
}
});
// Set whether to show or hide the template option
$('select[name=\"meta[template]\"]').closest('li').hide();
$('input[name=\"meta[surcharge_templates]\"]').closest('li').hide();
if ($('input[name=\"meta[set_template]\"]:checked').val() == 'admin') {
$('select[name=\"meta[template]\"]').closest('li').show();
$('input[name=\"meta[surcharge_templates]\"]').closest('li').hide();
} else {
$('select[name=\"meta[template]\"]').closest('li').hide();
$('input[name=\"meta[surcharge_templates]\"]').closest('li').show();
}
$('input[name=\"meta[set_template]\"]').change(function() {
if ($(this).val() == 'admin') {
$('select[name=\"meta[template]\"]').closest('li').show();
$('input[name=\"meta[surcharge_templates]\"]').closest('li').hide();
} else {
$('select[name=\"meta[template]\"]').closest('li').hide();
$('input[name=\"meta[surcharge_templates]\"]').closest('li').show();
}
});
});
</script>
");
// Fetch the 1st account from the list of accounts
$module_row = null;
$rows = $this->getModuleRows();
if (isset($rows[0])) {
$module_row = $rows[0];
}
unset($rows);
// Fetch all the plans available for the different server types
$baremetal_plans = [];
$server_plans = [];
$server_templates = [];
if ($module_row) {
$baremetal_plans = $this->getBaremetalPlans($module_row);
$server_plans = $this->getServerPlans($module_row);
$server_templates = $this->getTemplates($module_row);
}
// Set the Vultr server type as a selectable option
$server_type = $fields->label(
Language::_('Vultr.package_fields.server_type', true),
'vultr_server_type'
);
$server_type->attach(
$fields->fieldSelect(
'meta[server_type]',
$this->getServerTypes(),
(isset($vars->meta['server_type']) ? $vars->meta['server_type'] : null),
['id' => 'vultr_server_type']
)
);
$fields->setField($server_type);
// Set the Vultr bare metal plans as a selectable option
$baremetal_plan = $fields->label(
Language::_('Vultr.package_fields.baremetal_plan', true),
'vultr_baremetal_plan'
);
$baremetal_plan->attach(
$fields->fieldSelect(
'meta[baremetal_plan]',
$baremetal_plans,
(isset($vars->meta['baremetal_plan']) ? $vars->meta['baremetal_plan'] : null),
['id' => 'vultr_baremetal_plan']
)
);
$fields->setField($baremetal_plan);
// Set the Vultr server plans as a selectable option
$server_plan = $fields->label(
Language::_('Vultr.package_fields.server_plan', true),
'vultr_server_plan'
);
$server_plan->attach(
$fields->fieldSelect(
'meta[server_plan]',
$server_plans,
(isset($vars->meta['server_plan']) ? $vars->meta['server_plan'] : null),
['id' => 'vultr_server_plan']
)
);
$fields->setField($server_plan);
// Set the template options
$template_options = $fields->label(Language::_('Vultr.package_fields.set_template', true));
$admin_set_template_label = $fields->label(Language::_('Vultr.package_fields.admin_set_template', true));
$template_options->attach(
$fields->fieldRadio(
'meta[set_template]',
'admin',
(isset($vars->meta['set_template']) ? $vars->meta['set_template'] : 'admin') == 'admin',
['id' => 'vultr_admin_set_template'],
$admin_set_template_label
)
);
$client_set_template_label = $fields->label(Language::_('Vultr.package_fields.client_set_template', true));
$template_options->attach(
$fields->fieldRadio(
'meta[set_template]',
'client',
(isset($vars->meta['set_template']) ? $vars->meta['set_template'] : null) == 'client',
['id' => 'vultr_client_set_template'],
$client_set_template_label
)
);
$fields->setField($template_options);
// Set the server templates as a selectable option
$template = $fields->label(
Language::_('Vultr.package_fields.template', true),
'vultr_template'
);
$template->attach(
$fields->fieldSelect(
'meta[template]',
$server_templates,
(isset($vars->meta['template']) ? $vars->meta['template'] : null),
['id' => 'vultr_template']
)
);
$fields->setField($template);
// Set the surcharge templates permissions
$surcharge_templates_options = $fields->label(Language::_('Vultr.package_fields.surcharge_templates', true));
$allow_surcharge_templates_label = $fields->label(
Language::_('Vultr.package_fields.allow_surcharge_templates', true)
);
$surcharge_templates_options->attach(
$fields->fieldRadio(
'meta[surcharge_templates]',
'allow',
(isset($vars->meta['surcharge_templates']) ? $vars->meta['surcharge_templates'] : 'allow') == 'allow',
['id' => 'vultr_allow_surcharge_templates'],
$allow_surcharge_templates_label
)
);
$disallow_surcharge_templates_label = $fields->label(
Language::_('Vultr.package_fields.disallow_surcharge_templates', true)
);
$surcharge_templates_options->attach(
$fields->fieldRadio(
'meta[surcharge_templates]',
'disallow',
(isset($vars->meta['surcharge_templates']) ? $vars->meta['surcharge_templates'] : null) == 'disallow',
['id' => 'vultr_disallow_surcharge_templates'],
$disallow_surcharge_templates_label
)
);
$fields->setField($surcharge_templates_options);
return $fields;
}
/**
* Gets a list of available server types.
*
* @return array A key/value array of available server types and their language
*/
private function getServerTypes()
{
return [
'baremetal' => Language::_('Vultr.package_fields.server_type.baremetal', true),
'server' => Language::_('Vultr.package_fields.server_type.server', true)
];
}
/**
* Fetches a listing of all bare metal plans.
*
* @param stdClass $module_row A stdClass object representing a single server
* @return array An array of plans in key/value pair
*/
private function getBaremetalPlans($module_row)
{
$api = $this->getApi($module_row->meta->api_key);
$api->loadCommand('vultr_plans');
$plans_api = new VultrPlans($api);
$result = (array) $this->parseResponse($plans_api->listBaremetalPlans());
$plans = [];
foreach ($result as $plan) {
$plans[$plan->METALPLANID] = $plan->name;
}
return $plans;
}
/**
* Fetches a listing of all server plans.
*
* @param stdClass $module_row A stdClass object representing a single server
* @return array An array of plans in key/value pair
*/
private function getServerPlans($module_row)
{
$api = $this->getApi($module_row->meta->api_key);
$api->loadCommand('vultr_plans');
$plans_api = new VultrPlans($api);
$result = (array) $plans_api->listPlans()->response();
$plans = [];
foreach ($result as $plan) {
$plans[$plan->VPSPLANID] = $plan->name;
}
return $plans;
}
/**
* Fetches a listing of all server templates.
*
* @param stdClass $module_row A stdClass object representing a single server
* @param stdClass $package A stdClass object representing the selected package
* @param stdClass $service A stdClass object representing the service
* @return array An array of plans in key/value pair
*/
private function getTemplates($module_row, $package = null, $service = null)
{
$api = $this->getApi($module_row->meta->api_key);
$api->loadCommand('vultr_os');
$api->loadCommand('vultr_app');
$os_api = new VultrOs($api);
$app_api = new VultrApp($api);
$result_os = (array) $os_api->listOs()->response();
$result_app = (array) $app_api->listApps()->response();
// Get available apps for an specific service
if (!is_null($service)) {
$api->loadCommand('vultr_server');
$api->loadCommand('vultr_baremetal');
$server_api = new VultrServer($api);
$baremetal_api = new VultrBaremetal($api);
// Get the service fields
$service_fields = $this->serviceFieldsToObject($service->fields);
$params = [
'SUBID' => $service_fields->vultr_subid
];
if ($package->meta->server_type == 'server') {
$result_app_delta = (array) $server_api->appChangeList($params)->response();
} else {
$result_app_delta = (array) $baremetal_api->appChangeList($params)->response();
}
if (!empty($result_app_delta)) {
$result_app = $result_app_delta;
}
}
$excluded_os = [159, 164, 180, 186];
$templates = [];
foreach ($result_os as $os) {
if (!in_array($os->OSID, $excluded_os)) {
if (!$os->windows
|| is_null($package)
|| ($os->windows && (isset($package->meta->surcharge_templates) ? $package->meta->surcharge_templates : null) == 'allow')
) {
$templates['os-' . $os->OSID] = $os->name . ($os->windows && is_null($package) ? ' (+$16)' : null);
}
}
}
foreach ($result_app as $app) {
if (!$app->surcharge
|| is_null($package)
|| ($app->surcharge && (isset($package->meta->surcharge_templates) ? $package->meta->surcharge_templates : null) == 'allow')
) {
$templates['app-' . $app->APPID] = $app->deploy_name
. ($app->surcharge && is_null($package) ? ' (+$' . (int) $app->surcharge . ')' : null);
}
}
return $templates;
}
/**
* Fetches a listing of all the available locations.
*
* @param stdClass $module_row A stdClass object representing a single server
* @param stdClass $package A stdClass object representing the selected package
* @return array An array of plans in key/value pair
*/
private function getLocations($module_row, $package = null)
{
$api = $this->getApi($module_row->meta->api_key);
$api->loadCommand('vultr_regions');
$regions_api = new VultrRegions($api);
$result = (array) $regions_api->listRegions()->response();
$locations = [];
foreach ($result as $location) {
$locations[$location->DCID] = $location->name . ', ' . $location->country . '. ' . $location->continent;
}
// Check availability in the locations
if (!is_null($package)) {
foreach ($locations as $dcid => $location) {
if ($package->meta->server_type == 'server') {
$availability = (array) $regions_api->availabilityVc2(['DCID' => $dcid])->response();
} else {
$availability = (array) $regions_api->availabilityBaremetal(['DCID' => $dcid])->response();
}
if (empty($availability)) {
unset($locations[$dcid]);
}
// The Vultr API only allows a maximum of 2 request per second, we need wait
// one 0.25 seconds before the next request.
usleep(250000);
}
}
return $locations;
}
/**
* Parse the application information by replacing and removing Vultr
* apparitions, to return it as a white label information.
*
* @param mixed $api The vultr api instance
* @param stdClass $service A stdClass object representing the current service
* @return stdClass An object containing the application details
*/
private function getApplicationDetails($api, $service)
{
// Get the service fields
$service_fields = $this->serviceFieldsToObject($service->fields);
$params = [
'SUBID' => $service_fields->vultr_subid
];
$this->log('api.vultr.com|get_app_info', serialize($params), 'input', true);
$application_details = $this->parseResponse($api->getAppInfo($params));
if (isset($application_details->app_info)) {
Loader::loadModels($this, ['Companies']);
$company_id = Configure::get('Blesta.company_id');
$company = $this->Companies->get($company_id);
if ($company) {
$string = $application_details->app_info;
$string = str_replace('Vultr', $company->name, $string);
$string = str_replace('www.vultr.com', $company->hostname, $string);
$string = str_replace('vultr.com', $company->hostname, $string);
$footer_pos = strpos($string, 'Read more');
$string = substr($string, 0, $footer_pos);
if (!empty($string)) {
$application_details->app_info = trim($string);
}
}
}
return $application_details;
}
/**
* Validates input data when attempting to add a package, returns the meta
* data to save when adding a package. Performs any action required to add
* the package on the remote server. Sets Input errors on failure,
* preventing the package from being added.
*
* @param array An array of key/value pairs used to add the package
* @return array A numerically indexed array of meta fields to be stored for this package containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function addPackage(array $vars = null)
{
// Set rules to validate input data
$this->Input->setRules($this->getPackageRules($vars));
// Build meta data to return
$meta = [];
if ($this->Input->validates($vars)) {
// Return all package meta fields
foreach ($vars['meta'] as $key => $value) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => 0
];
}
}
return $meta;
}
/**
* Validates input data when attempting to edit a package, returns the meta
* data to save when editing a package. Performs any action required to edit
* the package on the remote server. Sets Input errors on failure,
* preventing the package from being edited.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array An array of key/value pairs used to edit the package
* @return array A numerically indexed array of meta fields to be stored for this package containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function editPackage($package, array $vars = null)
{
// The same as adding
return $this->addPackage($vars);
}
/**
* Returns the rendered view of the manage module page.
*
* @param mixed $module A stdClass object representing the module and its rows
* @param array $vars An array of post data submitted to or on the manager module
* page (used to repopulate fields after an error)
* @return string HTML content containing information to display when viewing the manager module page
*/
public function manageModule($module, array &$vars)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('manage', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'vultr' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
$this->view->set('module', $module);
return $this->view->fetch();
}
/**
* Returns the rendered view of the add module row page.
*
* @param array $vars An array of post data submitted to or on the add module
* row page (used to repopulate fields after an error)
* @return string HTML content containing information to display when viewing the add module row page
*/
public function manageAddRow(array &$vars)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('add_row', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'vultr' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
// Set unspecified checkboxes
if (!empty($vars)) {
if (empty($vars['use_ssl'])) {
$vars['use_ssl'] = 'false';
}
}
$this->view->set('vars', (object) $vars);
return $this->view->fetch();
}
/**
* Returns the rendered view of the edit module row page.
*
* @param stdClass $module_row The stdClass representation of the existing module row
* @param array $vars An array of post data submitted to or on the edit
* module row page (used to repopulate fields after an error)
* @return string HTML content containing information to display when viewing the edit module row page
*/
public function manageEditRow($module_row, array &$vars)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('edit_row', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'vultr' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
if (empty($vars)) {
$vars = $module_row->meta;
} else {
// Set unspecified checkboxes
if (empty($vars['use_ssl'])) {
$vars['use_ssl'] = 'false';
}
}
$this->view->set('vars', (object) $vars);
return $this->view->fetch();
}
/**
* Adds the module row on the remote server. Sets Input errors on failure,
* preventing the row from being added. Returns a set of data, which may be
* a subset of $vars, that is stored for this module row.
*
* @param array $vars An array of module info to add
* @return array A numerically indexed array of meta fields for the module row containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
*/
public function addModuleRow(array &$vars)
{
$meta_fields = ['account_name', 'api_key'];
$encrypted_fields = ['api_key'];
$this->Input->setRules($this->getRowRules($vars));
// Validate module row
if ($this->Input->validates($vars)) {
// Build the meta data for this row
$meta = [];
foreach ($vars as $key => $value) {
if (in_array($key, $meta_fields)) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => in_array($key, $encrypted_fields) ? 1 : 0
];
}
}
return $meta;
}
}
/**
* Edits the module row on the remote server. Sets Input errors on failure,
* preventing the row from being updated. Returns a set of data, which may be
* a subset of $vars, that is stored for this module row.
*
* @param stdClass $module_row The stdClass representation of the existing module row
* @param array $vars An array of module info to update
* @return array A numerically indexed array of meta fields for the module row containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
*/
public function editModuleRow($module_row, array &$vars)
{
$meta_fields = ['account_name', 'api_key'];
$encrypted_fields = ['api_key'];
// Set unspecified checkboxes
if (empty($vars['use_ssl'])) {
$vars['use_ssl'] = 'false';
}
$this->Input->setRules($this->getRowRules($vars));
// Validate module row
if ($this->Input->validates($vars)) {
// Build the meta data for this row
$meta = [];
foreach ($vars as $key => $value) {
if (in_array($key, $meta_fields)) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => in_array($key, $encrypted_fields) ? 1 : 0
];
}
}
return $meta;
}
}
/**
* Returns all fields to display to an admin attempting to add a service with the module.
*
* @param stdClass $package A stdClass object representing the selected package
* @param $vars stdClass A stdClass object representing a set of post fields
* @return ModuleFields A ModuleFields object, containg the fields to render
* as well as any additional HTML markup to include
*/
public function getAdminAddFields($package, $vars = null)
{
// Load the required helpers
Loader::loadHelpers($this, ['Html']);
// Fetch the module row available for this package
$module_row = $this->getModuleRow((isset($package->module_row) ? $package->module_row : 0));
// Get the available templates
$templates = $this->getTemplates($module_row, $package);
// Get the available locations
$locations = $this->getLocations($module_row, $package);
$fields = new ModuleFields();
// Create subid label
$subid = $fields->label(Language::_('Vultr.service_field.subid', true), 'vultr_subid');
// Create subid field and attach to subid label
$subid->attach(
$fields->fieldText(
'vultr_subid',
(isset($vars->vultr_subid) ? $vars->vultr_subid : ($vars->subid ?? null)),
['id' => 'vultr_subid']
)
);
// Add tooltip
$tooltip = $fields->tooltip(Language::_('Vultr.service_field.tooltip.subid', true));
$subid->attach($tooltip);
// Set the label as a field
$fields->setField($subid);
// Create hostname label
$hostname = $fields->label(Language::_('Vultr.service_field.hostname', true), 'vultr_hostname');
// Create hostname field and attach to hostname label
$hostname->attach(
$fields->fieldText(
'vultr_hostname',
(isset($vars->vultr_hostname) ? $vars->vultr_hostname : ($vars->hostname ?? null)),
['id' => 'vultr_hostname']
)
);
// Set the label as a field
$fields->setField($hostname);
// Set the server location as a selectable option
$location = $fields->label(Language::_('Vultr.service_field.location', true), 'vultr_location');
$location->attach(
$fields->fieldSelect(
'vultr_location',
$locations,
(isset($vars->vultr_location) ? $vars->vultr_location : ($vars->location ?? null)),
['id' => 'vultr_location']
)
);
$fields->setField($location);
// Set the server templates as a selectable option
if ($package->meta->set_template == 'client') {
$template = $fields->label(Language::_('Vultr.service_field.template', true), 'vultr_template');
$template->attach(
$fields->fieldSelect(
'vultr_template',
$templates,
(isset($vars->vultr_template) ? $vars->vultr_template : ($vars->template ?? null)),
['id' => 'vultr_template']
)
);
$fields->setField($template);
}
// Set the IPv6 options
$ipv6_options = $fields->label(Language::_('Vultr.service_field.ipv6', true));
$enable_ipv6_label = $fields->label(Language::_('Vultr.service_field.enable_ipv6', true));
$ipv6_options->attach(
$fields->fieldRadio(
'vultr_enable_ipv6',
'enable',
(isset($vars->vultr_enable_ipv6)
? $vars->vultr_enable_ipv6
: (isset($vars->ipv6) ? $vars->ipv6 : 'enable')
) == 'enable',
['id' => 'vultr_enable_ipv6'],
$enable_ipv6_label
)
);
$disable_ipv6_label = $fields->label(Language::_('Vultr.service_field.disable_ipv6', true));
$ipv6_options->attach(
$fields->fieldRadio(
'vultr_enable_ipv6',
'disable',
(isset($vars->vultr_enable_ipv6) ? $vars->vultr_enable_ipv6 : ($vars->ipv6 ?? null)) == 'disable',
['id' => 'vultr_disable_ipv6'],
$disable_ipv6_label
)
);
$fields->setField($ipv6_options);
return $fields;
}
/**
* Returns all fields to display to a client attempting to add a service with the module.
*
* @param stdClass $package A stdClass object representing the selected package
* @param $vars stdClass A stdClass object representing a set of post fields
* @return ModuleFields A ModuleFields object, containg the fields to render as well
* as any additional HTML markup to include
*/
public function getClientAddFields($package, $vars = null)
{
Loader::loadHelpers($this, ['Html']);
// Fetch the module row from the package
$module_row = $this->getModuleRow($package->module_row);
// Get the available templates
$templates = $this->getTemplates($module_row, $package);
// Get the available locations
$locations = $this->getLocations($module_row, $package);
$fields = new ModuleFields();
// Create hostname label
$hostname = $fields->label(Language::_('Vultr.service_field.hostname', true), 'vultr_hostname');
// Create hostname field and attach to hostname label
$hostname->attach(
$fields->fieldText(
'vultr_hostname',
(isset($vars->vultr_hostname) ? $vars->vultr_hostname : ($vars->hostname ?? null)),
['id' => 'vultr_hostname']
)
);
// Set the label as a field
$fields->setField($hostname);
// Set the server location as a selectable option
$location = $fields->label(Language::_('Vultr.service_field.location', true), 'vultr_location');
$location->attach(
$fields->fieldSelect(
'vultr_location',
$locations,
(isset($vars->vultr_location) ? $vars->vultr_location : ($vars->location ?? null)),
['id' => 'vultr_location']
)
);
$fields->setField($location);
// Set the server templates as a selectable option
if ($package->meta->set_template == 'client') {
$template = $fields->label(Language::_('Vultr.service_field.template', true), 'vultr_template');
$template->attach(
$fields->fieldSelect(
'vultr_template',
$templates,
(isset($vars->vultr_template) ? $vars->vultr_template : ($vars->template ?? null)),
['id' => 'vultr_template']
)
);
$fields->setField($template);
}
// Set the IPv6 options
$ipv6_options = $fields->label(Language::_('Vultr.service_field.ipv6', true));
$enable_ipv6_label = $fields->label(Language::_('Vultr.service_field.enable_ipv6', true));
$ipv6_options->attach(
$fields->fieldRadio(
'vultr_enable_ipv6',
'enable',
(isset($vars->vultr_enable_ipv6)
? $vars->vultr_enable_ipv6
: (isset($vars->ipv6) ? $vars->ipv6 : 'enable')
) == 'enable',
['id' => 'vultr_enable_ipv6'],
$enable_ipv6_label
)
);
$disable_ipv6_label = $fields->label(Language::_('Vultr.service_field.disable_ipv6', true));
$ipv6_options->attach(
$fields->fieldRadio(
'vultr_enable_ipv6',
'disable',
(isset($vars->vultr_enable_ipv6) ? $vars->vultr_enable_ipv6 : ($vars->ipv6 ?? null)) == 'disable',
['id' => 'vultr_disable_ipv6'],
$disable_ipv6_label
)
);
$fields->setField($ipv6_options);
return $fields;
}
/**
* Returns all fields to display to an admin attempting to edit a service with the module.
*
* @param stdClass $package A stdClass object representing the selected package
* @param $vars stdClass A stdClass object representing a set of post fields
* @return ModuleFields A ModuleFields object, containg the fields to render as
* well as any additional HTML markup to include
*/
public function getAdminEditFields($package, $vars = null)
{
// Load the required helpers
Loader::loadHelpers($this, ['Html']);
// Fetch the module row available for this package
$module_row = $this->getModuleRow((isset($package->module_row) ? $package->module_row : 0));
// Get the available templates
$templates = $this->getTemplates($module_row, $package);
$fields = new ModuleFields();
// Create subid label
$subid = $fields->label(Language::_('Vultr.service_field.subid', true), 'vultr_subid');
// Create subid field and attach to subid label
$subid->attach(
$fields->fieldText(
'vultr_subid',
(isset($vars->vultr_subid) ? $vars->vultr_subid : ($vars->subid ?? null)),
['id' => 'vultr_subid']
)
);
// Add tooltip
$tooltip = $fields->tooltip(Language::_('Vultr.service_field.tooltip.subid', true));
$subid->attach($tooltip);
// Set the label as a field
$fields->setField($subid);
// Set the server templates as a selectable option
if ($package->meta->set_template == 'client') {
$template = $fields->label(Language::_('Vultr.service_field.template', true), 'vultr_template');
$template->attach(
$fields->fieldSelect(
'vultr_template',
$templates,
(isset($vars->vultr_template) ? $vars->vultr_template : ($vars->template ?? null)),
['id' => 'vultr_template']
)
);
$fields->setField($template);
}
return $fields;
}
/**
* Attempts to validate service info. This is the top-level error checking method. Sets Input errors on failure.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array $vars An array of user supplied info to satisfy the request
* @return bool True if the service validates, false otherwise. Sets Input errors when false.
*/
public function validateService($package, array $vars = null)
{
$rules = $this->getServiceRules($vars, $package, false);
// Template must be given only if it can be set by the client
if (isset($package->meta->set_template) && $package->meta->set_template == 'admin') {
unset($rules['vultr_template']);
}