Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Issue #44 #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 94 additions & 39 deletions modules/servers/upCloudVps/lib/configOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public function configs(){
$product = Product::find(App::getFromRequest('id'));
if (App::getFromRequest('action') == 'save') {
$this->ensureCustomFields($product);
$this->createGlobalConfigurableOptions($product);
$this->createWindowsConfigurableOptions($product);
$this->createLinuxConfigurableOptions($product);
$this->createCloudConfigurableOptions($product);
$this->createCustomConfigurableOptions($product);
} // Save End
$this->createBackupLocationConfigurableOptions($product);
}

return [
'Default Location' => ['Type' => 'dropdown', 'Options' => $this->getZoneLocation()],
Expand All @@ -53,22 +56,68 @@ private function createCustomConfigurableOptions($product)
}
}

private function createGlobalConfigurableOptions($product)
private function createBackupLocationConfigurableOptions($product)
{
$currencyId = Capsule::table('tblcurrencies')->where('default', '1')->first()->id;
$currencyCode = Capsule::table('tblcurrencies')->where('default', '1')->first()->code;
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud - Backup and Location')->first()->id;

if(!$groupId){
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud - Backup and Location', 'description' => 'Auto generated by upCloudVps module']);
$groupIdLinks = Capsule::table('tblproductconfiglinks')->where('gid', $groupId)->where('pid', $product->id)->first()->gid;
if(!$groupIdLinks){
Capsule::table('tblproductconfiglinks')->insert(['gid' => $groupId, 'pid' => $product->id]);
}
$this->createBackupFields($groupId, $this->getBackups());
$this->createLocationFields($groupId, $this->getZoneLocation(), $currencyId);
}
}

private function createWindowsConfigurableOptions($product)
{
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud Native Windows')->first()->id;
$currencyId = Capsule::table('tblcurrencies')->where('default', '1')->first()->id;
$currencyCode = Capsule::table('tblcurrencies')->where('default', '1')->first()->code;
if(!$groupId){
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud Native Windows', 'description' => 'Auto generated by upCloudVps module']);
$groupIdLinks = Capsule::table('tblproductconfiglinks')->where('gid', $groupId)->where('pid', $product->id)->first()->gid;
if(!$groupIdLinks){
Capsule::table('tblproductconfiglinks')->insert(['gid' => $groupId, 'pid' => $product->id]);
}
$pomTemplates = $this->getTemplateIds("nativewindows");
$this->createTemplateFields($groupId, $pomTemplates, $currencyId, $currencyCode);
}
}

private function createLinuxConfigurableOptions($product)
{
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud Native Linux')->first()->id;
$currencyId = Capsule::table('tblcurrencies')->where('default', '1')->first()->id;
$currencyCode = Capsule::table('tblcurrencies')->where('default', '1')->first()->code;
if(!$groupId){
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud Native Linux', 'description' => 'Auto generated by upCloudVps module']);
$groupIdLinks = Capsule::table('tblproductconfiglinks')->where('gid', $groupId)->where('pid', $product->id)->first()->gid;
if(!$groupIdLinks){
Capsule::table('tblproductconfiglinks')->insert(['gid' => $groupId, 'pid' => $product->id]);
}
$pomTemplates = $this->getTemplateIds("nativelinux");
$this->createTemplateFields($groupId, $pomTemplates, $currencyId, $currencyCode);
}
}

private function createCloudConfigurableOptions($product)
{
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud Global')->first()->id;
$groupId = Capsule::table('tblproductconfiggroups')->where('name', 'Configurable options for UpCloud Cloud Linux')->first()->id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does "Cloud Linux" here and below mean that it is related to Linux instances set up from cloud-init templates? If I understand correctly, would be good to clarify the intents of these option sets in their names, for example

Configurable options for UpCloud - native Windows template VPS
Configurable options for UpCloud - native Linux template VPS
Configurable options for UpCloud - cloud-init Linux template VPS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names it self says https://prnt.sc/p-h7WOcY5_MF where Cloud Linux meaning cloud-init images

$currencyId = Capsule::table('tblcurrencies')->where('default', '1')->first()->id;
$currencyCode = Capsule::table('tblcurrencies')->where('default', '1')->first()->code;
if(!$groupId){
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud Global', 'description' => 'Auto generated by upCloudVps module']);
$groupId = Capsule::table('tblproductconfiggroups')->insertGetId(['name' => 'Configurable options for UpCloud Cloud Linux', 'description' => 'Auto generated by upCloudVps module']);
$groupIdLinks = Capsule::table('tblproductconfiglinks')->where('gid', $groupId)->where('pid', $product->id)->first()->gid;
if(!$groupIdLinks){
Capsule::table('tblproductconfiglinks')->insert(['gid' => $groupId, 'pid' => $product->id]);
}
$pomTemplates = $this->getTemplateIds();
$zones = $this->getZoneLocation();
$this->createLocationFields($groupId, $zones, $currencyId);
$this->createTemplateFields($groupId, $pomTemplates, $currencyId, $currencyCode);
$this->createBackupFields($groupId, $this->getBackups());
$pomTemplates = $this->getTemplateIds("cloudinit");
$this->createTemplateFields($groupId, $pomTemplates, $currencyId, $currencyCode);
}
}

