client = $client; } public function SuspendVirtualMachine($vmware_id = ''): array|string { if ($this->client->checkApiExits()) { $response = Http::withoutVerifying() ->withHeaders([ 'vmware-api-session-id' => $this->client->token ]) ->post("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id/power/suspend"); if ($response->status() === 200) { return [ 'error' => false, 'data' => 'success' ]; } return [ 'error' => true, 'data' => $response['value'] ]; } return "Login_Again"; } public function RebootVirtualMachine($product, $vmware_id = ''): array|string { if ($this->client->loginServer($product->ipaddress->host->datacenter->vCenter)) { $response = Http::withoutVerifying() ->withHeaders([ 'vmware-api-session-id' => $this->client->token ]) ->post("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id/guest/power?action=reboot"); if ($response->status() === 200) { return [ 'error' => false, 'data' => 'success' ]; } return [ 'error' => true, 'data' => $response['value'] ]; } return "Login_Again"; } public function StartVirtualMachine($product, $vmware_id = ''): array|string { if ($this->client->loginServer($product->ipaddress->host->datacenter->vCenter)) { $response = Http::withoutVerifying() ->withHeaders([ 'vmware-api-session-id' => $this->client->token ]) ->post("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id/power/start"); if ($response->status() === 200) { return [ 'error' => false, 'data' => 'success' ]; } return [ 'error' => true, 'data' => $response['value'] ]; } return "Login_Again"; } public function ShutdownGuestVirtualMachine($product, $vmware_id = ''): array|string { if ($this->client->loginServer($product->ipaddress->host->datacenter->vCenter)) { $response = Http::withoutVerifying() ->withHeaders([ 'vmware-api-session-id' => $this->client->token ]) ->post("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id/guest/power?action=shutdown"); if ($response->status() === 200) { return [ 'error' => false, 'data' => 'success' ]; } return [ 'error' => true, 'data' => $response['value'] ]; } return "Login_Again"; } public function PowerOffVirtualMachine($vmware_id = ''): array|string { if ($this->client->checkApiExits()) { $response = Http::withoutVerifying() ->withHeaders([ 'vmware-api-session-id' => $this->client->token ]) ->post("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id/power/stop"); if ($response->status() === 200) { return [ 'error' => false, 'data' => 'success' ]; } return [ 'error' => true, 'data' => $response['value'] ]; } return "Login_Again"; } public function DeleteVirtualMachine($vmware_id = ''): array|string { if ($this->client->checkApiExits()) { $response = Http::withoutVerifying() ->withHeaders([ 'vmware-api-session-id' => $this->client->token ]) ->delete("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id"); if ($response->status() === 200) { return [ 'error' => false, 'data' => 'success' ]; } return [ 'error' => true, 'data' => $response['value'] ]; } return "Login_Again"; } public function GetConsoleTickets($vmware_id = ''): array|string { if ($this->client->checkApiExits()) { $response = Http::withoutVerifying() ->withHeaders([ 'vmware-api-session-id' => $this->client->token ]) ->post("https://{$this->client->apiUri}/api/vcenter/vm/$vmware_id/console/tickets", [ "type" => "WEBMKS" ])->json(); if (!isset($response['error_type'])) { // vCenter returns "wss:///ticket/" — the esxi // host is only reachable through HAProxy's ?host= routing // (see config('vmware.console_nodes')), so split it out here. $esxiHost = parse_url((string) $response['ticket'], PHP_URL_HOST); $ticketId = basename(parse_url((string) $response['ticket'], PHP_URL_PATH)); // Array access, not config('vmware.console_nodes.'.$esxiHost) — // IPs contain dots, which collide with Laravel's dot notation. $consoleHost = config('vmware.console_nodes')[$esxiHost] ?? null; if (!$consoleHost) { return [ 'error' => true, 'data' => "Không tìm thấy console node cho ESXi host: $esxiHost", ]; } return [ 'error' => false, 'data' => [ 'ticket' => $ticketId, 'host' => $consoleHost, ], ]; } return [ 'error' => true, 'data' => $response['messages'] ]; } return "Login_Again"; } public function restartVM($product, $vmware_id = ''): array|string { try { $vmId = $vmware_id ?: ($product->vmware_id ?? null); if (!$vmId) { return [ 'error' => true, 'data' => 'Không tìm thấy vmware_id', ]; } $vcenter = $product->vCenter ?? null; if (!$vcenter && isset($product->ipAddress) && $product->ipAddress?->host?->datacenter?->vCenter) { $vcenter = $product->ipAddress->host->datacenter->vCenter; } if (!$vcenter) { return [ 'error' => true, 'data' => 'Không tìm thấy thông tin vCenter cho VM', ]; } $check = $this->client->loginServer($vcenter); if (!$check) { return "Login_Again"; } $response = Http::withoutVerifying() ->withHeaders([ 'vmware-api-session-id' => $this->client->token ]) ->post("https://{$this->client->apiUri}/rest/vcenter/vm/$vmId/guest/power?action=reboot"); if ($response->status() === 200) { return [ 'error' => false, 'data' => 'success' ]; } return [ 'error' => true, 'data' => $response->json('value') ?? $response->body(), ]; } catch (\Throwable $e) { return [ 'error' => true, 'data' => $e->getMessage(), ]; } } public function getVmPowerState($product, string $vmware_id = ''): array|string { try { $vmId = $vmware_id ?: ($product->vmware_id ?? null); if (!$vmId) { return ['error' => true, 'data' => 'Không tìm thấy vmware_id']; } $vcenter = $product->vCenter ?? null; if (!$vcenter && $product->ipAddress?->host?->datacenter?->vCenter) { $vcenter = $product->ipAddress->host->datacenter->vCenter; } if (!$vcenter) { return ['error' => true, 'data' => 'Không tìm thấy thông tin vCenter cho VM']; } if (!$this->client->loginServer($vcenter)) { return 'Login_Again'; } $response = Http::withoutVerifying() ->withHeaders(['vmware-api-session-id' => $this->client->token]) ->timeout(8) ->get("https://{$this->client->apiUri}/rest/vcenter/vm/$vmId/power"); if ($response->successful()) { $data = $response->json(); return ['error' => false, 'data' => $data['value'] ?? $data]; } return ['error' => true, 'data' => $response->json('value') ?? 'Không thể lấy trạng thái VM']; } catch (\Throwable $e) { return ['error' => true, 'data' => $e->getMessage()]; } } }