Skip to content

Commit

Permalink
Refactor resource api
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Aug 6, 2022
1 parent fc7d98c commit ab0ca41
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 24 deletions.
16 changes: 16 additions & 0 deletions app/Repositories/Proxmox/Server/ProxmoxServerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
87 changes: 63 additions & 24 deletions app/Services/Servers/CloudinitService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,34 @@
*/
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
* @return mixed
*/
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]);
}

}

/**
Expand All @@ -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]);
}

/**
Expand All @@ -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]);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -117,11 +163,4 @@ public function updateIpConfig(string|array $config)
]);
}
}

public function getServerInaccessibleConfig()
{
return ['nameserver' => 'server inaccessible,server inaccessible', 'bios' => 'seabios', 'searchdomain' => 'server inaccessible'];
}


}

0 comments on commit ab0ca41

Please sign in to comment.