diff --git a/app/config/collections.php b/app/config/collections.php index 445da94337..237ed3e74f 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1336,7 +1336,7 @@ $collections = [ [ '$collection' => Database::SYSTEM_COLLECTION_RULES, 'label' => 'Code Path', - 'key' => 'codePath', + 'key' => 'path', 'type' => Database::SYSTEM_VAR_TYPE_TEXT, 'default' => '', 'required' => false, @@ -1345,7 +1345,7 @@ $collections = [ [ '$collection' => Database::SYSTEM_COLLECTION_RULES, 'label' => 'Code Size', - 'key' => 'codeSize', + 'key' => 'size', 'type' => Database::SYSTEM_VAR_TYPE_NUMERIC, 'default' => '', 'required' => false, diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index a582d56914..3863efdfff 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -401,11 +401,11 @@ App::post('/v1/functions/:functionId/tags') 'read' => [], 'write' => [], ], - 'dateCreated' => time(), 'functionId' => $function->getId(), + 'dateCreated' => time(), 'command' => $command, - 'codePath' => $path, - 'codeSize' => $size, + 'path' => $path, + 'size' => $size, ]); if (false === $tag) { @@ -413,7 +413,7 @@ App::post('/v1/functions/:functionId/tags') } $usage - ->setParam('storage', $tag->getAttribute('codeSize', 0)) + ->setParam('storage', $tag->getAttribute('size', 0)) ; $response->setStatusCode(Response::STATUS_CODE_CREATED); @@ -518,7 +518,7 @@ App::delete('/v1/functions/:functionId/tags/:tagId') $device = Storage::getDevice('functions'); - if ($device->delete($tag->getAttribute('codePath', ''))) { + if ($device->delete($tag->getAttribute('path', ''))) { if (!$projectDB->deleteDocument($tag->getId())) { throw new Exception('Failed to remove tag from DB', 500); } @@ -535,7 +535,7 @@ App::delete('/v1/functions/:functionId/tags/:tagId') } $usage - ->setParam('storage', $tag->getAttribute('codeSize', 0) * -1) + ->setParam('storage', $tag->getAttribute('size', 0) * -1) ; $response->noContent(); diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index fdb68c11c6..116cb6cfb2 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -332,7 +332,7 @@ App::get('/v1/projects/:projectId/usage') ) + $projectDB->getCount( [ - 'attribute' => 'codeSize', + 'attribute' => 'size', 'filters' => [ '$collection='.Database::SYSTEM_COLLECTION_TAGS, ], diff --git a/app/views/console/functions/function.phtml b/app/views/console/functions/function.phtml index 96ad1cf148..dca2af0f84 100644 --- a/app/views/console/functions/function.phtml +++ b/app/views/console/functions/function.phtml @@ -112,7 +112,7 @@ $timeout = $this->getParam('timeout', 900);  
- +
getAttribute('codePath', '')); + if ($device->delete($document->getAttribute('path', ''))) { + Console::success('Delete code tag: '.$document->getAttribute('path', '')); } else { - Console::error('Dailed to delete code tag: '.$document->getAttribute('codePath', '')); + Console::error('Dailed to delete code tag: '.$document->getAttribute('path', '')); } }); diff --git a/app/workers/functions.php b/app/workers/functions.php index 458632454c..99868d4327 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -266,14 +266,14 @@ class FunctionsV1 $value = "\t\t\t--env {$key}={$value} \\"; }); - $tagPath = $tag->getAttribute('codePath', ''); + $tagPath = $tag->getAttribute('path', ''); $tagPathTarget = '/tmp/project-'.$projectId.'/'.$tag->getId().'/code.tar.gz'; $tagPathTargetDir = \pathinfo($tagPathTarget, PATHINFO_DIRNAME); $container = 'appwrite-function-'.$tag->getId(); $command = \escapeshellcmd($tag->getAttribute('command', '')); if(!\is_readable($tagPath)) { - throw new Exception('Code is not readable: '.$tag->getAttribute('codePath', '')); + throw new Exception('Code is not readable: '.$tag->getAttribute('path', '')); } if (!\file_exists($tagPathTargetDir)) { diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index cd4543340a..609fba3898 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -15,6 +15,7 @@ use Appwrite\Utopia\Response\Model\Session; use Appwrite\Utopia\Response\Model\Team; use Appwrite\Utopia\Response\Model\Locale; use Appwrite\Utopia\Response\Model\Membership; +use Appwrite\Utopia\Response\Model\Tag; use Utopia\Response as UtopiaResponse; class Response extends UtopiaResponse @@ -85,6 +86,7 @@ class Response extends UtopiaResponse ->setModel(new Team()) ->setModel(new Membership()) ->setModel(new Func()) + ->setModel(new Tag()) ; parent::__construct($time); @@ -150,13 +152,15 @@ class Response extends UtopiaResponse $document->setAttribute($key, $rule['default']); } else { + var_dump($data); throw new Exception('Missing response key: '.$key); } } if($rule['array']) { if(!is_array($data[$key])) { - throw new Exception($key.' must be an array of '.$rule['type'].' types'); + var_dump($data); + throw new Exception($key.' must be an array of type '.$rule['type']); } foreach ($data[$key] as &$item) { diff --git a/src/Appwrite/Utopia/Response/Model/Func.php b/src/Appwrite/Utopia/Response/Model/Func.php index 38596892fe..dbe563913f 100644 --- a/src/Appwrite/Utopia/Response/Model/Func.php +++ b/src/Appwrite/Utopia/Response/Model/Func.php @@ -55,7 +55,7 @@ class Func extends Model ->addRule('events', [ 'type' => 'string', 'description' => 'Function trigger events.', - 'default' => '', + 'default' => [], 'example' => 'account.create', 'array' => true, ]) @@ -69,11 +69,13 @@ class Func extends Model 'type' => 'integer', 'description' => 'Function next scheduled execution date in Unix timestamp.', 'example' => 1592981292, + 'default' => 0, ]) ->addRule('previous', [ 'type' => 'integer', 'description' => 'Function next scheduled execution date in Unix timestamp.', 'example' => 1592981237, + 'default' => 0, ]) ->addRule('timeout', [ 'type' => 'integer', diff --git a/src/Appwrite/Utopia/Response/Model/Tag.php b/src/Appwrite/Utopia/Response/Model/Tag.php new file mode 100644 index 0000000000..4b6facab82 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/Tag.php @@ -0,0 +1,60 @@ +addRule('$id', [ + 'type' => 'string', + 'description' => 'Tag ID.', + 'example' => '5e5ea5c16897e', + ]) + ->addRule('functionId', [ + 'type' => 'string', + 'description' => 'Function ID.', + 'example' => '5e5ea6g16897e', + ]) + ->addRule('dateCreated', [ + 'type' => 'integer', + 'description' => 'The tag creation date in Unix timestamp.', + 'example' => 1592981250, + ]) + ->addRule('command', [ + 'type' => 'string', + 'description' => 'The entrypoint command in use to execute the tag code.', + 'example' => 'enabled', + ]) + ->addRule('size', [ + 'type' => 'string', + 'description' => 'The code size in bytes.', + 'example' => 'python-3.8', + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName():string + { + return 'Tag'; + } + + /** + * Get Collection + * + * @return string + */ + public function getType():string + { + return Response::MODEL_TAG; + } +} \ No newline at end of file diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 541c8a69ea..a09f4b14eb 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -192,8 +192,7 @@ class FunctionsConsoleServerTest extends Scope $this->assertNotEmpty($tag['body']['$id']); $this->assertIsInt($tag['body']['dateCreated']); $this->assertEquals('php function.php', $tag['body']['command']); - $this->assertStringStartsWith('/storage/functions/app-', $tag['body']['codePath']); - $this->assertEquals(751, $tag['body']['codeSize']); + $this->assertEquals(751, $tag['body']['size']); /** * Test for FAILURE @@ -265,7 +264,7 @@ class FunctionsConsoleServerTest extends Scope ], $this->getHeaders())); $this->assertEquals(200, $function['headers']['status-code']); - $this->assertEquals(751, $function['body']['codeSize']); + $this->assertEquals(751, $function['body']['size']); /** * Test for FAILURE