Skip to content

Commit 974a8fa

Browse files
scopadityawbwce
andcommitted
feat: template specific login credentials types
Closes #44 Refs #65 Co-authored-by: adityawbwce <info@whmcsmodule.net>
1 parent f57d1b5 commit 974a8fa

2 files changed

Lines changed: 108 additions & 18 deletions

File tree

modules/servers/upCloudVps/lib/configOptions.php

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public function configs(){
2424
$product = Product::find(App::getFromRequest('id'));
2525
if (App::getFromRequest('action') == 'save') {
2626
$this->ensureCustomFields($product);
27-
$this->createGlobalConfigurableOptions($product);
27+
$this->createLinuxCloudInitConfigurableOptions($product);
28+
$this->createLinuxNativeConfigurableOptions($product);
29+
$this->createWindowsNativeConfigurableOptions($product);
30+
$this->createBackupLocationConfigurableOptions($product);
2831
$this->createCustomConfigurableOptions($product);
2932
} // Save End
3033

@@ -39,10 +42,10 @@ private function createCustomConfigurableOptions($product)
3942
{
4043
$currencyId = Capsule::table('tblcurrencies')->where('default', '1')->first()->id;
4144
$currencyCode = Capsule::table('tblcurrencies')->where('default', '1')->first()->code;
42-
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud - Custom Plans')->first()->id;
45+
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud - custom plans')->first()->id;
4346

4447
if(!$groupId){
45-
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud - Custom Plans', 'description' => 'Auto generated by upCloudVps module']);
48+
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud - custom plans', 'description' => 'Auto generated by upCloudVps module']);
4649
$groupIdLinks = Capsule::table('tblproductconfiglinks')->where('gid', $groupId)->where('pid', $product->id)->first()->gid;
4750
if(!$groupIdLinks){
4851
Capsule::table('tblproductconfiglinks')->insert(['gid' => $groupId, 'pid' => $product->id]);
@@ -53,22 +56,67 @@ private function createCustomConfigurableOptions($product)
5356
}
5457
}
5558

56-
private function createGlobalConfigurableOptions($product)
59+
private function createLinuxCloudInitConfigurableOptions($product)
5760
{
58-
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud Global')->first()->id;
61+
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud - Linux (cloud-init template)')->first()->id;
5962
$currencyId = Capsule::table('tblcurrencies')->where('default', '1')->first()->id;
6063
$currencyCode = Capsule::table('tblcurrencies')->where('default', '1')->first()->code;
61-
if(!$groupId){
62-
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud Global', 'description' => 'Auto generated by upCloudVps module']);
64+
if (!$groupId) {
65+
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud - Linux (cloud-init template)', 'description' => 'Auto generated by upCloudVps module']);
6366
$groupIdLinks = Capsule::table('tblproductconfiglinks')->where('gid', $groupId)->where('pid', $product->id)->first()->gid;
64-
if(!$groupIdLinks){
67+
if (!$groupIdLinks) {
6568
Capsule::table('tblproductconfiglinks')->insert(['gid' => $groupId, 'pid' => $product->id]);
6669
}
67-
$pomTemplates = $this->getTemplateIds();
68-
$zones = $this->getZoneLocation();
69-
$this->createLocationFields($groupId, $zones, $currencyId);
70-
$this->createTemplateFields($groupId, $pomTemplates, $currencyId, $currencyCode);
71-
$this->createBackupFields($groupId, $this->getBackups());
70+
$pomTemplates = $this->getTemplateIds("cloudinit");
71+
$this->createTemplateFields($groupId, $pomTemplates, $currencyId, $currencyCode);
72+
}
73+
}
74+
75+
private function createLinuxNativeConfigurableOptions($product)
76+
{
77+
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud - Linux (native template)')->first()->id;
78+
$currencyId = Capsule::table('tblcurrencies')->where('default', '1')->first()->id;
79+
$currencyCode = Capsule::table('tblcurrencies')->where('default', '1')->first()->code;
80+
if (!$groupId) {
81+
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud - Linux (native template)', 'description' => 'Auto generated by upCloudVps module']);
82+
$groupIdLinks = Capsule::table('tblproductconfiglinks')->where('gid', $groupId)->where('pid', $product->id)->first()->gid;
83+
if (!$groupIdLinks) {
84+
Capsule::table('tblproductconfiglinks')->insert(['gid' => $groupId, 'pid' => $product->id]);
85+
}
86+
$pomTemplates = $this->getTemplateIds("nativelinux");
87+
$this->createTemplateFields($groupId, $pomTemplates, $currencyId, $currencyCode);
88+
}
89+
}
90+
91+
private function createWindowsNativeConfigurableOptions($product)
92+
{
93+
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud - Windows (native template)')->first()->id;
94+
$currencyId = Capsule::table('tblcurrencies')->where('default', '1')->first()->id;
95+
$currencyCode = Capsule::table('tblcurrencies')->where('default', '1')->first()->code;
96+
if (!$groupId) {
97+
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud - Windows (native template)', 'description' => 'Auto generated by upCloudVps module']);
98+
$groupIdLinks = Capsule::table('tblproductconfiglinks')->where('gid', $groupId)->where('pid', $product->id)->first()->gid;
99+
if (!$groupIdLinks) {
100+
Capsule::table('tblproductconfiglinks')->insert(['gid' => $groupId, 'pid' => $product->id]);
101+
}
102+
$pomTemplates = $this->getTemplateIds("nativewindows");
103+
$this->createTemplateFields($groupId, $pomTemplates, $currencyId, $currencyCode);
104+
}
105+
}
106+
107+
private function createBackupLocationConfigurableOptions($product)
108+
{
109+
$currencyId = Capsule::table('tblcurrencies')->where('default', '1')->first()->id;
110+
$currencyCode = Capsule::table('tblcurrencies')->where('default', '1')->first()->code;
111+
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud - backup and location')->first()->id;
112+
if (!$groupId) {
113+
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud - backup and location', 'description' => 'Auto generated by upCloudVps module']);
114+
$groupIdLinks = Capsule::table('tblproductconfiglinks')->where('gid', $groupId)->where('pid', $product->id)->first()->gid;
115+
if (!$groupIdLinks) {
116+
Capsule::table('tblproductconfiglinks')->insert(['gid' => $groupId, 'pid' => $product->id]);
117+
}
118+
$this->createBackupFields($groupId, $this->getBackups());
119+
$this->createLocationFields($groupId, $this->getZoneLocation(), $currencyId);
72120
}
73121
}
74122

@@ -213,16 +261,28 @@ private function getZoneLocation()
213261
return $zoneLocation;
214262
}
215263

216-
private function getTemplateIds()
264+
private function getTemplateIds($vmtype = null)
217265
{
218-
$templates = $this->manager->GetTemplate()['response']['storages']['storage'];
219-
$templateIds = [];
266+
$templates = $this->manager->GetTemplate()['response']['storages']['storage'];
267+
$templateIds = [];
220268

269+
if (!$vmtype) {
270+
foreach ($templates as $template) {
271+
$templateIds[$template['uuid']] = $template['title'];
272+
}
273+
} else {
221274
foreach ($templates as $template) {
275+
if (
276+
($template['template_type'] == "native" &&
277+
(($vmtype == 'nativewindows' && preg_match('/Windows/', $template['title'])) ||
278+
($vmtype == 'nativelinux' && !preg_match('/Windows/', $template['title'])))) ||
279+
$template['template_type'] == "cloud-init" && $vmtype == 'cloudinit'
280+
) {
222281
$templateIds[$template['uuid']] = $template['title'];
282+
}
223283
}
224-
225-
return $templateIds;
284+
}
285+
return $templateIds;
226286
}
227287

228288
private function getVmplans()

modules/servers/upCloudVps/lib/upCloudVps.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public function CreateServer($ZoneID, $Hostname, $Plan, $OsUUID, $sshKey, $user_
156156
if ($Template['uuid'] == $OsUUID){
157157
$TemplateTitle = $Template['title'];
158158
$TemplateUUID = $Template['uuid'];
159+
$TemplateType = $Template['template_type'];
159160
break;
160161
}
161162
}
@@ -234,6 +235,35 @@ public function CreateServer($ZoneID, $Hostname, $Plan, $OsUUID, $sshKey, $user_
234235
];
235236
}
236237

238+
if ($TemplateType == 'native') {
239+
if (!preg_match('/Windows/', $TemplateTitle)) {
240+
if ($sshKey != "na") {
241+
$postData['server']['login_user'] = [
242+
'username' => 'root',
243+
'ssh_keys' => [
244+
'ssh_key' => [
245+
$sshKey,
246+
],
247+
],
248+
];
249+
}
250+
}
251+
} elseif ($TemplateType == 'cloud-init') {
252+
if ($sshKey != "na") {
253+
$postData['server']['login_user'] = [
254+
'username' => 'root',
255+
'ssh_keys' => [
256+
'ssh_key' => [
257+
$sshKey,
258+
],
259+
],
260+
];
261+
} else {
262+
$error['response']['error']['error_message'] = 'ssh key required';
263+
return $error;
264+
}
265+
}
266+
237267
if($networking == "ipv4only"){
238268
$postData['server']['networking'] = [
239269
"interfaces" => [

0 commit comments

Comments
 (0)