From ea417cf45e98f78ce32e263a9a3b29c95fb61be1 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sat, 16 Mar 2024 19:52:32 -0400 Subject: [PATCH] Remove allocation repository --- .../AllocationRepositoryInterface.php | 19 ---- .../Admin/Nodes/NodeViewController.php | 2 - .../Controllers/Admin/NodesController.php | 19 ++-- .../Servers/ServerTransferController.php | 12 ++- .../Controllers/Admin/ServersController.php | 2 - app/Providers/RepositoryServiceProvider.php | 3 - .../Eloquent/AllocationRepository.php | 101 ------------------ .../Allocations/AllocationDeletionService.php | 10 +- .../Allocations/AssignmentService.php | 6 +- .../Deployment/AllocationSelectionService.php | 78 ++++++++++++-- 10 files changed, 90 insertions(+), 162 deletions(-) delete mode 100644 app/Contracts/Repository/AllocationRepositoryInterface.php delete mode 100644 app/Repositories/Eloquent/AllocationRepository.php diff --git a/app/Contracts/Repository/AllocationRepositoryInterface.php b/app/Contracts/Repository/AllocationRepositoryInterface.php deleted file mode 100644 index 7ac1befa0..000000000 --- a/app/Contracts/Repository/AllocationRepositoryInterface.php +++ /dev/null @@ -1,19 +0,0 @@ -allocationRepository->deleteWhere([ - ['node_id', '=', $node], - ['server_id', '=', null], - ['ip', '=', $request->input('ip')], - ]); + /** @var Node $node */ + $node = Node::query()->findOrFail($node); + $node->allocations() + ->where('ip', $request->input('ip')) + ->whereNull('server_id') + ->delete(); $this->alert->success(trans('admin/node.notices.unallocated_deleted', ['ip' => $request->input('ip')])) ->flash(); @@ -134,9 +133,9 @@ class NodesController extends Controller */ public function allocationSetAlias(AllocationAliasFormRequest $request): \Symfony\Component\HttpFoundation\Response { - $this->allocationRepository->update($request->input('allocation_id'), [ - 'ip_alias' => (empty($request->input('alias'))) ? null : $request->input('alias'), - ]); + $allocation = Allocation::query()->findOrFail($request->input('allocation_id')); + $alias = (empty($request->input('alias'))) ? null : $request->input('alias'); + $allocation->update(['ip_alias' => $alias]); return response('', 204); } diff --git a/app/Http/Controllers/Admin/Servers/ServerTransferController.php b/app/Http/Controllers/Admin/Servers/ServerTransferController.php index 06f146d9e..57d2c033e 100644 --- a/app/Http/Controllers/Admin/Servers/ServerTransferController.php +++ b/app/Http/Controllers/Admin/Servers/ServerTransferController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers\Admin\Servers; use App\Exceptions\Http\Connection\DaemonConnectionException; +use App\Models\Allocation; +use App\Models\Node; use Carbon\CarbonImmutable; use GuzzleHttp\Exception\TransferException; use Illuminate\Http\Request; @@ -16,7 +18,6 @@ use Illuminate\Database\ConnectionInterface; use App\Http\Controllers\Controller; use App\Services\Nodes\NodeJWTService; use App\Repositories\Eloquent\NodeRepository; -use App\Contracts\Repository\AllocationRepositoryInterface; class ServerTransferController extends Controller { @@ -25,7 +26,6 @@ class ServerTransferController extends Controller */ public function __construct( private AlertsMessageBag $alert, - private AllocationRepositoryInterface $allocationRepository, private ConnectionInterface $connection, private NodeJWTService $nodeJWTService, private NodeRepository $nodeRepository @@ -120,7 +120,11 @@ class ServerTransferController extends Controller $allocations = $additional_allocations; $allocations[] = $allocation_id; - $unassigned = $this->allocationRepository->getUnassignedAllocationIds($node_id); + $node = Node::query()->findOrFail($node_id); + $unassigned = $node->allocations() + ->whereNull('server_id') + ->pluck('id') + ->toArray(); $updateIds = []; foreach ($allocations as $allocation) { @@ -132,7 +136,7 @@ class ServerTransferController extends Controller } if (!empty($updateIds)) { - $this->allocationRepository->updateWhereIn('id', $updateIds, ['server_id' => $server->id]); + Allocation::query()->whereIn('id', $updateIds)->update(['server_id' => $server->id]); } } } diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 45048a006..6eeb95267 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -28,7 +28,6 @@ use App\Repositories\Eloquent\DatabaseHostRepository; use App\Services\Databases\DatabaseManagementService; use Illuminate\Contracts\Config\Repository as ConfigRepository; use App\Contracts\Repository\DatabaseRepositoryInterface; -use App\Contracts\Repository\AllocationRepositoryInterface; use App\Services\Servers\ServerConfigurationStructureService; use App\Http\Requests\Admin\Servers\Databases\StoreServerDatabaseRequest; @@ -39,7 +38,6 @@ class ServersController extends Controller */ public function __construct( protected AlertsMessageBag $alert, - protected AllocationRepositoryInterface $allocationRepository, protected BuildModificationService $buildModificationService, protected ConfigRepository $config, protected DaemonServerRepository $daemonServerRepository, diff --git a/app/Providers/RepositoryServiceProvider.php b/app/Providers/RepositoryServiceProvider.php index 0613da335..296b5db28 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -10,7 +10,6 @@ use App\Repositories\Eloquent\SessionRepository; use App\Repositories\Eloquent\SubuserRepository; use App\Repositories\Eloquent\DatabaseRepository; use App\Repositories\Eloquent\SettingsRepository; -use App\Repositories\Eloquent\AllocationRepository; use App\Contracts\Repository\EggRepositoryInterface; use App\Repositories\Eloquent\EggVariableRepository; use App\Contracts\Repository\NodeRepositoryInterface; @@ -20,7 +19,6 @@ use App\Contracts\Repository\SessionRepositoryInterface; use App\Contracts\Repository\SubuserRepositoryInterface; use App\Contracts\Repository\DatabaseRepositoryInterface; use App\Contracts\Repository\SettingsRepositoryInterface; -use App\Contracts\Repository\AllocationRepositoryInterface; use App\Contracts\Repository\EggVariableRepositoryInterface; use App\Contracts\Repository\DatabaseHostRepositoryInterface; @@ -32,7 +30,6 @@ class RepositoryServiceProvider extends ServiceProvider public function register(): void { // Eloquent Repositories - $this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class); $this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class); $this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class); $this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class); diff --git a/app/Repositories/Eloquent/AllocationRepository.php b/app/Repositories/Eloquent/AllocationRepository.php deleted file mode 100644 index f104c3157..000000000 --- a/app/Repositories/Eloquent/AllocationRepository.php +++ /dev/null @@ -1,101 +0,0 @@ -select('id') - ->whereNull('server_id') - ->where('node_id', $node) - ->get() - ->pluck('id') - ->toArray(); - } - - /** - * Return a concatenated result set of node ips that already have at least one - * server assigned to that IP. This allows for filtering out sets for - * dedicated allocation IPs. - * - * If an array of nodes is passed the results will be limited to allocations - * in those nodes. - */ - protected function getDiscardableDedicatedAllocations(array $nodes = []): array - { - $query = Allocation::query()->selectRaw('CONCAT_WS("-", node_id, ip) as result'); - - if (!empty($nodes)) { - $query->whereIn('node_id', $nodes); - } - - return $query->whereNotNull('server_id') - ->groupByRaw('CONCAT(node_id, ip)') - ->get() - ->pluck('result') - ->toArray(); - } - - /** - * Return a single allocation from those meeting the requirements. - */ - public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false): ?Allocation - { - $query = Allocation::query()->whereNull('server_id'); - - if (!empty($nodes)) { - $query->whereIn('node_id', $nodes); - } - - if (!empty($ports)) { - $query->where(function (Builder $inner) use ($ports) { - $whereIn = []; - foreach ($ports as $port) { - if (is_array($port)) { - $inner->orWhereBetween('port', $port); - - continue; - } - - $whereIn[] = $port; - } - - if (!empty($whereIn)) { - $inner->orWhereIn('port', $whereIn); - } - }); - } - - // If this allocation should not be shared with any other servers get - // the data and modify the query as necessary, - if ($dedicated) { - $discard = $this->getDiscardableDedicatedAllocations($nodes); - - if (!empty($discard)) { - $query->whereNotIn( - $this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'), - $discard - ); - } - } - - return $query->inRandomOrder()->first(); - } -} diff --git a/app/Services/Allocations/AllocationDeletionService.php b/app/Services/Allocations/AllocationDeletionService.php index a47aadc71..642f77edb 100644 --- a/app/Services/Allocations/AllocationDeletionService.php +++ b/app/Services/Allocations/AllocationDeletionService.php @@ -3,18 +3,10 @@ namespace App\Services\Allocations; use App\Models\Allocation; -use App\Contracts\Repository\AllocationRepositoryInterface; use App\Exceptions\Service\Allocation\ServerUsingAllocationException; class AllocationDeletionService { - /** - * AllocationDeletionService constructor. - */ - public function __construct(private AllocationRepositoryInterface $repository) - { - } - /** * Delete an allocation from the database only if it does not have a server * that is actively attached to it. @@ -27,6 +19,6 @@ class AllocationDeletionService throw new ServerUsingAllocationException(trans('exceptions.allocations.server_using')); } - return $this->repository->delete($allocation->id); + return $allocation->delete(); } } diff --git a/app/Services/Allocations/AssignmentService.php b/app/Services/Allocations/AssignmentService.php index 8d103e482..0b6b4c01a 100644 --- a/app/Services/Allocations/AssignmentService.php +++ b/app/Services/Allocations/AssignmentService.php @@ -2,11 +2,11 @@ namespace App\Services\Allocations; +use App\Models\Allocation; use IPTools\Network; use App\Models\Node; use Illuminate\Database\ConnectionInterface; use App\Exceptions\DisplayException; -use App\Contracts\Repository\AllocationRepositoryInterface; use App\Exceptions\Service\Allocation\CidrOutOfRangeException; use App\Exceptions\Service\Allocation\PortOutOfRangeException; use App\Exceptions\Service\Allocation\InvalidPortMappingException; @@ -24,7 +24,7 @@ class AssignmentService /** * AssignmentService constructor. */ - public function __construct(protected AllocationRepositoryInterface $repository, protected ConnectionInterface $connection) + public function __construct(protected ConnectionInterface $connection) { } @@ -99,7 +99,7 @@ class AssignmentService ]; } - $this->repository->insertIgnore($insertData); + Allocation::query()->insertOrIgnore($insertData); } } diff --git a/app/Services/Deployment/AllocationSelectionService.php b/app/Services/Deployment/AllocationSelectionService.php index 7425453a4..24a45f582 100644 --- a/app/Services/Deployment/AllocationSelectionService.php +++ b/app/Services/Deployment/AllocationSelectionService.php @@ -5,7 +5,6 @@ namespace App\Services\Deployment; use App\Models\Allocation; use App\Exceptions\DisplayException; use App\Services\Allocations\AssignmentService; -use App\Contracts\Repository\AllocationRepositoryInterface; use App\Exceptions\Service\Deployment\NoViableAllocationException; class AllocationSelectionService @@ -16,13 +15,6 @@ class AllocationSelectionService protected array $ports = []; - /** - * AllocationSelectionService constructor. - */ - public function __construct(private AllocationRepositoryInterface $repository) - { - } - /** * Toggle if the selected allocation should be the only allocation belonging * to the given IP address. If true an allocation will not be selected if an IP @@ -84,7 +76,7 @@ class AllocationSelectionService */ public function handle(): Allocation { - $allocation = $this->repository->getRandomAllocation($this->nodes, $this->ports, $this->dedicated); + $allocation = $this->getRandomAllocation($this->nodes, $this->ports, $this->dedicated); if (is_null($allocation)) { throw new NoViableAllocationException(trans('exceptions.deployment.no_viable_allocations')); @@ -92,4 +84,72 @@ class AllocationSelectionService return $allocation; } + + /** + * Return a single allocation from those meeting the requirements. + */ + private function getRandomAllocation(array $nodes = [], array $ports = [], bool $dedicated = false): ?Allocation + { + $query = Allocation::query()->whereNull('server_id'); + + if (!empty($nodes)) { + $query->whereIn('node_id', $nodes); + } + + if (!empty($ports)) { + $query->where(function ($inner) use ($ports) { + $whereIn = []; + foreach ($ports as $port) { + if (is_array($port)) { + $inner->orWhereBetween('port', $port); + continue; + } + + $whereIn[] = $port; + } + + if (!empty($whereIn)) { + $inner->orWhereIn('port', $whereIn); + } + }); + } + + // If this allocation should not be shared with any other servers get + // the data and modify the query as necessary, + if ($dedicated) { + $discard = $this->getDiscardableDedicatedAllocations($nodes); + + if (!empty($discard)) { + $query->whereNotIn( + Allocation::query()->raw('CONCAT_WS("-", node_id, ip)'), + $discard + ); + } + } + + return $query->inRandomOrder()->first(); + } + + /** + * Return a concatenated result set of node ips that already have at least one + * server assigned to that IP. This allows for filtering out sets for + * dedicated allocation IPs. + * + * If an array of nodes is passed the results will be limited to allocations + * in those nodes. + */ + private function getDiscardableDedicatedAllocations(array $nodes = []): array + { + $query = Allocation::query()->selectRaw('CONCAT_WS("-", node_id, ip) as result'); + + if (!empty($nodes)) { + $query->whereIn('node_id', $nodes); + } + + return $query->whereNotNull('server_id') + ->groupByRaw('CONCAT(node_id, ip)') + ->get() + ->pluck('result') + ->toArray(); + } }