From ab0ca410a000553de2070594dde3d9dd36035814 Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Sat, 6 Aug 2022 17:47:46 +0000 Subject: [PATCH] Refactor resource api --- .../Server/ProxmoxServerRepository.php | 16 ++++ app/Services/Servers/CloudinitService.php | 87 ++++++++++++++----- 2 files changed, 79 insertions(+), 24 deletions(-) diff --git a/app/Repositories/Proxmox/Server/ProxmoxServerRepository.php b/app/Repositories/Proxmox/Server/ProxmoxServerRepository.php index 057f44d77fc..17a10a3493f 100644 --- a/app/Repositories/Proxmox/Server/ProxmoxServerRepository.php +++ b/app/Repositories/Proxmox/Server/ProxmoxServerRepository.php @@ -26,4 +26,20 @@ public function getStatus() $data = json_decode($response->getBody(), true); return $data['data'] ?? $data; } + + public function getResources() + { + Assert::isInstanceOf($this->server, Server::class); + + try { + $response = $this->getHttpClient()->get('/api2/json/cluster/resources'); + } catch (GuzzleException $e) { + throw new ProxmoxConnectionException($e); + } + + $json = json_decode($response->getBody(), true); + $data = $json['data'] ?? $json; + + return collect($data)->where('vmid', $this->server->vmid)->firstOrFail(); + } } \ No newline at end of file diff --git a/app/Services/Servers/CloudinitService.php b/app/Services/Servers/CloudinitService.php index e72f8d82a41..f170473cbbc 100644 --- a/app/Services/Servers/CloudinitService.php +++ b/app/Services/Servers/CloudinitService.php @@ -18,24 +18,19 @@ */ class CloudinitService extends ProxmoxService { + /** + * @var ProxmoxCloudinitRepository + */ protected ProxmoxCloudinitRepository $cloudinitRepository; + /** + * + */ public function __construct() { $this->cloudinitRepository = new ProxmoxCloudinitRepository(); } - public function fetchConfig() - { - return $this->instance()->config()->get(); - } - - // dumps YAML formatted config (I know, it's terrible) - public function dumpConfig(string $type = 'network') - { - return $this->removeDataProperty($this->instance()->cloudinit()->dump()->get(['type' => $type])); - } - /** * @param string $password * @param array $params @@ -43,13 +38,14 @@ public function dumpConfig(string $type = 'network') */ public function changePassword(string $password, AuthenticationType $type) { + $this->cloudinitRepository->setServer($this->server); + if (AuthenticationType::KEY === $type) { - return $this->instance()->config()->post([$type->value => rawurlencode($password)]); + return $this->cloudinitRepository->update([$type->value => rawurlencode($password)]); } else { - return $this->instance()->config()->post([$type->value => $password]); + return $this->cloudinitRepository->update([$type->value => $password]); } - } /** @@ -58,9 +54,14 @@ public function changePassword(string $password, AuthenticationType $type) * @return mixed */ // Generally needed for Windows VM's with over 2TB disk, still WIP since I still need to add EFI disk + /** + * @param BiosType $type + * @return mixed + * @throws ProxmoxConnectionException + */ public function changeBIOS(BiosType $type) { - return $this->instance()->config()->post(['bios' => $type->value]); + return $this->cloudinitRepository->setServer($this->server)->update(['bios' => $type->value]); } /** @@ -70,7 +71,7 @@ public function changeBIOS(BiosType $type) */ public function changeHostname(string $hostname) { - return $this->instance()->config()->post(['searchdomain' => $hostname]); + return $this->cloudinitRepository->setServer($this->server)->update(['searchdomain' => $hostname]); } /** @@ -80,9 +81,54 @@ public function changeHostname(string $hostname) */ public function changeNameserver(string $nameserver) { - return $this->instance()->config()->post(['nameserver' => $nameserver]); + return $this->cloudinitRepository->setServer($this->server)->update(['nameserver' => $nameserver]); } + public function getIpConfig(): array + { + $data = $this->cloudinitRepository->setServer($this->server)->getConfig(); + + $config = [ + 'ipv4' => null, + 'ipv6' => null, + ]; + + $rawConfig = collect($data)->where('key', 'ipconfig0')->first(); + + if ($rawConfig) + { + $configs = explode(',', Arr::get($rawConfig, 'value')); + + Arr::map($configs, function ($value) use (&$config) { + $property = explode('=', $value); + + if ($property[0] === 'ip') + { + $cidr = explode('/', $property[1]); + $config['ipv4']['address'] = $cidr[0]; + $config['ipv4']['cidr'] = $cidr[1]; + } + if ($property[0] === 'ip6') + { + $cidr = explode('/', $property[1]); + $config['ipv6']['address'] = $cidr[0]; + $config['ipv6']['cidr'] = $cidr[1]; + } + if ($property[0] === 'gw') + $config['ipv4']['gateway'] = $property[1]; + if ($property[0] === 'gw6') + $config['ipv6']['gateway'] = $property[1]; + }); + } + + return $config; + } + + /** + * @param string|array $config + * @return mixed|void + * @throws ProxmoxConnectionException + */ public function updateIpConfig(string|array $config) { $this->cloudinitRepository->setServer($this->server); @@ -117,11 +163,4 @@ public function updateIpConfig(string|array $config) ]); } } - - public function getServerInaccessibleConfig() - { - return ['nameserver' => 'server inaccessible,server inaccessible', 'bios' => 'seabios', 'searchdomain' => 'server inaccessible']; - } - - }