Skip to content

Commit 54bef1e

Browse files
committed
Basic allocation information
Allows deleting ports, nothing else yet
1 parent 2160613 commit 54bef1e

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

app/Http/Controllers/Admin/NodesController.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ public function postNew(Request $request)
6767
public function getView(Request $request, $id)
6868
{
6969
$node = Models\Node::findOrFail($id);
70+
$allocations = [];
71+
$alloc = Models\Allocation::select('ip', 'port', 'assigned_to')->where('node', $node->id)->get();
72+
if ($alloc) {
73+
foreach($alloc as &$alloc) {
74+
if (!array_key_exists($alloc->ip, $allocations)) {
75+
$allocations[$alloc->ip] = [[
76+
'port' => $alloc->port,
77+
'assigned_to' => $alloc->assigned_to
78+
]];
79+
} else {
80+
array_push($allocations[$alloc->ip], [
81+
'port' => $alloc->port,
82+
'assigned_to' => $alloc->assigned_to
83+
]);
84+
}
85+
}
86+
}
7087
return view('admin.nodes.view', [
7188
'node' => $node,
7289
'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail', 'services.name as a_serviceName')
@@ -75,6 +92,7 @@ public function getView(Request $request, $id)
7592
->where('node', $id)->paginate(10),
7693
'stats' => Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node', $node->id)->first(),
7794
'locations' => Models\Location::all(),
95+
'allocations' => json_decode(json_encode($allocations), false),
7896
]);
7997
}
8098

@@ -104,4 +122,16 @@ public function postView(Request $request, $id)
104122
])->withInput();
105123
}
106124

125+
public function deletePortAllocation(Request $request, $id, $ip, $port)
126+
{
127+
$allocation = Models\Allocation::where('node', $id)->whereNull('assigned_to')->where('ip', $ip)->where('port', $port)->first();
128+
if (!$allocation) {
129+
return response()->json([
130+
'error' => 'Unable to find an allocation matching those details to delete.'
131+
], 400);
132+
}
133+
$allocation->delete();
134+
return response('', 204);
135+
}
136+
107137
}

app/Http/Routes/AdminRoutes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ public function map(Router $router) {
172172
'uses' => 'Admin\NodesController@postView'
173173
]);
174174

175+
$router->delete('/view/{id}/allocation/{ip}/{port}', [
176+
'uses' => 'Admin\NodesController@deletePortAllocation'
177+
]);
178+
175179
});
176180

177181
}

resources/views/admin/nodes/view.blade.php

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,42 @@
285285
<div class="panel panel-default">
286286
<div class="panel-heading"></div>
287287
<div class="panel-body">
288-
Allocations
288+
<table class="table table-striped table-bordered table-hover">
289+
<thead>
290+
<td>IP Address</td>
291+
<td>Ports</td>
292+
<td></td>
293+
</thead>
294+
<tbody>
295+
@foreach($allocations as $ip => $ports)
296+
<tr>
297+
<td>{{ $ip }}</td>
298+
<td>
299+
@foreach($ports as $id => $allocation)
300+
@if (($id % 2) === 0)
301+
@if($allocation->assigned_to === null)
302+
<span style="cursor:pointer" data-action="delete" data-ip="{{ $ip }}" data-port="{{ $allocation->port }}"><i class="fa fa-fw fa-square-o"></i> {{ $allocation->port }} <br /></span>
303+
@else
304+
<i class="fa fa-fw fa-check-square-o"></i> {{ $allocation->port }} <br />
305+
@endif
306+
@endif
307+
@endforeach
308+
</td>
309+
<td>
310+
@foreach($ports as $id => $allocation)
311+
@if (($id % 2) === 1)
312+
@if($allocation->assigned_to === null)
313+
<span style="cursor:pointer" data-action="delete" data-ip="{{ $ip }}" data-port="{{ $allocation->port }}"><i class="fa fa-fw fa-square-o"></i> {{ $allocation->port }} <br /></span>
314+
@else
315+
<i class="fa fa-fw fa-check-square-o"></i> {{ $allocation->port }} <br />
316+
@endif
317+
@endif
318+
@endforeach
319+
</td>
320+
</tr>
321+
@endforeach
322+
</tbody>
323+
</table>
289324
</div>
290325
</div>
291326
</div>
@@ -600,6 +635,50 @@
600635
});
601636
});
602637
638+
$('span[data-action="delete"]').hover(function() {
639+
$(this).find('i').css('color', '#d9534f').removeClass('fa-square-o').addClass('fa-minus-square');
640+
}, function () {
641+
$(this).find('i').css('color', 'inherit').addClass('fa-square-o').removeClass('fa-minus-square');
642+
});
643+
644+
$('span[data-action="delete"]').click(function (event) {
645+
event.preventDefault();
646+
var element = $(this);
647+
var deleteIp = $(this).data('ip');
648+
var deletePort = $(this).data('port');
649+
swal({
650+
title: '',
651+
text: 'Are you sure you want to delete this port?',
652+
type: 'warning',
653+
showCancelButton: true,
654+
allowOutsideClick: true,
655+
closeOnConfirm: false,
656+
confirmButtonText: 'Delete',
657+
confirmButtonColor: '#d9534f',
658+
showLoaderOnConfirm: true
659+
}, function () {
660+
$.ajax({
661+
method: 'DELETE',
662+
url: '{{ route('admin.nodes.view', $node->id) }}/allocation/' + deleteIp + '/' + deletePort,
663+
headers: {
664+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
665+
}
666+
}).done(function (data) {
667+
swal({
668+
type: 'success',
669+
title: 'Port Deleted!',
670+
});
671+
}).fail(function (jqXHR) {
672+
console.error(jqXHR);
673+
swal({
674+
title: 'Whoops!',
675+
text: jqXHR.responseJSON.error,
676+
type: 'error'
677+
});
678+
});
679+
});
680+
});
681+
603682
});
604683
</script>
605684
@endsection

0 commit comments

Comments
 (0)