2024-07-16 05:26:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Appwrite\Functions\Validator;
|
|
|
|
|
|
|
|
|
|
use Utopia\Validator;
|
|
|
|
|
|
2024-07-31 11:27:32 +00:00
|
|
|
class RuntimeSpecification extends Validator
|
2024-07-16 05:26:53 +00:00
|
|
|
{
|
|
|
|
|
private array $plan;
|
|
|
|
|
|
2024-08-06 09:54:46 +00:00
|
|
|
private array $specifications;
|
|
|
|
|
|
|
|
|
|
private float $maxCpus;
|
|
|
|
|
|
|
|
|
|
private int $maxMemory;
|
|
|
|
|
|
|
|
|
|
public function __construct(array $plan, array $specifications, float $maxCpus, int $maxMemory)
|
2024-07-16 05:26:53 +00:00
|
|
|
{
|
|
|
|
|
$this->plan = $plan;
|
2024-08-06 09:54:46 +00:00
|
|
|
$this->specifications = $specifications;
|
|
|
|
|
$this->maxCpus = $maxCpus;
|
|
|
|
|
$this->maxMemory = $maxMemory;
|
2024-07-16 05:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-07-31 11:27:32 +00:00
|
|
|
* Get Allowed Specifications.
|
2024-07-16 05:26:53 +00:00
|
|
|
*
|
2024-07-31 11:27:32 +00:00
|
|
|
* Get allowed specifications taking into account the limits set by the environment variables and the plan.
|
2024-07-16 05:26:53 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2024-07-31 11:27:32 +00:00
|
|
|
public function getAllowedSpecifications(): array
|
2024-07-16 05:26:53 +00:00
|
|
|
{
|
2024-08-02 07:45:22 +00:00
|
|
|
$allowedSpecifications = [];
|
2024-07-16 05:26:53 +00:00
|
|
|
|
2024-08-06 09:54:46 +00:00
|
|
|
foreach ($this->specifications as $size => $values) {
|
|
|
|
|
if ($values['cpus'] <= $this->maxCpus && $values['memory'] <= $this->maxMemory) {
|
2024-08-20 04:23:11 +00:00
|
|
|
if (!empty($this->plan) && array_key_exists('runtimeSpecifications', $this->plan)) {
|
2024-07-31 11:27:32 +00:00
|
|
|
if (!\in_array($size, $this->plan['runtimeSpecifications'])) {
|
2024-07-16 05:26:53 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 11:27:32 +00:00
|
|
|
$allowedSpecifications[] = $size;
|
2024-07-16 05:26:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 11:27:32 +00:00
|
|
|
return $allowedSpecifications;
|
2024-07-16 05:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Description.
|
|
|
|
|
*
|
|
|
|
|
* Returns validator description.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getDescription(): string
|
|
|
|
|
{
|
2024-08-06 09:54:46 +00:00
|
|
|
return 'Specification must be one of: ' . implode(', ', $this->getAllowedSpecifications());
|
2024-07-16 05:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is valid.
|
|
|
|
|
*
|
|
|
|
|
* Returns true if valid or false if not.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isValid($value): bool
|
|
|
|
|
{
|
|
|
|
|
if (empty($value)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!\is_string($value)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 11:27:32 +00:00
|
|
|
if (!\in_array($value, $this->getAllowedSpecifications())) {
|
2024-07-16 05:26:53 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is array.
|
|
|
|
|
*
|
|
|
|
|
* Function will return true if object is array.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isArray(): bool
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Type.
|
|
|
|
|
*
|
|
|
|
|
* Returns validator type.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getType(): string
|
|
|
|
|
{
|
|
|
|
|
return self::TYPE_STRING;
|
|
|
|
|
}
|
|
|
|
|
}
|