Skip to content

Commit

Permalink
add sql query to insert ips in bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Sep 3, 2023
1 parent c2d6c03 commit cb827d3
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function store(StoreAddressRequest $request, AddressPool $addressPool)
];
}




} else {
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/Admin/Nodes/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public function index(Request $request)
$nodes = QueryBuilder::for(Node::query())
->withCount(['servers'])
->allowedFilters(
[AllowedFilter::exact('id'), 'name', 'fqdn', AllowedFilter::exact('location_id'), AllowedFilter::custom('*', new FiltersNode)],
[AllowedFilter::exact('id'), 'name', 'fqdn', AllowedFilter::exact('location_id'), AllowedFilter::custom(
'*',
new FiltersNode,
)],
)
->paginate(min($request->query('per_page', 50), 100))->appends($request->query());

Expand Down
118 changes: 118 additions & 0 deletions app/Repositories/Eloquent/AddressRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Convoy\Repositories\Eloquent;

use Convoy\Models\Address;
use Illuminate\Support\Facades\DB;
use Convoy\Enums\Network\AddressType;
use Convoy\Repositories\Eloquent\EloquentRepository;

class AddressRepository extends EloquentRepository
{
public function model(): string
{
return Address::class;
}

public function createIPv4AddressesInBulk(
string $startingAddress,
string $endingAddress,
int $addressPoolId,
?int $serverId,
int $cidr,
string $gateway,
?string $macAddress,
): void
{
$query = <<<'SQL'
INSERT INTO ip_addresses (address_pool_id, server_id, type, address, cidr, gateway, mac_address, created_at)
WITH RECURSIVE ips_to_insert AS (
SELECT INET_NTOA(INET_ATON(:starting_address)) AS ip
UNION ALL
SELECT INET_NTOA(INET_ATON(ip) + 1)
FROM ips_to_insert
WHERE INET_ATON(ip) < INET_ATON(:ending_address)
)
SELECT
:address_pool_id AS address_pool_id,
:server_id AS server_id,
:type AS type,
r.ip AS address,
:cidr AS cidr,
:gateway AS gateway,
:mac_address AS mac_address,
NOW() as created_at
FROM ips_to_insert AS r
LEFT JOIN ip_addresses AS existing_ips_in_pool
ON r.ip = existing_ips_in_pool.address
AND existing_ips_in_pool.address_pool_id = :address_pool_id_2
WHERE existing_ips_in_pool.address IS NULL;
SQL;

DB::select(
$query,
[
'address_pool_id' => $addressPoolId,
'address_pool_id_2' => $addressPoolId,
'server_id' => $serverId,
'type' => AddressType::IPV4->value,
'starting_address' => $startingAddress,
'ending_address' => $endingAddress,
'cidr' => $cidr,
'gateway' => $gateway,
'mac_address' => $macAddress,
],
);
}

public function createIPv6AddressesInBulk(
string $startingAddress,
string $endingAddress,
int $addressPoolId,
?int $serverId,
int $cidr,
string $gateway,
?string $macAddress,
): void
{
$query = <<<'SQL'
INSERT INTO ip_addresses (address_pool_id, server_id, type, address, cidr, gateway, mac_address, created_at)
WITH RECURSIVE ips_to_insert AS (
SELECT INET6_NTOA(INET6_ATON(:starting_address)) AS ip
UNION ALL
SELECT INET6_NTOA(INE6T_ATON(ip) + 1)
FROM ips_to_insert
WHERE INET6_ATON(ip) < INET6_ATON(:ending_address)
)
SELECT
:address_pool_id AS address_pool_id,
:server_id AS server_id,
:type AS type,
r.ip AS address,
:cidr AS cidr,
:gateway AS gateway,
:mac_address AS mac_address,
NOW() as created_at
FROM ips_to_insert AS r
LEFT JOIN ip_addresses AS existing_ips_in_pool
ON r.ip = existing_ips_in_pool.address
AND existing_ips_in_pool.address_pool_id = :address_pool_id_2
WHERE existing_ips_in_pool.address IS NULL;
SQL;

DB::select(
$query,
[
'address_pool_id' => $addressPoolId,
'address_pool_id_2' => $addressPoolId,
'server_id' => $serverId,
'type' => AddressType::IPV4->value,
'starting_address' => $startingAddress,
'ending_address' => $endingAddress,
'cidr' => $cidr,
'gateway' => $gateway,
'mac_address' => $macAddress,
],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement('ALTER TABLE ip_addresses MODIFY COLUMN type varchar(255) AFTER server_id');
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement('ALTER TABLE ip_addresses MODIFY COLUMN type varchar(255) AFTER mac_address');
}
};

0 comments on commit cb827d3

Please sign in to comment.