Files
portal-api/app/Services/VirtualMachine/VmModuleService.php
T

49 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\VirtualMachine;
use App\Models\ModuleVmware;
use App\Models\OrderItem;
use App\Services\VirtualMachine\Dto\VmInfrastructureContext;
use App\Services\VmProvisionService;
class VmModuleService
{
/**
* Create ModuleVmware shell. IP and final password are filled in later
* by AssignIpToGuestJob → ChangePasswordViaVsphereJob.
*/
public function create(OrderItem $item, VmInfrastructureContext $infra, string $vmId): ModuleVmware
{
$customer = $item->order->customer;
$serverName = $item->server_name ?? ('VM-' . $vmId);
$months = (int) ($item->months ?? 0);
$firstPayment = (int) $item->price * ($months ?: 1);
$nextDueDate = $months > 0 ? now()->addMonths($months)->timestamp : null;
return ModuleVmware::create([
'name' => $serverName,
'module_ip_address' => null,
'ip' => null,
'port' => $infra->vmPort,
'order_id' => $item->order->id,
'first_payment' => $firstPayment,
'next_payment' => $firstPayment,
'next_due_date' => $nextDueDate,
'next_invoice' => $nextDueDate,
'user' => $infra->vmUser,
'password' => VmProvisionService::templatePassword(),
'plain_password' => VmProvisionService::templatePassword(),
'hostname' => $serverName,
'vmware_id' => $vmId,
'vcenter_id' => $infra->vcenter->id,
'os_id' => $item->os_id ?? 1,
'customer_id' => $customer->id,
'status' => 1,
]);
}
}