This commit is contained in:
Saeed Vaziry
2024-09-27 20:36:03 +02:00
committed by GitHub
parent b62c40c97d
commit f6bc04763b
122 changed files with 6609 additions and 807 deletions

View File

@ -10,7 +10,6 @@
use Exception;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
use Throwable;
class AWS extends AbstractProvider
@ -21,12 +20,7 @@ class AWS extends AbstractProvider
public function createRules(array $input): array
{
$rules = [
'os' => [
'required',
Rule::in(config('core.operating_systems')),
],
];
$rules = [];
// plans
$plans = [];
foreach (config('serverproviders.aws.plans') as $plan) {
@ -82,14 +76,18 @@ public function connect(?array $credentials = null): bool
}
}
public function plans(): array
public function plans(?string $region): array
{
return config('serverproviders.aws.plans');
return collect(config('serverproviders.aws.plans'))
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']])
->toArray();
}
public function regions(): array
{
return config('serverproviders.aws.regions');
return collect(config('serverproviders.aws.regions'))
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']])
->toArray();
}
public function create(): void

View File

@ -3,15 +3,19 @@
namespace App\ServerProviders;
use App\Models\Server;
use App\Models\ServerProvider as Provider;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
abstract class AbstractProvider implements ServerProvider
{
protected ?Provider $serverProvider;
protected ?Server $server;
public function __construct(?Server $server = null)
public function __construct(?Provider $serverProvider = null, ?Server $server = null)
{
$this->serverProvider = $serverProvider;
$this->server = $server;
}

View File

@ -15,7 +15,7 @@ public function createRules(array $input): array
'ip' => [
'required',
Rule::unique('servers', 'ip'),
new RestrictedIPAddressesRule(),
new RestrictedIPAddressesRule,
],
'port' => [
'required',
@ -46,7 +46,7 @@ public function connect(?array $credentials = null): bool
return true;
}
public function plans(): array
public function plans(?string $region): array
{
return [];
}

View File

@ -16,9 +16,7 @@ class DigitalOcean extends AbstractProvider
public function createRules(array $input): array
{
$rules = [
'os' => 'required|in:'.implode(',', config('core.operating_systems')),
];
$rules = [];
// plans
$plans = [];
foreach (config('serverproviders.digitalocean.plans') as $plan) {
@ -70,14 +68,18 @@ public function connect(?array $credentials = null): bool
return true;
}
public function plans(): array
public function plans(?string $region): array
{
return config('serverproviders.digitalocean.plans');
return collect(config('serverproviders.digitalocean.plans'))
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']])
->toArray();
}
public function regions(): array
{
return config('serverproviders.digitalocean.regions');
return collect(config('serverproviders.digitalocean.regions'))
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']])
->toArray();
}
/**

View File

@ -6,6 +6,8 @@
use App\Exceptions\ServerProviderError;
use App\Facades\Notifier;
use App\Notifications\FailedToDeleteServerFromProvider;
use Exception;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
@ -16,7 +18,6 @@ class Hetzner extends AbstractProvider
public function createRules(array $input): array
{
return [
'os' => 'required|in:'.implode(',', config('core.operating_systems')),
'plan' => 'required',
'region' => 'required',
];
@ -46,6 +47,7 @@ public function data(array $input): array
/**
* @throws CouldNotConnectToProvider
* @throws ConnectionException
*/
public function connect(?array $credentials = null): bool
{
@ -57,18 +59,53 @@ public function connect(?array $credentials = null): bool
return true;
}
public function plans(): array
public function plans(?string $region): array
{
return config('serverproviders.hetzner.plans');
try {
$plans = Http::withToken($this->serverProvider->credentials['token'])
->get($this->apiUrl.'/server_types', ['per_page' => 50])
->json();
return collect($plans['server_types'])->filter(function ($type) use ($region) {
return collect($type['prices'])->filter(function ($price) use ($region) {
return $price['location'] === $region;
});
})
->mapWithKeys(function ($value) {
return [
$value['name'] => __('server_providers.plan', [
'name' => $value['name'],
'cpu' => $value['cores'],
'architecture' => $value['architecture'],
'memory' => $value['memory'],
'disk' => $value['disk'],
]),
];
})
->toArray();
} catch (Exception) {
return [];
}
}
public function regions(): array
{
return config('serverproviders.hetzner.regions');
try {
$regions = Http::withToken($this->serverProvider->credentials['token'])
->get($this->apiUrl.'/locations', ['per_page' => 50])
->json();
return collect($regions['locations'])
->mapWithKeys(fn ($value) => [$value['name'] => $value['city'].' - '.$value['country']])
->toArray();
} catch (Exception) {
return [];
}
}
/**
* @throws ServerProviderError
* @throws ConnectionException
*/
public function create(): void
{
@ -106,6 +143,9 @@ public function create(): void
$this->server->save();
}
/**
* @throws ConnectionException
*/
public function isRunning(): bool
{
$status = Http::withToken($this->server->serverProvider->credentials['token'])
@ -118,6 +158,9 @@ public function isRunning(): bool
return $status->json()['server']['status'] == 'running';
}
/**
* @throws ConnectionException
*/
public function delete(): void
{
if (isset($this->server->provider_data['hetzner_id'])) {

View File

@ -15,9 +15,7 @@ class Linode extends AbstractProvider
public function createRules($input): array
{
$rules = [
'os' => 'required|in:'.implode(',', config('core.operating_systems')),
];
$rules = [];
// plans
$plans = [];
foreach (config('serverproviders.linode.plans') as $plan) {
@ -69,14 +67,18 @@ public function connect(?array $credentials = null): bool
return true;
}
public function plans(): array
public function plans(?string $region): array
{
return config('serverproviders.linode.plans');
return collect(config('serverproviders.linode.plans'))
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']])
->toArray();
}
public function regions(): array
{
return config('serverproviders.linode.regions');
return collect(config('serverproviders.linode.regions'))
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']])
->toArray();
}
/**

View File

@ -14,7 +14,7 @@ public function data(array $input): array;
public function connect(?array $credentials = null): bool;
public function plans(): array;
public function plans(?string $region): array;
public function regions(): array;

View File

@ -17,9 +17,7 @@ class Vultr extends AbstractProvider
public function createRules($input): array
{
$rules = [
'os' => 'required|in:'.implode(',', config('core.operating_systems')),
];
$rules = [];
// plans
$plans = [];
foreach (config('serverproviders.vultr.plans') as $plan) {
@ -71,14 +69,18 @@ public function connect(?array $credentials = null): bool
return true;
}
public function plans(): array
public function plans(?string $region): array
{
return config('serverproviders.vultr.plans');
return collect(config('serverproviders.vultr.plans'))
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']])
->toArray();
}
public function regions(): array
{
return config('serverproviders.vultr.regions');
return collect(config('serverproviders.vultr.regions'))
->mapWithKeys(fn ($value) => [$value['value'] => $value['title']])
->toArray();
}
/**