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; namespace App\Http\Controllers;
use App\Models\IPs;
use App\Models\Labels; use App\Models\Labels;
use App\Models\OS; use App\Models\OS;
use App\Models\Pricing; use App\Models\Pricing;
@ -74,8 +75,8 @@ class ServerController extends Controller
$request->validate([ $request->validate([
'hostname' => 'required|min:5', 'hostname' => 'required|min:5',
'ipv4' => 'nullable|ipv4', 'ip1' => 'nullable|ip',
'ipv6' => 'nullable|ipv6', 'ip2' => 'nullable|ip',
'service_type' => 'numeric', 'service_type' => 'numeric',
'server_type' => 'numeric', 'server_type' => 'numeric',
'ram' => 'numeric', 'ram' => 'numeric',
@ -94,8 +95,6 @@ class ServerController extends Controller
Server::create([ Server::create([
'id' => $server_id, 'id' => $server_id,
'hostname' => $request->hostname, 'hostname' => $request->hostname,
'ipv4' => $request->ipv4,
'ipv6' => $request->ipv6,
'server_type' => $request->server_type, 'server_type' => $request->server_type,
'os_id' => $request->os_id, 'os_id' => $request->os_id,
'ssh' => $request->ssh_port, 'ssh' => $request->ssh_port,
@ -115,6 +114,30 @@ class ServerController extends Controller
'was_promo' => $request->was_promo '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(); $pricing = new Pricing();
$as_usd = $pricing->convertToUSD($request->price, $request->currency); $as_usd = $pricing->convertToUSD($request->price, $request->currency);
@ -158,12 +181,16 @@ class ServerController extends Controller
->where('network_speed.server_id', '=', $server->id) ->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') $labels = DB::table('labels_assigned as l')
->join('labels', 'l.label_id', '=', 'labels.id') ->join('labels', 'l.label_id', '=', 'labels.id')
->where('l.service_id', '=', $server->id) ->where('l.service_id', '=', $server->id)
->get(['labels.label']); ->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) public function edit(Server $server)
@ -176,12 +203,17 @@ class ServerController extends Controller
->get(['labels.id', 'labels.label']); ->get(['labels.id', 'labels.label']);
$os = DB::table('os')->get(['*']); $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') $server = DB::table('servers as s')
->join('pricings as p', 's.id', '=', 'p.service_id') ->join('pricings as p', 's.id', '=', 'p.service_id')
->where('s.id', '=', $server->id) ->where('s.id', '=', $server->id)
->get(['s.*', 'p.*']); ->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) public function update(Request $request, Server $server)
@ -203,8 +235,6 @@ class ServerController extends Controller
->where('id', $request->server_id) ->where('id', $request->server_id)
->update([ ->update([
'hostname' => $request->hostname, 'hostname' => $request->hostname,
'ipv4' => $request->ipv4,
'ipv6' => $request->ipv6,
'server_type' => $request->server_type, 'server_type' => $request->server_type,
'os_id' => $request->os_id, 'os_id' => $request->os_id,
'ssh' => $request->ssh, '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') return redirect()->route('servers.index')
->with('success', 'Server Updated Successfully.'); ->with('success', 'Server Updated Successfully.');
} }
@ -267,6 +311,8 @@ class ServerController extends Controller
Labels::deleteLabelsAssignedTo($server->id); Labels::deleteLabelsAssignedTo($server->id);
IPs::deleteIPsAssignedTo($server->id);
return redirect()->route('servers.index') return redirect()->route('servers.index')
->with('success', 'Server was deleted Successfully.'); ->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\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class IPs extends Model class IPs extends Model
{ {
@ -14,4 +15,10 @@ class IPs extends Model
protected $fillable = ['id', 'active', 'service_id', 'address', 'is_ipv4']; protected $fillable = ['id', 'active', 'service_id', 'address', 'is_ipv4'];
public $incrementing = false; 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('hostname');
$table->string('ns1')->nullable()->default(null); $table->string('ns1')->nullable()->default(null);
$table->string('ns2')->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->tinyInteger('server_type')->default(1);
$table->integer('os_id')->default(0); $table->integer('os_id')->default(0);
$table->integer('provider_id')->default(9999); $table->integer('provider_id')->default(9999);

View File

@ -27,7 +27,6 @@ class ServersSeeder extends Seeder
"provider_id" => 90, "provider_id" => 90,
"location_id" => 35, "location_id" => 35,
"bandwidth" => 1000, "bandwidth" => 1000,
"ipv4" => '127.0.0.1',
"cpu" => 1, "cpu" => 1,
"ram" => 512, "ram" => 512,
"ram_type" => 'MB', "ram_type" => 'MB',
@ -46,7 +45,6 @@ class ServersSeeder extends Seeder
"provider_id" => 90, "provider_id" => 90,
"location_id" => 71, "location_id" => 71,
"bandwidth" => 2000, "bandwidth" => 2000,
"ipv4" => '127.0.0.1',
"cpu" => 1, "cpu" => 1,
"ram" => 2048, "ram" => 2048,
"ram_type" => 'MB', "ram_type" => 'MB',
@ -88,5 +86,26 @@ class ServersSeeder extends Seeder
]; ];
DB::table('pricings')->insert($pricing); 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> </div>
<div class="row"> <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="col-12 col-lg-3 mb-4">
<div class="input-group"> <div class="input-group">
<div class="input-group-prepend"><span class="input-group-text">IPv4</span></div> <div class="input-group-prepend"><span class="input-group-text">IP</span></div>
<input type="text" id="ipv4" name="ipv4" class="form-control" minlength="4" <input type="text" name="ip1" class="form-control" minlength="4"
maxlength="124" v-model="ipv4_in"> maxlength="255" v-model="ipv4_in">
</div> </div>
</div> </div>
<div class="col-12 col-lg-3 mb-4"> <div class="col-12 col-lg-3 mb-4">
<div class="input-group"> <div class="input-group">
<div class="input-group-prepend"><span class="input-group-text">IPv6</span></div> <div class="input-group-prepend"><span class="input-group-text">IP</span></div>
<input type="text" id="ipv6" name="ipv6" class="form-control" minlength="4" <input type="text" name="ip2" class="form-control" minlength="4"
maxlength="124" v-model="ipv6_in"> maxlength="255" v-model="ipv6_in">
</div> </div>
</div> </div>
<div class="col-12 col-lg-3 mb-4"> <div class="col-12 col-lg-3 mb-4">

View File

@ -51,20 +51,6 @@
</div> </div>
</div> </div>
<div class="row"> <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"> <div class="col-12 col-lg-3 mb-4">
<x-text-input> <x-text-input>
<x-slot name="title">NS1</x-slot> <x-slot name="title">NS1</x-slot>
@ -228,7 +214,7 @@
</x-date-input> </x-date-input>
</div> </div>
</div> </div>
<div class="row mb-3"> <div class="row">
<div class="col-12 col-lg-3 mb-4"> <div class="col-12 col-lg-3 mb-4">
<x-labels-select> <x-labels-select>
<x-slot name="title">label</x-slot> <x-slot name="title">label</x-slot>
@ -266,7 +252,19 @@
</x-labels-select> </x-labels-select>
</div> </div>
</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" <input class="form-check-input" name="is_active" type="checkbox"
value="1" {{ ($server[0]->active === 1) ? 'checked' : '' }}> value="1" {{ ($server[0]->active === 1) ? 'checked' : '' }}>
<label class="form-check-label"> <label class="form-check-label">

View File

@ -76,14 +76,17 @@
<td class="px-2 py-2 font-bold text-muted">Bandwidth</td> <td class="px-2 py-2 font-bold text-muted">Bandwidth</td>
<td>{{ $server_extras[0]->bandwidth }} GB</td> <td>{{ $server_extras[0]->bandwidth }} GB</td>
</tr> </tr>
@foreach($ip_addresses as $ip)
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">IPv4</td> <td class="px-2 py-2 font-bold text-muted">@if($ip['is_ipv4'])
<td><code>{{ $server_extras[0]->ipv4 }}</code></td> IPv4
</tr> @else
<tr> IPv6
<td class="px-2 py-2 font-bold text-muted">IPv6</td> @endif
<td><code>{{ $server_extras[0]->ipv6 }}</code></td> </td>
<td><code>{{ $ip['address'] }}</code></td>
</tr> </tr>
@endforeach
<tr> <tr>
<td class="px-2 py-2 font-bold text-muted">Was promo</td> <td class="px-2 py-2 font-bold text-muted">Was promo</td>
<td>{{ ($server_extras[0]->was_promo === 1) ? 'Yes' : 'No' }}</td> <td>{{ ($server_extras[0]->was_promo === 1) ? 'Yes' : 'No' }}</td>
@ -179,7 +182,8 @@
</table> </table>
</div> </div>
@else @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 @endif
</div> </div>
</div> </div>