Files
2026-09-28 08:26:29 +07:00

283 lines
10 KiB
PHP

<?php
namespace App\Http\Services;
use App\Http\Services\vCenter\Client;
use App\Http\Services\vCenter\PowerManagementService;
use App\Http\Services\vCenter\VirtualMachineService;
use App\Http\Services\vCenter\SnapshotService;
use App\Http\Services\vCenter\ResourceService;
use App\Http\Services\vCenter\TemplateService;
use App\Http\Services\vCenter\VMConfigurationService;
use App\Http\Services\vCenter\PowerShellService;
use App\Http\Services\vCenter\Concerns\ManagesSsh;
class vCenter
{
use ManagesSsh;
public ?string $token = null;
public string $apiUri;
private string $username;
private string $password;
private int $diskCapacity = 1073741824;
private int $memory = 1024;
private ?string $os = null;
private Client $client;
private PowerManagementService $power;
private VirtualMachineService $vm;
private SnapshotService $snapshot;
private ResourceService $resource;
private TemplateService $template;
private VMConfigurationService $vmConfig;
private PowerShellService $shell;
public function __construct()
{
$this->client = new Client();
$this->shell = new PowerShellService($this->client);
$this->power = new PowerManagementService($this->client);
$this->vm = new VirtualMachineService($this->client, $this->shell);
$this->snapshot = new SnapshotService($this->client, $this->shell);
$this->resource = new ResourceService($this->client);
$this->template = new TemplateService($this->client);
$this->vmConfig = new VMConfigurationService($this->client, $this->shell);
}
// ─── Auth & Setup ────────────────────────────────────────────────────────
public function loginServer(\App\Models\Vcenter $vcenterModel): bool|string
{
$result = $this->client->loginServer($vcenterModel);
$this->token = $this->client->token;
$this->apiUri = $this->client->apiUri;
$this->username = $this->client->getUsername();
$this->password = $this->client->getPassword();
return $result;
}
public function checkApiExits(): bool
{
return $this->client->checkApiExits();
}
public function setConnection(string $apiUri, string $token): static
{
$this->apiUri = $apiUri;
$this->token = $token;
$this->client->apiUri = $apiUri;
$this->client->token = $token;
return $this;
}
protected function SetConfig($data = []): static
{
$this->client->SetConfig($data);
$this->username = $this->client->getUsername();
$this->password = $this->client->getPassword();
return $this;
}
public function resolveVCenterFromProduct(\App\Models\ModuleVmware $product): ?\App\Models\vCenter
{
return $this->client->resolveVCenterFromProduct($product);
}
// ─── Resources ───────────────────────────────────────────────────────────
public function getAllDatacenter()
{
return $this->resource->getAllDatacenter();
}
public function getAllHost($datacenter = '')
{
return $this->resource->getAllHost($datacenter);
}
public function getAllFolder($datacenter = '')
{
return $this->resource->getAllFolder($datacenter);
}
public function getAllResourcePool($host = '', $datacenter = '')
{
return $this->resource->getAllResourcePool($host, $datacenter);
}
public function getAllDataStore($host = '')
{
return $this->resource->getAllDataStore($host);
}
public function loadDatastore($data = [], $host_id = '')
{
return $this->resource->loadDatastore($host_id);
}
public function loadNetWork($data = [], $host_id = '', $datacenter_id = '')
{
return $this->resource->loadNetWork($host_id, $datacenter_id);
}
public function getAllNetwork($host = '', $datacenter = '')
{
return $this->resource->getNetwork($host, $datacenter);
}
public function getNetwork($host = '', $datacenter = ''): array|bool|string
{
return $this->resource->getNetwork($host, $datacenter);
}
// ─── Templates ───────────────────────────────────────────────────────────
public function getAllTemplate()
{
return $this->template->getAllTemplate();
}
// ─── VM Lifecycle ─────────────────────────────────────────────────────────
public function getAllVirtualMachine($datacenter = '', $host = '')
{
return $this->vm->getAllVirtualMachine($datacenter, $host);
}
public function findVirtualMachineByName(string $vmName): ?array
{
return $this->vm->findVirtualMachineByName($vmName);
}
public function CreateVirtualMachine($product_data): array|string
{
return $this->vm->CreateVirtualMachine($product_data);
}
public function CheckVirtualMachine($vmware_id = ''): string|array
{
return $this->vm->CheckVirtualMachine($vmware_id);
}
public function GetDetailVirtualMachine($vmware_id = ''): array
{
return $this->vm->GetDetailVirtualMachine($vmware_id);
}
public function GetIPAddressVirtualMachine($vmware_id = ''): array
{
return $this->vm->GetIPAddressVirtualMachine($vmware_id);
}
public function GetVMStats($vmware_id = ''): array
{
return $this->vm->GetVMStats($vmware_id);
}
public function UpgradeMemoryVirtualMachine($vmware_id = '', $ram = 0): array|string
{
return $this->vm->UpgradeMemoryVirtualMachine($vmware_id, $ram);
}
public function UpgradeCPUVirtualMachine($vmware_id = '', $cpu = 0): array|string
{
return $this->vm->UpgradeCPUVirtualMachine($vmware_id, $cpu);
}
public function UpgradeDiskVirtualMachine($product, $disk = 0): array|string
{
return $this->vm->UpgradeDiskVirtualMachine($product, $disk);
}
// ─── Power Management ────────────────────────────────────────────────────
public function StartVirtualMachine($product, $vmware_id = ''): array|string
{
return $this->power->StartVirtualMachine($product, $vmware_id);
}
public function ShutdownGuestVirtualMachine($product, $vmware_id = ''): array|string
{
return $this->power->ShutdownGuestVirtualMachine($product, $vmware_id);
}
public function PowerOffVirtualMachine($vmware_id = ''): array|string
{
return $this->power->PowerOffVirtualMachine($vmware_id);
}
public function RebootVirtualMachine($product, $vmware_id = ''): array|string
{
return $this->power->RebootVirtualMachine($product, $vmware_id);
}
public function restartVM($product, $vmware_id = ''): array|string
{
return $this->power->restartVM($product, $vmware_id);
}
public function SuspendVirtualMachine($vmware_id = ''): array|string
{
return $this->power->SuspendVirtualMachine($vmware_id);
}
public function getVmPowerState($product, string $vmware_id = ''): array|string
{
return $this->power->getVmPowerState($product, $vmware_id);
}
public function DeleteVirtualMachine($vmware_id = ''): array|string
{
return $this->power->DeleteVirtualMachine($vmware_id);
}
public function GetConsoleTickets($vmware_id = ''): array|string
{
return $this->power->GetConsoleTickets($vmware_id);
}
// ─── Snapshots ───────────────────────────────────────────────────────────
public function CreateSnapShot($product, ?string $snapshotName = null): string|array
{
return $this->snapshot->CreateSnapShot($product, $snapshotName);
}
public function getSnapShot($product): string|array
{
return $this->snapshot->getSnapShot($product);
}
public function RemoveSnapShot($product, $snap = ''): string|array
{
return $this->snapshot->RemoveSnapShot($product, $snap);
}
public function RevertSnapShot($product, $snap = ''): string|array
{
return $this->snapshot->RevertSnapShot($product, $snap);
}
public function ConfigWindowsVersion10($vmware_id = '', $product = [], $password = ''): void
{
$this->vmConfig->ConfigWindowsVersion10($vmware_id, $product, $password);
}
public function ConfigWindowsServer($vmware_id = '', $product = [], $password = ''): void
{
$this->vmConfig->ConfigWindowsServer($vmware_id, $product, $password);
}
public function ConfigFixWindowsClick($vmware_id = '', $product = []): void
{
$this->vmConfig->ConfigFixWindowsClick($vmware_id, $product);
}
public function ConfigRockyLinuxVersion9($vmware_id = '', $product = [], $password = ''): void
{
$this->vmConfig->ConfigRockyLinuxVersion9($vmware_id, $product, $password);
}
public function changeRDPPortWindows(
string $vmName,
string $guestUser,
string $guestPassword,
string $newPort,
): array|string {
return $this->shell->changeRDPPortWindows($vmName, $guestUser, $guestPassword, $newPort);
}
public function changeIpVM(
string $vmName,
string $vmwareId,
string $ipAddress,
string $gateway,
string $osType,
string $guestUser = '',
string $guestPassword = '',
string $dns = '8.8.8.8',
string $hostname = '',
): string {
return $this->shell->changeIpVM(
$vmName,
$vmwareId,
$ipAddress,
$gateway,
$osType,
$guestUser,
$guestPassword,
$dns,
$hostname,
);
}
}