moving to provider stored in database

This commit is contained in:
2025-04-07 14:38:28 +01:00
parent 6bd12bd6ca
commit b800a9d83a
11 changed files with 100 additions and 14 deletions

View File

@@ -4,7 +4,7 @@ namespace App\Enums;
use App\Enums\Concerns\Arrayable;
enum ServerProvider: string
enum ProviderType: string
{
use Arrayable;

View File

@@ -72,7 +72,7 @@ class ServerController extends Controller
$server = $organisation->servers()->create([
'name' => $createdServer->name,
'provider' => ServerProvider::tryFrom($request->provider),
// 'provider' => ServerProvider::tryFrom($request->provider), // @todo
'provider_id' => $createdServer->id,
'ipv4' => $createdServer->ipv4,
'ipv6' => $createdServer->ipv6,

View File

@@ -16,7 +16,6 @@ class Network extends Model
{
return [
'type' => NetworkType::class,
'provider' => ServerProvider::class,
];
}
@@ -34,4 +33,9 @@ class Network extends Model
{
return $this->belongsTo(Organisation::class);
}
public function provider(): BelongsTo
{
return $this->belongsTo(Provider::class);
}
}

30
app/Models/Provider.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models;
use App\Enums\ProviderType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Provider extends Model
{
protected $guarded = [];
protected function casts(): array
{
return [
'token' => 'encrypted',
'type' => ProviderType::class,
];
}
public function networks(): HasMany
{
return $this->hasMany(Network::class);
}
public function servers(): HasMany
{
return $this->hasMany(Server::class);
}
}

View File

@@ -20,7 +20,6 @@ class Server extends Model
protected function casts(): array
{
return [
'provider' => ServerProvider::class,
'status' => ServerStatus::class,
];
}
@@ -66,6 +65,11 @@ class Server extends Model
return $this->hasMany(FirewallRule::class);
}
public function provider(): BelongsTo
{
return $this->belongsTo(Provider::class);
}
public function sshClient(string $user = 'root'): Ssh
{
return Ssh::create($user, $this->ipv4)

View File

@@ -35,8 +35,8 @@ return [
],
],
'hetzner' => [
'key' => env('HETZNER_KEY'),
],
// 'hetzner' => [
// 'key' => env('HETZNER_KEY'),
// ],
];

View File

@@ -20,8 +20,6 @@ class ServerFactory extends Factory
{
return [
'name' => $this->faker->word(),
'provider' => ServerProvider::HETZNER,
'provider_id' => $this->faker->uuid(),
'ipv4' => $this->faker->ipv4(),
'ipv6' => $this->faker->ipv6(),
'provider_status' => '',
@@ -41,4 +39,13 @@ class ServerFactory extends Factory
];
});
}
public function forProvider(string $providerId): static
{
return $this->state(function (array $attributes) use ($providerId) {
return [
'provider_id' => $providerId,
];
});
}
}

View File

@@ -2,6 +2,7 @@
use App\Models\Network;
use App\Models\Organisation;
use App\Models\Provider;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@@ -15,9 +16,9 @@ return new class extends Migration
$table->foreignIdFor(Organisation::class);
$table->foreignIdFor(Network::class, 'external_network_id');
$table->foreignIdFor(Network::class, 'internal_network_id');
$table->foreignIdFor(Provider::class);
$table->string('provider_external_id')->nullable();
$table->string('name');
$table->string('provider');
$table->string('provider_id');
$table->string('ipv4');
$table->string('ipv6');
$table->string('private_ip');

View File

@@ -1,6 +1,7 @@
<?php
use App\Models\Organisation;
use App\Models\Provider;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@@ -12,9 +13,9 @@ return new class extends Migration
Schema::create('networks', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Organisation::class);
$table->foreignIdFor(Provider::class);
$table->string('provider_external_id')->nullable();
$table->string('type');
$table->string('provider')->nullable();
$table->string('provider_id')->nullable();
$table->string('name');
$table->string('ip_range');
$table->timestamps();

View File

@@ -0,0 +1,29 @@
<?php
use App\Models\Organisation;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('providers', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Organisation::class);
$table->string('name');
$table->string('type');
$table->text('token');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('providers');
}
};

View File

@@ -3,6 +3,7 @@
namespace Database\Seeders;
use App\Enums\OrganisationRole;
use App\Enums\ProviderType;
use App\Enums\RepositoryType;
use App\Models\Organisation;
use App\Models\Server;
@@ -32,7 +33,16 @@ class DatabaseSeeder extends Seeder
'owner_id' => 1,
]);
$servers = Server::factory(40)->forOrganisation($organisation->id)->create();
$provider = $organisation->providers()->create([
'name' => 'Hetzner',
'type' => ProviderType::HETZNER,
'token' => env('HETZNER_KEY'),
]);
$servers = Server::factory(40)
->forOrganisation($organisation->id)
->forProvider($provider->id)
->create();
$organisation->servers()->saveMany($servers);