Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat 4103 create wipe appwrite collection java #158

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/examples.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions .idea/runConfigurations/Run.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions java/AppwriteCollectionWiper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# OS
## Mac
.DS_Store
92 changes: 92 additions & 0 deletions java/AppwriteCollectionWiper/Index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import java.util.Map;
import java.util.HashMap;
import java.util.*;
import io.openruntimes.java.*;
import com.google.gson.Gson;
import io.appwrite.Client;
import io.appwrite.exceptions.AppwriteException;
import io.appwrite.services.Databases;
import kotlin.Result;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlin.coroutines.EmptyCoroutineContext;
import org.jetbrains.annotations.NotNull;
import okhttp3.Response;
import com.google.gson.JsonParser;
import com.google.gson.JsonObject;

final Gson gson = new Gson();


public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws Exception {



// Initialize the Appwrite client and databases service
var client = new Client();
var databases = new Databases(client);
var variables = req.getVariables();
var payloadString = req.getPayload();

// Check if the required environment variables are set
if (variables == null
|| !variables.containsKey("APPWRITE_FUNCTION_ENDPOINT")
|| !variables.containsKey("APPWRITE_FUNCTION_API_KEY")
|| !variables.containsKey("APPWRITE_FUNCTION_PROJECT_ID")
|| variables.get("APPWRITE_FUNCTION_ENDPOINT") == null
|| variables.get("APPWRITE_FUNCTION_API_KEY") == null
|| variables.get("APPWRITE_FUNCTION_PROJECT_ID") == null) {
return res.json(Map.of("Environment variables are not set. Function cannot use Appwrite SDK.", false));
} else {
// Set the Appwrite client properties
client
.setEndpoint(variables.get("APPWRITE_FUNCTION_ENDPOINT"))
.setProject(variables.get("APPWRITE_FUNCTION_PROJECT_ID"))
.setKey(variables.get("APPWRITE_FUNCTION_API_KEY"));
}

try {
// Parse the payload data into a Map

Map<String, Object> payload = gson.fromJson(payloadString, Map.class);
String databaseId = (String) payload.get("databaseId");
String collectionId = (String) payload.get("collectionId");
if (payload == null || databaseId == null || collectionId == null) {
return res.json(Map.of("Invalid payload.", false));
}



databases.deleteCollection(
databaseId,
collectionId,
new Continuation<Object>() {
@NotNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}

@Override
public void resumeWith(@NotNull Object o) {
String json = "";
try {
if (o instanceof Result.Failure) {
Result.Failure failure = (Result.Failure) o;
throw failure.exception;
} else {
Response response = (Response) o;
}
} catch (Throwable th) {
System.out.println("ERROR: " + th.toString());
}
}
}
);
return res.json(Map.of("Success", true));

}catch (AppwriteException e) {

return res.json(Map.of("Collection not found", false));
}
}
45 changes: 45 additions & 0 deletions java/AppwriteCollectionWiper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# AppwriteCollectionWiper

Welcome to the documentation of this function 👋 We strongly recommend keeping this file in sync with your function's logic to make sure anyone can easily understand your function in the future. If you don't need documentation, you can remove this file.

## 🤖 Documentation

Simple function similar to typical "hello world" example, but instead, we return a simple JSON that tells everyone how awesome developers are.

<!-- Update with your description, for example 'Create Stripe payment and return payment URL' -->

_Example input:_

This function expects no input

<!-- If input is expected, add example -->

_Example output:_

<!-- Update with your expected output -->

```json
{
"areDevelopersAwesome": true
}
```

## 📝 Environment Variables

List of environment variables used by this cloud function:

- No special environment variables are required for the cloud function.

## 🚀 Deployment

There are two ways of deploying the Appwrite function, both having the same results, but each using a different process. We highly recommend using CLI deployment to achieve the best experience.

### Using CLI

