Updated servers for new IP CRUD

Updated servers for new IP CRUD
This commit is contained in:
cp6 2022-02-22 13:30:39 +11:00
parent 226c1ed4c2
commit e15b317bb4
7 changed files with 118 additions and 45 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Models\IPs;
use App\Models\Labels;
use App\Models\OS;
use App\Models\Pricing;
@ -54,7 +55,7 @@ class ServerController extends Controller
->Join('os as o', 's.os_id', '=', 'o.id')
->LeftJoin('yabs as y', 's.id', '=', 'y.server_id')
->LeftJoin('disk_speed as ds', 'y.id', '=', 'ds.id')
->get(['pr.currency', 'pr.price', 'pr.term', 'pr.as_usd', 'pr.next_due_date', 'pr.service_id', 'p.name as provider_name', 'l.name as location', 'o.name as os_name', 'y.*', 'y.id as yabs_id', 'ds.*','s.*']);
->get(['pr.currency', 'pr.price', 'pr.term', 'pr.as_usd', 'pr.next_due_date', 'pr.service_id', 'p.name as provider_name', 'l.name as location', 'o.name as os_name', 'y.*', 'y.id as yabs_id', 'ds.*', 's.*']);
return view('servers.public-index', compact('servers'));
}
@ -74,8 +75,8 @@ class ServerController extends Controller
$request->validate([
'hostname' => 'required|min:5',
'ipv4' => 'nullable|ipv4',
'ipv6' => 'nullable|ipv6',
'ip1' => 'nullable|ip',
'ip2' => 'nullable|ip',
'service_type' => 'numeric',
'server_type' => 'numeric',
'ram' => 'numeric',
@ -94,8 +95,6 @@ class ServerController extends Controller
Server::create([
'id' => $server_id,
'hostname' => $request->hostname,
'ipv4' => $request->ipv4,
'ipv6' => $request->ipv6,
'server_type' => $request->server_type,
'os_id' => $request->os_id,
'ssh' => $request->ssh_port,
@ -115,6 +114,30 @@ class ServerController extends Controller
'was_promo' => $request->was_promo
]);
if (!is_null($request->ip1)) {
IPs::create(
[
'id' => Str::random(8),
'service_id' => $server_id,
'address' => $request->ip1,
'is_ipv4' => (filter_var($request->ip1, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) ? 0 : 1,
'active' => 1
]
);
}
if (!is_null($request->ip2)) {
IPs::create(
[
'id' => Str::random(8),
'service_id' => $server_id,
'address' => $request->ip2,
'is_ipv4' => (filter_var($request->ip2, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) ? 0 : 1,
'active' => 1
]
);
}
$pricing = new Pricing();
$as_usd = $pricing->convertToUSD($request->price, $request->currency);
@ -156,14 +179,18 @@ class ServerController extends Controller
$network_speeds = json_decode(DB::table('network_speed')
->where('network_speed.server_id', '=', $server->id)
->get(),true);
->get(), true);
$ip_addresses = json_decode(DB::table('ips as i')
->where('i.service_id', '=', $server->id)
->get(), true);
$labels = DB::table('labels_assigned as l')
->join('labels', 'l.label_id', '=', 'labels.id')
->where('l.service_id', '=', $server->id)
->get(['labels.label']);
return view('servers.show', compact(['server', 'server_extras', 'network_speeds', 'labels']));
return view('servers.show', compact(['server', 'server_extras', 'network_speeds', 'labels', 'ip_addresses']));
}
public function edit(Server $server)
@ -176,12 +203,17 @@ class ServerController extends Controller
->get(['labels.id', 'labels.label']);
$os = DB::table('os')->get(['*']);
$ip_addresses = json_decode(DB::table('ips as i')
->where('i.service_id', '=', $server->id)
->get(), true);
$server = DB::table('servers as s')
->join('pricings as p', 's.id', '=', 'p.service_id')
->where('s.id', '=', $server->id)
->get(['s.*', 'p.*']);
return view('servers.edit', compact(['server', 'locations', 'providers', 'os', 'labels']));
return view('servers.edit', compact(['server', 'locations', 'providers', 'os', 'labels', 'ip_addresses']));
}
public function update(Request $request, Server $server)
@ -203,8 +235,6 @@ class ServerController extends Controller
->where('id', $request->server_id)
->update([
'hostname' => $request->hostname,
'ipv4' => $request->ipv4,
'ipv6' => $request->ipv6,
'server_type' => $request->server_type,
'os_id' => $request->os_id,
'ssh' => $request->ssh,
@ -252,6 +282,20 @@ class ServerController extends Controller
}
}
$deleted = DB::table('ips')->where('service_id', '=', $server->id)->delete();
for ($i = 1; $i <= 8; $i++) {//Max of 8 ips
$obj = 'ip' . $i;
if (isset($request->$obj) && !is_null($request->$obj)) {
DB::insert('INSERT IGNORE INTO ips (id, address, service_id, is_ipv4) values (?, ?, ?, ?)', [
Str::random(8),
$request->$obj,
$request->server_id,
(filter_var($request->$obj, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) ? 0 : 1
]);
}
}
return redirect()->route('servers.index')
->with('success', 'Server Updated Successfully.');
}
@ -267,6 +311,8 @@ class ServerController extends Controller
Labels::deleteLabelsAssignedTo($server->id);
IPs::deleteIPsAssignedTo($server->id);
return redirect()->route('servers.index')
->with('success', 'Server was deleted Successfully.');
}