Expand All @@ -85,19 +134,17 @@ private function createBackupFields($groupId, $backup){
private function createRAMFields($groupId, $currencyId, $currencyCode){
$mems = array();
for ($i = 4; $i <= 64; $i += 1) {
$mems["$i"] = "$i Gigabyte (GB)";
$mems["$i"] = "$i Gigabyte [GB]";
Comment on lines -88 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what is the rationale for this change, and its relation to #44?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was creating some conflicts so added [] brackets

}
$optionId = Capsule::table('tblproductconfigoptions')->where('gid', $groupId)->where('optionname', 'ram|Memory (RAM)')->first()->id;
if(!$optionId){
$optionId = Capsule::table('tblproductconfigoptions')->insertGetId(['gid' => $groupId, 'optionname' => 'ram|Memory (RAM)', 'optiontype' => 1]);
foreach ($mems as $mem => $mvals) {
Capsule::table('tblproductconfigoptionssub')->updateOrInsert(['optionname' => $mem.'|'.$mvals], ['configid' => $optionId]);
}
foreach (Capsule::table('tblproductconfigoptionssub')->where('configid', $optionId)->get() as $id) {
$parts = explode("|", $id->optionname);
$fprice = $parts[0] * "2";
$monthlys = $this->manager->CurrencyConvert('EUR', $currencyCode, $fprice);
Capsule::table('tblpricing')->updateOrInsert(['type' => 'configoptions', 'relid' => $id->id, 'monthly' => $monthlys['convertedAmount'] ],['currency' => $currencyId]);
$relId = Capsule::table('tblproductconfigoptionssub')->where('optionname', $mem.'|'.$mvals)->where('configid', $optionId)->first()->id;
$fprice = $mem * "2";
$monthlys = $this->manager->CurrencyConvert('EUR', $currencyCode, $fprice);
Capsule::table('tblpricing')->updateOrInsert(['type' => 'configoptions', 'relid' => $relId, 'monthly' => $monthlys['convertedAmount'] ],['currency' => $currencyId]);
Comment on lines -95 to +147
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I read this correctly, or how it is related to issue #44. Could we clarify the purpose of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that I made any changes createRAMFields function, please check again.

}
}
}
Expand All @@ -112,12 +159,10 @@ private function createCPUFields($groupId, $currencyId, $currencyCode){
$optionId = Capsule::table('tblproductconfigoptions')->insertGetId(['gid' => $groupId, 'optionname' => 'vcpu|vCPU', 'optiontype' => 1]);
foreach ($cpus as $cpu => $cvals) {
Capsule::table('tblproductconfigoptionssub')->updateOrInsert(['optionname' => $cpu.'|'.$cvals], ['configid' => $optionId]);
}
foreach (Capsule::table('tblproductconfigoptionssub')->where('configid', $optionId)->get() as $id) {
$parts = explode("|", $id->optionname);
$fprice = $parts[0] * "6";
$monthlys = $this->manager->CurrencyConvert('EUR', $currencyCode, $fprice);
Capsule::table('tblpricing')->updateOrInsert(['type' => 'configoptions', 'relid' => $id->id, 'monthly' => $monthlys['convertedAmount'] ],['currency' => $currencyId]);
$relId = Capsule::table('tblproductconfigoptionssub')->where('optionname', $cpu.'|'.$cvals)->where('configid', $optionId)->first()->id;
$fprice = $cpu * "6";
$monthlys = $this->manager->CurrencyConvert('EUR', $currencyCode, $fprice);
Capsule::table('tblpricing')->updateOrInsert(['type' => 'configoptions', 'relid' => $relId, 'monthly' => $monthlys['convertedAmount'] ],['currency' => $currencyId]);
Comment on lines -115 to +165
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above comment for memory, but for CPU here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that I made any changes createCPUFields function, please check again.

}
}
}
Expand All @@ -132,12 +177,10 @@ private function createStorageFields($groupId, $currencyId, $currencyCode){
$optionId = Capsule::table('tblproductconfigoptions')->insertGetId(['gid' => $groupId, 'optionname' => 'storage|Storage', 'optiontype' => 1]);
foreach ($storages as $storage => $vals) {
Capsule::table('tblproductconfigoptionssub')->updateOrInsert(['optionname' => $storage.'|'.$vals], ['configid' => $optionId]);
}
foreach (Capsule::table('tblproductconfigoptionssub')->where('configid', $optionId)->get() as $id) {
$parts = explode("|", $id->optionname);
$fprice = $parts[0] * "0.10";
$monthlys = $this->manager->CurrencyConvert('EUR', $currencyCode, $fprice);
Capsule::table('tblpricing')->updateOrInsert(['type' => 'configoptions', 'relid' => $id->id, 'monthly' => $monthlys['convertedAmount'] ], ['currency' => $currencyId] );
$relId = Capsule::table('tblproductconfigoptionssub')->where('optionname', $storage.'|'.$vals)->where('configid', $optionId)->first()->id;
$fprice = $storage * "0.10";
$monthlys = $this->manager->CurrencyConvert('EUR', $currencyCode, $fprice);
Capsule::table('tblpricing')->updateOrInsert(['type' => 'configoptions', 'relid' => $relId, 'monthly' => $monthlys['convertedAmount'] ], ['currency' => $currencyId] );
Comment on lines -135 to +183
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above comment for memory, but for storage here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above for createStorageFields function

}
}
}
Expand Down Expand Up @@ -200,7 +243,6 @@ private function getBackups()
return $backups;
}


