Handle allocation assignment using services

Function is significantly quicker and uses 1 SQL query per IP rather than 1 query per port.
This commit is contained in:
Dane Everitt 2017-08-05 21:10:32 -05:00
parent 396b5c22d9
commit 669119c8f8
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
17 changed files with 754 additions and 5915 deletions

View file

@ -29,7 +29,7 @@ use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Services\LocationService;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Admin\LocationRequest;
use Pterodactyl\Http\Requests\Admin\LocationFormRequest;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class LocationController extends Controller
@ -94,13 +94,13 @@ class LocationController extends Controller
/**
* Handle request to create new location.
*
* @param \Pterodactyl\Http\Requests\Admin\LocationRequest $request
* @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Throwable
* @throws \Watson\Validating\ValidationException
*/
public function create(LocationRequest $request)
public function create(LocationFormRequest $request)
{
$location = $this->service->create($request->normalize());
$this->alert->success('Location was created successfully.')->flash();
@ -111,14 +111,14 @@ class LocationController extends Controller
/**
* Handle request to update or delete location.
*
* @param \Pterodactyl\Http\Requests\Admin\LocationRequest $request
* @param \Pterodactyl\Models\Location $location
* @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request
* @param \Pterodactyl\Models\Location $location
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Throwable
* @throws \Watson\Validating\ValidationException
*/
public function update(LocationRequest $request, Location $location)
public function update(LocationFormRequest $request, Location $location)
{
if ($request->input('action') === 'delete') {
return $this->delete($location);

View file

@ -24,25 +24,25 @@
namespace Pterodactyl\Http\Controllers\Admin;
use Log;
use Alert;
use Javascript;
use Illuminate\Http\Request;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Allocation;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\NodeRepository;
use Pterodactyl\Services\Nodes\UpdateService;
use Pterodactyl\Services\Nodes\CreationService;
use Pterodactyl\Services\Nodes\DeletionService;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Cache\Repository as CacheRepository;
use Pterodactyl\Http\Requests\Admin\NodeFormRequest;
use Pterodactyl\Exceptions\DisplayValidationException;
use Pterodactyl\Services\Allocations\AssignmentService;
use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest;
class NodesController extends Controller
{
@ -51,6 +51,16 @@ class NodesController extends Controller
*/
protected $alert;
/**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
*/
protected $allocationRepository;
/**
* @var \Pterodactyl\Services\Allocations\AssignmentService
*/
protected $assignmentService;
/**
* @var \Illuminate\Cache\Repository
*/
@ -86,8 +96,24 @@ class NodesController extends Controller
*/
protected $updateService;
/**
* NodesController constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
* @param \Illuminate\Cache\Repository $cache
* @param \Pterodactyl\Services\Nodes\CreationService $creationService
* @param \Pterodactyl\Services\Nodes\DeletionService $deletionService
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Pterodactyl\Services\Nodes\UpdateService $updateService
*/
public function __construct(
AlertsMessageBag $alert,
AllocationRepositoryInterface $allocationRepository,
AssignmentService $assignmentService,
CacheRepository $cache,
CreationService $creationService,
DeletionService $deletionService,
@ -97,6 +123,8 @@ class NodesController extends Controller
UpdateService $updateService
) {
$this->alert = $alert;
$this->allocationRepository = $allocationRepository;
$this->assignmentService = $assignmentService;
$this->cache = $cache;
$this->creationService = $creationService;
$this->deletionService = $deletionService;
@ -139,7 +167,7 @@ class NodesController extends Controller
/**
* Post controller to create a new node on the system.
*
* @param \Pterodactyl\Http\Requests\Admin\NodeFormRequest $request
* @param \Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
@ -224,8 +252,8 @@ class NodesController extends Controller
/**
* Updates settings for a node.
*
* @param \Pterodactyl\Http\Requests\Admin\NodeFormRequest $request
* @param \Pterodactyl\Models\Node $node
* @param \Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest $request
* @param \Pterodactyl\Models\Node $node
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\DisplayException
@ -242,12 +270,11 @@ class NodesController extends Controller
/**
* Removes a single allocation from a node.
*
* @param \Illuminate\Http\Request $request
* @param int $node
* @param int $allocation
* @param int $node
* @param int $allocation
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/
public function allocationRemoveSingle(Request $request, $node, $allocation)
public function allocationRemoveSingle($node, $allocation)
{
$query = Allocation::where('node_id', $node)->whereNull('server_id')->where('id', $allocation)->delete();
if ($query < 1) {
@ -284,55 +311,35 @@ class NodesController extends Controller
/**
* Sets an alias for a specific allocation on a node.
*
* @param \Illuminate\Http\Request $request
* @param int $node
* @return \Illuminate\Http\Response
* @param \Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function allocationSetAlias(Request $request, $node)
public function allocationSetAlias(AllocationAliasFormRequest $request)
{
if (! $request->input('allocation_id')) {
return response('Missing required parameters.', 422);
}
$this->allocationRepository->update($request->input('allocation_id'), [
'ip_alias' => (empty($request->input('alias'))) ? null : $request->input('alias'),
]);
try {
$update = Allocation::findOrFail($request->input('allocation_id'));
$update->ip_alias = (empty($request->input('alias'))) ? null : $request->input('alias');
$update->save();
return response('', 204);
} catch (\Exception $ex) {
throw $ex;
}
return response('', 204);
}
/**
* Creates new allocations on a node.
*
* @param \Illuminate\Http\Request $request
* @param int $node
* @param \Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest $request
* @param int|\Pterodactyl\Models\Node $node
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function createAllocation(Request $request, $node)
public function createAllocation(AllocationFormRequest $request, Node $node)
{
$repo = new NodeRepository;
$this->assignmentService->handle($node, $request->normalize());
$this->alert->success($this->translator->trans('admin/node.notices.allocations_added'))->flash();
try {
$repo->addAllocations($node, $request->intersect(['allocation_ip', 'allocation_alias', 'allocation_ports']));
Alert::success('Successfully added new allocations!')->flash();
} catch (DisplayValidationException $ex) {
return redirect()
->route('admin.nodes.view.allocation', $node)
->withErrors(json_decode($ex->getMessage()))
->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An unhandled exception occured while attempting to add allocations this node. This error has been logged.')
->flash();
}
return redirect()->route('admin.nodes.view.allocation', $node);
return redirect()->route('admin.nodes.view.allocation', $node->id);
}
/**

View file

@ -26,7 +26,7 @@ namespace Pterodactyl\Http\Requests\Admin;
use Pterodactyl\Models\Location;
class LocationRequest extends AdminFormRequest
class LocationFormRequest extends AdminFormRequest
{
/**
* Setup the validation rules to use for these requests.
@ -36,7 +36,7 @@ class LocationRequest extends AdminFormRequest
public function rules()
{
if ($this->method() === 'PATCH') {
return Location::getUpdateRulesForId($this->location->id);
return Location::getUpdateRulesForId($this->route()->parameter('location')->id);
}
return Location::getCreateRules();

View file

@ -0,0 +1,41 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Http\Requests\Admin\Node;
use Pterodactyl\Http\Requests\Admin\AdminFormRequest;
class AllocationAliasFormRequest extends AdminFormRequest
{
/**
* @return array
*/
public function rules()
{
return [
'alias' => 'required|nullable|string',
'allocation_id' => 'required|numeric|exists:allocations,id',
];
}
}

View file

@ -0,0 +1,42 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Http\Requests\Admin\Node;
use Pterodactyl\Http\Requests\Admin\AdminFormRequest;
class AllocationFormRequest extends AdminFormRequest
{
/**
* @return array
*/
public function rules()
{
return [
'allocation_ip' => 'required|string',
'allocation_alias' => 'sometimes|string|max:255',
'allocation_ports' => 'required|array',
];
}
}

View file

@ -22,9 +22,10 @@
* SOFTWARE.
*/
namespace Pterodactyl\Http\Requests\Admin;
namespace Pterodactyl\Http\Requests\Admin\Node;
use Pterodactyl\Models\Node;
use Pterodactyl\Http\Requests\Admin\AdminFormRequest;
class NodeFormRequest extends AdminFormRequest
{

View file

@ -34,7 +34,7 @@ class UserFormRequest extends AdminFormRequest
public function rules()
{
if ($this->method() === 'PATCH') {
return User::getUpdateRulesForId($this->user->id);
return User::getUpdateRulesForId($this->route()->parameter('user')->id);
}
return User::getCreateRules();