-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2d6c03
commit cb827d3
Showing
4 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,7 @@ public function store(StoreAddressRequest $request, AddressPool $addressPool) | |
]; | ||
} | ||
|
||
|
||
|
||
|
||
} else { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
database/migrations/2023_09_03_174812_move_types_column_in_ip_addresses_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |