Skip to content

Commit

Permalink
so fucking much
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Nov 6, 2023
1 parent 4b39f3d commit 9278967
Show file tree
Hide file tree
Showing 40 changed files with 984 additions and 772 deletions.
8 changes: 6 additions & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxSingleQuote": true,
"printWidth": 120,
"printWidth": 80,
"proseWrap": "preserve",
"quoteProps": "consistent",
"requirePragma": false,
Expand All @@ -16,5 +16,9 @@
"trailingComma": "es5",
"useTabs": false,
"vueIndentScriptAndStyle": false,
"tabWidth": 4
"tabWidth": 4,
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"importOrder": ["^@/api/(.*)$", "^@/components/elements/(.*)$", "^@/components/(.*)$", "^[./]"],
"plugins": ["@trivago/prettier-plugin-sort-imports"]
}
67 changes: 40 additions & 27 deletions app/Http/Controllers/Admin/AddressPools/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
namespace Convoy\Http\Controllers\Admin\AddressPools;

use Convoy\Models\Address;
use Illuminate\Support\Facades\Log;
use Convoy\Enums\Network\AddressType;
use Convoy\Services\Servers\NetworkService;
use Convoy\Jobs\Server\SyncNetworkSettings;
use Illuminate\Database\ConnectionInterface;
use Convoy\Transformers\Admin\AddressTransformer;
use Convoy\Services\Nodes\Addresses\AddressHelpers;
use Convoy\Repositories\Eloquent\AddressRepository;
use Convoy\Http\Controllers\ApplicationApiController;
use Convoy\Models\AddressPool;
use Convoy\Models\Filters\AllowedNullableFilter;
use Convoy\Models\Filters\FiltersAddress;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Convoy\Exceptions\Repository\Proxmox\ProxmoxConnectionException;
use Convoy\Http\Requests\Admin\AddressPools\Addresses\StoreAddressRequest;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Convoy\Http\Requests\Admin\AddressPools\Addresses\UpdateAddressRequest;

