load DO regions and plans dynamically (#368)

This commit is contained in:
Saeed Vaziry
2024-11-17 11:20:15 +01:00
committed by GitHub
parent 34c5750950
commit 57b2771c7e
2 changed files with 79 additions and 48 deletions

View File

@ -105,13 +105,6 @@ public static function plans(?int $id, ?string $region): array
return []; return [];
} }
if (Cache::get('plans-'.$id.'-'.$region)) { return $profile->provider()->plans($region);
return Cache::get('plans-'.$id.'-'.$region);
}
$plans = $profile->provider()->plans($region);
Cache::put('plans-'.$id.'-'.$region, $plans, 60);
return $plans;
} }
} }

View File

@ -16,21 +16,10 @@ class DigitalOcean extends AbstractProvider
public function createRules(array $input): array public function createRules(array $input): array
{ {
$rules = []; return [
// plans 'plan' => 'required',
$plans = []; 'region' => 'required',
foreach (config('serverproviders.digitalocean.plans') as $plan) { ];
$plans[] = $plan['value'];
}
$rules['plan'] = 'required|in:'.implode(',', $plans);
// regions
$regions = [];
foreach (config('serverproviders.digitalocean.regions') as $region) {
$regions[] = $region['value'];
}
$rules['region'] = 'required|in:'.implode(',', $regions);
return $rules;
} }
public function credentialValidationRules(array $input): array public function credentialValidationRules(array $input): array
@ -60,7 +49,12 @@ public function data(array $input): array
*/ */
public function connect(?array $credentials = null): bool public function connect(?array $credentials = null): bool
{ {
$connect = Http::withToken($credentials['token'])->get($this->apiUrl.'/account'); try {
$connect = Http::withToken($credentials['token'])->get($this->apiUrl.'/account');
} catch (Exception) {
throw new CouldNotConnectToProvider('DigitalOcean');
}
if (! $connect->ok()) { if (! $connect->ok()) {
throw new CouldNotConnectToProvider('DigitalOcean'); throw new CouldNotConnectToProvider('DigitalOcean');
} }
@ -70,16 +64,46 @@ public function connect(?array $credentials = null): bool
public function plans(?string $region): array public function plans(?string $region): array
{ {
return collect(config('serverproviders.digitalocean.plans')) try {
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']]) $plans = Http::withToken($this->serverProvider->credentials['token'])
->toArray(); ->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']);
})
->mapWithKeys(function ($value) {
return [
$value['slug'] => __('server_providers.plan', [
'name' => $value['description'],
'cpu' => $value['vcpus'],
'memory' => $value['memory'],
'disk' => $value['disk'],
]),
];
})
->toArray();
} catch (Exception) {
return [];
}
} }
public function regions(): array public function regions(): array
{ {
return collect(config('serverproviders.digitalocean.regions')) try {
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']]) $regions = Http::withToken($this->serverProvider->credentials['token'])
->toArray(); ->get($this->apiUrl.'/regions', ['per_page' => 200])
->json();
return collect($regions['regions'])
->where('available', true)
->mapWithKeys(fn ($value) => [$value['slug'] => $value['name']])
->toArray();
} catch (Exception) {
return [];
}
} }
/** /**
@ -89,26 +113,36 @@ public function create(): void
{ {
$this->generateKeyPair(); $this->generateKeyPair();
$createSshKey = Http::withToken($this->server->serverProvider->credentials['token']) try {
->post($this->apiUrl.'/account/keys', [ $createSshKey = Http::withToken($this->server->serverProvider->credentials['token'])
'public_key' => $this->server->sshKey()['public_key'], ->post($this->apiUrl.'/account/keys', [
'name' => str($this->server->name)->slug().'-'.$this->server->id, 'public_key' => $this->server->sshKey()['public_key'],
]); 'name' => str($this->server->name)->slug().'-'.$this->server->id,
]);
} catch (Exception) {
throw new ServerProviderError('DigitalOcean SSH Key');
}
if ($createSshKey->status() != 201) { if ($createSshKey->status() != 201) {
throw new ServerProviderError('DigitalOcean SSH Key'); throw new ServerProviderError('DigitalOcean SSH Key');
} }
$create = Http::withToken($this->server->serverProvider->credentials['token']) try {
->post($this->apiUrl.'/droplets', [ $create = Http::withToken($this->server->serverProvider->credentials['token'])
'name' => str($this->server->name)->slug(), ->post($this->apiUrl.'/droplets', [
'region' => $this->server->provider_data['region'], 'name' => str($this->server->name)->slug(),
'size' => $this->server->provider_data['plan'], 'region' => $this->server->provider_data['region'],
'image' => config('serverproviders.digitalocean.images')[$this->server->os], 'size' => $this->server->provider_data['plan'],
'backups' => false, 'image' => config('serverproviders.digitalocean.images')[$this->server->os],
'ipv6' => false, 'backups' => false,
'monitoring' => false, 'ipv6' => false,
'ssh_keys' => [$createSshKey->json()['ssh_key']['id']], 'monitoring' => false,
]); 'ssh_keys' => [$createSshKey->json()['ssh_key']['id']],
]);
} catch (Exception) {
throw new ServerProviderError('DigitalOcean');
}
if ($create->status() != 202) { if ($create->status() != 202) {
$msg = __('Failed to create server on DigitalOcean'); $msg = __('Failed to create server on DigitalOcean');
Log::error('Failed to create server on DigitalOcean', $create->json()); Log::error('Failed to create server on DigitalOcean', $create->json());
@ -122,8 +156,12 @@ public function create(): void
public function isRunning(): bool public function isRunning(): bool
{ {
$status = Http::withToken($this->server->serverProvider->credentials['token']) try {
->get($this->apiUrl.'/droplets/'.$this->server->provider_data['droplet_id']); $status = Http::withToken($this->server->serverProvider->credentials['token'])
->get($this->apiUrl.'/droplets/'.$this->server->provider_data['droplet_id']);
} catch (Exception) {
return false;
}
if (! $status->ok()) { if (! $status->ok()) {
return false; return false;