create network if doesn't already exist on server, wip test
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\GenerateRandomSlug;
|
||||
use App\Enums\NetworkType;
|
||||
use App\Enums\ServerStatus;
|
||||
use App\Jobs\Servers\WaitForServerToConnect;
|
||||
use App\Models\Organisation;
|
||||
@@ -57,7 +58,15 @@ class ServerController extends Controller
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'provider' => ['required', 'exists:providers,id'],
|
||||
'server_type' => ['required', 'string'],
|
||||
'location' => ['required', 'string'],
|
||||
'image' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
$sudoPassword = Str::random(32);
|
||||
/** @var Provider $provider */
|
||||
$provider = Provider::findOrFail($request->provider);
|
||||
$providerService = $provider->service();
|
||||
|
||||
@@ -65,11 +74,27 @@ class ServerController extends Controller
|
||||
return back()->with('error', 'Invalid provider');
|
||||
}
|
||||
|
||||
if (! $network = $provider->networks()->first()) {
|
||||
// we need a keystone network to create this server
|
||||
$createdNetwork = $providerService->createNetwork(
|
||||
name: 'keystone',
|
||||
);
|
||||
|
||||
$network = $provider->networks()->create([
|
||||
'organisation_id' => $provider->organisation_id,
|
||||
'external_id' => $createdNetwork->id,
|
||||
'type' => NetworkType::EXTERNAL,
|
||||
'name' => $createdNetwork->name,
|
||||
'ip_range' => $createdNetwork->ipRange,
|
||||
]);
|
||||
}
|
||||
|
||||
$createdServer = $providerService->createServer(
|
||||
name: app(GenerateRandomSlug::class)->execute(), // @todo allow custom name
|
||||
serverType: $request->server_type,
|
||||
location: $request->location,
|
||||
image: $request->image,
|
||||
networkId: $network->external_id,
|
||||
);
|
||||
|
||||
$organisation = Organisation::findOrFail($request->route('organisation'));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Integrations\Requests\Hetzner\Servers;
|
||||
namespace App\Http\Integrations\Requests\Hetzner\Networks;
|
||||
|
||||
use Saloon\Contracts\Body\HasBody;
|
||||
use Saloon\Enums\Method;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Integrations\Requests\Hetzner\Servers;
|
||||
namespace App\Http\Integrations\Requests\Hetzner\Networks;
|
||||
|
||||
use Saloon\Contracts\Body\HasBody;
|
||||
use Saloon\Enums\Method;
|
||||
|
||||
@@ -18,6 +18,7 @@ class CreateServerRequest extends Request implements HasBody
|
||||
protected ?string $name = null,
|
||||
protected ?string $serverType = null,
|
||||
protected ?string $location = null,
|
||||
protected ?array $networks = null,
|
||||
) {}
|
||||
|
||||
protected function defaultBody(): array
|
||||
@@ -27,6 +28,7 @@ class CreateServerRequest extends Request implements HasBody
|
||||
'name' => $this->name,
|
||||
'server_type' => $this->serverType,
|
||||
'location' => $this->location,
|
||||
'networks' => $this->networks,
|
||||
'user_data' => file_get_contents(resource_path('scripts/hetzner-cloudinit.yml')),
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user