From 54bef1e7d5948865168e1e915092b08cbfc2118b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 8 Jan 2016 20:01:18 -0500 Subject: [PATCH] Basic allocation information Allows deleting ports, nothing else yet --- .../Controllers/Admin/NodesController.php | 30 +++++++ app/Http/Routes/AdminRoutes.php | 4 + resources/views/admin/nodes/view.blade.php | 81 ++++++++++++++++++- 3 files changed, 114 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Admin/NodesController.php b/app/Http/Controllers/Admin/NodesController.php index 402cd6659..8b97e7f92 100644 --- a/app/Http/Controllers/Admin/NodesController.php +++ b/app/Http/Controllers/Admin/NodesController.php @@ -67,6 +67,23 @@ class NodesController extends Controller public function getView(Request $request, $id) { $node = Models\Node::findOrFail($id); + $allocations = []; + $alloc = Models\Allocation::select('ip', 'port', 'assigned_to')->where('node', $node->id)->get(); + if ($alloc) { + foreach($alloc as &$alloc) { + if (!array_key_exists($alloc->ip, $allocations)) { + $allocations[$alloc->ip] = [[ + 'port' => $alloc->port, + 'assigned_to' => $alloc->assigned_to + ]]; + } else { + array_push($allocations[$alloc->ip], [ + 'port' => $alloc->port, + 'assigned_to' => $alloc->assigned_to + ]); + } + } + } return view('admin.nodes.view', [ 'node' => $node, 'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail', 'services.name as a_serviceName') @@ -75,6 +92,7 @@ class NodesController extends Controller ->where('node', $id)->paginate(10), 'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first(), 'locations' => Models\Location::all(), + 'allocations' => json_decode(json_encode($allocations), false), ]); } @@ -104,4 +122,16 @@ class NodesController extends Controller ])->withInput(); } + public function deletePortAllocation(Request $request, $id, $ip, $port) + { + $allocation = Models\Allocation::where('node', $id)->whereNull('assigned_to')->where('ip', $ip)->where('port', $port)->first(); + if (!$allocation) { + return response()->json([ + 'error' => 'Unable to find an allocation matching those details to delete.' + ], 400); + } + $allocation->delete(); + return response('', 204); + } + } diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index bf4131a1e..ffc21220c 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -172,6 +172,10 @@ class AdminRoutes { 'uses' => 'Admin\NodesController@postView' ]); + $router->delete('/view/{id}/allocation/{ip}/{port}', [ + 'uses' => 'Admin\NodesController@deletePortAllocation' + ]); + }); } diff --git a/resources/views/admin/nodes/view.blade.php b/resources/views/admin/nodes/view.blade.php index fd6357c73..8880ee6dc 100644 --- a/resources/views/admin/nodes/view.blade.php +++ b/resources/views/admin/nodes/view.blade.php @@ -285,7 +285,42 @@
- Allocations + + + + + + + + @foreach($allocations as $ip => $ports) + + + + + + @endforeach + +
IP AddressPorts
{{ $ip }} + @foreach($ports as $id => $allocation) + @if (($id % 2) === 0) + @if($allocation->assigned_to === null) + {{ $allocation->port }}
+ @else + {{ $allocation->port }}
+ @endif + @endif + @endforeach +
+ @foreach($ports as $id => $allocation) + @if (($id % 2) === 1) + @if($allocation->assigned_to === null) + {{ $allocation->port }}
+ @else + {{ $allocation->port }}
+ @endif + @endif + @endforeach +
@@ -600,6 +635,50 @@ $(document).ready(function () { }); }); + $('span[data-action="delete"]').hover(function() { + $(this).find('i').css('color', '#d9534f').removeClass('fa-square-o').addClass('fa-minus-square'); + }, function () { + $(this).find('i').css('color', 'inherit').addClass('fa-square-o').removeClass('fa-minus-square'); + }); + + $('span[data-action="delete"]').click(function (event) { + event.preventDefault(); + var element = $(this); + var deleteIp = $(this).data('ip'); + var deletePort = $(this).data('port'); + swal({ + title: '', + text: 'Are you sure you want to delete this port?', + type: 'warning', + showCancelButton: true, + allowOutsideClick: true, + closeOnConfirm: false, + confirmButtonText: 'Delete', + confirmButtonColor: '#d9534f', + showLoaderOnConfirm: true + }, function () { + $.ajax({ + method: 'DELETE', + url: '{{ route('admin.nodes.view', $node->id) }}/allocation/' + deleteIp + '/' + deletePort, + headers: { + 'X-CSRF-TOKEN': '{{ csrf_token() }}' + } + }).done(function (data) { + swal({ + type: 'success', + title: 'Port Deleted!', + }); + }).fail(function (jqXHR) { + console.error(jqXHR); + swal({ + title: 'Whoops!', + text: jqXHR.responseJSON.error, + type: 'error' + }); + }); + }); + }); + }); @endsection