diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index b3740787b3..07e09a3b89 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -49,6 +49,7 @@ class Base extends Action $planKey ); $allowedSpecifications = $specificationValidator->getAllowedSpecifications(); + $planKey = $specificationValidator->getPlanKey(); if (empty($allowedSpecifications)) { return $fallback; diff --git a/src/Appwrite/Platform/Modules/Compute/Validator/Specification.php b/src/Appwrite/Platform/Modules/Compute/Validator/Specification.php index 5f1337c97f..8a77afef24 100644 --- a/src/Appwrite/Platform/Modules/Compute/Validator/Specification.php +++ b/src/Appwrite/Platform/Modules/Compute/Validator/Specification.php @@ -25,6 +25,15 @@ class Specification extends Validator $this->planKey = $planKey; } + public function getPlanKey(): string + { + if ($this->planKey === 'buildSpecifications' && !array_key_exists('buildSpecifications', $this->plan) && array_key_exists('runtimeSpecifications', $this->plan)) { + return 'runtimeSpecifications'; + } + + return $this->planKey; + } + /** * Get Allowed Specifications. * @@ -35,11 +44,12 @@ class Specification extends Validator public function getAllowedSpecifications(): array { $allowedSpecifications = []; + $planKey = $this->getPlanKey(); foreach ($this->specifications as $size => $values) { if ((empty($this->maxCpus) || $values['cpus'] <= $this->maxCpus) && (empty($this->maxMemory) || $values['memory'] <= $this->maxMemory)) { - if (!empty($this->plan) && array_key_exists($this->planKey, $this->plan)) { - if (!\in_array($size, $this->plan[$this->planKey])) { + if (!empty($this->plan) && array_key_exists($planKey, $this->plan)) { + if (!\in_array($size, $this->plan[$planKey])) { continue; } } diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php index 8cbff906fa..e8177abdbd 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Modules\Functions\Http\Specifications; use Appwrite\Platform\Modules\Compute\Base; +use Appwrite\Platform\Modules\Compute\Validator\Specification; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; @@ -56,7 +57,7 @@ class XList extends Base public function action(string $type, Response $response, array $plan) { $allSpecs = Config::getParam('specifications', []); - $planKey = $type === 'builds' ? 'buildSpecifications' : 'runtimeSpecifications'; + $planKey = (new Specification($plan, [], 0, 0, $type === 'builds' ? 'buildSpecifications' : 'runtimeSpecifications'))->getPlanKey(); $specs = []; foreach ($allSpecs as $spec) { diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php index a4f865d859..735e45bcdc 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Modules\Sites\Http\Specifications; use Appwrite\Platform\Modules\Compute\Base; +use Appwrite\Platform\Modules\Compute\Validator\Specification; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; @@ -56,7 +57,7 @@ class XList extends Base public function action(string $type, Response $response, array $plan) { $allSpecs = Config::getParam('specifications', []); - $planKey = $type === 'builds' ? 'buildSpecifications' : 'runtimeSpecifications'; + $planKey = (new Specification($plan, [], 0, 0, $type === 'builds' ? 'buildSpecifications' : 'runtimeSpecifications'))->getPlanKey(); $specs = []; foreach ($allSpecs as $spec) { diff --git a/tests/unit/Platform/Modules/Compute/Validator/SpecificationTest.php b/tests/unit/Platform/Modules/Compute/Validator/SpecificationTest.php index abc6296d0a..15d9961aa8 100644 --- a/tests/unit/Platform/Modules/Compute/Validator/SpecificationTest.php +++ b/tests/unit/Platform/Modules/Compute/Validator/SpecificationTest.php @@ -96,4 +96,27 @@ final class SpecificationTest extends TestCase $this->assertContains(SpecificationConstants::S_1VCPU_1GB, $allowed); $this->assertContains(SpecificationConstants::S_2VCPU_2GB, $allowed); } + + public function testGetAllowedSpecificationsWithLegacyBuildPlanLimits(): void + { + $plan = [ + 'runtimeSpecifications' => [ + SpecificationConstants::S_05VCPU_512MB, + SpecificationConstants::S_1VCPU_512MB, + ] + ]; + $validator = new Specification( + plan: $plan, + specifications: $this->specifications, + maxCpus: 0, + maxMemory: 0, + planKey: 'buildSpecifications' + ); + + $allowed = $validator->getAllowedSpecifications(); + $this->assertEquals('runtimeSpecifications', $validator->getPlanKey()); + $this->assertCount(2, $allowed); + $this->assertContains(SpecificationConstants::S_05VCPU_512MB, $allowed); + $this->assertContains(SpecificationConstants::S_1VCPU_512MB, $allowed); + } }