NetworkZone

This commit is contained in:
2025-05-22 14:51:54 +01:00
parent 7d03b81723
commit bfe0f8eabf
8 changed files with 48 additions and 13 deletions

View File

@@ -9,5 +9,6 @@ class Location
public string $name,
public string $country,
public string $city,
public ?string $networkZone = null,
) {}
}

View File

@@ -8,5 +8,6 @@ class Network
public string $id,
public string $name,
public string $ipRange,
public ?string $networkZone = 'global',
) {}
}

View File

@@ -63,6 +63,7 @@ class ServerController extends Controller
'server_type' => ['required', 'string'],
'location' => ['required', 'string'],
'image' => ['required', 'string'],
'network_zone' => ['nullable', 'string'],
]);
$sudoPassword = Str::random(32);
@@ -74,10 +75,19 @@ class ServerController extends Controller
return back()->with('error', 'Invalid provider');
}
if (! $network = $provider->networks()->first()) {
// we need a keystone network to create this server
$networkZone = $request->network_zone ?? 'global';
// Look for an existing network with the same network_zone
$network = $provider->networks()
->where('network_zone', $networkZone)
->first();
if (! $network) {
// We need to create a network with the correct network zone
$networkName = "keystone-{$networkZone}";
$createdNetwork = $providerService->createNetwork(
name: 'keystone',
name: $networkName,
networkZone: $networkZone
);
$network = $provider->networks()->create([
@@ -86,6 +96,7 @@ class ServerController extends Controller
'type' => NetworkType::EXTERNAL,
'name' => $createdNetwork->name,
'ip_range' => $createdNetwork->ipRange,
'network_zone' => $networkZone,
]);
}

View File

@@ -15,14 +15,27 @@ class CreateNetworkRequest extends Request implements HasBody
public function __construct(
protected ?string $name = null,
protected ?string $networkZone = null,
) {}
protected function defaultBody(): array
{
return [
$body = [
'name' => $this->name,
'ip_range' => '10.0.0.0/16',
];
if ($this->networkZone) {
$body['subnets'] = [
[
'type' => 'cloud',
'ip_range' => '10.0.1.0/24',
'network_zone' => $this->networkZone
]
];
}
return $body;
}
public function resolveEndpoint(): string

View File

@@ -111,6 +111,7 @@ class HetznerService extends ServerProviderService
name: $location['name'],
country: $location['country'],
city: $location['city'],
networkZone: $location['network_zone'] ?? null,
);
})->values();
}
@@ -135,7 +136,7 @@ class HetznerService extends ServerProviderService
})->where('osVersion', '!=', 'unknown')->values();
}
public function findNetwork(string $name): ?Network
public function findNetwork(string $name, ?string $networkZone = null): ?Network
{
$response = $this->connector->send(new GetNetworksRequest(
name: $name,
@@ -152,16 +153,18 @@ class HetznerService extends ServerProviderService
id: $network['id'],
name: $network['name'],
ipRange: $network['ip_range'],
networkZone: $networkZone ?? 'global',
);
}
return null;
}
public function createNetwork(string $name): Network
public function createNetwork(string $name, ?string $networkZone = null): Network
{
$response = $this->connector->send(new CreateNetworkRequest(
name: $name,
networkZone: $networkZone,
));
if ($response->status() !== 201) {
@@ -169,6 +172,7 @@ class HetznerService extends ServerProviderService
'response' => $response->json(),
'status' => $response->status(),
'name' => $name,
'networkZone' => $networkZone,
]);
throw new Exception('Failed to create network on Hetzner');
}
@@ -177,6 +181,7 @@ class HetznerService extends ServerProviderService
id: $response->json('network.id'),
name: $response->json('network.name'),
ipRange: $response->json('network.ip_range'),
networkZone: $response->json('network.network_zone') ?? $networkZone ?? 'global',
);
}
}

View File

@@ -28,7 +28,7 @@ abstract class ServerProviderService
abstract public function getImages(): Collection;
abstract public function findNetwork(string $name): ?Network;
abstract public function findNetwork(string $name, ?string $networkZone = null): ?Network;
abstract public function createNetwork(string $name): Network;
abstract public function createNetwork(string $name, ?string $networkZone = null): Network;
}