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

149 lines
5.2 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Invoice;
use App\Http\Services\vCenter;
use Illuminate\Support\Facades\Log;
class TestVMInfo extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:vm-info {invoice_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test getting VM information from vCenter API';
/**
* Execute the console command.
*/
public function handle()
{
$invoiceId = $this->argument('invoice_id');
$this->info("Testing VM info for Invoice #{$invoiceId}");
try {
// Tìm invoice
$invoice = Invoice::with(['invoiceItem.addons', 'invoiceItem.os.osGroup', 'customer'])->find($invoiceId);
if (!$invoice) {
$this->error("Invoice #{$invoiceId} not found!");
return 1;
}
$this->info("Found invoice: #{$invoice->id}");
$this->info("VM ID: " . ($invoice->vm_id ?? 'NULL'));
if (!$invoice->vm_id) {
$this->error("Invoice #{$invoiceId} doesn't have VM ID!");
return 1;
}
// Lấy thông tin vCenter
$orderItem = $invoice->invoiceItem->first();
if (!$orderItem) {
$this->error("No order item found for invoice #{$invoiceId}!");
return 1;
}
$product = $orderItem->getProductAttribute();
if (!$product) {
$this->error("No product found for order item #{$orderItem->id}!");
return 1;
}
$host = $product->host;
$datacenter = $host?->datacenter;
$vcenter = $datacenter?->vcenter;
if (!$vcenter) {
$this->error("No vCenter information found!");
return 1;
}
$this->info("vCenter Host: {$vcenter->host}");
$this->info("vCenter User: {$vcenter->user}");
// Test vCenter service
$vCenterService = new vCenter();
$this->info("Attempting to login to vCenter...");
if (!$vCenterService->loginServer($vcenter)) {
$this->error("Failed to login to vCenter!");
return 1;
}
$this->info("✅ Successfully logged in to vCenter");
// Test getting VM details
$this->info("Getting VM details...");
$vmDetail = $vCenterService->GetDetailVirtualMachine($invoice->vm_id);
if ($vmDetail['error'] === false) {
$this->info("✅ Successfully got VM details");
$this->info("VM Name: " . ($vmDetail['data']['name'] ?? 'Unknown'));
$this->info("VM Version: " . ($vmDetail['data']['version'] ?? 'Unknown'));
} else {
$this->error("❌ Failed to get VM details: " . ($vmDetail['message'] ?? 'Unknown error'));
return 1;
}
// Test getting power status
$this->info("Getting power status...");
$powerStatus = $vCenterService->CheckVirtualMachine($invoice->vm_id);
if ($powerStatus['error'] === false) {
$this->info("✅ Successfully got power status");
$this->info("Power State: " . ($powerStatus['data']['state'] ?? 'Unknown'));
} else {
$this->warn("⚠️ Failed to get power status: " . ($powerStatus['message'] ?? 'Unknown error'));
}
// Test getting IP address
$this->info("Getting IP address...");
$ipInfo = $vCenterService->GetIPAddressVirtualMachine($invoice->vm_id);
if ($ipInfo['error'] === false) {
$this->info("✅ Successfully got IP info");
$this->info("IP Info data structure:");
$this->line(json_encode($ipInfo['data'], JSON_PRETTY_PRINT));
if (!empty($ipInfo['data']['ip_addresses'])) {
$ipAddresses = $ipInfo['data']['ip_addresses'];
$this->info("IP Addresses type: " . gettype($ipAddresses));
$this->info("IP Addresses value: " . json_encode($ipAddresses));
if (is_array($ipAddresses)) {
$this->info("IP Addresses: " . implode(', ', $ipAddresses));
} else {
$this->info("IP Addresses: " . $ipAddresses);
}
} else {
$this->info("No IP addresses found");
}
} else {
$this->warn("⚠️ Failed to get IP info: " . ($ipInfo['message'] ?? 'Unknown error'));
}
// Display full VM data
$this->info("\n=== Full VM Data ===");
$this->line(json_encode($vmDetail['data'], JSON_PRETTY_PRINT));
return 0;
} catch (\Exception $e) {
$this->error("Error: " . $e->getMessage());
Log::error('TestVMInfo error: ' . $e->getMessage());
return 1;
}
}
}