Skip to content

Commit

Permalink
add database changes for Coterm & base controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Nov 12, 2023
1 parent e6cec05 commit f3e6356
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 9 deletions.
30 changes: 30 additions & 0 deletions app/Http/Controllers/Admin/CotermController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Convoy\Http\Controllers\Admin;

use Illuminate\Http\Request;

class CotermController
{
public function index()
{

}

public function store(Request $request)
{

}

public function show($id)
{
}

public function update(Request $request, $id)
{
}

public function destroy($id)
{
}
}
3 changes: 0 additions & 3 deletions app/Models/AddressPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class AddressPool extends Model
{
/**
* Fields that aren't mass assignable
*/
protected $guarded = ['id', 'updated_at', 'created_at'];

public static $validationRules = [
Expand Down
42 changes: 42 additions & 0 deletions app/Models/Coterm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Convoy\Models;

use Convoy\Casts\NullableEncrypter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Coterm extends Model
{
public const COTERM_TOKEN_ID_LENGTH = 16;
public const COTERM_TOKEN_LENGTH = 64;

protected $guarded = [
'id',
'created_at',
'updated_at',
];

protected $hidden = [
'token_id', 'token',
];

protected $casts = [
'is_tls_enabled' => 'boolean',
'coterm_token' => NullableEncrypter::class,
];

public static $validationRules = [
'is_tls_enabled' => 'required|boolean',
'fqdn' => 'required|string|max:191',
'port' => 'required|integer|min:1|max:65535',
'token_id' => 'required|string|max:191',
'token' => 'required|string|max:191',
];

public function nodes(): HasMany
{
return $this->hasMany(Node::class);
}
}
16 changes: 10 additions & 6 deletions app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class Node extends Model
Expand Down Expand Up @@ -60,12 +61,7 @@ class Node extends Model
'backup_storage' => ['required', 'string', 'max:191', 'regex:/^\S*$/u'],
'iso_storage' => ['required', 'string', 'max:191', 'regex:/^\S*$/u'],
'network' => ['required', 'string', 'max:191', 'regex:/^\S*$/u'],
'coterm_enabled' => 'sometimes|boolean',
'coterm_tls_enabled' => 'sometimes|boolean',
'coterm_fqdn' => 'sometimes|nullable|string|max:191',
'coterm_port' => 'sometimes|integer',
'coterm_token_id' => 'required_if:coterm_enabled,1',
'coterm_token' => 'required_if:coterm_enabled,1',
'coterm_id' => 'required|nullable|integer|exists:coterms,id',
];

/**
Expand Down Expand Up @@ -136,6 +132,14 @@ public function location(): BelongsTo
return $this->belongsTo(Location::class);
}

/**
* Gets the instance of Coterm that's connected with this node.
*/
public function coterm(): BelongsTo
{
return $this->belongsTo(Coterm::class);
}

/**
* Gets the total disk used from adding up all the associated servers' disk sizes.
*/
Expand Down
26 changes: 26 additions & 0 deletions database/migrations/2023_11_11_175741_create_coterms_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
public function up(): void
{
Schema::create('coterms', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('is_tls_enabled');
$table->string('fqdn');
$table->integer('port');
$table->string('token_id');
$table->string('token');
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('coterms');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
public function up(): void
{
Schema::table('nodes', function (Blueprint $table) {
$table->dropUnique(['coterm_token_id']);
$table->dropColumn(
['coterm_enabled', 'coterm_tls_enabled', 'coterm_fqdn', 'coterm_port', 'coterm_token_id', 'coterm_token'],
);

$table->foreignId('coterm_id')->after('network')->nullable()->constrained()->nullOnDelete();
});
}

public function down(): void
{
Schema::table('nodes', function (Blueprint $table) {
$table->after('network', function (Blueprint $table) {
$table->boolean('coterm_enabled')->default(false);
$table->boolean('coterm_tls_enabled')->default(true);
$table->string('coterm_fqdn')->nullable();
$table->integer('coterm_port')->default(443);
$table->string('coterm_token_id')->nullable()->unique();
$table->text('coterm_token')->nullable();
});

$table->dropForeign(['coterm_id']);
$table->dropColumn('coterm_id');
});
}
};

0 comments on commit f3e6356

Please sign in to comment.