class AddressController extends ApplicationApiController
{
public function __construct(private NetworkService $networkService, private AddressHelpers $addressHelpers, private ConnectionInterface $connection)
public function __construct(private NetworkService $networkService, private AddressRepository $repository, private ConnectionInterface $connection)
{
}

Expand All @@ -46,27 +45,9 @@ public function index(Request $request, AddressPool $addressPool)

public function store(StoreAddressRequest $request, AddressPool $addressPool)
{
/** @var Address|Address[] $address */
$address = $this->connection->transaction(function () use ($request, $addressPool) {
if ($request->boolean('is_bulk_action')) {
$addressesToAdd = $this->addressHelpers->expandIpRange(
$request->enum('type', AddressType::class),
$request->starting_address,
$request->ending_address,
);

foreach ($addressesToAdd as &$address) {
$address = [
'address' => $address,
'address_pool_id' => $addressPool->id,
...$request->safe()->only(['server_id', 'type', 'cidr', 'gateway', 'mac_address'])
];
}




} else {
if (!$request->boolean('is_bulk_action')) {
/** @var Address $address */
$address = $this->connection->transaction(function () use ($request, $addressPool) {
$address = $addressPool->addresses()->create($request->validated());

if ($request->server_id) {
Expand All @@ -80,10 +61,42 @@ public function store(StoreAddressRequest $request, AddressPool $addressPool)
}

return $address;
});

return fractal($address, new AddressTransformer)->parseIncludes($request->include)->respond();
}

if ($request->boolean('is_bulk_action')) {
$this->connection->transaction(function () use ($request, $addressPool) {
if ($request->enum('type', AddressType::class) === AddressType::IPV4) {
$this->repository->bulkCreateIPv4Addresses(
$request->starting_address,
$request->ending_address,
$addressPool->id,
$request->server_id,
$request->cidr,
$request->gateway,
$request->mac_address,
);
} else {
$this->repository->bulkCreateIPv6Addresses(
$request->starting_address,
$request->ending_address,
$addressPool->id,
$request->server_id,
$request->cidr,
$request->gateway,
$request->mac_address,
);
}
});

if (!is_null($request->server_id)) {
SyncNetworkSettings::dispatch($request->integer('server_id'));
}
});

return fractal($address, new AddressTransformer)->parseIncludes($request->include)->respond();
return $this->returnNoContent();
}
}

public function update(UpdateAddressRequest $request, AddressPool $addressPool, Address $address)
Expand Down
125 changes: 61 additions & 64 deletions app/Http/Controllers/Admin/Nodes/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,99 @@

namespace Convoy\Http\Controllers\Admin\Nodes;

use Convoy\Models\Node;
use Convoy\Models\Address;
use Illuminate\Http\Request;
use Convoy\Models\AddressPool;
use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter;
use Convoy\Http\Controllers\Controller;
use Convoy\Http\Requests\Admin\Nodes\Addresses\StoreAddressRequest;
use Convoy\Http\Requests\Admin\Nodes\Addresses\UpdateAddressRequest;
use Convoy\Models\Filters\AllowedNullableFilter;
use Convoy\Models\Filters\FiltersAddress;
use Convoy\Models\Address;
use Convoy\Models\Node;
use Convoy\Services\Servers\NetworkService;
use Illuminate\Database\ConnectionInterface;
use Convoy\Models\Filters\AllowedNullableFilter;
use Convoy\Transformers\Admin\AddressTransformer;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
use Convoy\Exceptions\Repository\Proxmox\ProxmoxConnectionException;
use Convoy\Http\Requests\Admin\AddressPools\Addresses\UpdateAddressRequest;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;

class AddressController extends Controller
{
public function __construct(private NetworkService $networkService)
public function __construct(private NetworkService $networkService, private ConnectionInterface $connection)
{
}

public function index(Request $request, Node $node)
{
$addresses = QueryBuilder::for(Address::query())
$addresses = QueryBuilder::for($node->addresses())
->with('server')
->where('ip_addresses.node_id', $node->id)
->allowedFilters(['address', AllowedFilter::exact('type'), AllowedFilter::custom('*', new FiltersAddress), AllowedNullableFilter::exact('server_id')])
->defaultSort('-id')
->allowedFilters(
['address', AllowedFilter::exact('type'), AllowedFilter::custom(
'*',
new FiltersAddress,
), AllowedNullableFilter::exact('server_id')],
)
->paginate(min($request->query('per_page', 50), 100))->appends($request->query());

return fractal($addresses, new AddressTransformer)->parseIncludes($request->include)->respond();
}

public function store(StoreAddressRequest $request, Node $node)
{
$address = Address::create(array_merge(['node_id' => $node->id], $request->validated()));

$address->load('server');

try {
if ($request->server_id) {
$this->networkService->syncSettings($address->server);
}
} catch (Exception $e) {
// do nothing
}

return fractal($address, new AddressTransformer)->parseIncludes(['server'])->respond();
}

public function update(UpdateAddressRequest $request, Node $node, Address $address)
{
$address->load('server');

$oldServer = $address->server;

$address->update($request->validated());
$address = $this->connection->transaction(function () use ($request, $address) {
$oldLinkedServer = $address->server;

$address->refresh();
$address->update($request->validated());

$newServer = $address->server;

try {
if ($oldServer?->id !== $newServer?->id) {
if ($oldServer) {
$this->networkService->syncSettings($oldServer);
try {
// Detach old server
if ($oldLinkedServer) {
$this->networkService->syncSettings($oldLinkedServer);
}

if ($newServer) {
$this->networkService->syncSettings($newServer);
// Attach new server
if ($address->server) {
$this->networkService->syncSettings($address->server);
}
} catch (ProxmoxConnectionException) {
if ($oldLinkedServer && !$address->server) {
throw new ServiceUnavailableHttpException(
message: "Server {$oldLinkedServer->uuid} failed to sync network settings.",
);
} elseif (!$oldLinkedServer && $address->server) {
throw new ServiceUnavailableHttpException(
message: "Server {$address->server->uuid} failed to sync network settings.",
);
} elseif ($oldLinkedServer && $address->server) {
throw new ServiceUnavailableHttpException(
message: "Servers {$oldLinkedServer->uuid} and {$address->server->uuid} failed to sync network settings.",
);
}
}
} catch (Exception $e) {
// do nothing
}

$address->load('server');
return $address;
});

return fractal($address, new AddressTransformer)->parseIncludes(['server'])->respond();
return fractal($address, new AddressTransformer)->parseIncludes($request->include)->respond();
}

public function destroy(Node $node, Address $address): Response
public function destroy(Node $node, Address $address)
{
$address->load('server');

$server = $address->server;

$address->delete();

try {
if ($server) {
$this->networkService->syncSettings($server);
$this->connection->transaction(function () use ($address) {
$address->delete();

if ($address->server) {
try {
$this->networkService->syncSettings($address->server);
} catch (ProxmoxConnectionException) {
throw new ServiceUnavailableHttpException(
message: "Server {$address->server->uuid} failed to sync network settings.",
);
}
}
} catch (Exception $e) {
// do nothing
}
});

return response()->noContent();
return $this->returnNoContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ public function rules(): array

public function withValidator(Validator $validator): void
{
if (!$this->boolean('is_bulk_action')) {
$pool = $this->parameter('address_pool', AddressPool::class);
$rules = [];

$rules = [
new ValidateAddressType($this->enum('type', AddressType::class), ['address', 'gateway']),
];
if ($this->boolean('is_bulk_action')) {
$rules[] = new ValidateAddressType($this->enum('type', AddressType::class), ['starting_address', 'ending_address', 'gateway']);
}

if (!$this->boolean('is_bulk_action')) {
$pool = $this->parameter('address_pool', AddressPool::class);
$rules[] = new ValidateAddressType($this->enum('type', AddressType::class), ['address', 'gateway']);
$rules[] = new ValidateAddressUniqueness($pool->id);

$validator->after($rules);
}

$validator->after($rules);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Convoy\Models\AddressPool;
use Illuminate\Validation\Validator;
use Convoy\Http\Requests\FormRequest;
use Convoy\Enums\Network\AddressType;
use Convoy\Validation\ValidateAddressType;
use Convoy\Validation\ValidateAddressUniqueness;

Expand All @@ -25,8 +26,8 @@ public function withValidator(Validator $validator): void
$address = $this->parameter('address', Address::class);

$validator->after([
new ValidateAddressType,
new ValidateAddressUniqueness($pool->id, $address->address),
new ValidateAddressType($this->enum('type', AddressType::class), ['address']),
new ValidateAddressUniqueness($pool->id, $address->address),
]);
}
}
Loading

0 comments on commit 9278967

Please sign in to comment.