Skip to content

Commit

Permalink
validate whether the user is detaching a node that's using IPs from t…
Browse files Browse the repository at this point in the history
…he pool
  • Loading branch information
ericwang401 committed Nov 12, 2023
1 parent c28294d commit e6cec05
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions app/Http/Requests/Admin/AddressPools/UpdateAddressPoolRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace Convoy\Http\Requests\Admin\AddressPools;

use Convoy\Models\Node;
use Convoy\Models\AddressPool;
use Illuminate\Validation\Validator;
use Convoy\Http\Requests\FormRequest;

class UpdateAddressPoolRequest extends FormRequest
{
public function rules(): array
{
/** @var AddressPool $addressPool */
$addressPool = $this->parameter('address_pool', AddressPool::class);

return [
Expand All @@ -20,7 +23,36 @@ public function rules(): array

public function after(): array
{
// TODO: if nodes are removed, check whether their servers are using any of IPs from this pool before unmounting
return [];
/** @var AddressPool $addressPool */
$addressPool = $this->parameter('address_pool', AddressPool::class);

return [
function (Validator $validator) use ($addressPool) {
/** @var int[] $nodeIdsToSync */
if ($nodeIdsToSync = $this->node_ids) {
$existingAttachedNodeIds = $addressPool->nodes()->pluck('id');

$nodeIdsRemoved = $existingAttachedNodeIds->diff($nodeIdsToSync);

$isAddressesAllocated = Node::whereIn('nodes.id', $nodeIdsRemoved)->join(
'servers',
'nodes.id',
'=',
'servers.node_id',
)
->join('ip_addresses', 'servers.id', '=', 'ip_addresses.server_id')
->where(
'ip_addresses.address_pool_id',
'=',
$addressPool->id,
)
->exists();

if ($isAddressesAllocated) {
$validator->errors()->add('node_ids', 'Cannot detach nodes with servers using addresses from this pool.');
}
}
},
];
}
}

0 comments on commit e6cec05

Please sign in to comment.