|
|
|
@ -106,7 +106,7 @@ App::get('/v1/functions')
|
|
|
|
|
$cursorFunction = $dbForProject->getDocument('functions', $cursor);
|
|
|
|
|
|
|
|
|
|
if ($cursorFunction->isEmpty()) {
|
|
|
|
|
throw new Exception("Function '{$cursor}' for the 'cursor' value not found.", 400);
|
|
|
|
|
throw new Exception("Function '{$cursor}' for the 'cursor' value not found.", 400, Exception::GENERAL_CURSOR_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -171,7 +171,7 @@ App::get('/v1/functions/:functionId')
|
|
|
|
|
$function = $dbForProject->getDocument('functions', $functionId);
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$response->dynamic($function, Response::MODEL_FUNCTION);
|
|
|
|
@ -200,7 +200,7 @@ App::get('/v1/functions/:functionId/usage')
|
|
|
|
|
$function = $dbForProject->getDocument('functions', $functionId);
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$usage = [];
|
|
|
|
@ -311,7 +311,7 @@ App::put('/v1/functions/:functionId')
|
|
|
|
|
$function = $dbForProject->getDocument('functions', $functionId);
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$original = $function->getAttribute('schedule', '');
|
|
|
|
@ -371,7 +371,7 @@ App::patch('/v1/functions/:functionId/deployments/:deploymentId')
|
|
|
|
|
$build = $dbForProject->getDocument('builds', $deployment->getAttribute('buildId', ''));
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($deployment->isEmpty()) {
|
|
|
|
@ -379,11 +379,11 @@ App::patch('/v1/functions/:functionId/deployments/:deploymentId')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($build->isEmpty()) {
|
|
|
|
|
throw new Exception('Build not found', 404);
|
|
|
|
|
throw new Exception('Build not found', 404, Exception::BUILD_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($build->getAttribute('status') !== 'ready') {
|
|
|
|
|
throw new Exception('Build not ready', 400);
|
|
|
|
|
throw new Exception('Build not ready', 400, Exception::BUILD_NOT_READY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$schedule = $function->getAttribute('schedule', '');
|
|
|
|
@ -430,11 +430,11 @@ App::delete('/v1/functions/:functionId')
|
|
|
|
|
$function = $dbForProject->getDocument('functions', $functionId);
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$dbForProject->deleteDocument('functions', $function->getId())) {
|
|
|
|
|
throw new Exception('Failed to remove function from DB', 500);
|
|
|
|
|
throw new Exception('Failed to remove function from DB', 500, Exception::GENERAL_SERVER_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$deletes
|
|
|
|
@ -484,7 +484,7 @@ App::post('/v1/functions/:functionId/deployments')
|
|
|
|
|
$function = $dbForProject->getDocument('functions', $functionId);
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$file = $request->getFiles('code');
|
|
|
|
@ -493,7 +493,7 @@ App::post('/v1/functions/:functionId/deployments')
|
|
|
|
|
$upload = new Upload();
|
|
|
|
|
|
|
|
|
|
if (empty($file)) {
|
|
|
|
|
throw new Exception('No file sent', 400);
|
|
|
|
|
throw new Exception('No file sent', 400, Exception::STORAGE_FILE_EMPTY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure we handle a single file and multiple files the same way
|
|
|
|
@ -502,7 +502,7 @@ App::post('/v1/functions/:functionId/deployments')
|
|
|
|
|
$fileSize = (\is_array($file['size']) && isset($file['size'][0])) ? $file['size'][0] : $file['size'];
|
|
|
|
|
|
|
|
|
|
if (!$fileExt->isValid($file['name'])) { // Check if file type is allowed
|
|
|
|
|
throw new Exception('File type not allowed', 400);
|
|
|
|
|
throw new Exception('File type not allowed', 400, Exception::STORAGE_FILE_TYPE_UNSUPPORTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$contentRange = $request->getHeader('content-range');
|
|
|
|
@ -516,7 +516,7 @@ App::post('/v1/functions/:functionId/deployments')
|
|
|
|
|
$fileSize = $request->getContentRangeSize();
|
|
|
|
|
$deploymentId = $request->getHeader('x-appwrite-id', $deploymentId);
|
|
|
|
|
if(is_null($start) || is_null($end) || is_null($fileSize)) {
|
|
|
|
|
throw new Exception('Invalid content-range header', 400);
|
|
|
|
|
throw new Exception('Invalid content-range header', 400, Exception::STORAGE_INVALID_CONTENT_RANGE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($end === $fileSize) {
|
|
|
|
@ -530,11 +530,11 @@ App::post('/v1/functions/:functionId/deployments')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$fileSizeValidator->isValid($fileSize)) { // Check if file size is exceeding allowed limit
|
|
|
|
|
throw new Exception('File size not allowed', 400);
|
|
|
|
|
throw new Exception('File size not allowed', 400, Exception::STORAGE_INVALID_FILE_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$upload->isValid($fileTmpName)) {
|
|
|
|
|
throw new Exception('Invalid file', 403);
|
|
|
|
|
throw new Exception('Invalid file', 403, Exception::STORAGE_INVALID_FILE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save to storage
|
|
|
|
@ -555,7 +555,7 @@ App::post('/v1/functions/:functionId/deployments')
|
|
|
|
|
$chunksUploaded = $deviceFunctions->upload($fileTmpName, $path, $chunk, $chunks, $metadata);
|
|
|
|
|
|
|
|
|
|
if (empty($chunksUploaded)) {
|
|
|
|
|
throw new Exception('Failed moving file', 500);
|
|
|
|
|
throw new Exception('Failed moving file', 500, Exception::GENERAL_SERVER_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($chunksUploaded === $chunks) {
|
|
|
|
@ -664,7 +664,7 @@ App::get('/v1/functions/:functionId/deployments')
|
|
|
|
|
$function = $dbForProject->getDocument('functions', $functionId);
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($cursor)) {
|
|
|
|
@ -723,7 +723,7 @@ App::get('/v1/functions/:functionId/deployments/:deploymentId')
|
|
|
|
|
$function = $dbForProject->getDocument('functions', $functionId);
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$deployment = $dbForProject->getDocument('deployments', $deploymentId);
|
|
|
|
@ -766,7 +766,7 @@ App::delete('/v1/functions/:functionId/deployments/:deploymentId')
|
|
|
|
|
|
|
|
|
|
$function = $dbForProject->getDocument('functions', $functionId);
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$deployment = $dbForProject->getDocument('deployments', $deploymentId);
|
|
|
|
@ -832,7 +832,7 @@ App::post('/v1/functions/:functionId/executions')
|
|
|
|
|
$function = Authorization::skip(fn() => $dbForProject->getDocument('functions', $functionId));
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$runtimes = Config::getParam('runtimes', []);
|
|
|
|
@ -840,7 +840,7 @@ App::post('/v1/functions/:functionId/executions')
|
|
|
|
|
$runtime = (isset($runtimes[$function->getAttribute('runtime', '')])) ? $runtimes[$function->getAttribute('runtime', '')] : null;
|
|
|
|
|
|
|
|
|
|
if (\is_null($runtime)) {
|
|
|
|
|
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported', 400);
|
|
|
|
|
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported', 400, Exception::FUNCTION_RUNTIME_UNSUPPORTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$deployment = Authorization::skip(fn() => $dbForProject->getDocument('deployments', $function->getAttribute('deployment', '')));
|
|
|
|
@ -856,17 +856,17 @@ App::post('/v1/functions/:functionId/executions')
|
|
|
|
|
/** Check if build has completed */
|
|
|
|
|
$build = Authorization::skip(fn() => $dbForProject->getDocument('builds', $deployment->getAttribute('buildId', '')));
|
|
|
|
|
if ($build->isEmpty()) {
|
|
|
|
|
throw new Exception('Build not found', 404);
|
|
|
|
|
throw new Exception('Build not found', 404, Exception::BUILD_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($build->getAttribute('status') !== 'ready') {
|
|
|
|
|
throw new Exception('Build not ready', 400);
|
|
|
|
|
throw new Exception('Build not ready', 400, Exception::BUILD_NOT_READY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$validator = new Authorization('execute');
|
|
|
|
|
|
|
|
|
|
if (!$validator->isValid($function->getAttribute('execute'))) { // Check if user has write access to execute function
|
|
|
|
|
throw new Exception($validator->getDescription(), 401);
|
|
|
|
|
throw new Exception($validator->getDescription(), 401, Exception::USER_UNAUTHORIZED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$executionId = $dbForProject->getId();
|
|
|
|
@ -1000,14 +1000,14 @@ App::get('/v1/functions/:functionId/executions')
|
|
|
|
|
$function = Authorization::skip(fn() => $dbForProject->getDocument('functions', $functionId));
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($cursor)) {
|
|
|
|
|
$cursorExecution = $dbForProject->getDocument('executions', $cursor);
|
|
|
|
|
|
|
|
|
|
if ($cursorExecution->isEmpty()) {
|
|
|
|
|
throw new Exception("Execution '{$cursor}' for the 'cursor' value not found.", 400);
|
|
|
|
|
throw new Exception("Execution '{$cursor}' for the 'cursor' value not found.", 400, Exception::GENERAL_CURSOR_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1050,17 +1050,17 @@ App::get('/v1/functions/:functionId/executions/:executionId')
|
|
|
|
|
$function = Authorization::skip(fn() => $dbForProject->getDocument('functions', $functionId));
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$execution = $dbForProject->getDocument('executions', $executionId);
|
|
|
|
|
|
|
|
|
|
if ($execution->getAttribute('functionId') !== $function->getId()) {
|
|
|
|
|
throw new Exception('Execution not found', 404);
|
|
|
|
|
throw new Exception('Execution not found', 404, Exception::EXECUTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($execution->isEmpty()) {
|
|
|
|
|
throw new Exception('Execution not found', 404);
|
|
|
|
|
throw new Exception('Execution not found', 404, Exception::EXECUTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$response->dynamic($execution, Response::MODEL_EXECUTION);
|
|
|
|
@ -1092,21 +1092,21 @@ App::post('/v1/functions/:functionId/deployments/:deploymentId/builds/:buildId')
|
|
|
|
|
$deployment = $dbForProject->getDocument('deployments', $deploymentId);
|
|
|
|
|
|
|
|
|
|
if ($function->isEmpty()) {
|
|
|
|
|
throw new Exception('Function not found', 404);
|
|
|
|
|
throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($deployment->isEmpty()) {
|
|
|
|
|
throw new Exception('Deployment not found', 404);
|
|
|
|
|
throw new Exception('Deployment not found', 404, Exception::DEPLOYMENT_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$build = Authorization::skip(fn() => $dbForProject->getDocument('builds', $buildId));
|
|
|
|
|
|
|
|
|
|
if ($build->isEmpty()) {
|
|
|
|
|
throw new Exception('Build not found', 404);
|
|
|
|
|
throw new Exception('Build not found', 404, Exception::BUILD_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($build->getAttribute('status') !== 'failed') {
|
|
|
|
|
throw new Exception('Build not failed', 400);
|
|
|
|
|
throw new Exception('Build not failed', 400, Exception::BUILD_IN_PROGRESS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enqueue a message to start the build
|
|
|
|
|