Make sure you have [Appwrite CLI](https://appwrite.io/docs/command-line#installation) installed, and you have successfully logged into your Appwrite server. To make sure Appwrite CLI is ready, you can use the command `appwrite client --debug` and it should respond with green text `✓ Success`.

Make sure you are in the same folder as your `appwrite.json` file and run `appwrite deploy function` to deploy your function. You will be prompted to select which functions you want to deploy.

### Manual using tar.gz

Manual deployment has no requirements and uses Appwrite Console to deploy the tag. First, enter the folder of your function. Then, create a tarball of the whole folder and gzip it. After creating `.tar.gz` file, visit Appwrite Console, click on the `Deploy Tag` button and switch to the `Manual` tab. There, set the `entrypoint` to `src/Index.java`, and upload the file we just generated.
4 changes: 4 additions & 0 deletions java/AppwriteCollectionWiper/deps.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
implementation 'io.appwrite:sdk-for-kotlin:1.2.0'
implementation 'com.google.code.gson:gson:2.9.0'
}
20 changes: 20 additions & 0 deletions java/appwrite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"projectId": "649f0fde61b9f6d9d83c",
"projectName": "CodeDay Open Source",
"functions": [
{
"$id": "64b036a92b2e743106ca",
"name": "AppwriteCollectionWiper",
"runtime": "java-17.0",
"path": "AppwriteCollectionWiper",
"entrypoint": "src/Index.java",
"ignore": [
"build"
],
"execute": [],
"events": [],
"schedule": "",
"timeout": 15
}
]
}
94 changes: 94 additions & 0 deletions java/appwrite/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
_APP_ENV=production
_APP_LOCALE=en
_APP_OPTIONS_ABUSE=enabled
_APP_OPTIONS_FORCE_HTTPS=disabled
_APP_OPENSSL_KEY_V1=Rik
_APP_DOMAIN=localhost
_APP_DOMAIN_TARGET=localhost
_APP_CONSOLE_WHITELIST_ROOT=enabled
_APP_CONSOLE_WHITELIST_EMAILS=
_APP_CONSOLE_WHITELIST_IPS=
_APP_SYSTEM_EMAIL_NAME=Appwrite
_APP_SYSTEM_EMAIL_ADDRESS=team@appwrite.io
_APP_SYSTEM_RESPONSE_FORMAT=
_APP_SYSTEM_SECURITY_EMAIL_ADDRESS=certs@appwrite.io
_APP_USAGE_STATS=enabled
_APP_LOGGING_PROVIDER=
_APP_LOGGING_CONFIG=
_APP_USAGE_AGGREGATION_INTERVAL=30
_APP_USAGE_TIMESERIES_INTERVAL=30
_APP_USAGE_DATABASE_INTERVAL=900
_APP_WORKER_PER_CORE=6
_APP_REDIS_HOST=redis
_APP_REDIS_PORT=6379
_APP_REDIS_USER=
_APP_REDIS_PASS=
_APP_DB_HOST=mariadb
_APP_DB_PORT=3306
_APP_DB_SCHEMA=appwrite
_APP_DB_USER=user
_APP_DB_PASS=password
_APP_DB_ROOT_PASS=rootsecretpassword
_APP_INFLUXDB_HOST=influxdb
_APP_INFLUXDB_PORT=8086
_APP_STATSD_HOST=telegraf
_APP_STATSD_PORT=8125
_APP_SMTP_HOST=
_APP_SMTP_PORT=
_APP_SMTP_SECURE=
_APP_SMTP_USERNAME=
_APP_SMTP_PASSWORD=
_APP_SMS_PROVIDER=
_APP_SMS_FROM=
_APP_STORAGE_LIMIT=30000000
_APP_STORAGE_PREVIEW_LIMIT=20000000
_APP_STORAGE_ANTIVIRUS=disabled
_APP_STORAGE_ANTIVIRUS_HOST=clamav
_APP_STORAGE_ANTIVIRUS_PORT=3310
_APP_STORAGE_DEVICE=local
_APP_STORAGE_S3_ACCESS_KEY=
_APP_STORAGE_S3_SECRET=
_APP_STORAGE_S3_REGION=us-east-1
_APP_STORAGE_S3_BUCKET=
_APP_STORAGE_DO_SPACES_ACCESS_KEY=
_APP_STORAGE_DO_SPACES_SECRET=
_APP_STORAGE_DO_SPACES_REGION=us-east-1
_APP_STORAGE_DO_SPACES_BUCKET=
_APP_STORAGE_BACKBLAZE_ACCESS_KEY=
_APP_STORAGE_BACKBLAZE_SECRET=
_APP_STORAGE_BACKBLAZE_REGION=us-west-004
_APP_STORAGE_BACKBLAZE_BUCKET=
_APP_STORAGE_LINODE_ACCESS_KEY=
_APP_STORAGE_LINODE_SECRET=
_APP_STORAGE_LINODE_REGION=eu-central-1
_APP_STORAGE_LINODE_BUCKET=
_APP_STORAGE_WASABI_ACCESS_KEY=
_APP_STORAGE_WASABI_SECRET=
_APP_STORAGE_WASABI_REGION=eu-central-1
_APP_STORAGE_WASABI_BUCKET=
_APP_FUNCTIONS_SIZE_LIMIT=30000000
_APP_FUNCTIONS_TIMEOUT=900
_APP_FUNCTIONS_BUILD_TIMEOUT=900
_APP_FUNCTIONS_CONTAINERS=10
_APP_FUNCTIONS_CPUS=0
_APP_FUNCTIONS_MEMORY=0
_APP_FUNCTIONS_MEMORY_SWAP=0
_APP_FUNCTIONS_RUNTIMES=php-8.0,python-3.9,ruby-3.0,java-17.0
_APP_EXECUTOR_SECRET=your-secret-key
_APP_EXECUTOR_HOST=http://appwrite-executor/v1
_APP_EXECUTOR_RUNTIME_NETWORK=appwrite_runtimes
_APP_FUNCTIONS_ENVS=php-7.4,python-3.9,ruby-3.0,java-17.0
_APP_FUNCTIONS_INACTIVE_THRESHOLD=60
DOCKERHUB_PULL_USERNAME=
DOCKERHUB_PULL_PASSWORD=
DOCKERHUB_PULL_EMAIL=
OPEN_RUNTIMES_NETWORK=appwrite_runtimes
_APP_MAINTENANCE_INTERVAL=86400
_APP_MAINTENANCE_RETENTION_CACHE=2592000
_APP_MAINTENANCE_RETENTION_EXECUTION=1209600
_APP_MAINTENANCE_RETENTION_AUDIT=1209600
_APP_MAINTENANCE_RETENTION_ABUSE=86400
_APP_MAINTENANCE_RETENTION_USAGE_HOURLY=8640000
_APP_GRAPHQL_MAX_BATCH_SIZE=10
_APP_GRAPHQL_MAX_COMPLEXITY=250
_APP_GRAPHQL_MAX_DEPTH=3
Loading