From 5763493c6c4f1c91d090f6cd17fe7d528bb57120 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 17 Oct 2020 14:23:00 -0700 Subject: [PATCH] Allow setting the backup limit via the API; closes #2535 --- .../Api/Application/Servers/StoreServerRequest.php | 2 ++ .../Servers/UpdateServerBuildConfigurationRequest.php | 7 +++++-- app/Models/Server.php | 2 +- app/Services/Servers/BuildModificationService.php | 6 +++--- .../Services/Servers/ServerCreationServiceTest.php | 6 +++--- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php b/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php index 15780d695..5e4f9c200 100644 --- a/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php +++ b/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php @@ -55,6 +55,7 @@ class StoreServerRequest extends ApplicationApiRequest 'feature_limits' => 'required|array', 'feature_limits.databases' => $rules['database_limit'], 'feature_limits.allocations' => $rules['allocation_limit'], + 'feature_limits.backups' => $rules['backup_limit'], // Placeholders for rules added in withValidator() function. 'allocation.default' => '', @@ -102,6 +103,7 @@ class StoreServerRequest extends ApplicationApiRequest 'start_on_completion' => array_get($data, 'start_on_completion', false), 'database_limit' => array_get($data, 'feature_limits.databases'), 'allocation_limit' => array_get($data, 'feature_limits.allocations'), + 'backup_limit' => array_get($data, 'feature_limits.backups'), ]; } diff --git a/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php b/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php index 3f0e1c8ca..a387634c1 100644 --- a/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php +++ b/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php @@ -47,6 +47,7 @@ class UpdateServerBuildConfigurationRequest extends ServerWriteRequest 'feature_limits' => 'required|array', 'feature_limits.databases' => $rules['database_limit'], 'feature_limits.allocations' => $rules['allocation_limit'], + 'feature_limits.backups' => $rules['backup_limit'], ]; } @@ -60,8 +61,9 @@ class UpdateServerBuildConfigurationRequest extends ServerWriteRequest $data = parent::validated(); $data['allocation_id'] = $data['allocation']; - $data['database_limit'] = $data['feature_limits']['databases']; - $data['allocation_limit'] = $data['feature_limits']['allocations']; + $data['database_limit'] = $data['feature_limits']['databases'] ?? null; + $data['allocation_limit'] = $data['feature_limits']['allocations'] ?? null; + $data['backup_limit'] = $data['feature_limits']['backups'] ?? null; unset($data['allocation'], $data['feature_limits']); // Adjust the limits field to match what is expected by the model. @@ -90,6 +92,7 @@ class UpdateServerBuildConfigurationRequest extends ServerWriteRequest 'remove_allocations.*' => 'allocation to remove', 'feature_limits.databases' => 'Database Limit', 'feature_limits.allocations' => 'Allocation Limit', + 'feature_limits.backups' => 'Backup Limit', ]; } diff --git a/app/Models/Server.php b/app/Models/Server.php index aa4a39f06..60a5de439 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -122,7 +122,7 @@ class Server extends Model 'installed' => 'in:0,1,2', 'database_limit' => 'present|nullable|integer|min:0', 'allocation_limit' => 'sometimes|nullable|integer|min:0', - 'backup_limit' => 'present|integer|min:0', + 'backup_limit' => 'present|nullable|integer|min:0', ]; /** diff --git a/app/Services/Servers/BuildModificationService.php b/app/Services/Servers/BuildModificationService.php index 86ff5bb69..e56d8f9b4 100644 --- a/app/Services/Servers/BuildModificationService.php +++ b/app/Services/Servers/BuildModificationService.php @@ -101,9 +101,9 @@ class BuildModificationService 'threads' => array_get($data, 'threads'), 'disk' => array_get($data, 'disk'), 'allocation_id' => array_get($data, 'allocation_id'), - 'database_limit' => array_get($data, 'database_limit', 0), - 'allocation_limit' => array_get($data, 'allocation_limit', 0), - 'backup_limit' => array_get($data, 'backup_limit', 0), + 'database_limit' => array_get($data, 'database_limit', 0) ?? null, + 'allocation_limit' => array_get($data, 'allocation_limit', 0) ?? null, + 'backup_limit' => array_get($data, 'backup_limit', 0) ?? null, ]); $updateData = $this->structureService->handle($server); diff --git a/tests/Integration/Services/Servers/ServerCreationServiceTest.php b/tests/Integration/Services/Servers/ServerCreationServiceTest.php index 7c7126682..daf198f22 100644 --- a/tests/Integration/Services/Servers/ServerCreationServiceTest.php +++ b/tests/Integration/Services/Servers/ServerCreationServiceTest.php @@ -144,9 +144,9 @@ class ServerCreationServiceTest extends IntegrationTestCase $this->assertFalse($response->suspended); $this->assertTrue($response->oom_disabled); - $this->assertEmpty($response->database_limit); - $this->assertEmpty($response->allocation_limit); - $this->assertEmpty($response->backup_limit); + $this->assertSame(0, $response->database_limit); + $this->assertSame(0, $response->allocation_limit); + $this->assertSame(0, $response->backup_limit); } /**