diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 40e325ca6..550ea0164 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1111,3 +1111,243 @@ App::post('/v1/functions/:functionId/deployments/:deploymentId/builds/:buildId') $response->noContent(); }); + +// Variables + +App::post('/v1/functions/variables/:functionId') + ->desc('Create Variable') + ->groups(['api', 'functions']) + ->label('scope', 'functions.write') + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) + ->label('sdk.namespace', 'functions') + ->label('sdk.method', 'createVariable') + ->label('sdk.response.code', Response::STATUS_CODE_CREATED) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_VARIABLE) + ->param('functionId', null, new UID(), 'Function unique ID.', false) + ->param('key', null, new Text(255), 'Variable key. Max length: 255 chars.', false) + ->param('value', null, new Text(16384), 'Variable value. Max length: 16384 chars.', false) + ->inject('response') + ->inject('dbForConsole') + ->inject('dbForProject') + ->inject('projectId') + ->action(function (string $functionId, string $key, string $value, Response $response, Database $dbForConsole, Database $dbForProject, string $projectId) { + + $project = $dbForConsole->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); + } + + $function = $dbForProject->getDocument('functions', $functionId); + + if ($function->isEmpty()) { + throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); + } + + $variable = new Document([ + '$id' => $dbForProject->getId(), + '$read' => ['role:all'], + '$write' => ['role:all'], + 'functionInternalId' => $function->getInternalId(), + 'functionId' => $function->getId(), + 'key' => $key, + 'value' => $value, + ]); + + try { + $variable = $dbForProject->createDocument('variables', $variable); + } catch (DuplicateException $th) { + throw new Exception('Variable name already used.', 409, Exception::VARIABLE_ALREADY_EXISTS); + } + + $dbForProject->deleteCachedDocument('functions', $function->getId()); + + $response->setStatusCode(Response::STATUS_CODE_CREATED); + $response->dynamic($variable, Response::MODEL_VARIABLE); + }); + +App::get('/v1/functions/variables/:functionId') + ->desc('List Variables') + ->groups(['api', 'functions']) + ->label('scope', 'functions.read') + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) + ->label('sdk.namespace', 'functions') + ->label('sdk.method', 'listVariables') + ->label('sdk.response.code', Response::STATUS_CODE_OK) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_VARIABLE_LIST) + ->param('functionId', null, new UID(), 'Function unique ID.', false) + // TODO: Pagination + ->inject('response') + ->inject('dbForConsole') + ->inject('dbForProject') + ->inject('projectId') + ->action(function (string $functionId, Response $response, Database $dbForConsole, Database $dbForProject, string $projectId) { + + $project = $dbForConsole->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); + } + + $function = $dbForProject->getDocument('functions', $functionId); + + if ($function->isEmpty()) { + throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); + } + + $variables = $dbForProject->find('variables', [ + new Query('functionInternalId', Query::TYPE_EQUAL, [$function->getInternalId()]), + ], 5000); + + $response->dynamic(new Document([ + 'variables' => $variables, + 'total' => count($variables), + ]), Response::MODEL_VARIABLE_LIST); + }); + +App::get('/v1/functions/variables/:functionId/:variableId') + ->desc('Get Variable') + ->groups(['api', 'functions']) + ->label('scope', 'functions.read') + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) + ->label('sdk.namespace', 'functions') + ->label('sdk.method', 'getVariable') + ->label('sdk.response.code', Response::STATUS_CODE_OK) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_VARIABLE) + ->param('functionId', null, new UID(), 'Function unique ID.', false) + ->param('variableId', null, new UID(), 'Variable unique ID.', false) + ->inject('response') + ->inject('dbForConsole') + ->inject('dbForProject') + ->inject('projectId') + ->action(function (string $functionId, string $variableId, Response $response, Database $dbForConsole, Database $dbForProject, string $projectId) { + + $project = $dbForConsole->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); + } + + $function = $dbForProject->getDocument('functions', $functionId); + + if ($function->isEmpty()) { + throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); + } + + $variable = $dbForProject->findOne('variables', [ + new Query('_uid', Query::TYPE_EQUAL, [$variableId]), + new Query('functionInternalId', Query::TYPE_EQUAL, [$function->getInternalId()]) + ]); + + if ($variable === false || $variable->isEmpty()) { + throw new Exception('Variable not found', 404, Exception::VARIABLE_NOT_FOUND); + } + + $response->dynamic($variable, Response::MODEL_VARIABLE); + }); + +App::put('/v1/functions/variables/:functionId/:variableId') + ->desc('Update Variable') + ->groups(['api', 'functions']) + ->label('scope', 'functions.write') + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) + ->label('sdk.namespace', 'functions') + ->label('sdk.method', 'updateVariable') + ->label('sdk.response.code', Response::STATUS_CODE_OK) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_VARIABLE) + ->param('functionId', null, new UID(), 'Function unique ID.', false) + ->param('variableId', null, new UID(), 'Variable unique ID.', false) + ->param('key', null, new Text(255), 'Variable key. Max length: 255 chars.', true) + ->param('value', null, new Text(16384), 'Variable value. Max length: 16384 chars.', true) + ->inject('response') + ->inject('dbForConsole') + ->inject('dbForProject') + ->action(function (string $projectId, string $functionId, string $variableId, ?string $key, ?string $value, Response $response, Database $dbForConsole, Database $dbForProject) { + if (empty($key) && empty($value)) { + throw new Exception('Missing key or value. Define at least one.', 400, Exception::VARIABLE_MISSING_PAYLOAD); + } + + $project = $dbForConsole->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); + } + + $function = $dbForProject->getDocument('functions', $functionId); + + if ($function->isEmpty()) { + throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); + } + + $variable = $dbForProject->findOne('variables', [ + new Query('_uid', Query::TYPE_EQUAL, [$variableId]), + new Query('functionInternalId', Query::TYPE_EQUAL, [$function->getInternalId()]) + ]); + + if ($variable === false || $variable->isEmpty()) { + throw new Exception('Variable not found', 404, Exception::VARIABLE_NOT_FOUND); + } + + $variable + ->setAttribute('key', $key ?? $variable->getAttribute('key')) + ->setAttribute('value', $value ?? $variable->getAttribute('value')) + ; + + try { + $dbForProject->updateDocument('variables', $variable->getId(), $variable); + } catch (DuplicateException $th) { + throw new Exception('Variable name already used.', 409, Exception::VARIABLE_ALREADY_EXISTS); + } + + $dbForProject->deleteCachedDocument('functions', $function->getId()); + + $response->dynamic($variable, Response::MODEL_VARIABLE); + }); + +App::delete('/v1/functions/variables/:functionId/:variableId') + ->desc('Delete Variable') + ->groups(['api', 'functions']) + ->label('scope', 'functions.write') + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) + ->label('sdk.namespace', 'functions') + ->label('sdk.method', 'deleteVariable') + ->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT) + ->label('sdk.response.model', Response::MODEL_NONE) + ->param('functionId', null, new UID(), 'Function unique ID.', false) + ->param('variableId', null, new UID(), 'Variable unique ID.', false) + ->inject('response') + ->inject('dbForConsole') + ->inject('dbForProject') + ->action(function (string $projectId, string $functionId, string $variableId, Response $response, Database $dbForConsole, Database $dbForProject) { + + $project = $dbForConsole->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); + } + + $function = $dbForProject->getDocument('functions', $functionId); + + if ($function->isEmpty()) { + throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); + } + + $variable = $dbForProject->findOne('variables', [ + new Query('_uid', Query::TYPE_EQUAL, [$variableId]), + new Query('functionInternalId', Query::TYPE_EQUAL, [$function->getInternalId()]) + ]); + + if ($variable === false || $variable->isEmpty()) { + throw new Exception('Variable not found', 404, Exception::VARIABLE_NOT_FOUND); + } + + $dbForProject->deleteDocument('variables', $variable->getId()); + + $dbForProject->deleteCachedDocument('functions', $function->getId()); + + $response->noContent(); + }); diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 1e1eb23a6..650fc43f4 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -1425,247 +1425,4 @@ App::delete('/v1/projects/:projectId/domains/:domainId') ->setDocument($domain); $response->noContent(); - }); - - -// Variables - -App::post('/v1/projects/:projectId/variables/:functionId') - ->desc('Create Variable') - ->groups(['api', 'projectsScoped']) - ->label('scope', 'projects.write') - ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) - ->label('sdk.namespace', 'projects') - ->label('sdk.method', 'createVariable') - ->label('sdk.response.code', Response::STATUS_CODE_CREATED) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_VARIABLE) - ->param('projectId', null, new UID(), 'Project unique ID.') - ->param('functionId', null, new UID(), 'Function unique ID.', true) - ->param('key', null, new Text(255), 'Variable key. Max length: 255 chars.') - ->param('value', null, new Text(16384), 'Variable value. Max length: 16384 chars.') - ->inject('response') - ->inject('dbForConsole') - ->inject('dbForProject') - ->action(function (string $projectId, string $functionId, string $key, string $value, Response $response, Database $dbForConsole, Database $dbForProject) { - - $project = $dbForConsole->getDocument('projects', $projectId); - - if ($project->isEmpty()) { - throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); - } - - $function = $dbForProject->getDocument('functions', $functionId); - - if ($function->isEmpty()) { - throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); - } - - $variable = new Document([ - '$id' => $dbForProject->getId(), - '$read' => ['role:all'], - '$write' => ['role:all'], - 'functionInternalId' => $function->getInternalId(), - 'functionId' => $function->getId(), - 'key' => $key, - 'value' => $value, - ]); - - try { - $variable = $dbForProject->createDocument('variables', $variable); - } catch (DuplicateException $th) { - throw new Exception('Variable name already used.', 409, Exception::VARIABLE_ALREADY_EXISTS); - } - - $dbForProject->deleteCachedDocument('functions', $function->getId()); - - $response->setStatusCode(Response::STATUS_CODE_CREATED); - $response->dynamic($variable, Response::MODEL_VARIABLE); - }); - -App::get('/v1/projects/:projectId/variables/:functionId') - ->desc('List Variables') - ->groups(['api', 'projectsScoped']) - ->label('scope', 'projects.read') - ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) - ->label('sdk.namespace', 'projects') - ->label('sdk.method', 'listVariables') - ->label('sdk.response.code', Response::STATUS_CODE_OK) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_VARIABLE_LIST) - ->param('projectId', null, new UID(), 'Project unique ID.') - ->param('functionId', null, new UID(), 'Function unique ID.', true) - // TODO: Pagination - ->inject('response') - ->inject('dbForConsole') - ->inject('dbForProject') - ->action(function (string $projectId, string $functionId, Response $response, Database $dbForConsole, Database $dbForProject) { - - $project = $dbForConsole->getDocument('projects', $projectId); - - if ($project->isEmpty()) { - throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); - } - - $function = $dbForProject->getDocument('functions', $functionId); - - if ($function->isEmpty()) { - throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); - } - - $variables = $dbForProject->find('variables', [ - new Query('functionInternalId', Query::TYPE_EQUAL, [$function->getInternalId()]), - ], 5000); - - $response->dynamic(new Document([ - 'variables' => $variables, - 'total' => count($variables), - ]), Response::MODEL_VARIABLE_LIST); - }); - -App::get('/v1/projects/:projectId/variables/:functionId/:variableId') - ->desc('Get Variable') - ->groups(['api', 'projectsScoped']) - ->label('scope', 'projects.read') - ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) - ->label('sdk.namespace', 'projects') - ->label('sdk.method', 'getVariable') - ->label('sdk.response.code', Response::STATUS_CODE_OK) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_VARIABLE) - ->param('projectId', null, new UID(), 'Project unique ID.') - ->param('functionId', null, new UID(), 'Function unique ID.') - ->param('variableId', null, new UID(), 'Variable unique ID.') - ->inject('response') - ->inject('dbForConsole') - ->inject('dbForProject') - ->action(function (string $projectId, string $functionId, string $variableId, Response $response, Database $dbForConsole, Database $dbForProject) { - - $project = $dbForConsole->getDocument('projects', $projectId); - - if ($project->isEmpty()) { - throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); - } - - $function = $dbForProject->getDocument('functions', $functionId); - - if ($function->isEmpty()) { - throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); - } - - $variable = $dbForProject->findOne('variables', [ - new Query('_uid', Query::TYPE_EQUAL, [$variableId]), - new Query('functionInternalId', Query::TYPE_EQUAL, [$function->getInternalId()]) - ]); - - if ($variable === false || $variable->isEmpty()) { - throw new Exception('Variable not found', 404, Exception::VARIABLE_NOT_FOUND); - } - - $response->dynamic($variable, Response::MODEL_VARIABLE); - }); - -App::put('/v1/projects/:projectId/variables/:functionId/:variableId') - ->desc('Update Variable') - ->groups(['api', 'projectsScoped']) - ->label('scope', 'projects.write') - ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) - ->label('sdk.namespace', 'projects') - ->label('sdk.method', 'updateVariable') - ->label('sdk.response.code', Response::STATUS_CODE_OK) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_VARIABLE) - ->param('projectId', null, new UID(), 'Project unique ID.') - ->param('functionId', null, new UID(), 'Function unique ID.') - ->param('variableId', null, new UID(), 'Variable unique ID.') - ->param('key', null, new Text(255), 'Variable key. Max length: 255 chars.', true) - ->param('value', null, new Text(16384), 'Variable value. Max length: 16384 chars.', true) - ->inject('response') - ->inject('dbForConsole') - ->inject('dbForProject') - ->action(function (string $projectId, string $functionId, string $variableId, ?string $key, ?string $value, Response $response, Database $dbForConsole, Database $dbForProject) { - if (empty($key) && empty($value)) { - throw new Exception('Missing key or value. Define at least one.', 400, Exception::VARIABLE_MISSING_PAYLOAD); - } - - $project = $dbForConsole->getDocument('projects', $projectId); - - if ($project->isEmpty()) { - throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); - } - - $function = $dbForProject->getDocument('functions', $functionId); - - if ($function->isEmpty()) { - throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); - } - - $variable = $dbForProject->findOne('variables', [ - new Query('_uid', Query::TYPE_EQUAL, [$variableId]), - new Query('functionInternalId', Query::TYPE_EQUAL, [$function->getInternalId()]) - ]); - - if ($variable === false || $variable->isEmpty()) { - throw new Exception('Variable not found', 404, Exception::VARIABLE_NOT_FOUND); - } - - $variable - ->setAttribute('key', $key ?? $variable->getAttribute('key')) - ->setAttribute('value', $value ?? $variable->getAttribute('value')) - ; - - try { - $dbForProject->updateDocument('variables', $variable->getId(), $variable); - } catch (DuplicateException $th) { - throw new Exception('Variable name already used.', 409, Exception::VARIABLE_ALREADY_EXISTS); - } - - $dbForProject->deleteCachedDocument('functions', $function->getId()); - - $response->dynamic($variable, Response::MODEL_VARIABLE); - }); - -App::delete('/v1/projects/:projectId/variables/:functionId/:variableId') - ->desc('Delete Variable') - ->groups(['api', 'projectsScoped']) - ->label('scope', 'projects.write') - ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) - ->label('sdk.namespace', 'projects') - ->label('sdk.method', 'deleteVariable') - ->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT) - ->label('sdk.response.model', Response::MODEL_NONE) - ->param('projectId', null, new UID(), 'Project unique ID.') - ->param('functionId', null, new UID(), 'Function unique ID.') - ->param('variableId', null, new UID(), 'Variable unique ID.') - ->inject('response') - ->inject('dbForConsole') - ->inject('dbForProject') - ->action(function (string $projectId, string $functionId, string $variableId, Response $response, Database $dbForConsole, Database $dbForProject) { - - $project = $dbForConsole->getDocument('projects', $projectId); - - if ($project->isEmpty()) { - throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); - } - - $function = $dbForProject->getDocument('functions', $functionId); - - if ($function->isEmpty()) { - throw new Exception('Function not found', 404, Exception::FUNCTION_NOT_FOUND); - } - - $variable = $dbForProject->findOne('variables', [ - new Query('_uid', Query::TYPE_EQUAL, [$variableId]), - new Query('functionInternalId', Query::TYPE_EQUAL, [$function->getInternalId()]) - ]); - - if ($variable === false || $variable->isEmpty()) { - throw new Exception('Variable not found', 404, Exception::VARIABLE_NOT_FOUND); - } - - $dbForProject->deleteDocument('variables', $variable->getId()); - - $dbForProject->deleteCachedDocument('functions', $function->getId()); - - $response->noContent(); - }); + }); \ No newline at end of file diff --git a/app/views/console/functions/function.phtml b/app/views/console/functions/function.phtml index 5d5cc20b9..a1a85109e 100644 --- a/app/views/console/functions/function.phtml +++ b/app/views/console/functions/function.phtml @@ -686,6 +686,57 @@ sort($patterns); +
  • +

    Variables

    + + +
    +
    +

    There are currently no variables

    + +

    Add your first variable to store information securely.

    +
    +
    + + + + +