Skip to content

Commit 3114b7e

Browse files
committed
Complete implementation of new Server model.
1 parent 644ee85 commit 3114b7e

File tree

10 files changed

+170
-202
lines changed

10 files changed

+170
-202
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
2020
* Admin API and base routes for user management now define the fields that should be passed to repositories rather than passing all fields.
2121
* User model now defines mass assignment fields using `$fillable` rather than `$guarded`.
2222
* 2FA checkpoint on login is now its own page, and not an AJAX based call. Improves security on that front.
23+
* Updated Server model code to be more efficient, as well as make life easier for backend changes and work.
2324

24-
### Removed
25+
### Deprecated
2526
* `Server::getUserDaemonSecret(Server $server)` was removed and replaced with `User::daemonSecret(Server $server)` in order to clean up models.
27+
* `Server::getByUUID()` was replaced with `Server::byUuid()` as well as various other functions through-out the Server model.
28+
* `Server::getHeaders()` was removed and replaced with `Server::getClient()` which returns a Guzzle Client with the correct headers already assigned.
2629

2730
## v0.5.6 (Bodacious Boreopterus)
2831
### Added

app/Http/Controllers/API/User/ServerController.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,10 @@ class ServerController extends BaseController
3434
{
3535
public function info(Request $request, $uuid)
3636
{
37-
$server = Models\Server::getByUUID($uuid);
38-
$node = Models\Node::findOrFail($server->node_id);
39-
$client = Models\Node::guzzleRequest($node->id);
37+
$server = Models\Server::byUuid($uuid)->load('allocations');
4038

4139
try {
42-
$response = $client->request('GET', '/server', [
43-
'headers' => [
44-
'X-Access-Token' => $server->daemonSecret,
45-
'X-Access-Server' => $server->uuid,
46-
],
47-
]);
40+
$response = $server->guzzleClient()->request('GET', '/server');
4841

4942
$json = json_decode($response->getBody());
5043
$daemon = [
@@ -59,8 +52,7 @@ public function info(Request $request, $uuid)
5952
Log::error($ex);
6053
}
6154

62-
$allocations = Models\Allocation::select('id', 'ip', 'port', 'ip_alias as alias')->where('assigned_to', $server->id)->get();
63-
foreach ($allocations as &$allocation) {
55+
foreach ($server->allocations as &$allocation) {
6456
$allocation->default = ($allocation->id === $server->allocation_id);
6557
unset($allocation->id);
6658
}
@@ -69,7 +61,7 @@ public function info(Request $request, $uuid)
6961
'uuidShort' => $server->uuidShort,
7062
'uuid' => $server->uuid,
7163
'name' => $server->name,
72-
'node' => $node->name,
64+
'node' => $server->node->name,
7365
'limits' => [
7466
'memory' => $server->memory,
7567
'swap' => $server->swap,
@@ -78,7 +70,7 @@ public function info(Request $request, $uuid)
7870
'cpu' => $server->cpu,
7971
'oom_disabled' => (bool) $server->oom_disabled,
8072
],
81-
'allocations' => $allocations,
73+
'allocations' => $server->allocations,
8274
'sftp' => [
8375
'username' => (Auth::user()->can('view-sftp', $server)) ? $server->username : null,
8476
],
@@ -91,16 +83,10 @@ public function info(Request $request, $uuid)
9183

9284
public function power(Request $request, $uuid)
9385
{
94-
$server = Models\Server::getByUUID($uuid);
95-
$client = Models\Node::guzzleRequest($server->node_id);
96-
86+
$server = Models\Server::byUuid($uuid);
9787
Auth::user()->can('power-' . $request->input('action'), $server);
9888

99-
$res = $client->request('PUT', '/server/power', [
100-
'headers' => [
101-
'X-Access-Server' => $server->uuid,
102-
'X-Access-Token' => $server->daemonSecret,
103-
],
89+
$res = $server->guzzleClient()->request('PUT', '/server/power', [
10490
'exceptions' => false,
10591
'json' => [
10692
'action' => $request->input('action'),

app/Http/Controllers/Server/AjaxController.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,14 @@ public function __construct()
6767
*/
6868
public function getStatus(Request $request, $uuid)
6969
{
70-
$server = Models\Server::getByUUID($uuid);
70+
$server = Models\Server::byUuid($uuid);
7171

7272
if (! $server) {
7373
return response()->json([], 404);
7474
}
7575

76-
$client = Models\Node::guzzleRequest($server->node_id);
77-
7876
try {
79-
$res = $client->request('GET', '/server', [
80-
'headers' => Models\Server::getGuzzleHeaders($uuid),
81-
]);
77+
$res = $server->guzzleClient()->request('GET', '/server');
8278
if ($res->getStatusCode() === 200) {
8379
return response()->json(json_decode($res->getBody()));
8480
}
@@ -98,10 +94,10 @@ public function getStatus(Request $request, $uuid)
9894
*/
9995
public function postDirectoryList(Request $request, $uuid)
10096
{
101-
$server = Models\Server::getByUUID($uuid);
102-
$this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/');
97+
$server = Models\Server::byUuid($uuid);
10398
$this->authorize('list-files', $server);
10499

100+
$this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/');
105101
$prevDir = [
106102
'header' => ($this->directory !== '/') ? $this->directory : '',
107103
];
@@ -149,7 +145,7 @@ public function postDirectoryList(Request $request, $uuid)
149145
*/
150146
public function postSaveFile(Request $request, $uuid)
151147
{
152-
$server = Models\Server::getByUUID($uuid);
148+
$server = Models\Server::byUuid($uuid);
153149
$this->authorize('save-files', $server);
154150

155151
$controller = new Repositories\Daemon\FileRepository($uuid);
@@ -175,7 +171,7 @@ public function postSaveFile(Request $request, $uuid)
175171
*/
176172
public function postSetPrimary(Request $request, $uuid)
177173
{
178-
$server = Models\Server::getByUUID($uuid);
174+
$server = Models\Server::byUuid($uuid)->load('allocations');
179175
$this->authorize('set-connection', $server);
180176

181177
if ((int) $request->input('allocation') === $server->allocation_id) {
@@ -185,7 +181,7 @@ public function postSetPrimary(Request $request, $uuid)
185181
}
186182

187183
try {
188-
$allocation = Models\Allocation::where('id', $request->input('allocation'))->where('assigned_to', $server->id)->first();
184+
$allocation = $server->allocations->where('id', $request->input('allocation'))->where('assigned_to', $server->id)->first();
189185
if (! $allocation) {
190186
return response()->json([
191187
'error' => 'No allocation matching your request was found in the system.',
@@ -217,10 +213,10 @@ public function postSetPrimary(Request $request, $uuid)
217213

218214
public function postResetDatabasePassword(Request $request, $uuid)
219215
{
220-
$server = Models\Server::getByUUID($uuid);
221-
$database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail();
222-
216+
$server = Models\Server::byUuid($uuid);
223217
$this->authorize('reset-db-password', $server);
218+
219+
$database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail();
224220
try {
225221
$repo = new Repositories\DatabaseRepository;
226222
$password = str_random(16);

0 commit comments

Comments
 (0)