Files
portal-api/app/Console/Commands/TestAddonUpdate.php
T
2025-07-24 16:18:27 +07:00

91 lines
2.7 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Http\Services\vCenter;
use App\Models\Vcenter as VcenterModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class TestAddonUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:addon-update
{vm_id : VM ID to test}
{--cpu=1 : CPU addon amount}
{--ram=1 : RAM addon amount in GB}
{--disk=0 : Disk addon amount in GB}
{--vcenter=1 : vCenter ID}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test addon update functionality for a specific VM';
/**
* Execute the console command.
*/
public function handle()
{
$vmId = $this->argument('vm_id');
$cpuAddon = (int) $this->option('cpu');
$ramAddon = (int) $this->option('ram');
$diskAddon = (int) $this->option('disk');
$vcenterId = (int) $this->option('vcenter');
$this->info("🧪 Testing addon update for VM: {$vmId}");
$this->info("Addon config: CPU={$cpuAddon}, RAM={$ramAddon}GB, Disk={$diskAddon}GB");
try {
// Lấy vCenter model
$vcenterModel = VcenterModel::find($vcenterId);
if (!$vcenterModel) {
$this->error("❌ Không tìm thấy vCenter với ID: {$vcenterId}");
return 1;
}
$this->info("📡 Connecting to vCenter: {$vcenterModel->host}");
// Khởi tạo vCenter service
$vCenter = new vCenter();
// Login to vCenter
if (!$vCenter->loginServer($vcenterModel)) {
$this->error("❌ Không thể login vào vCenter");
return 1;
}
$this->info("✅ Login thành công");
// Test addon update
$addon = [
'cpu' => $cpuAddon,
'ram' => $ramAddon,
'disk' => $diskAddon
];
$result = $vCenter->testAddonUpdate($vmId, $addon, 1, 1);
if ($result['error']) {
$this->error("❌ Test thất bại: " . $result['message']);
return 1;
}
$this->info("✅ Test thành công: " . $result['message']);
$this->info("📋 Kiểm tra logs để xem chi tiết");
return 0;
} catch (\Exception $e) {
$this->error("❌ Exception: " . $e->getMessage());
Log::error("TestAddonUpdate command error: " . $e->getMessage());
return 1;
}
}
}