Add datastore_id to OS model and related functionality; update validation and relationships

This commit is contained in:
baodev0
2026-07-21 11:10:45 +07:00
parent c3b5417b19
commit 2d43e95855
9 changed files with 69 additions and 15 deletions
@@ -109,18 +109,19 @@ class IpPoolController extends Controller
}
/**
* Bulk create: ip_address may be comma-separated (e.g. "10.0.0.1,10.0.0.2").
* Bulk create: ip_address may be separated by commas and/or whitespace (e.g. "10.0.0.1, 10.0.0.2 10.0.0.3").
*/
public function store(Request $request): JsonResponse
{
$request->validate([
'ip_address' => 'required|string',
'module_network_id' => 'required|integer|exists:module_network,id',
'gateway' => 'nullable|string|max:45',
]);
$networkId = (int) $request->module_network_id;
$rawIps = $request->ip_address;
$ips = array_filter(array_map('trim', explode(',', $rawIps)));
$ips = array_filter(array_map('trim', preg_split('/[\s,]+/', $rawIps)));
if (empty($ips)) {
return response()->json(['error' => true, 'message' => 'Không có địa chỉ IP hợp lệ'], 422);
@@ -161,6 +162,7 @@ class IpPoolController extends Controller
$created[] = IpPool::create([
'ip_address' => $ip,
'module_network_id' => $networkId,
'gateway' => $request->gateway,
]);
}
DB::commit();
@@ -183,6 +185,7 @@ class IpPoolController extends Controller
$request->validate([
'ip_address' => 'required|string|max:45',
'module_network_id' => 'required|integer|exists:module_network,id',
'gateway' => 'nullable|string|max:45',
]);
if (!filter_var($request->ip_address, FILTER_VALIDATE_IP)) {
@@ -204,7 +207,7 @@ class IpPoolController extends Controller
DB::beginTransaction();
try {
$pool->update($request->only(['ip_address', 'module_network_id']));
$pool->update($request->only(['ip_address', 'module_network_id', 'gateway']));
DB::commit();
return response()->json(['error' => false, 'data' => $pool->fresh('network')]);
@@ -278,7 +281,8 @@ class IpPoolController extends Controller
public function networks(): JsonResponse
{
$networks = ModuleNetwork::select(['id', 'name', 'type', 'network'])
$networks = ModuleNetwork::select(['id', 'name', 'type', 'network', 'module_host_id'])
->with(['host:id,name,module_datacenter_id', 'host.datacenter:id,name'])
->orderBy('name')
->get();