load Vultr regions and plans dynamically (#369)

This commit is contained in:
Saeed Vaziry
2024-11-17 12:09:41 +01:00
committed by GitHub
parent 57b2771c7e
commit 20944421de
4 changed files with 144 additions and 56 deletions

View File

@ -69,8 +69,6 @@ public function plans(?string $region): array
->get($this->apiUrl.'/sizes', ['per_page' => 200])
->json();
ds($region);
return collect($plans['sizes'])->filter(function ($size) use ($region) {
return in_array($region, $size['regions']);
})
@ -133,7 +131,7 @@ public function create(): void
'name' => str($this->server->name)->slug(),
'region' => $this->server->provider_data['region'],
'size' => $this->server->provider_data['plan'],
'image' => config('serverproviders.digitalocean.images')[$this->server->os],
'image' => $this->getImageId($this->server->os, $this->server->provider_data['region']),
'backups' => false,
'ipv6' => false,
'monitoring' => false,
@ -195,4 +193,33 @@ public function delete(): void
}
}
}
/**
* @throws Exception
*/
private function getImageId(string $os, string $region): int
{
$version = config('core.operating_system_versions.'.$os);
try {
$result = Http::withToken($this->serverProvider->credentials['token'])
->get($this->apiUrl.'/images', [
'per_page' => 200,
'type' => 'distribution',
])
->json();
$image = collect($result['images'])
->filter(function ($image) use ($region, $version) {
return in_array($region, $image['regions']) && str_contains($image['name'], $version);
})
->where('distribution', 'Ubuntu')
->where('status', 'available')
->first();
return $image['id'];
} catch (Exception) {
throw new Exception('Could not find image ID');
}
}
}