|
@@ -11,66 +11,40 @@ use Exception;
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
|
use Illuminate\Http\Client\Response;
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
-use Illuminate\Validation\Validator;
|
|
|
|
|
|
class Pterodactyl
|
|
|
{
|
|
|
/**
|
|
|
- * @return PendingRequest
|
|
|
+ * @return null
|
|
|
+ * @throws Exception
|
|
|
*/
|
|
|
- public static function client()
|
|
|
+ public static function getNests()
|
|
|
{
|
|
|
- return Http::withHeaders([
|
|
|
- 'Authorization' => 'Bearer ' . env('PTERODACTYL_TOKEN', false),
|
|
|
- 'Content-type' => 'application/json',
|
|
|
- 'Accept' => 'Application/vnd.pterodactyl.v1+json',
|
|
|
- ])->baseUrl(env('PTERODACTYL_URL') . '/api');
|
|
|
+ $response = self::client()->get('/application/nests');
|
|
|
+ if ($response->failed()) throw self::getException();
|
|
|
+ return $response->json()['data'];
|
|
|
}
|
|
|
|
|
|
//TODO: Extend error handling (maybe logger for more errors when debugging)
|
|
|
- /**
|
|
|
- * Get user by pterodactyl id
|
|
|
- * @param int $pterodactylId
|
|
|
- * @return mixed
|
|
|
- */
|
|
|
- public function getUser(int $pterodactylId)
|
|
|
- {
|
|
|
- $response = self::client()->get("/application/users/{$pterodactylId}");
|
|
|
-
|
|
|
- if ($response->failed()) return $response->json();
|
|
|
- return $response->json()['attributes'];
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
- * @param Node $node
|
|
|
- * @return array|mixed|null
|
|
|
- * @throws Exception
|
|
|
+ * @return PendingRequest
|
|
|
*/
|
|
|
- public static function getFreeAllocations(Node $node)
|
|
|
+ public static function client()
|
|
|
{
|
|
|
- $response = self::getAllocations($node);
|
|
|
- $freeAllocations = [];
|
|
|
-
|
|
|
- if (isset($response['data'])) {
|
|
|
- if (!empty($response['data'])) {
|
|
|
- foreach ($response['data'] as $allocation) {
|
|
|
- if (!$allocation['attributes']['assigned']) array_push($freeAllocations, $allocation);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $freeAllocations;
|
|
|
+ return Http::withHeaders([
|
|
|
+ 'Authorization' => 'Bearer ' . env('PTERODACTYL_TOKEN', false),
|
|
|
+ 'Content-type' => 'application/json',
|
|
|
+ 'Accept' => 'Application/vnd.pterodactyl.v1+json',
|
|
|
+ ])->baseUrl(env('PTERODACTYL_URL') . '/api');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @return null
|
|
|
- * @throws Exception
|
|
|
+ * @return Exception
|
|
|
*/
|
|
|
- public static function getNests()
|
|
|
+ private static function getException(): Exception
|
|
|
{
|
|
|
- $response = self::client()->get('/application/nests');
|
|
|
- if ($response->failed()) throw self::getException();
|
|
|
- return $response->json()['data'];
|
|
|
+ return new Exception('Request Failed, is pterodactyl set-up correctly?');
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -85,7 +59,6 @@ class Pterodactyl
|
|
|
return $response->json()['data'];
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* @return mixed
|
|
|
* @throws Exception
|
|
@@ -94,10 +67,10 @@ class Pterodactyl
|
|
|
{
|
|
|
$response = self::client()->get('/application/nodes');
|
|
|
if ($response->failed()) throw self::getException();
|
|
|
+ dd($response->json());
|
|
|
return $response->json()['data'];
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* @return mixed
|
|
|
* @throws Exception
|
|
@@ -119,6 +92,26 @@ class Pterodactyl
|
|
|
return self::getFreeAllocations($node)[0]['attributes']['id'] ?? null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param Node $node
|
|
|
+ * @return array|mixed|null
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static function getFreeAllocations(Node $node)
|
|
|
+ {
|
|
|
+ $response = self::getAllocations($node);
|
|
|
+ $freeAllocations = [];
|
|
|
+
|
|
|
+ if (isset($response['data'])) {
|
|
|
+ if (!empty($response['data'])) {
|
|
|
+ foreach ($response['data'] as $allocation) {
|
|
|
+ if (!$allocation['attributes']['assigned']) array_push($freeAllocations, $allocation);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $freeAllocations;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* @param Node $node
|
|
@@ -132,7 +125,6 @@ class Pterodactyl
|
|
|
return $response->json();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* @param String $route
|
|
|
* @return string
|
|
@@ -151,21 +143,21 @@ class Pterodactyl
|
|
|
public static function createServer(Server $server, Egg $egg, int $allocationId)
|
|
|
{
|
|
|
return self::client()->post("/application/servers", [
|
|
|
- "name" => $server->name,
|
|
|
- "external_id" => $server->id,
|
|
|
- "user" => $server->user->pterodactyl_id,
|
|
|
- "egg" => $egg->id,
|
|
|
- "docker_image" => $egg->docker_image,
|
|
|
- "startup" => $egg->startup,
|
|
|
- "environment" => $egg->getEnvironmentVariables(),
|
|
|
- "limits" => [
|
|
|
+ "name" => $server->name,
|
|
|
+ "external_id" => $server->id,
|
|
|
+ "user" => $server->user->pterodactyl_id,
|
|
|
+ "egg" => $egg->id,
|
|
|
+ "docker_image" => $egg->docker_image,
|
|
|
+ "startup" => $egg->startup,
|
|
|
+ "environment" => $egg->getEnvironmentVariables(),
|
|
|
+ "limits" => [
|
|
|
"memory" => $server->product->memory,
|
|
|
"swap" => $server->product->swap,
|
|
|
"disk" => $server->product->disk,
|
|
|
"io" => $server->product->io,
|
|
|
"cpu" => $server->product->cpu
|
|
|
],
|
|
|
- "feature_limits" => [
|
|
|
+ "feature_limits" => [
|
|
|
"databases" => $server->product->databases,
|
|
|
"backups" => $server->product->backups,
|
|
|
"allocations" => $server->product->allocations,
|
|
@@ -174,6 +166,7 @@ class Pterodactyl
|
|
|
"default" => $allocationId
|
|
|
]
|
|
|
]);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public static function suspendServer(Server $server)
|
|
@@ -191,10 +184,15 @@ class Pterodactyl
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @return Exception
|
|
|
+ * Get user by pterodactyl id
|
|
|
+ * @param int $pterodactylId
|
|
|
+ * @return mixed
|
|
|
*/
|
|
|
- private static function getException(): Exception
|
|
|
+ public function getUser(int $pterodactylId)
|
|
|
{
|
|
|
- return new Exception('Request Failed, is pterodactyl set-up correctly?');
|
|
|
+ $response = self::client()->get("/application/users/{$pterodactylId}");
|
|
|
+
|
|
|
+ if ($response->failed()) return $response->json();
|
|
|
+ return $response->json()['attributes'];
|
|
|
}
|
|
|
}
|