View File

@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class IPs extends Model
{
@ -14,4 +15,10 @@ class IPs extends Model
protected $fillable = ['id', 'active', 'service_id', 'address', 'is_ipv4'];
public $incrementing = false;
public static function deleteIPsAssignedTo($service_id)
{
DB::table('ips')->where('service_id', '=', $service_id)->delete();
}
}

View File

@ -19,8 +19,6 @@ class CreateServersTable extends Migration
$table->string('hostname');
$table->string('ns1')->nullable()->default(null);
$table->string('ns2')->nullable()->default(null);
$table->string('ipv4')->nullable()->default(null);
$table->string('ipv6')->nullable()->default(null);
$table->tinyInteger('server_type')->default(1);
$table->integer('os_id')->default(0);
$table->integer('provider_id')->default(9999);

View File

@ -27,7 +27,6 @@ class ServersSeeder extends Seeder
"provider_id" => 90,
"location_id" => 35,
"bandwidth" => 1000,
"ipv4" => '127.0.0.1',
"cpu" => 1,
"ram" => 512,
"ram_type" => 'MB',
@ -46,7 +45,6 @@ class ServersSeeder extends Seeder
"provider_id" => 90,
"location_id" => 71,
"bandwidth" => 2000,
"ipv4" => '127.0.0.1',
"cpu" => 1,
"ram" => 2048,
"ram_type" => 'MB',
@ -88,5 +86,26 @@ class ServersSeeder extends Seeder
];
DB::table('pricings')->insert($pricing);
$ips = [
[
"id" => Str::random(8),
"service_id" => $id1,
"address" => '127.0.0.1',
"is_ipv4" => 1,
"active" => 1,
"created_at" => Carbon::now()
],
[
"id" => Str::random(8),
"service_id" => $id2,
"address" => '127.0.0.1',
"is_ipv4" => 1,
"active" => 1,
"created_at" => Carbon::now()
],
];
DB::table('ips')->insert($ips);
}
}

View File