private function getZoneLocation()
{
$zones = $this->manager->GetZones()['response']['zones']['zone'];
Expand All @@ -213,17 +255,30 @@ private function getZoneLocation()
return $zoneLocation;
}

private function getTemplateIds()
{
$templates = $this->manager->GetTemplate()['response']['storages']['storage'];
$templateIds = [];
private function getTemplateIds($vmtype = null)
{
$templates = $this->manager->GetTemplate()['response']['storages']['storage'];
$templateIds = [];

foreach ($templates as $template) {
$templateIds[$template['uuid']] = $template['title'];
}
foreach ($templates as $template) {
if ($template['template_type'] == "native") {
if ($vmtype == 'nativewindows' && preg_match('/Windows/', $template['title'])) {
$templateIds[$template['uuid']] = $template['title'];
} elseif ($vmtype == 'nativelinux' && !preg_match('/Windows/', $template['title'])) {
$templateIds[$template['uuid']] = $template['title'];
}
} elseif ($template['template_type'] == "cloud-init" && $vmtype == 'cloudinit') {
$templateIds[$template['uuid']] = $template['title'];
Comment on lines +265 to +271
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bodies of all these conditions are the same,

$templateIds[$template['uuid']] = $template['title'];

Is this on purpose? If yes, I feel this would be clearer refactored into a single if statement with a series of || separated conditions, instead of a nested if-else chain. As currently written, it gives the feeling that there might be a bug in that all the condition bodies are the same (i.e. maybe they should be somehow different)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How they are same? Have you checked the conditions?

}
}
if (!$vmtype) {
foreach ($templates as $template) {
$templateIds[$template['uuid']] = $template['title'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's yet another same condition body as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do verify the conditions for the function getTemplateIds and let me know your issues

}
}

return $templateIds;
}
return $templateIds;
}

private function getVmplans()
{
Expand Down
40 changes: 31 additions & 9 deletions modules/servers/upCloudVps/lib/upCloudVps.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function CreateServer($ZoneID, $Hostname, $Plan, $OsUUID, $sshKey, $user_
if ($Template['uuid'] == $OsUUID){
$TemplateTitle = $Template['title'];
$TemplateUUID = $Template['uuid'];
$template_type = $Template['template_type']; #44
break;
}
}
Expand Down Expand Up @@ -222,17 +223,38 @@ public function CreateServer($ZoneID, $Hostname, $Plan, $OsUUID, $sshKey, $user_
];

}

if($sshKey != "na"){
$postData['server']['login_user'] = [
'username' => 'root',
'ssh_keys' => [
'ssh_key' => [
$sshKey,
#44
if ($template_type == 'native') {
if(!preg_match('/Windows/', $TemplateTitle)){
if($sshKey != "na"){
$postData['server']['login_user'] = [
'username' => 'root',
'ssh_keys' => [
'ssh_key' => [
$sshKey,
],
],
],
];
];
}
}
}

if ($template_type == 'cloud-init') {
if($sshKey != "na"){
$postData['server']['login_user'] = [
'username' => 'root',
'ssh_keys' => [
'ssh_key' => [
$sshKey,
],
],
];
} else {
$error['response']['error']['error_message'] = 'ssh key required';
return $error;
}
}
#44

if($networking == "ipv4only"){
$postData['server']['networking'] = [
Expand Down