Updated functions service
This commit is contained in:
parent
abf90d0c72
commit
d699c8aba9
File diff suppressed because it is too large
Load diff
|
@ -2,9 +2,6 @@
|
|||
|
||||
use Ahc\Jwt\JWT;
|
||||
use Appwrite\Auth\Auth;
|
||||
use Appwrite\Database\Database;
|
||||
use Appwrite\Database\Document;
|
||||
use Appwrite\Database\Validator\Authorization;
|
||||
use Appwrite\Database\Validator\UID;
|
||||
use Utopia\Storage\Storage;
|
||||
use Utopia\Storage\Validator\File;
|
||||
|
@ -14,6 +11,10 @@ use Utopia\Storage\Validator\Upload;
|
|||
use Appwrite\Utopia\Response;
|
||||
use Appwrite\Task\Validator\Cron;
|
||||
use Utopia\App;
|
||||
use Utopia\Exception;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Validator\ArrayList;
|
||||
use Utopia\Validator\Assoc;
|
||||
use Utopia\Validator\Text;
|
||||
|
@ -21,7 +22,6 @@ use Utopia\Validator\Range;
|
|||
use Utopia\Validator\WhiteList;
|
||||
use Utopia\Config\Config;
|
||||
use Cron\CronExpression;
|
||||
use Utopia\Exception;
|
||||
|
||||
include_once __DIR__ . '/../shared/api.php';
|
||||
|
||||
|
@ -45,16 +45,13 @@ App::post('/v1/functions')
|
|||
->param('schedule', '', new Cron(), 'Schedule CRON syntax.', true)
|
||||
->param('timeout', 15, new Range(1, 900), 'Function maximum execution time in seconds.', true)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($name, $execute, $env, $vars, $events, $schedule, $timeout, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($name, $execute, $env, $vars, $events, $schedule, $timeout, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$function = $projectDB->createDocument([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_FUNCTIONS,
|
||||
'$permissions' => [
|
||||
'execute' => $execute,
|
||||
],
|
||||
$function = $dbForInternal->createDocument('functions', new Document([
|
||||
'execute' => $execute,
|
||||
'dateCreated' => time(),
|
||||
'dateUpdated' => time(),
|
||||
'status' => 'disabled',
|
||||
|
@ -64,10 +61,10 @@ App::post('/v1/functions')
|
|||
'vars' => $vars,
|
||||
'events' => $events,
|
||||
'schedule' => $schedule,
|
||||
'schedulePrevious' => null,
|
||||
'scheduleNext' => null,
|
||||
'schedulePrevious' => 0,
|
||||
'scheduleNext' => 0,
|
||||
'timeout' => $timeout,
|
||||
]);
|
||||
]));
|
||||
|
||||
if (false === $function) {
|
||||
throw new Exception('Failed saving function to DB', 500);
|
||||
|
@ -75,7 +72,7 @@ App::post('/v1/functions')
|
|||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic($function, Response::MODEL_FUNCTION)
|
||||
->dynamic2($function, Response::MODEL_FUNCTION)
|
||||
;
|
||||
});
|
||||
|
||||
|
@ -95,24 +92,14 @@ App::get('/v1/functions')
|
|||
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
|
||||
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($search, $limit, $offset, $orderType, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($search, $limit, $offset, $orderType, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$results = $projectDB->getCollection([
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'orderType' => $orderType,
|
||||
'search' => $search,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_FUNCTIONS,
|
||||
],
|
||||
]);
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'sum' => $projectDB->getSum(),
|
||||
'functions' => $results
|
||||
$response->dynamic2(new Document([
|
||||
'sum' => $dbForInternal->count('functions', [], 5000),
|
||||
'functions' => $dbForInternal->find('functions', [], $limit, $offset, [], [])
|
||||
]), Response::MODEL_FUNCTION_LIST);
|
||||
});
|
||||
|
||||
|
@ -129,18 +116,18 @@ App::get('/v1/functions/:functionId')
|
|||
->label('sdk.response.model', Response::MODEL_FUNCTION)
|
||||
->param('functionId', '', new UID(), 'Function unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($functionId, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($functionId, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
$response->dynamic($function, Response::MODEL_FUNCTION);
|
||||
$response->dynamic2($function, Response::MODEL_FUNCTION);
|
||||
});
|
||||
|
||||
App::get('/v1/functions/:functionId/usage')
|
||||
|
@ -154,18 +141,18 @@ App::get('/v1/functions/:functionId/usage')
|
|||
->param('range', '30d', new WhiteList(['24h', '7d', '30d', '90d']), 'Date range.', true)
|
||||
->inject('response')
|
||||
->inject('project')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('register')
|
||||
->action(function ($functionId, $range, $response, $project, $projectDB, $register) {
|
||||
->action(function ($functionId, $range, $response, $project, $dbForInternal, $register) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Document $project */
|
||||
/** @var Appwrite\Database\Database $consoleDB */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $consoleDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Utopia\Registry\Registry $register */
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
|
@ -284,35 +271,33 @@ App::put('/v1/functions/:functionId')
|
|||
->param('schedule', '', new Cron(), 'Schedule CRON syntax.', true)
|
||||
->param('timeout', 15, new Range(1, 900), 'Function maximum execution time in seconds.', true)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('project')
|
||||
->action(function ($functionId, $name, $execute, $vars, $events, $schedule, $timeout, $response, $projectDB, $project) {
|
||||
->action(function ($functionId, $name, $execute, $vars, $events, $schedule, $timeout, $response, $dbForInternal, $project) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Database\Document $project */
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
$original = $function->getAttribute('schedule', '');
|
||||
$cron = (!empty($function->getAttribute('tag', null)) && !empty($schedule)) ? new CronExpression($schedule) : null;
|
||||
$next = (!empty($function->getAttribute('tag', null)) && !empty($schedule)) ? $cron->getNextRunDate()->format('U') : null;
|
||||
$next = (!empty($function->getAttribute('tag', null)) && !empty($schedule)) ? $cron->getNextRunDate()->format('U') : 0;
|
||||
|
||||
$function = $projectDB->updateDocument(array_merge($function->getArrayCopy(), [
|
||||
'$permissions' => [
|
||||
'execute' => $execute,
|
||||
],
|
||||
$function = $dbForInternal->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [
|
||||
'execute' => $execute,
|
||||
'dateUpdated' => time(),
|
||||
'name' => $name,
|
||||
'vars' => $vars,
|
||||
'events' => $events,
|
||||
'schedule' => $schedule,
|
||||
'scheduleNext' => $next,
|
||||
'scheduleNext' => (int)$next,
|
||||
'timeout' => $timeout,
|
||||
]));
|
||||
])));
|
||||
|
||||
if (false === $function) {
|
||||
throw new Exception('Failed saving function to DB', 500);
|
||||
|
@ -327,7 +312,7 @@ App::put('/v1/functions/:functionId')
|
|||
]); // Async task rescheduale
|
||||
}
|
||||
|
||||
$response->dynamic($function, Response::MODEL_FUNCTION);
|
||||
$response->dynamic2($function, Response::MODEL_FUNCTION);
|
||||
});
|
||||
|
||||
App::patch('/v1/functions/:functionId/tag')
|
||||
|
@ -345,32 +330,32 @@ App::patch('/v1/functions/:functionId/tag')
|
|||
->param('functionId', '', new UID(), 'Function unique ID.')
|
||||
->param('tag', '', new UID(), 'Tag unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('project')
|
||||
->action(function ($functionId, $tag, $response, $projectDB, $project) {
|
||||
->action(function ($functionId, $tag, $response, $dbForInternal, $project) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Database\Document $project */
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$tag = $projectDB->getDocument($tag);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
$tag = $dbForInternal->getDocument('tags', $tag);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
if (empty($tag->getId()) || Database::SYSTEM_COLLECTION_TAGS != $tag->getCollection()) {
|
||||
if (empty($tag->getId())) {
|
||||
throw new Exception('Tag not found', 404);
|
||||
}
|
||||
|
||||
$schedule = $function->getAttribute('schedule', '');
|
||||
$cron = (empty($function->getAttribute('tag')) && !empty($schedule)) ? new CronExpression($schedule) : null;
|
||||
$next = (empty($function->getAttribute('tag')) && !empty($schedule)) ? $cron->getNextRunDate()->format('U') : null;
|
||||
$next = (empty($function->getAttribute('tag')) && !empty($schedule)) ? $cron->getNextRunDate()->format('U') : 0;
|
||||
|
||||
$function = $projectDB->updateDocument(array_merge($function->getArrayCopy(), [
|
||||
$function = $dbForInternal->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [
|
||||
'tag' => $tag->getId(),
|
||||
'scheduleNext' => $next,
|
||||
]));
|
||||
'scheduleNext' => (int)$next,
|
||||
])));
|
||||
|
||||
if ($next) { // Init first schedule
|
||||
ResqueScheduler::enqueueAt($next, 'v1-functions', 'FunctionsV1', [
|
||||
|
@ -385,7 +370,7 @@ App::patch('/v1/functions/:functionId/tag')
|
|||
throw new Exception('Failed saving function to DB', 500);
|
||||
}
|
||||
|
||||
$response->dynamic($function, Response::MODEL_FUNCTION);
|
||||
$response->dynamic2($function, Response::MODEL_FUNCTION);
|
||||
});
|
||||
|
||||
App::delete('/v1/functions/:functionId')
|
||||
|
@ -401,20 +386,20 @@ App::delete('/v1/functions/:functionId')
|
|||
->label('sdk.response.model', Response::MODEL_NONE)
|
||||
->param('functionId', '', new UID(), 'Function unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('deletes')
|
||||
->action(function ($functionId, $response, $projectDB, $deletes) {
|
||||
->action(function ($functionId, $response, $dbForInternal, $deletes) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Event\Event $deletes */
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
if (!$projectDB->deleteDocument($function->getId())) {
|
||||
if (!$dbForInternal->deleteDocument('functions', $function->getId())) {
|
||||
throw new Exception('Failed to remove function from DB', 500);
|
||||
}
|
||||
|
||||
|
@ -445,17 +430,17 @@ App::post('/v1/functions/:functionId/tags')
|
|||
->param('code', [], new File(), 'Gzip file with your code package. When used with the Appwrite CLI, pass the path to your code directory, and the CLI will automatically package your code. Use a path that is within the current directory.', false)
|
||||
->inject('request')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('usage')
|
||||
->action(function ($functionId, $command, $file, $request, $response, $projectDB, $usage) {
|
||||
->action(function ($functionId, $command, $file, $request, $response, $dbForInternal, $usage) {
|
||||
/** @var Utopia\Swoole\Request $request */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Event\Event $usage */
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
|
@ -494,18 +479,15 @@ App::post('/v1/functions/:functionId/tags')
|
|||
throw new Exception('Failed moving file', 500);
|
||||
}
|
||||
|
||||
$tag = $projectDB->createDocument([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_TAGS,
|
||||
'$permissions' => [
|
||||
'read' => [],
|
||||
'write' => [],
|
||||
],
|
||||
$tag = $dbForInternal->createDocument('tags', new Document([
|
||||
'$read' => [],
|
||||
'$write' => [],
|
||||
'functionId' => $function->getId(),
|
||||
'dateCreated' => time(),
|
||||
'command' => $command,
|
||||
'path' => $path,
|
||||
'size' => $size,
|
||||
]);
|
||||
]));
|
||||
|
||||
if (false === $tag) {
|
||||
throw new Exception('Failed saving tag to DB', 500);
|
||||
|
@ -517,7 +499,7 @@ App::post('/v1/functions/:functionId/tags')
|
|||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic($tag, Response::MODEL_TAG)
|
||||
->dynamic2($tag, Response::MODEL_TAG)
|
||||
;
|
||||
});
|
||||
|
||||
|
@ -538,30 +520,27 @@ App::get('/v1/functions/:functionId/tags')
|
|||
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
|
||||
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($functionId, $search, $limit, $offset, $orderType, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($functionId, $search, $limit, $offset, $orderType, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
$results = $projectDB->getCollection([
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'orderType' => $orderType,
|
||||
'search' => $search,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_TAGS,
|
||||
'functionId='.$function->getId(),
|
||||
],
|
||||
]);
|
||||
$results = $dbForInternal->find('tags', [
|
||||
new Query('functionId', Query::TYPE_EQUAL, [$function->getId()]),
|
||||
], $limit, $offset);
|
||||
|
||||
$sum = $dbForInternal->count('tags', [
|
||||
new Query('functionId', Query::TYPE_EQUAL, [$function->getId()]),
|
||||
], 5000);
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'sum' => $projectDB->getSum(),
|
||||
$response->dynamic2(new Document([
|
||||
'sum' => $sum,
|
||||
'tags' => $results
|
||||
]), Response::MODEL_TAG_LIST);
|
||||
});
|
||||
|
@ -580,28 +559,28 @@ App::get('/v1/functions/:functionId/tags/:tagId')
|
|||
->param('functionId', '', new UID(), 'Function unique ID.')
|
||||
->param('tagId', '', new UID(), 'Tag unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($functionId, $tagId, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($functionId, $tagId, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
$tag = $projectDB->getDocument($tagId);
|
||||
$tag = $dbForInternal->getDocument('tags', $tagId);
|
||||
|
||||
if ($tag->getAttribute('functionId') !== $function->getId()) {
|
||||
throw new Exception('Tag not found', 404);
|
||||
}
|
||||
|
||||
if (empty($tag->getId()) || Database::SYSTEM_COLLECTION_TAGS != $tag->getCollection()) {
|
||||
if (empty($tag->getId())) {
|
||||
throw new Exception('Tag not found', 404);
|
||||
}
|
||||
|
||||
$response->dynamic($tag, Response::MODEL_TAG);
|
||||
$response->dynamic2($tag, Response::MODEL_TAG);
|
||||
});
|
||||
|
||||
App::delete('/v1/functions/:functionId/tags/:tagId')
|
||||
|
@ -618,41 +597,41 @@ App::delete('/v1/functions/:functionId/tags/:tagId')
|
|||
->param('functionId', '', new UID(), 'Function unique ID.')
|
||||
->param('tagId', '', new UID(), 'Tag unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('usage')
|
||||
->action(function ($functionId, $tagId, $response, $projectDB, $usage) {
|
||||
->action(function ($functionId, $tagId, $response, $dbForInternal, $usage) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Event\Event $usage */
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
$tag = $projectDB->getDocument($tagId);
|
||||
$tag = $dbForInternal->getDocument('tags', $tagId);
|
||||
|
||||
if ($tag->getAttribute('functionId') !== $function->getId()) {
|
||||
throw new Exception('Tag not found', 404);
|
||||
}
|
||||
|
||||
if (empty($tag->getId()) || Database::SYSTEM_COLLECTION_TAGS != $tag->getCollection()) {
|
||||
if (empty($tag->getId())) {
|
||||
throw new Exception('Tag not found', 404);
|
||||
}
|
||||
|
||||
$device = Storage::getDevice('functions');
|
||||
|
||||
if ($device->delete($tag->getAttribute('path', ''))) {
|
||||
if (!$projectDB->deleteDocument($tag->getId())) {
|
||||
if (!$dbForInternal->deleteDocument('tags', $tag->getId())) {
|
||||
throw new Exception('Failed to remove tag from DB', 500);
|
||||
}
|
||||
}
|
||||
|
||||
if($function->getAttribute('tag') === $tag->getId()) { // Reset function tag
|
||||
$function = $projectDB->updateDocument(array_merge($function->getArrayCopy(), [
|
||||
$function = $dbForInternal->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [
|
||||
'tag' => '',
|
||||
]));
|
||||
])));
|
||||
|
||||
if (false === $function) {
|
||||
throw new Exception('Failed saving function to DB', 500);
|
||||
|
@ -685,29 +664,29 @@ App::post('/v1/functions/:functionId/executions')
|
|||
// ->param('async', 1, new Range(0, 1), 'Execute code asynchronously. Pass 1 for true, 0 for false. Default value is 1.', true)
|
||||
->inject('response')
|
||||
->inject('project')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('user')
|
||||
->action(function ($functionId, $data, /*$async,*/ $response, $project, $projectDB, $user) {
|
||||
->action(function ($functionId, $data, /*$async,*/ $response, $project, $dbForInternal, $user) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Document $project */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Database\Document $user */
|
||||
|
||||
Authorization::disable();
|
||||
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
$tag = $projectDB->getDocument($function->getAttribute('tag'));
|
||||
$tag = $dbForInternal->getDocument('tags', $function->getAttribute('tag'));
|
||||
|
||||
if ($tag->getAttribute('functionId') !== $function->getId()) {
|
||||
throw new Exception('Tag not found. Deploy tag before trying to execute a function', 404);
|
||||
}
|
||||
|
||||
if (empty($tag->getId()) || Database::SYSTEM_COLLECTION_TAGS != $tag->getCollection()) {
|
||||
if (empty($tag->getId())) {
|
||||
throw new Exception('Tag not found. Deploy tag before trying to execute a function', 404);
|
||||
}
|
||||
|
||||
|
@ -715,27 +694,25 @@ App::post('/v1/functions/:functionId/executions')
|
|||
|
||||
$validator = new Authorization($function, 'execute');
|
||||
|
||||
if (!$validator->isValid($function->getPermissions())) { // Check if user has write access to execute function
|
||||
if (!$validator->isValid($function->getAttribute('execute'))) { // Check if user has write access to execute function
|
||||
throw new Exception($validator->getDescription(), 401);
|
||||
}
|
||||
|
||||
Authorization::disable();
|
||||
|
||||
$execution = $projectDB->createDocument([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS,
|
||||
'$permissions' => [
|
||||
'read' => (!empty($user->getId())) ? ['user:' . $user->getId()] : [],
|
||||
'write' => [],
|
||||
],
|
||||
$execution = $dbForInternal->createDocument('executions', new Document([
|
||||
'$read' => (!empty($user->getId())) ? ['user:' . $user->getId()] : [],
|
||||
'$write' => [],
|
||||
'dateCreated' => time(),
|
||||
'functionId' => $function->getId(),
|
||||
'tagId' => $tag->getId(),
|
||||
'trigger' => 'http', // http / schedule / event
|
||||
'status' => 'waiting', // waiting / processing / completed / failed
|
||||
'exitCode' => 0,
|
||||
'stdout' => '',
|
||||
'stderr' => '',
|
||||
'time' => 0,
|
||||
]);
|
||||
'time' => 0.0,
|
||||
]));
|
||||
|
||||
Authorization::reset();
|
||||
|
||||
|
@ -776,7 +753,7 @@ App::post('/v1/functions/:functionId/executions')
|
|||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic($execution, Response::MODEL_EXECUTION)
|
||||
->dynamic2($execution, Response::MODEL_EXECUTION)
|
||||
;
|
||||
});
|
||||
|
||||
|
@ -797,32 +774,29 @@ App::get('/v1/functions/:functionId/executions')
|
|||
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
|
||||
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($functionId, $search, $limit, $offset, $orderType, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($functionId, $search, $limit, $offset, $orderType, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
Authorization::disable();
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
Authorization::reset();
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
$results = $projectDB->getCollection([
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'orderType' => $orderType,
|
||||
'search' => $search,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_EXECUTIONS,
|
||||
'functionId='.$function->getId(),
|
||||
],
|
||||
]);
|
||||
$results = $dbForInternal->find('executions', [
|
||||
new Query('functionId', Query::TYPE_EQUAL, [$function->getId()]),
|
||||
], $limit, $offset);
|
||||
|
||||
$sum = $dbForInternal->count('executions', [
|
||||
new Query('functionId', Query::TYPE_EQUAL, [$function->getId()]),
|
||||
], 5000);
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'sum' => $projectDB->getSum(),
|
||||
$response->dynamic2(new Document([
|
||||
'sum' => $sum,
|
||||
'executions' => $results
|
||||
]), Response::MODEL_EXECUTION_LIST);
|
||||
});
|
||||
|
@ -841,28 +815,28 @@ App::get('/v1/functions/:functionId/executions/:executionId')
|
|||
->param('functionId', '', new UID(), 'Function unique ID.')
|
||||
->param('executionId', '', new UID(), 'Execution unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($functionId, $executionId, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($functionId, $executionId, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
Authorization::disable();
|
||||
$function = $projectDB->getDocument($functionId);
|
||||
$function = $dbForInternal->getDocument('functions', $functionId);
|
||||
Authorization::reset();
|
||||
|
||||
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
|
||||
if (empty($function->getId())) {
|
||||
throw new Exception('Function not found', 404);
|
||||
}
|
||||
|
||||
$execution = $projectDB->getDocument($executionId);
|
||||
$execution = $dbForInternal->getDocument('executions', $executionId);
|
||||
|
||||
if ($execution->getAttribute('functionId') !== $function->getId()) {
|
||||
throw new Exception('Execution not found', 404);
|
||||
}
|
||||
|
||||
if (empty($execution->getId()) || Database::SYSTEM_COLLECTION_EXECUTIONS != $execution->getCollection()) {
|
||||
if (empty($execution->getId())) {
|
||||
throw new Exception('Execution not found', 404);
|
||||
}
|
||||
|
||||
$response->dynamic($execution, Response::MODEL_EXECUTION);
|
||||
$response->dynamic2($execution, Response::MODEL_EXECUTION);
|
||||
});
|
||||
|
|
29
app/init.php
29
app/init.php
|
@ -33,7 +33,7 @@ use PDO as PDONative;
|
|||
use Utopia\Cache\Adapter\Redis as RedisCache;
|
||||
use Utopia\Cache\Cache;
|
||||
use Utopia\Database\Adapter\MariaDB;
|
||||
use Utopia\Database\Database as DatabaseDatabase;
|
||||
use Utopia\Database\Database as Database2;
|
||||
use Utopia\Database\Validator\Authorization as Authorization2;
|
||||
|
||||
const APP_NAME = 'Appwrite';
|
||||
|
@ -147,6 +147,27 @@ Database::addFilter('encrypt',
|
|||
}
|
||||
);
|
||||
|
||||
Database2::addFilter('encrypt',
|
||||
function($value) {
|
||||
$key = App::getEnv('_APP_OPENSSL_KEY_V1');
|
||||
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
|
||||
$tag = null;
|
||||
return json_encode([
|
||||
'data' => OpenSSL::encrypt($value, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag),
|
||||
'method' => OpenSSL::CIPHER_AES_128_GCM,
|
||||
'iv' => bin2hex($iv),
|
||||
'tag' => bin2hex($tag),
|
||||
'version' => '1',
|
||||
]);
|
||||
},
|
||||
function($value) {
|
||||
$value = json_decode($value, true);
|
||||
$key = App::getEnv('_APP_OPENSSL_KEY_V'.$value['version']);
|
||||
|
||||
return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag']));
|
||||
}
|
||||
);
|
||||
|
||||
/*
|
||||
* Registry
|
||||
*/
|
||||
|
@ -512,7 +533,7 @@ App::setResource('projectDB', function($register, $project) {
|
|||
App::setResource('dbForInternal', function($register, $project) {
|
||||
$cache = new Cache(new RedisCache($register->get('cache')));
|
||||
|
||||
$database = new DatabaseDatabase(new MariaDB($register->get('db')), $cache);
|
||||
$database = new Database2(new MariaDB($register->get('db')), $cache);
|
||||
$database->setNamespace('project_'.$project->getId().'_internal');
|
||||
|
||||
return $database;
|
||||
|
@ -521,7 +542,7 @@ App::setResource('dbForInternal', function($register, $project) {
|
|||
App::setResource('dbForExternal', function($register, $project) {
|
||||
$cache = new Cache(new RedisCache($register->get('cache')));
|
||||
|
||||
$database = new DatabaseDatabase(new MariaDB($register->get('db')), $cache);
|
||||
$database = new Database2(new MariaDB($register->get('db')), $cache);
|
||||
$database->setNamespace('project_'.$project->getId().'_external');
|
||||
|
||||
return $database;
|
||||
|
@ -530,7 +551,7 @@ App::setResource('dbForExternal', function($register, $project) {
|
|||
App::setResource('dbForConsole', function($register) {
|
||||
$cache = new Cache(new RedisCache($register->get('cache')));
|
||||
|
||||
$database = new DatabaseDatabase(new MariaDB($register->get('db')), $cache);
|
||||
$database = new Database2(new MariaDB($register->get('db')), $cache);
|
||||
$database->setNamespace('project_console');
|
||||
|
||||
return $database;
|
||||
|
|
4
composer.lock
generated
4
composer.lock
generated
|
@ -1923,7 +1923,7 @@
|
|||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/database",
|
||||
"reference": "2299091c63aec2315a4f40bc8dd16968c5bec6bf"
|
||||
"reference": "52c8cacf1d7930c7ae4cad034666a4322e3c56b0"
|
||||
},
|
||||
"require": {
|
||||
"ext-mongodb": "*",
|
||||
|
@ -1970,7 +1970,7 @@
|
|||
"upf",
|
||||
"utopia"
|
||||
],
|
||||
"time": "2021-05-03T22:49:00+00:00"
|
||||
"time": "2021-05-04T21:14:11+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/domains",
|
||||
|
|
Loading…
Reference in a new issue