@ -58,18 +58,19 @@
</div>
</div>
<div class="row">
<p class="text-muted">If you need to add more IPs go <a href="{{route('IPs.create')}}" class="text-decoration-none">here</a>.</p>
<div class="col-12 col-lg-3 mb-4">
<div class="input-group">
<div class="input-group-prepend"><span class="input-group-text">IPv4</span></div>
<input type="text" id="ipv4" name="ipv4" class="form-control" minlength="4"
maxlength="124" v-model="ipv4_in">
<div class="input-group-prepend"><span class="input-group-text">IP</span></div>
<input type="text" name="ip1" class="form-control" minlength="4"
maxlength="255" v-model="ipv4_in">
</div>
</div>
<div class="col-12 col-lg-3 mb-4">
<div class="input-group">
<div class="input-group-prepend"><span class="input-group-text">IPv6</span></div>
<input type="text" id="ipv6" name="ipv6" class="form-control" minlength="4"
maxlength="124" v-model="ipv6_in">
<div class="input-group-prepend"><span class="input-group-text">IP</span></div>
<input type="text" name="ip2" class="form-control" minlength="4"
maxlength="255" v-model="ipv6_in">
</div>
</div>
<div class="col-12 col-lg-3 mb-4">

View File

@ -51,20 +51,6 @@
</div>
</div>
<div class="row">
<div class="col-12 col-lg-3 mb-4">
<div class="input-group">
<div class="input-group-prepend"><span class="input-group-text">IPv4</span></div>
<input type="text" name="ipv4" class="form-control" minlength="4"
maxlength="124" value="{{ $server[0]->ipv4 }}">
</div>
</div>
<div class="col-12 col-lg-3 mb-4">
<div class="input-group">
<div class="input-group-prepend"><span class="input-group-text">IPv6</span></div>
<input type="text" name="ipv6" class="form-control" minlength="4"
maxlength="124" value="{{ $server[0]->ipv6 }}">
</div>
</div>
<div class="col-12 col-lg-3 mb-4">
<x-text-input>
<x-slot name="title">NS1</x-slot>
@ -228,7 +214,7 @@
</x-date-input>
</div>
</div>
<div class="row mb-3">
<div class="row">
<div class="col-12 col-lg-3 mb-4">
<x-labels-select>
<x-slot name="title">label</x-slot>
@ -266,7 +252,19 @@
</x-labels-select>
</div>
</div>
<div class="form-check mt-2">
<div class="row">
@foreach($ip_addresses as $ip)
<div class="col-12 col-lg-3 mb-4">
<x-text-input>
<x-slot name="title">IP</x-slot>
<x-slot name="name">ip{{{$loop->iteration}}}</x-slot>
<x-slot name="max">255</x-slot>
<x-slot name="value">{{ $ip['address'] }}</x-slot>
</x-text-input>
</div>
@endforeach
</div>
<div class="form-check mt-3">
<input class="form-check-input" name="is_active" type="checkbox"
value="1" {{ ($server[0]->active === 1) ? 'checked' : '' }}>
<label class="form-check-label">

View File

@ -76,14 +76,17 @@
<td class="px-2 py-2 font-bold text-muted">Bandwidth</td>
<td>{{ $server_extras[0]->bandwidth }} GB</td>
</tr>
<tr>
<td class="px-2 py-2 font-bold text-muted">IPv4</td>
<td><code>{{ $server_extras[0]->ipv4 }}</code></td>
</tr>
<tr>
<td class="px-2 py-2 font-bold text-muted">IPv6</td>
<td><code>{{ $server_extras[0]->ipv6 }}</code></td>
</tr>
@foreach($ip_addresses as $ip)
<tr>
<td class="px-2 py-2 font-bold text-muted">@if($ip['is_ipv4'])
IPv4
@else
IPv6
@endif
</td>
<td><code>{{ $ip['address'] }}</code></td>
</tr>
@endforeach
<tr>
<td class="px-2 py-2 font-bold text-muted">Was promo</td>
<td>{{ ($server_extras[0]->was_promo === 1) ? 'Yes' : 'No' }}</td>
@ -179,7 +182,8 @@
</table>
</div>
@else
<p>Please <a href="{{ route('yabs.create') }}" class="text-decoration-none">add a YABs</a> to see Geekbench, disk and network speeds</p>
<p>Please <a href="{{ route('yabs.create') }}" class="text-decoration-none">add a YABs</a> to
see Geekbench, disk and network speeds</p>
@endif
</div>
</div>