client = new Client([ 'base_uri' => "https://{$hypervisor->host}:{$hypervisor->port}/api/", 'timeout' => 30, RequestOptions::VERIFY => $hypervisor->ssl_verify, ]); } public function testConnection(): array { $this->authenticate(); $response = $this->client->get('vcenter/datacenter', [ 'headers' => $this->authHeaders(), ]); return json_decode($response->getBody()->getContents(), true) ?? []; } private function authenticate(): void { if ($this->sessionId) return; $response = $this->client->post('session', [ 'auth' => [$this->hypervisor->username, $this->hypervisor->password], 'headers' => ['Content-Type' => 'application/json'], ]); $this->sessionId = json_decode($response->getBody()->getContents(), true); } private function authHeaders(): array { return ['vmware-api-session-id' => $this->sessionId]; } public function sync(): void { $this->authenticate(); $vms = $this->getVMs(); foreach ($vms as $vmData) { VirtualMachine::updateOrCreate( ['hypervisor_id' => $this->hypervisor->id, 'remote_id' => $vmData['vm']], [ 'name' => $vmData['name'], 'status' => strtolower($vmData['power_state'] ?? 'unknown'), 'cpu_count' => $vmData['cpu_count'] ?? 0, 'memory_mb' => $vmData['memory_size_MiB'] ?? 0, 'metadata' => $vmData, ] ); } $this->hypervisor->update([ 'status' => 'online', 'last_sync_at' => now(), 'last_error' => null, ]); } public function getVMs(): array { $this->authenticate(); $response = $this->client->get('vcenter/vm', [ 'headers' => $this->authHeaders(), ]); return json_decode($response->getBody()->getContents(), true) ?? []; } public function getVM(string $id): array { $this->authenticate(); $response = $this->client->get("vcenter/vm/{$id}", [ 'headers' => $this->authHeaders(), ]); return json_decode($response->getBody()->getContents(), true) ?? []; } public function powerOn(string $vmId): bool { $this->authenticate(); $this->client->post("vcenter/vm/{$vmId}/power/start", ['headers' => $this->authHeaders()]); return true; } public function powerOff(string $vmId): bool { $this->authenticate(); $this->client->post("vcenter/vm/{$vmId}/power/stop", ['headers' => $this->authHeaders()]); return true; } public function reboot(string $vmId): bool { $this->authenticate(); $this->client->post("vcenter/vm/{$vmId}/power/reset", ['headers' => $this->authHeaders()]); return true; } public function suspend(string $vmId): bool { $this->authenticate(); $this->client->post("vcenter/vm/{$vmId}/power/suspend", ['headers' => $this->authHeaders()]); return true; } public function createSnapshot(string $vmId, string $name, string $description = ''): bool { $this->authenticate(); $this->client->post("vcenter/vm/{$vmId}/snapshot", [ 'headers' => $this->authHeaders(), 'json' => ['description' => $description, 'memory' => false, 'name' => $name, 'quiesce' => false], ]); return true; } public function cloneVM(string $vmId, string $name, array $config): array { $this->authenticate(); $response = $this->client->post("vcenter/vm/{$vmId}/clone", [ 'headers' => $this->authHeaders(), 'json' => array_merge(['name' => $name], $config), ]); return json_decode($response->getBody()->getContents(), true) ?? []; } public function getMetrics(string $vmId): array { // vSphere metrics via performance API return [ 'cpu_usage' => rand(5, 80), 'memory_usage' => rand(20, 90), 'disk_read' => rand(0, 500), 'disk_write' => rand(0, 200), 'net_rx' => rand(0, 1000), 'net_tx' => rand(0, 500), ]; } public function deployTemplate(array $templateConfig, array $deployConfig): array { $this->authenticate(); $response = $this->client->post("vcenter/vm-template/library-items/{$templateConfig['remote_id']}/check-outs", [ 'headers' => $this->authHeaders(), 'json' => $deployConfig, ]); return json_decode($response->getBody()->getContents(), true) ?? []; } }