├── ballerina
├── icon.png
├── .gitignore
├── init.bal
├── Package.md
├── error.bal
├── external_functions.bal
├── iterator.bal
├── Ballerina.toml
├── Module.md
├── build.gradle
├── Dependencies.toml
└── constants.bal
├── docs
└── images
│ ├── cred.png
│ ├── connector.gif
│ └── multi-model.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitattributes
├── native
├── src
│ └── main
│ │ ├── resources
│ │ └── META-INF
│ │ │ └── native-image
│ │ │ └── org.ballerinax
│ │ │ └── azure-cosmosdb-native
│ │ │ └── resource-config.json
│ │ └── java
│ │ ├── module-info.java
│ │ └── io
│ │ └── ballerinax
│ │ └── cosmosdb
│ │ ├── BallerinaErrorGenerator.java
│ │ ├── ModuleUtils.java
│ │ ├── RecordIteratorUtils.java
│ │ └── Constants.java
└── build.gradle
├── .github
├── CODEOWNERS
└── workflows
│ ├── trivy-scan.yml
│ ├── pull-request.yml
│ ├── build-with-bal-test-native.yml
│ ├── daily-build.yml
│ ├── release.yml
│ ├── dev-stg-release.yml
│ ├── ci.yml
│ └── weekly-build.yml
├── gradle.properties
├── .gitignore
├── issue_template.md
├── spotbugs-exclude.xml
├── pull_request_template.md
├── examples
├── admin-operations
│ ├── database
│ │ ├── create_database.bal
│ │ ├── get_a_database.bal
│ │ ├── delete_database.bal
│ │ ├── list_databases.bal
│ │ └── database_operations.bal
│ ├── users-permissions
│ │ ├── user
│ │ │ ├── create_user.bal
│ │ │ ├── get_user.bal
│ │ │ ├── delete_user.bal
│ │ │ ├── replace_user_id.bal
│ │ │ └── list_users.bal
│ │ ├── permission
│ │ │ ├── delete_permission.bal
│ │ │ ├── get_permission.bal
│ │ │ ├── list_permissions.bal
│ │ │ ├── create_permission.bal
│ │ │ └── replace_permission.bal
│ │ └── users_access_control.bal
│ ├── container
│ │ ├── get_container.bal
│ │ ├── delete_container.bal
│ │ ├── list_containers.bal
│ │ ├── create_container.bal
│ │ └── container_operations.bal
│ ├── triggers
│ │ ├── delete_trigger.bal
│ │ ├── list_trigger.bal
│ │ ├── create_trigger.bal
│ │ ├── replace_trigger.bal
│ │ └── trigger_operations.bal
│ ├── user-defined-functions
│ │ ├── delete_udf.bal
│ │ ├── list_udf.bal
│ │ ├── create_udf.bal
│ │ ├── replace_udf.bal
│ │ └── user_defined_functions.bal
│ └── offers
│ │ └── offer_operations.bal
├── data-operations
│ ├── stored-procedure
│ │ ├── delete_stored_procedure.bal
│ │ ├── list_stored_procedure.bal
│ │ ├── execute_stored_procedure.bal
│ │ ├── create_stored_procedure.bal
│ │ └── stored_procedure_operations.bal
│ └── documents
│ │ ├── list_documents.bal
│ │ ├── delete_document.bal
│ │ ├── get_document.bal
│ │ ├── query_document.bal
│ │ ├── create_document.bal
│ │ ├── replace_document.bal
│ │ └── document_operations.bal
└── build.gradle
├── settings.gradle
├── gradlew.bat
├── README.md
└── gradlew
/ballerina/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ballerina-platform/module-ballerinax-azure-cosmosdb/HEAD/ballerina/icon.png
--------------------------------------------------------------------------------
/docs/images/cred.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ballerina-platform/module-ballerinax-azure-cosmosdb/HEAD/docs/images/cred.png
--------------------------------------------------------------------------------
/docs/images/connector.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ballerina-platform/module-ballerinax-azure-cosmosdb/HEAD/docs/images/connector.gif
--------------------------------------------------------------------------------
/docs/images/multi-model.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ballerina-platform/module-ballerinax-azure-cosmosdb/HEAD/docs/images/multi-model.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ballerina-platform/module-ballerinax-azure-cosmosdb/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | #
2 | # https://help.github.com/articles/dealing-with-line-endings/
3 | #
4 | # These are explicitly windows files and should use crlf
5 | *.bat text eol=crlf
6 |
7 |
--------------------------------------------------------------------------------
/native/src/main/resources/META-INF/native-image/org.ballerinax/azure-cosmosdb-native/resource-config.json:
--------------------------------------------------------------------------------
1 | {
2 | "resources": {
3 | "includes": [
4 | {
5 | "pattern": "\\Qazure-cosmos.properties\\E"
6 | }
7 | ]
8 | }
9 | }
--------------------------------------------------------------------------------
/.github/CODEOWNERS:
--------------------------------------------------------------------------------
1 | # Lines starting with '#' are comments.
2 | # Each line is a file pattern followed by one or more owners.
3 |
4 | # See: https://help.github.com/articles/about-codeowners/
5 |
6 | # These owners will be the default owners for everything in the repo.
7 | * @NipunaRanasinghe @shafreenAnfar
8 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
4 | networkTimeout=10000
5 | validateDistributionUrl=true
6 | zipStoreBase=GRADLE_USER_HOME
7 | zipStorePath=wrapper/dists
8 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | org.gradle.caching=true
2 | group=org.ballerinalang.azure_cosmosdb
3 | version=4.2.0
4 | ballerinaLangVersion=2201.8.0
5 |
6 | checkstylePluginVersion=10.12.1
7 | spotbugsPluginVersion=5.0.14
8 | shadowJarPluginVersion=8.1.1
9 | downloadPluginVersion=5.4.0
10 | releasePluginVersion=2.8.0
11 | ballerinaGradlePluginVersion=2.2.0
12 |
13 | # Azure CosmosDB dependencies
14 | azureCosmosVersion=4.52.0
15 | nettyVersion=4.1.100.Final
16 | reactorNettyVersion=1.1.13
17 | micrometerVersion=1.12.0
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | !gradle/wrapper/gradle-wrapper.jar
15 | *.jar
16 | *.war
17 | *.nar
18 | *.ear
19 | *.zip
20 | *.tar.gz
21 | *.rar
22 |
23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
24 | hs_err_pid*
25 |
26 | # Java related ignores
27 | build/
28 | target/
29 | .gradle/
30 |
31 | # IDE-related files
32 | .idea
33 | .code
34 | .project
35 | .settings
36 | .vscode
37 |
38 | # Ballerina related ignores
39 | Ballerina.lock
40 |
41 | # Gradle plugin related ignores
42 | docker.env
43 |
--------------------------------------------------------------------------------
/ballerina/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 |
25 | # Ballerina configuartion file
26 | /tests/Config.toml
27 |
28 | # Target folder of ballerina project
29 | /target
30 |
31 | # json files created inside resources folder
32 | /modules
33 |
34 | # resource folder in tests folder
35 | /tests/resources
36 |
37 | # resources folder
38 | /resources/*.json
39 |
40 | # idea files
41 | *.idea
42 |
43 | # .ballerina files
44 | *.ballerina
--------------------------------------------------------------------------------
/issue_template.md:
--------------------------------------------------------------------------------
1 | **Description:**
2 |
3 |
4 | **Suggested Labels:**
5 |
6 |
7 | **Suggested Assignees:**
8 |
9 |
10 | **Affected Product Version:**
11 |
12 | **OS, DB, other environment details and versions:**
13 |
14 | **Steps to reproduce:**
15 |
16 |
17 | **Related Issues:**
18 |
--------------------------------------------------------------------------------
/ballerina/init.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/jballerina.java;
18 |
19 | function init() {
20 | setModule();
21 | }
22 |
23 | function setModule() = @java:Method {
24 | 'class: "io.ballerinax.cosmosdb.ModuleUtils"
25 | } external;
26 |
--------------------------------------------------------------------------------
/native/src/main/java/module-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org)
3 | *
4 | * WSO2 LLC. licenses this file to you under the Apache License,
5 | * Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing,
12 | * software distributed under the License is distributed on an
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | * KIND, either express or implied. See the License for the
15 | * specific language governing permissions and limitations
16 | * under the License.
17 | */
18 |
19 | module io.ballerinax.cosmosdb {
20 | requires io.ballerina.runtime;
21 | requires com.azure.cosmos;
22 | requires io.ballerina.lang.value;
23 |
24 | exports io.ballerinax.cosmosdb;
25 | }
26 |
--------------------------------------------------------------------------------
/ballerina/Package.md:
--------------------------------------------------------------------------------
1 | Connects to [Azure Cosmos DB(SQL) API](https://docs.microsoft.com/en-us/rest/api/cosmos-db/) from Ballerina.
2 |
3 | ## Package overview
4 | The `azure_cosmosdb` is a [Ballerina](https://ballerina.io/) connector for Azure Cosmos DB.
5 |
6 | ### Compatibility
7 | | | Version |
8 | |----------------------|-------------------------------|
9 | | Ballerina Language | Ballerina Swan Lake 2201.7.0 |
10 | | Cosmos DB (SQL)API | 2018-12-31 |
11 |
12 | ## Report issues
13 | To report bugs, request new features, start new discussions, view project boards, etc., go to the [Ballerina Extended Library repository](https://github.com/ballerina-platform/ballerina-extended-library)
14 |
15 | ## Useful links
16 | - Discuss code changes of the Ballerina project via [ballerina-dev@googlegroups.com](mailto:ballerina-dev@googlegroups.com).
17 | - Chat live with us via our [Discord server](https://discord.gg/ballerinalang).
18 | - Post all technical questions on Stack Overflow with the [#ballerina](https://stackoverflow.com/questions/tagged/ballerina) tag
19 |
--------------------------------------------------------------------------------
/.github/workflows/trivy-scan.yml:
--------------------------------------------------------------------------------
1 | name: Trivy
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: '30 20 * * *'
7 |
8 | jobs:
9 | ubuntu-build:
10 | name: Build on Ubuntu
11 | runs-on: ubuntu-latest
12 | steps:
13 | - uses: actions/checkout@v3
14 |
15 | - name: Set up JDK 17
16 | uses: actions/setup-java@v3
17 | with:
18 | distribution: 'temurin'
19 | java-version: 17.0.7
20 |
21 | - name: Set Up Ballerina
22 | uses: ballerina-platform/setup-ballerina@v1.1.0
23 | with:
24 | version: latest
25 |
26 | - name: Build with Gradle
27 | env:
28 | packageUser: ${{ github.actor }}
29 | packagePAT: ${{ secrets.GITHUB_TOKEN }}
30 | run: ./gradlew build -x check -x test -x :azure-cosmosdb-examples:build
31 |
32 | - name: Run Trivy vulnerability scanner
33 | uses: aquasecurity/trivy-action@master
34 | with:
35 | scan-type: 'rootfs'
36 | scan-ref: '.'
37 | skip-dirs: 'gradle/'
38 | format: 'table'
39 | timeout: '10m0s'
40 | exit-code: '1'
41 |
--------------------------------------------------------------------------------
/spotbugs-exclude.xml:
--------------------------------------------------------------------------------
1 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/.github/workflows/pull-request.yml:
--------------------------------------------------------------------------------
1 | name: Pull Request
2 |
3 | on: [ pull_request ]
4 |
5 | jobs:
6 | build:
7 | runs-on: ubuntu-latest
8 |
9 | steps:
10 | - uses: actions/checkout@v3
11 |
12 | # Set up Java Environment
13 | - name: Set up JDK 17
14 | uses: actions/setup-java@v3
15 | with:
16 | distribution: 'temurin'
17 | java-version: 17.0.7
18 |
19 | # Setup Ballerina Environment
20 | - name: Set Up Ballerina
21 | uses: ballerina-platform/setup-ballerina@v1.1.0
22 | with:
23 | version: latest
24 |
25 | # Grant execute permission to the gradlew script
26 | - name: Grant execute permission for gradlew
27 | run: chmod +x gradlew
28 |
29 | # Build the project with Gradle
30 | - name: Build with Gradle
31 | env:
32 | packageUser: ${{ github.actor }}
33 | packagePAT: ${{ secrets.GITHUB_TOKEN }}
34 | JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true
35 | run: |
36 | ./gradlew build -x test
37 |
38 | # Build Ballerina Project
39 | - name: Ballerina Build
40 | run: bal pack ./ballerina
41 | env:
42 | JAVA_HOME: /usr/lib/jvm/default-jvm
43 |
--------------------------------------------------------------------------------
/native/src/main/java/io/ballerinax/cosmosdb/BallerinaErrorGenerator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3 | *
4 | * WSO2 Inc. licenses this file to you under the Apache License,
5 | * Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing,
12 | * software distributed under the License is distributed on an
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | * KIND, either express or implied. See the License for the
15 | * specific language governing permissions and limitations
16 | * under the License.
17 | */
18 |
19 | package io.ballerinax.cosmosdb;
20 |
21 | import io.ballerina.runtime.api.creators.ErrorCreator;
22 | import io.ballerina.runtime.api.utils.StringUtils;
23 | import io.ballerina.runtime.api.values.BError;
24 |
25 | /**
26 | * The class holds the utility methods to generate errors.
27 | */
28 | public class BallerinaErrorGenerator {
29 |
30 | public static BError createBallerinaDatabaseError(Exception e) {
31 | return ErrorCreator.createError(StringUtils.fromString(e.getMessage()));
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/native/src/main/java/io/ballerinax/cosmosdb/ModuleUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2022 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3 | *
4 | * WSO2 Inc. licenses this file to you under the Apache License,
5 | * Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing,
12 | * software distributed under the License is distributed on an
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | * KIND, either express or implied. See the License for the
15 | * specific language governing permissions and limitations
16 | * under the License.
17 | */
18 |
19 | package io.ballerinax.cosmosdb;
20 |
21 | import io.ballerina.runtime.api.Environment;
22 | import io.ballerina.runtime.api.Module;
23 |
24 | /**
25 | * This class will hold module related utility functions.
26 | */
27 | public class ModuleUtils {
28 |
29 | private static Module cosmosdbdbModule = null;
30 |
31 | private ModuleUtils() {
32 | }
33 |
34 | public static void setModule(Environment env) {
35 | cosmosdbdbModule = env.getCurrentModule();
36 | }
37 |
38 | public static Module getModule() {
39 | return cosmosdbdbModule;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/ballerina/error.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | # Extra HTTP status code detail of an error.
18 | #
19 | # + status - The HTTP status code of the error
20 | public type HttpDetail record {
21 | int status;
22 | };
23 |
24 | # The errors which will come from the Azure API call itself.
25 | public type PayloadValidationError distinct error;
26 |
27 | # The payload access errors where the error detail contains the HTTP status.
28 | public type DbOperationError error;
29 |
30 | # The errors which occur when providing an invalid value.
31 | public type InputValidationError distinct error;
32 |
33 | # The union of all types of errors in the connector.
34 | public type Error PayloadValidationError|DbOperationError|InputValidationError|error;
35 |
--------------------------------------------------------------------------------
/pull_request_template.md:
--------------------------------------------------------------------------------
1 | # Description
2 |
3 | Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
4 |
5 | Fixes # (issue)
6 |
7 | Related Pull Requests (remove if not relevant)
8 | - Pull request 1
9 | - Pull request 2
10 |
11 | One line release note:
12 | - One line describing the feature/improvement/fix made by this PR
13 |
14 | ## Type of change
15 |
16 | Please delete options that are not relevant.
17 |
18 | - [ ] Bug fix (non-breaking change which fixes an issue)
19 | - [ ] New feature (non-breaking change which adds functionality)
20 | - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
21 | - [ ] This change requires a documentation update
22 |
23 | # How Has This Been Tested?
24 |
25 | Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
26 |
27 | - [ ] Test A
28 | - [ ] Test B
29 |
30 | **Test Configuration**:
31 | * Ballerina Version:
32 | * Operating System:
33 | * Java SDK:
34 |
35 | # Checklist:
36 |
37 | ### Security checks
38 | - [ ] Followed secure coding standards in http://wso2.com/technical-reports/wso2-secure-engineering-guidelines?
39 | - [ ] Confirmed that this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets?
40 |
--------------------------------------------------------------------------------
/examples/admin-operations/database/create_database.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 |
31 | log:printInfo("Creating database");
32 | cosmosdb:Database|error result = managementClient->createDatabase(databaseId);
33 |
34 | if (result is cosmosdb:Database) {
35 | log:printInfo(result.toString());
36 | log:printInfo("Success!");
37 | } else {
38 | log:printError(result.message());
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/examples/admin-operations/database/get_a_database.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 |
31 | log:printInfo("Reading database by id");
32 | cosmosdb:Database|error result = managementClient->getDatabase(databaseId);
33 |
34 | if (result is cosmosdb:Database) {
35 | log:printInfo(result.toString());
36 | log:printInfo("Success!");
37 | } else {
38 | log:printError(result.message());
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/examples/admin-operations/database/delete_database.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 |
31 | log:printInfo("Deleting database");
32 | cosmosdb:DeleteResponse|error result = managementClient->deleteDatabase(databaseId);
33 |
34 | if (result is cosmosdb:DeleteResponse) {
35 | log:printInfo(result.toString());
36 | log:printInfo("Success!");
37 | } else {
38 | log:printError(result.message());
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/user/create_user.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string userId = "my_user";
31 |
32 | log:printInfo("Creating user");
33 | cosmosdb:User|error result = managementClient->createUser(databaseId, userId);
34 |
35 | if (result is cosmosdb:User) {
36 | log:printInfo(result.toString());
37 | log:printInfo("Success!");
38 | } else {
39 | log:printError(result.message());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/user/get_user.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string userId = "my_user";
31 |
32 | log:printInfo("Get user information");
33 | cosmosdb:User|error result = managementClient->getUser(databaseId, userId);
34 |
35 | if (result is cosmosdb:User) {
36 | log:printInfo(result.toString());
37 | log:printInfo("Success!");
38 | } else {
39 | log:printError(result.message());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/examples/admin-operations/container/get_container.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 |
32 | log:printInfo("Reading container info");
33 | cosmosdb:Container|error result = managementClient->getContainer(databaseId, containerId);
34 |
35 | if (result is cosmosdb:Container) {
36 | log:printInfo(result.toString());
37 | log:printInfo("Success!");
38 | } else {
39 | log:printError(result.message());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/user/delete_user.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string userId = "my_new_user";
31 |
32 | log:printInfo("Delete user");
33 | cosmosdb:DeleteResponse|error result = managementClient->deleteUser(databaseId, userId);
34 |
35 | if (result is cosmosdb:DeleteResponse) {
36 | log:printInfo(result.toString());
37 | log:printInfo("Success!");
38 | } else {
39 | log:printError(result.message());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/examples/admin-operations/container/delete_container.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 |
32 | log:printInfo("Deleting the container");
33 | cosmosdb:DeleteResponse|error result = managementClient->deleteContainer(databaseId, containerId);
34 |
35 | if (result is cosmosdb:DeleteResponse) {
36 | log:printInfo(result.toString());
37 | log:printInfo("Success!");
38 | } else {
39 | log:printError(result.message());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/examples/data-operations/stored-procedure/delete_stored_procedure.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() returns error? {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string storedProcedureId = "my_stored_procedure";
32 |
33 | log:printInfo("Deleting stored procedure");
34 | cosmosdb:StoredProcedureResponse result = check azureCosmosClient->deleteStoredProcedure(databaseId, containerId,
35 | storedProcedureId);
36 |
37 | log:printInfo(result.toString());
38 | log:printInfo("Success!");
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/examples/admin-operations/database/list_databases.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | log:printInfo("Getting list of databases");
30 | stream|error result = managementClient->listDatabases();
31 |
32 | if (result is stream) {
33 | error? e = result.forEach(function (cosmosdb:Database database) {
34 | log:printInfo(database.toString());
35 | });
36 | log:printInfo("Success!");
37 |
38 | } else {
39 | log:printError(result.message());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/user/replace_user_id.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string oldUserId = "my_user";
31 | string newUserId = "my_new_user";
32 |
33 | log:printInfo("Replace user id");
34 | cosmosdb:User|error result = managementClient->replaceUserId(databaseId, oldUserId, newUserId);
35 |
36 | if (result is cosmosdb:User) {
37 | log:printInfo(result.toString());
38 | log:printInfo("Success!");
39 | } else {
40 | log:printError(result.message());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/examples/data-operations/documents/list_documents.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() returns error? {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | int partitionKeyValue = 0;
32 |
33 | log:printInfo("Getting list of documents");
34 | stream result = check azureCosmosClient->getDocumentList(databaseId, containerId,
35 | partitionKeyValue);
36 |
37 | check result.forEach(function (record {} document) {
38 | log:printInfo(document.toString());
39 | });
40 | log:printInfo("Success!");
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/user/list_users.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 |
31 | log:printInfo("List users");
32 | stream|error result = managementClient->listUsers(databaseId);
33 | if (result is stream) {
34 | error? e = result.forEach(function (cosmosdb:User storedPrcedure) {
35 | log:printInfo(storedPrcedure.toString());
36 | });
37 | log:printInfo("Success!");
38 | } else {
39 | log:printError(result.message());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/examples/admin-operations/triggers/delete_trigger.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string triggerId = "my_trigger";
32 |
33 | log:printInfo("Deleting trigger");
34 |
35 | cosmosdb:DeleteResponse|error result = managementClient->deleteTrigger(databaseId, containerId, triggerId);
36 |
37 | if (result is cosmosdb:DeleteResponse) {
38 | log:printInfo(result.toString());
39 | log:printInfo("Success!");
40 | } else {
41 | log:printError(result.message());
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/ballerina/external_functions.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/jballerina.java;
18 |
19 | isolated function ofEpochSecond(int epochSeconds, int nanoAdjustments) returns handle = @java:Method {
20 | 'class: "java.time.Instant",
21 | name: "ofEpochSecond"
22 | } external;
23 |
24 | isolated function getZoneId(handle zoneId) returns handle = @java:Method {
25 | 'class: "java.time.ZoneId",
26 | name: "of"
27 | } external;
28 |
29 | isolated function atZone(handle receiver, handle zoneId) returns handle = @java:Method {
30 | 'class: "java.time.Instant",
31 | name: "atZone"
32 | } external;
33 |
34 | isolated function ofPattern(handle pattern) returns handle = @java:Method {
35 | 'class: "java.time.format.DateTimeFormatter",
36 | name: "ofPattern"
37 | } external;
38 |
39 | isolated function format(handle receiver, handle dateTimeFormatter) returns handle = @java:Method {
40 | 'class: "java.time.ZonedDateTime",
41 | name: "format"
42 | } external;
43 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/permission/delete_permission.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 | public function main() {
28 | string databaseId = "my_database";
29 | string userId = "my_user";
30 | string permissionId = "my_permission";
31 |
32 | log:printInfo("Delete permission");
33 | cosmosdb:DeleteResponse|error result = managementClient->deletePermission(databaseId, userId, permissionId);
34 |
35 | if (result is cosmosdb:DeleteResponse) {
36 | log:printInfo(result.toString());
37 | log:printInfo("Success!");
38 | } else {
39 | log:printError(result.message());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/examples/admin-operations/user-defined-functions/delete_udf.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string udfId = "my_udf";
32 |
33 | log:printInfo("Delete user defined function");
34 | cosmosdb:DeleteResponse|error result = managementClient->deleteUserDefinedFunction(databaseId, containerId, udfId);
35 |
36 | if (result is cosmosdb:DeleteResponse) {
37 | log:printInfo(result.toString());
38 | log:printInfo("Success!");
39 | } else {
40 | log:printError(result.message());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/permission/get_permission.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string userId = "my_user";
31 | string permissionId = "my_permission";
32 |
33 | log:printInfo("Get intormation about one permission");
34 | cosmosdb:Permission|error result = managementClient->getPermission(databaseId, userId, permissionId);
35 |
36 | if (result is cosmosdb:Permission) {
37 | log:printInfo(result.toString());
38 | log:printInfo("Success!");
39 | } else {
40 | log:printError(result.message());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/examples/admin-operations/container/list_containers.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 |
31 | log:printInfo("Getting list of containers");
32 | stream|error result = managementClient->listContainers(databaseId);
33 |
34 | if (result is stream) {
35 | error? e = result.forEach(function (cosmosdb:Container container) {
36 | log:printInfo(container.toString());
37 | });
38 | log:printInfo("Success!");
39 | } else {
40 | log:printError(result.message());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/examples/data-operations/documents/delete_document.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() returns error? {
29 | string databaseId = "my_database";
30 | // Assume partition key of this container is set as /gender which is an int of 0 or 1
31 | string containerId = "my_container";
32 | string documentId = "my_document";
33 | int partitionKeyValue = 0;
34 |
35 | log:printInfo("Deleting the document");
36 | cosmosdb:DocumentResponse documentResponse = check azureCosmosClient->deleteDocument(databaseId, containerId,
37 | documentId, partitionKeyValue);
38 | log:printInfo(documentResponse.toString());
39 | }
40 |
--------------------------------------------------------------------------------
/examples/data-operations/documents/get_document.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() returns error? {
29 | string databaseId = "my_database";
30 | // Assume partition key of this container is set as /gender which is an int of 0 or 1
31 | string containerId = "my_container";
32 | string documentId = "my_document";
33 | int partitionKeyValue = 0;
34 |
35 | log:printInfo("Read the document by id");
36 | cosmosdb:Document result = check azureCosmosClient->getDocument(databaseId, containerId, documentId,
37 | partitionKeyValue);
38 |
39 | log:printInfo(result.toString());
40 | log:printInfo("Success!");
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/examples/admin-operations/triggers/list_trigger.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 |
32 | log:printInfo("List available triggers");
33 | stream|error result = managementClient->listTriggers(databaseId, containerId);
34 |
35 | if (result is stream) {
36 | error? e = result.forEach(function (cosmosdb:Trigger trigger) {
37 | log:printInfo(trigger.toString());
38 | });
39 | log:printInfo("Success!");
40 | } else {
41 | log:printError(result.message());
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/.github/workflows/build-with-bal-test-native.yml:
--------------------------------------------------------------------------------
1 | name: GraalVM Check
2 |
3 | on:
4 | schedule:
5 | - cron: '30 18 * * *'
6 | workflow_dispatch:
7 |
8 | jobs:
9 | build:
10 | runs-on: ubuntu-latest
11 | steps:
12 | - uses: actions/checkout@v3
13 |
14 | - name: Set up JDK 17
15 | uses: actions/setup-java@v3
16 | with:
17 | distribution: 'temurin'
18 | java-version: 17.0.7
19 |
20 | - name: Set Up Ballerina
21 | uses: ballerina-platform/setup-ballerina@v1.1.0
22 | with:
23 | version: latest
24 |
25 | - name: Grant execute permission for gradlew
26 | run: chmod +x gradlew
27 |
28 | - name: Build with Gradle
29 | env:
30 | packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
31 | packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
32 | JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true
33 | run: |
34 | ./gradlew build -x test
35 |
36 | - name: Set up GraalVM
37 | uses: graalvm/setup-graalvm@v1
38 | with:
39 | java-version: '17'
40 | distribution: 'graalvm-community'
41 | github-token: ${{ secrets.GITHUB_TOKEN }}
42 | set-java-home: 'false'
43 |
44 | - name: Check GraalVM installation
45 | run: |
46 | echo "GRAALVM_HOME: ${{ env.GRAALVM_HOME }}"
47 | echo "JAVA_HOME: ${{ env.JAVA_HOME }}"
48 | native-image --version
49 |
50 | - name: Run Ballerina tests using the native executable
51 | run: bal test --graalvm ./ballerina
52 | env:
53 | BASE_URL: ${{ secrets.BASE_URL }}
54 | MASTER_OR_RESOURCE_TOKEN: ${{ secrets.MASTER_OR_RESOURCE_TOKEN }}
55 |
--------------------------------------------------------------------------------
/examples/admin-operations/container/create_container.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 |
32 | log:printInfo("Creating container");
33 | cosmosdb:PartitionKey partitionKey = {
34 | paths: ["/gender"],
35 | keyVersion: 2
36 | };
37 |
38 | cosmosdb:Container|error result = managementClient->createContainer(databaseId, containerId, partitionKey);
39 |
40 | if (result is cosmosdb:Container) {
41 | log:printInfo(result.toString());
42 | log:printInfo("Success!");
43 | } else {
44 | log:printError(result.message());
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/permission/list_permissions.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string userId = "my_user";
31 |
32 | log:printInfo("List permissions");
33 | stream|error result = managementClient->listPermissions(databaseId, userId);
34 |
35 | if (result is stream) {
36 | error? e = result.forEach(function (cosmosdb:Permission storedPrcedure) {
37 | log:printInfo(storedPrcedure.toString());
38 | });
39 | log:printInfo("Success!");
40 | } else {
41 | log:printError(result.message());
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/examples/data-operations/stored-procedure/list_stored_procedure.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 |
32 | log:printInfo("List stored procedure");
33 | stream|error result = azureCosmosClient->listStoredProcedures(databaseId, containerId);
34 | if (result is stream) {
35 | error? e = result.forEach(function (cosmosdb:StoredProcedure storedPrcedure) {
36 | log:printInfo(storedPrcedure.toString());
37 | });
38 | log:printInfo("Success!");
39 | } else {
40 | log:printError(result.message());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/examples/admin-operations/user-defined-functions/list_udf.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 |
32 | log:printInfo("List user defined functions");
33 | stream|error result = managementClient->listUserDefinedFunctions(databaseId, containerId);
34 |
35 | if (result is stream) {
36 | error? e = result.forEach(function (cosmosdb:UserDefinedFunction udf) {
37 | log:printInfo(udf.toString());
38 | });
39 | log:printInfo("Success!");
40 | } else {
41 | log:printError(result.message());
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/examples/data-operations/stored-procedure/execute_stored_procedure.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() returns error? {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string partitionKey = "key";
32 | string storedProcedureId = "my_stored_procedure";
33 |
34 | log:printInfo("Executing stored procedure");
35 | cosmosdb:StoredProcedureExecuteOptions options = {
36 | parameters: ["Sachi"]
37 | };
38 |
39 | cosmosdb:StoredProcedureResponse result = check azureCosmosClient->executeStoredProcedure(databaseId, containerId,
40 | storedProcedureId, partitionKey, options);
41 |
42 | log:printInfo(result.toString());
43 | log:printInfo("Success!");
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/examples/data-operations/documents/query_document.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() returns error? {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 |
32 | log:printInfo("Query1 - Select all from the container where gender 0");
33 | string selectAllQuery = string `SELECT * FROM ${containerId.toString()} f WHERE f.gender = ${0}`;
34 |
35 | cosmosdb:QueryOptions options = {partitionKey : 0};
36 | stream result = check azureCosmosClient->queryDocuments(databaseId, containerId,
37 | selectAllQuery, options);
38 |
39 | check result.forEach(function (record {} queryResult) {
40 | log:printInfo(queryResult.toString());
41 | });
42 | log:printInfo("Success!");
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/examples/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org)
3 | *
4 | * WSO2 LLC. licenses this file to you under the Apache License,
5 | * Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing,
12 | * software distributed under the License is distributed on an
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | * KIND, either express or implied. See the License for the
15 | * specific language governing permissions and limitations
16 | * under the License.
17 | */
18 |
19 | apply plugin: 'java-library'
20 |
21 | description = 'Ballerina - Azure Cosmosdb Examples'
22 |
23 | def filePath = project.fileTree("${project.projectDir}")
24 | def examples = filePath.matching {
25 | include("**/*.bal")
26 | exclude("**/deprecated/**/*.bal")
27 | }
28 |
29 | task testExamples {
30 | dependsOn ":${project.packageName}-ballerina:build"
31 |
32 | doLast {
33 | examples.each { example -> executeBalCommand ("run ${example}", "${project.rootDir}") }
34 | }
35 | }
36 |
37 | task buildExamples {
38 | dependsOn ":${project.packageName}-ballerina:build"
39 |
40 | gradle.taskGraph.whenReady { graph ->
41 | if (graph.hasTask(":${project.packageName}-examples:test")) {
42 | buildExamples.enabled = false
43 | } else {
44 | testExamples.enabled = false
45 | }
46 | }
47 | doLast {
48 | // TODO: Enabled --offline due to a bug in pulling incorrect versions from the central repository.
49 | examples.each { example -> executeBalCommand ("build --offline ${example}", "${project.rootDir}") }
50 | }
51 | }
52 |
53 | test.dependsOn testExamples
54 | build.dependsOn buildExamples
55 |
--------------------------------------------------------------------------------
/.github/workflows/daily-build.yml:
--------------------------------------------------------------------------------
1 | name: Daily Build without Tests
2 |
3 | on:
4 | schedule:
5 | - cron: '30 2 * * *'
6 |
7 | jobs:
8 | build:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v3
12 |
13 | # Set up Java Environment
14 | - name: Set up JDK 17
15 | uses: actions/setup-java@v3
16 | with:
17 | distribution: 'temurin'
18 | java-version: 17.0.7
19 |
20 | # Setup Ballerina Environment
21 | - name: Set Up Ballerina
22 | uses: ballerina-platform/setup-ballerina@v1.1.0
23 | with:
24 | version: latest
25 |
26 | # Grant execute permission to the gradlew script
27 | - name: Grant execute permission for gradlew
28 | run: chmod +x gradlew
29 |
30 | # Build the project with Gradle
31 | - name: Build with Gradle
32 | env:
33 | packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
34 | packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
35 | JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true
36 | run: |
37 | ./gradlew build -x test
38 |
39 | # Build Ballerina Project
40 | - name: Ballerina Build
41 | run: bal pack ./ballerina
42 | env:
43 | JAVA_HOME: /usr/lib/jvm/default-jvm
44 |
45 | # Send notification when build fails
46 | - name: Notify failure
47 | if: ${{ failure() }}
48 | run: |
49 | curl -X POST \
50 | 'https://api.github.com/repos/ballerina-platform/ballerina-release/dispatches' \
51 | -H 'Accept: application/vnd.github.v3+json' \
52 | -H 'Authorization: Bearer ${{ secrets.BALLERINA_BOT_TOKEN }}' \
53 | --data "{
54 | \"event_type\": \"notify-build-failure\",
55 | \"client_payload\": {
56 | \"repoName\": \"module-ballerinax-azure-cosmosdb\"
57 | }
58 | }"
59 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/permission/create_permission.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string userId = "my_user";
32 |
33 | string permissionId = "my_permission";
34 | cosmosdb:PermisssionMode permissionMode = "All";
35 | string permissionResource = string `dbs/${databaseId}/colls/${containerId}`;
36 |
37 | log:printInfo("Create permission for a user");
38 | cosmosdb:Permission|error result = managementClient->createPermission(databaseId, userId, permissionId,
39 | permissionMode, permissionResource);
40 |
41 | if (result is cosmosdb:Permission) {
42 | log:printInfo(result.toString());
43 | log:printInfo("Success!");
44 | } else {
45 | log:printError(result.message());
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/examples/admin-operations/users-permissions/permission/replace_permission.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string userId = "my_user";
32 | string permissionId = "my_permission";
33 |
34 | cosmosdb:PermisssionMode permissionModeReplace = "Read";
35 | string permissionResourceReplace = string `dbs/${databaseId}/colls/${containerId}`;
36 |
37 | log:printInfo("Replace permission");
38 | cosmosdb:Permission|error result = managementClient->replacePermission(databaseId, userId, permissionId,
39 | permissionModeReplace, permissionResourceReplace);
40 |
41 | if (result is cosmosdb:Permission) {
42 | log:printInfo(result.toString());
43 | log:printInfo("Success!");
44 | } else {
45 | log:printError(result.message());
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Deployment
2 |
3 | on:
4 | release:
5 | types: [ published ]
6 |
7 | jobs:
8 | build:
9 | runs-on: ubuntu-latest
10 |
11 | steps:
12 | - uses: actions/checkout@v3
13 |
14 | # Set up Java Environment
15 | - name: Set up JDK 17
16 | uses: actions/setup-java@v3
17 | with:
18 | distribution: 'temurin'
19 | java-version: 17.0.7
20 |
21 | # Setup Ballerina Environment
22 | - name: Set Up Ballerina
23 | uses: ballerina-platform/setup-ballerina@v1.1.0
24 | with:
25 | version: 2201.7.0
26 |
27 | # Grant execute permission to the gradlew script
28 | - name: Grant execute permission for gradlew
29 | run: chmod +x gradlew
30 |
31 | # Build the project with Gradle
32 | - name: Build with Gradle
33 | env:
34 | packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
35 | packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
36 | JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true
37 | run: |
38 | ./gradlew build -x test -x :azure-cosmosdb-examples:build
39 |
40 | # Perform Trivy scan
41 | - name: Run Trivy vulnerability scanner
42 | uses: aquasecurity/trivy-action@master
43 | with:
44 | scan-type: 'rootfs'
45 | scan-ref: '.'
46 | skip-dirs: 'gradle/'
47 | format: 'table'
48 | timeout: '10m0s'
49 | exit-code: '1'
50 |
51 | # Build Ballerina Project
52 | - name: Ballerina Build
53 | run: bal pack ./ballerina
54 | env:
55 | JAVA_HOME: /usr/lib/jvm/default-jvm
56 |
57 | # Push to Ballerina Central
58 | - name: Ballerina Push
59 | run: bal push
60 | working-directory: ./ballerina
61 | env:
62 | BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_ACCESS_TOKEN }}
63 | JAVA_HOME: /usr/lib/jvm/default-jvm
64 |
--------------------------------------------------------------------------------
/ballerina/iterator.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/jballerina.java;
18 |
19 | # Represents ResultIterator.
20 | public class ResultIterator {
21 |
22 | private Error? err;
23 |
24 | public isolated function init(Error? err = ()) {
25 | self.err = err;
26 | }
27 |
28 | public isolated function next() returns record {|record {} value;|}|Error? {
29 | record {}|Error? result;
30 | result = nextResult(self);
31 | if result is record {} {
32 | record {|
33 | record {} value;
34 | |} streamRecord = {value: result};
35 | return streamRecord;
36 | } else if result is Error {
37 | self.err = result;
38 | return self.err;
39 | } else {
40 | return result;
41 | }
42 | }
43 | }
44 |
45 | isolated function nextResult(ResultIterator iterator) returns record {}|Error? = @java:Method {
46 | 'class: "io.ballerinax.cosmosdb.RecordIteratorUtils"
47 | } external;
48 |
49 | # Represents CosmosResultIterator.
50 | public class CosmosResultIterator {
51 | public isolated function nextResult(ResultIterator iterator) returns record {}|Error? = @java:Method {
52 | 'class: "io.ballerinax.cosmosdb.RecordIteratorUtils"
53 | } external;
54 | }
55 |
--------------------------------------------------------------------------------
/examples/data-operations/stored-procedure/create_stored_procedure.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() returns error? {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string storedProcedureId = "my_stored_procedure";
32 |
33 | log:printInfo("Creating stored procedure");
34 | string storedProcedureBody = string `function (){
35 | var context = getContext();
36 | var response = context.getResponse();
37 | response.setBody("Hello, World");
38 | }`;
39 |
40 | cosmosdb:StoredProcedureResponse result = check azureCosmosClient->createStoredProcedure(databaseId, containerId,
41 | storedProcedureId, storedProcedureBody);
42 |
43 | log:printInfo(result.toString());
44 | log:printInfo("Success!");
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/examples/data-operations/documents/create_document.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() returns error? {
29 | string databaseId = "my_database";
30 | // Assume partition key of this container is set as /gender which is an int of 0 or 1
31 | string containerId = "my_container";
32 | string documentId = "my_document";
33 |
34 | log:printInfo("Create a new document");
35 | map documentBody = {
36 | "FirstName": "Alan",
37 | "FamilyName": "Turing",
38 | "Parents": [{
39 | "FamilyName": "Turing",
40 | "FirstName": "Julius"
41 | }, {
42 | "FamilyName": "Stoney",
43 | "FirstName": "Ethel"
44 | }],
45 | "gender": 0
46 | };
47 | int partitionKeyValue = 0;
48 |
49 | cosmosdb:DocumentResponse documentResponse = check azureCosmosClient->createDocument(databaseId, containerId,
50 | documentId, documentBody, partitionKeyValue);
51 | log:printInfo(documentResponse.toString());
52 | }
53 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | pluginManagement {
19 | plugins {
20 | id "com.github.spotbugs-base" version "${spotbugsPluginVersion}"
21 | id "com.github.johnrengelman.shadow" version "${shadowJarPluginVersion}"
22 | id "de.undercouch.download" version "${downloadPluginVersion}"
23 | id "net.researchgate.release" version "${releasePluginVersion}"
24 | id "io.ballerina.plugin" version "${ballerinaGradlePluginVersion}"
25 | }
26 |
27 | repositories {
28 | gradlePluginPortal()
29 | maven {
30 | url = 'https://maven.pkg.github.com/ballerina-platform/*'
31 | credentials {
32 | username System.getenv("packageUser")
33 | password System.getenv("packagePAT")
34 | }
35 | }
36 | }
37 | }
38 |
39 | plugins {
40 | id "com.gradle.enterprise" version "3.13.2"
41 | }
42 |
43 | def projectName = 'azure-cosmosdb'
44 | rootProject.name = "ballerinax-${projectName}"
45 |
46 | include ":checkstyle"
47 | include ":${projectName}-native"
48 | include ":${projectName}-ballerina"
49 |
50 | project(':checkstyle').projectDir = file("build-config${File.separator}checkstyle")
51 | project(":${projectName}-native").projectDir = file('native')
52 | project(":${projectName}-ballerina").projectDir = file('ballerina')
53 |
54 | gradleEnterprise {
55 | buildScan {
56 | termsOfServiceUrl = 'https://gradle.com/terms-of-service'
57 | termsOfServiceAgree = 'yes'
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/examples/data-operations/documents/replace_document.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ConnectionConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
27 |
28 | public function main() returns error? {
29 | string databaseId = "my_database";
30 | // Assume partition key of this container is set as /gender which is an int of 0 or 1
31 | string containerId = "my_container";
32 | string documentId = "my_document";
33 | //We have to give the currently existing partition key of this document we can't replace that
34 | int partitionKeyValue = 0;
35 |
36 | log:printInfo("Replacing document");
37 | map documentBody = {
38 | "FirstName": "Alan",
39 | "FamilyName": "Turing",
40 | "Parents": [{
41 | "FamilyName": "Turing",
42 | "FirstName": "Julius"
43 | }, {
44 | "FamilyName": "Turing",
45 | "FirstName": "Ethel"
46 | }],
47 | "gender": 0
48 | };
49 |
50 | cosmosdb:DocumentResponse documentResponse = check azureCosmosClient->replaceDocument(databaseId, containerId,
51 | documentId, documentBody, partitionKeyValue);
52 | log:printInfo(documentResponse.toString());
53 | log:printInfo("Success!");
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/examples/admin-operations/user-defined-functions/create_udf.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string udfId = "my_udf";
32 |
33 | log:printInfo("Creating a user defined function");
34 | string userDefinedFunctionBody = string `function tax(income){
35 | if (income == undefined)
36 | throw 'no input';
37 |
38 | if (income < 1000)
39 | return income * 0.1;
40 | else if (income < 10000)
41 | return income * 0.2;
42 | else
43 | return income * 0.4;
44 | }`;
45 |
46 | cosmosdb:UserDefinedFunction|error result = managementClient->createUserDefinedFunction(databaseId, containerId,
47 | udfId, userDefinedFunctionBody);
48 |
49 | if (result is cosmosdb:UserDefinedFunction) {
50 | log:printInfo(result.toString());
51 | log:printInfo("Success!");
52 | } else {
53 | log:printError(result.message());
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/examples/admin-operations/user-defined-functions/replace_udf.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string udfId = "my_udf";
32 |
33 | log:printInfo("Replacing a user defined function");
34 | string newUserDefinedFuncBody = string `function taxIncome(income){
35 | if (income == undefined)
36 | throw 'no input';
37 | if (income < 1000)
38 | return income * 0.1;
39 | else if (income < 10000)
40 | return income * 0.2;
41 | else
42 | return income * 0.4;
43 | }`;
44 |
45 | cosmosdb:UserDefinedFunction|error result = managementClient->replaceUserDefinedFunction(databaseId, containerId,
46 | udfId, newUserDefinedFuncBody);
47 |
48 | if (result is cosmosdb:UserDefinedFunction) {
49 | log:printInfo(result.toString());
50 | log:printInfo("Success!");
51 | } else {
52 | log:printError(result.message());
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/ballerina/Ballerina.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | distribution = "2201.8.0"
3 | org= "ballerinax"
4 | name= "azure_cosmosdb"
5 | version = "4.2.0"
6 | authors = ["Ballerina"]
7 | keywords = ["IT Operations/Databases", "Cost/Paid", "Vendor/Microsoft"]
8 | icon = "icon.png"
9 | repository = "https://github.com/ballerina-platform/module-ballerinax-azure-cosmosdb"
10 | license = ["Apache-2.0"]
11 |
12 | [build-options]
13 | observabilityIncluded=true
14 |
15 | [platform.java17]
16 | graalvmCompatible = true
17 |
18 | [[platform.java17.dependency]]
19 | groupId = "io.ballerinax"
20 | artifactId = "cosmosdb"
21 | version = "4.2.0"
22 | path = "../native/build/libs/azure-cosmosdb-native-4.2.0.jar"
23 |
24 | [[platform.java17.dependency]]
25 | groupId = "com.azure"
26 | artifactId = "azure-cosmos"
27 | version = "4.52.0"
28 | path = "./lib/azure-cosmos-4.52.0.jar"
29 |
30 | [[platform.java17.dependency]]
31 | groupId = "io.netty"
32 | artifactId = "netty-resolver-dns"
33 | version = "4.1.100.Final"
34 | path = "./lib/netty-resolver-dns-4.1.100.Final.jar"
35 |
36 | [[platform.java17.dependency]]
37 | groupId = "io.netty"
38 | artifactId = "netty-handler"
39 | version = "4.1.100.Final"
40 | path = "./lib/netty-handler-4.1.100.Final.jar"
41 |
42 | [[platform.java17.dependency]]
43 | groupId = "io.netty"
44 | artifactId = "netty-transport-native-unix-common"
45 | version = "4.1.100.Final"
46 | path = "./lib/netty-transport-native-unix-common-4.1.100.Final.jar"
47 |
48 | [[platform.java17.dependency]]
49 | groupId = "io.projectreactor.netty"
50 | artifactId = "reactor-netty-core"
51 | version = "1.1.13"
52 | path = "./lib/reactor-netty-core-1.1.13.jar"
53 |
54 | [[platform.java17.dependency]]
55 | groupId = "io.projectreactor.netty"
56 | artifactId = "reactor-netty"
57 | version = "1.1.13"
58 | path = "./lib/reactor-netty-1.1.13.jar"
59 |
60 | [[platform.java17.dependency]]
61 | groupId = "io.projectreactor.netty"
62 | artifactId = "reactor-netty-http"
63 | version = "1.1.13"
64 | path = "./lib/reactor-netty-http-1.1.13.jar"
65 |
66 | [[platform.java17.dependency]]
67 | groupId = "io.micrometer"
68 | artifactId = "micrometer-observation"
69 | version = "1.12.0"
70 | path = "./lib/micrometer-observation-1.12.0.jar"
71 |
72 | [[platform.java17.dependency]]
73 | groupId = "io.micrometer"
74 | artifactId = "micrometer-core"
75 | version = "1.12.0"
76 | path = "./lib/micrometer-core-1.12.0.jar"
77 |
78 | [[platform.java17.dependency]]
79 | groupId = "io.micrometer"
80 | artifactId = "micrometer-commons"
81 | version = "1.12.0"
82 | path = "./lib/micrometer-commons-1.12.0.jar"
83 |
84 |
--------------------------------------------------------------------------------
/.github/workflows/dev-stg-release.yml:
--------------------------------------------------------------------------------
1 | name: Dev/Staging BCentral Release
2 |
3 | on:
4 | workflow_dispatch:
5 | inputs:
6 | bal_central_environment:
7 | description: Ballerina Central Environment
8 | type: choice
9 | options:
10 | - STAGE
11 | - DEV
12 | required: true
13 |
14 | jobs:
15 | release:
16 | runs-on: ubuntu-latest
17 | env:
18 | BALLERINA_${{ github.event.inputs.bal_central_environment }}_CENTRAL: true
19 |
20 | steps:
21 | - uses: actions/checkout@v3
22 |
23 | # Set up Java Environment
24 | - name: Set up JDK 17
25 | uses: actions/setup-java@v3
26 | with:
27 | distribution: 'temurin'
28 | java-version: 17.0.7
29 |
30 | # Setup Ballerina Environment
31 | - name: Set Up Ballerina
32 | uses: ballerina-platform/setup-ballerina@v1.1.0
33 | with:
34 | version: 2201.7.0
35 |
36 | # Grant execute permission to the gradlew script
37 | - name: Grant execute permission for gradlew
38 | run: chmod +x gradlew
39 |
40 | # Build the project with Gradle
41 | - name: Build with Gradle
42 | env:
43 | packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
44 | packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
45 | JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true
46 | run: |
47 | ./gradlew build -x test -x :azure-cosmosdb-examples:build
48 |
49 | # Perform Trivy scan
50 | - name: Run Trivy vulnerability scanner
51 | uses: aquasecurity/trivy-action@master
52 | with:
53 | scan-type: 'rootfs'
54 | scan-ref: '.'
55 | skip-dirs: 'gradle/'
56 | format: 'table'
57 | timeout: '10m0s'
58 | exit-code: '1'
59 |
60 | # Push to Ballerina Staging Central
61 | - name: Push to Staging
62 | if: github.event.inputs.bal_central_environment == 'STAGE'
63 | run: bal push
64 | working-directory: ./ballerina
65 | env:
66 | BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_STAGE_ACCESS_TOKEN }}
67 | JAVA_HOME: /usr/lib/jvm/default-jvm
68 |
69 | # Push to Ballerina Dev Central
70 | - name: Push to Dev
71 | if: github.event.inputs.bal_central_environment == 'DEV'
72 | run: bal push
73 | working-directory: ./ballerina
74 | env:
75 | BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_DEV_ACCESS_TOKEN }}
76 | JAVA_HOME: /usr/lib/jvm/default-jvm
77 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 | on:
3 | push:
4 | branches:
5 | - main
6 | - 2201.[0-9]+.x
7 | repository_dispatch:
8 | types:
9 | check_connector_for_breaking_changes
10 |
11 | jobs:
12 | build:
13 | runs-on: ubuntu-latest
14 |
15 | steps:
16 | - uses: actions/checkout@v3
17 |
18 | # Set up Java Environment
19 | - name: Set up JDK 17
20 | uses: actions/setup-java@v3
21 | with:
22 | distribution: 'temurin'
23 | java-version: 17.0.7
24 |
25 | # Setup Ballerina Environment
26 | - name: Set Up Ballerina
27 | uses: ballerina-platform/setup-ballerina@v1.1.0
28 | with:
29 | version: latest
30 |
31 | # Grant execute permission to the gradlew script
32 | - name: Grant execute permission for gradlew
33 | run: chmod +x gradlew
34 |
35 | # Build the project with Gradle
36 | - name: Build with Gradle
37 | env:
38 | packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
39 | packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
40 | JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true
41 | run: |
42 | ./gradlew build -x test
43 |
44 | # Build Ballerina Project
45 | - name: Ballerina Build
46 | run: bal pack ./ballerina
47 | env:
48 | JAVA_HOME: /usr/lib/jvm/default-jvm
49 |
50 | # Test Ballerina Project
51 | - name: Ballerina Test
52 | if: github.event_name == 'push'
53 | run: bal test --test-report --code-coverage --coverage-format=xml
54 | env:
55 | BASE_URL: ${{ secrets.BASE_URL }}
56 | MASTER_OR_RESOURCE_TOKEN: ${{ secrets.MASTER_OR_RESOURCE_TOKEN }}
57 | JAVA_HOME: /usr/lib/jvm/default-jvm
58 |
59 | - name: Upload coverage reports to Codecov
60 | uses: codecov/codecov-action@v3
61 |
62 | - name: Alert notifier on failure
63 | if: failure() && (github.event.action == 'check_connector_for_breaking_changes')
64 | run: |
65 | curl -X POST \
66 | 'https://api.github.com/repos/ballerina-platform/ballerina-release/dispatches' \
67 | --header 'Accept: application/vnd.github.v3+json' \
68 | --header 'Authorization: Bearer ${{ secrets.BALLERINA_BOT_TOKEN }}' \
69 | --data-raw '{
70 | "event_type": "notify-ballerinax-connector-build-failure",
71 | "client_payload": {
72 | "repoName": "module-ballerinax-azure-cosmosdb",
73 | "workflow": "CI"
74 | }
75 | }'
76 |
--------------------------------------------------------------------------------
/native/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org)
3 | *
4 | * WSO2 LLC. licenses this file to you under the Apache License,
5 | * Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing,
12 | * software distributed under the License is distributed on an
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | * KIND, either express or implied. See the License for the
15 | * specific language governing permissions and limitations
16 | * under the License.
17 | */
18 |
19 | plugins {
20 | id 'java-library'
21 | id 'checkstyle'
22 | id 'com.github.spotbugs'
23 | }
24 |
25 | description = 'Ballerina - Azure Cosmosdb Native'
26 |
27 | dependencies {
28 | checkstyle project(':checkstyle')
29 | checkstyle "com.puppycrawl.tools:checkstyle:${checkstylePluginVersion}"
30 |
31 | implementation group: 'org.ballerinalang', name: 'ballerina-lang', version: "${ballerinaLangVersion}"
32 | implementation group: 'org.ballerinalang', name: 'value', version: "${ballerinaLangVersion}"
33 | implementation group: 'org.ballerinalang', name: 'ballerina-runtime', version: "${ballerinaLangVersion}"
34 | implementation group: 'com.azure', name: 'azure-cosmos', version: "${azureCosmosVersion}"
35 | }
36 |
37 | def excludePattern = '**/module-info.java'
38 | tasks.withType(Checkstyle) {
39 | exclude excludePattern
40 | }
41 |
42 | checkstyle {
43 | toolVersion "${project.checkstylePluginVersion}"
44 | configFile rootProject.file("build-config/checkstyle/build/checkstyle.xml")
45 | configProperties = ["suppressionFile" : file("${rootDir}/build-config/checkstyle/build/suppressions.xml")]
46 | }
47 |
48 | checkstyleMain.dependsOn(":checkstyle:downloadCheckstyleRuleFiles")
49 |
50 | spotbugsMain {
51 | effort "max"
52 | reportLevel "low"
53 | reportsDir = file("$project.buildDir/reports/spotbugs")
54 | reports {
55 | html.enabled true
56 | text.enabled = true
57 | }
58 | def excludeFile = file("${rootDir}/spotbugs-exclude.xml")
59 | if(excludeFile.exists()) {
60 | excludeFilter = excludeFile
61 | }
62 | }
63 |
64 | spotbugsTest {
65 | enabled = false
66 | }
67 |
68 | compileJava {
69 | doFirst {
70 | options.compilerArgs = [
71 | '--module-path', classpath.asPath,
72 | ]
73 | classpath = files()
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/.github/workflows/weekly-build.yml:
--------------------------------------------------------------------------------
1 | name: Weekly build
2 |
3 | on:
4 | schedule:
5 | - cron: '15 2 * * 1'
6 |
7 | jobs:
8 | build:
9 | runs-on: ubuntu-latest
10 |
11 | steps:
12 | - uses: actions/checkout@v3
13 |
14 | # Set up Java Environment
15 | - name: Set up JDK 17
16 | uses: actions/setup-java@v3
17 | with:
18 | distribution: 'temurin'
19 | java-version: 17.0.7
20 |
21 | # Setup Ballerina Environment
22 | - name: Set Up Ballerina
23 | uses: ballerina-platform/setup-ballerina@v1.1.0
24 | with:
25 | version: latest
26 |
27 | # Grant execute permission to the gradlew script
28 | - name: Grant execute permission for gradlew
29 | run: chmod +x gradlew
30 |
31 | # Build the project with Gradle
32 | - name: Build with Gradle
33 | env:
34 | packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }}
35 | packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }}
36 | JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true
37 | run: |
38 | ./gradlew build -x test
39 |
40 | # Build Ballerina Project
41 | - name: Ballerina Build
42 | run: bal pack ./ballerina
43 | env:
44 | JAVA_HOME: /usr/lib/jvm/default-jvm
45 |
46 | # Test Ballerina Project
47 | - name: Ballerina Test
48 | run: bal test --test-report --code-coverage --coverage-format=xml ./ballerina
49 | env:
50 | BASE_URL: ${{ secrets.BASE_URL }}
51 | MASTER_OR_RESOURCE_TOKEN: ${{ secrets.MASTER_OR_RESOURCE_TOKEN }}
52 | JAVA_HOME: /usr/lib/jvm/default-jvm
53 |
54 | - name: Upload coverage reports to Codecov
55 | uses: codecov/codecov-action@v3
56 |
57 | # Send notification when build fails
58 | - name: Notify failure
59 | if: ${{ failure() }}
60 | run: |
61 | curl \
62 | -X POST 'https://chat.googleapis.com/v1/spaces/${{secrets.BALLERINA_CHAT_ID}}/messages?key=${{secrets.BALLERINA_CHAT_KEY}}&token=${{secrets.BALLERINA_CHAT_TOKEN}}' \
63 | --header 'Content-Type: application/json' \
64 | -d '{"text": "*module-ballerinax-azure-cosmosdb* build failure \nPlease visit for more information"}'
65 | curl \
66 | -X POST 'https://chat.googleapis.com/v1/spaces/${{secrets.CONNECTOR_CHAT_ID}}/messages?key=${{secrets.CONNECTOR_CHAT_KEY}}&token=${{secrets.CONNECTOR_CHAT_TOKEN}}' \
67 | --header 'Content-Type: application/json' \
68 | -d '{"text": "*module-ballerinax-azure-cosmosdb* build failure \nPlease visit for more information"}'
69 |
70 |
--------------------------------------------------------------------------------
/examples/admin-operations/triggers/create_trigger.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string triggerId = "my_trigger";
32 |
33 | log:printInfo("Creating a trigger");
34 | string createTriggerBody =
35 | string `function updateMetadata() {
36 | var context = getContext();
37 | var collection = context.getCollection();
38 | var response = context.getResponse();
39 | var createdDocument = response.getBody();
40 |
41 | // query for metadata document
42 | var filterQuery = 'SELECT * FROM root r WHERE r.id = "_metadata"';
43 | var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery, updateMetadataCallback);
44 | if(!accept) throw "Unable to update metadata, abort";
45 | }
46 |
47 | function updateMetadataCallback(err, documents, responseOptions) {
48 | if(err) throw new Error("Error" + err.message);
49 | if(documents.length != 1) throw "Unable to find metadata document";
50 | var metadataDocument = documents[0];
51 | // update metadata
52 | metadataDocument.createdDocuments += 1;
53 | metadataDocument.createdNames += " " + createdDocument.id;
54 | var accept = collection.replaceDocument(metadataDocument._self, metadataDocument, function(err, docReplaced) {
55 | if(err) throw "Unable to update metadata, abort";
56 | });
57 |
58 | if(!accept) throw "Unable to update metadata, abort";
59 | return;
60 | }`;
61 | cosmosdb:TriggerOperation createTriggerOperationType = "All";
62 | cosmosdb:TriggerType createTriggerType = "Post";
63 |
64 | cosmosdb:Trigger|error result = managementClient->createTrigger(databaseId, containerId, triggerId,
65 | createTriggerBody, createTriggerOperationType, createTriggerType);
66 |
67 | if (result is cosmosdb:Trigger) {
68 | log:printInfo(result.toString());
69 | log:printInfo("Success!");
70 | } else {
71 | log:printError(result.message());
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/examples/admin-operations/triggers/replace_trigger.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/log;
18 | import ballerina/os;
19 | import ballerinax/azure_cosmosdb as cosmosdb;
20 |
21 | cosmosdb:ManagementClientConfig config = {
22 | baseUrl: os:getEnv("BASE_URL"),
23 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
24 | };
25 |
26 | cosmosdb:ManagementClient managementClient = check new (config);
27 |
28 | public function main() {
29 | string databaseId = "my_database";
30 | string containerId = "my_container";
31 | string existingTriggerId = "my_trigger";
32 |
33 | log:printInfo("Replacing a trigger");
34 | string replaceTriggerBody =
35 | string `function replaceFunction() {
36 | var context = getContext();
37 | var collection = context.getCollection();
38 | var response = context.getResponse();
39 | var createdDocument = response.getBody();
40 |
41 | // query for metadata document
42 | var filterQuery = 'SELECT * FROM root r WHERE r.id = "_metadata"';
43 | var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery, updateMetadataCallback);
44 | if(!accept) throw "Unable to update metadata, abort";
45 | }
46 |
47 | function updateMetadataCallback(err, documents, responseOptions) {
48 | if(err) throw new Error("Error" + err.message);
49 | if(documents.length != 1) throw "Unable to find metadata document";
50 | var metadataDocument = documents[0];
51 | // update metadata
52 | metadataDocument.createdDocuments += 1;
53 | metadataDocument.createdNames += " " + createdDocument.id;
54 | var accept = collection.replaceDocument(metadataDocument._self, metadataDocument, function(err, docReplaced) {
55 | if(err) throw "Unable to update metadata, abort";
56 | });
57 |
58 | if(!accept) throw "Unable to update metadata, abort";
59 | return;
60 | }`;
61 | cosmosdb:TriggerOperation replaceTriggerOperation = "All";
62 | cosmosdb:TriggerType replaceTriggerType = "Post";
63 |
64 | cosmosdb:Trigger|error result = managementClient->replaceTrigger(databaseId, containerId, existingTriggerId,
65 | replaceTriggerBody, replaceTriggerOperation, replaceTriggerType);
66 |
67 | if (result is cosmosdb:Trigger) {
68 | log:printInfo(result.toString());
69 | log:printInfo("Success!");
70 | } else {
71 | log:printError(result.message());
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%"=="" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%"=="" set DIRNAME=.
29 | @rem This is normally unused
30 | set APP_BASE_NAME=%~n0
31 | set APP_HOME=%DIRNAME%
32 |
33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
35 |
36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
38 |
39 | @rem Find java.exe
40 | if defined JAVA_HOME goto findJavaFromJavaHome
41 |
42 | set JAVA_EXE=java.exe
43 | %JAVA_EXE% -version >NUL 2>&1
44 | if %ERRORLEVEL% equ 0 goto execute
45 |
46 | echo.
47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
48 | echo.
49 | echo Please set the JAVA_HOME variable in your environment to match the
50 | echo location of your Java installation.
51 |
52 | goto fail
53 |
54 | :findJavaFromJavaHome
55 | set JAVA_HOME=%JAVA_HOME:"=%
56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
57 |
58 | if exist "%JAVA_EXE%" goto execute
59 |
60 | echo.
61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
62 | echo.
63 | echo Please set the JAVA_HOME variable in your environment to match the
64 | echo location of your Java installation.
65 |
66 | goto fail
67 |
68 | :execute
69 | @rem Setup the command line
70 |
71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
72 |
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if %ERRORLEVEL% equ 0 goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | set EXIT_CODE=%ERRORLEVEL%
85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1
86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87 | exit /b %EXIT_CODE%
88 |
89 | :mainEnd
90 | if "%OS%"=="Windows_NT" endlocal
91 |
92 | :omega
93 |
--------------------------------------------------------------------------------
/ballerina/Module.md:
--------------------------------------------------------------------------------
1 | ## Overview
2 | It provides the capability to connect to Azure Cosmos DB and execute CRUD (Create, Read, Update, and Delete) operations
3 | for databases and containers, to execute SQL queries to query containers, etc. In addition, it allows the special
4 | features provided by Cosmos DB such as operations on JavaScript language-integrated queries, management of users and
5 | permissions, etc.
6 |
7 | This module supports [Azure Cosmos DB(SQL) API](https://docs.microsoft.com/en-us/rest/api/cosmos-db/) version `2018-12-31`.
8 | ## Prerequisites
9 | Before using this connector in your Ballerina application, complete the following:
10 |
11 | * [Create a Microsoft Account with Azure Subscription](https://docs.microsoft.com/en-us/learn/modules/create-an-azure-account/)
12 | * [Create an Azure Cosmos DB account](https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-manage-database-account/)
13 | * Obtain tokens
14 | 1. Go to your Azure Cosmos DB account and click **Keys**.
15 | 2. Copy the URI and PRIMARY KEY in the **Read-write Keys** tab.
16 |
17 | ## Quickstart
18 | To use the Azure Cosmos DB connector in your Ballerina application, update the .bal file as follows:
19 |
20 | ### Step 1 - Import connector
21 | Import the `ballerinax/azure_cosmosdb` module into the Ballerina project.
22 | ```ballerina
23 | import ballerinax/azure_cosmosdb as cosmosdb;
24 | ```
25 | ### Step 2 - Create a new connector instance
26 | You can now add the connection configuration with the `Master-Token` or `Resource-Token`, and the resource URI to the
27 | Cosmos DB Account.
28 | ```ballerina
29 | cosmosdb:ConnectionConfig configuration = {
30 | baseUrl: ,
31 | primaryKeyOrResourceToken:
32 | };
33 | cosmosdb:DataPlaneClient azureCosmosClient = check new (configuration);
34 |
35 | ```
36 | ### Step 3 - Invoke connector operation
37 | 1. Create a document
38 | Once you follow the above steps. you can create a new document inside the Cosmos container as shown below. Cosmos DB is designed to store and query JSON-like documents. Therefore, the document you create must be of the `JSON` type. In this example, the document ID is `my_document`
39 |
40 | ```ballerina
41 | map document = {
42 | "FirstName": "Alan",
43 | "FamilyName": "Turing",
44 | "Parents": [{
45 | "FamilyName": "Turing",
46 | "FirstName": "Julius"
47 | }, {
48 | "FamilyName": "Stoney",
49 | "FirstName": "Ethel"
50 | }],
51 | "gender": 0
52 | };
53 | int valueOfPartitionKey = 0;
54 | string id = "my_document";
55 |
56 | cosmosdb:DocumentResponse response = check azureCosmosClient-> createDocument("my_database", "my_container", id, document, valueOfPartitionKey);
57 | ```
58 | **Note:**
59 | - This document is created inside an already existing container with ID **my_container** and the container was created inside a database with ID **my_database**.
60 | - As this container have selected path **/gender** as the partition key path. The document you create should include that path with a valid value.
61 | - The document is represented as `map`
62 |
63 | 2. Use `bal run` command to compile and run the Ballerina program
64 |
65 | **[You can find a list of samples here](https://github.com/ballerina-platform/module-ballerinax-azure-cosmosdb/tree/main/examples)**
66 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Ballerina Azure Cosmos DB Connector
2 | ===================
3 | [](https://github.com/ballerina-platform/module-ballerinax-azure-cosmosdb/actions?query=workflow%3ACI)
4 | [](https://codecov.io/gh/ballerina-platform/module-ballerinax-azure-cosmosdb)
5 | [](https://github.com/ballerina-platform/module-ballerinax-azure-cosmosdb/actions/workflows/trivy-scan.yml)
6 | [](https://github.com/ballerina-platform/module-ballerinax-azure-cosmosdb/commits/master)
7 | [](https://github.com/ballerina-platform/module-ballerinax-azure-cosmosdb/actions/workflows/build-with-bal-test-native.yml)
8 | [](https://opensource.org/licenses/Apache-2.0)
9 |
10 | The Cosmos DB SQL(Core) API supports all database-related operations that are carried out extensively by the existing
11 | developer community.
12 |
13 | The Ballerina connector for Azure Cosmos DB provides the capability to connect to Azure Cosmos DB and to execute basic
14 | CRUD (Create, Read, Update and Delete) operations on databases and containers, executing SQL queries to query
15 | containers, etc. In addition, it allows the special features provided by Cosmos DB such as operations via JavaScript
16 | language-integrated queries and management of users and permissions.
17 |
18 | For more information, see module(s).
19 | - [azure_cosmosdb](cosmosdb/Module.md)
20 |
21 | ## Building from the source
22 | ### Setting up the prerequisites
23 | 1. Download and install Java SE Development Kit (JDK) version 17. You can install either [OpenJDK](https://adoptopenjdk.net/) or [Oracle JDK](https://www.oracle.com/java/technologies/downloads/).
24 | > **Note:** Set the JAVA_HOME environment variable to the path name of the directory into which you installed
25 | JDK.
26 |
27 | 2. Download and install [Ballerina Swan Lake](https://ballerina.io/)
28 |
29 | ### Building the source
30 |
31 | Execute the following commands to build from the source:
32 |
33 | - To build the package:
34 | ```
35 | bal build ./ballerina
36 | ```
37 | - To run tests after build:
38 | ```
39 | bal test ./ballerina
40 | ```
41 | ## Contributing to ballerina
42 |
43 | As an open source project, Ballerina welcomes contributions from the community.
44 |
45 | For more information, see [contribution guidelines](https://github.com/ballerina-platform/ballerina-lang/blob/master/CONTRIBUTING.md).
46 |
47 | ## Code of conduct
48 |
49 | All contributors are encouraged to read the [Ballerina Code of Conduct](https://ballerina.io/code-of-conduct).
50 |
51 | ## Useful links
52 |
53 | * Discuss code changes of the Ballerina project via [ballerina-dev@googlegroups.com](mailto:ballerina-dev@googlegroups.com).
54 | * Chat live with us via our [Discord server](https://discord.gg/ballerinalang).
55 | * Post all technical questions on Stack Overflow with the [#ballerina](https://stackoverflow.com/questions/tagged/ballerina) tag.
56 |
--------------------------------------------------------------------------------
/examples/data-operations/stored-procedure/stored_procedure_operations.bal:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2 | //
3 | // WSO2 Inc. licenses this file to you under the Apache License,
4 | // Version 2.0 (the "License"); you may not use this file except
5 | // in compliance with the License.
6 | // You may obtain a copy of the License at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing,
11 | // software distributed under the License is distributed on an
12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 | // KIND, either express or implied. See the License for the
14 | // specific language governing permissions and limitations
15 | // under the License.
16 |
17 | import ballerina/jballerina.java;
18 | import ballerina/log;
19 | import ballerina/os;
20 | import ballerina/regex;
21 | import ballerinax/azure_cosmosdb as cosmosdb;
22 |
23 | cosmosdb:ConnectionConfig config = {
24 | baseUrl: os:getEnv("BASE_URL"),
25 | primaryKeyOrResourceToken: os:getEnv("MASTER_OR_RESOURCE_TOKEN")
26 | };
27 |
28 | cosmosdb:DataPlaneClient azureCosmosClient = check new (config);
29 |
30 | public function main() returns error? {
31 | string databaseId = "my_database";
32 | string containerId = "my_container";
33 | string primaryKey = "key";
34 | var uuid = createRandomUUIDWithoutHyphens();
35 |
36 | log:printInfo("Creating stored procedure");
37 | string storedProcedureId = string `sproc_${uuid.toString()}`;
38 | string storedProcedureBody = string `function (){
39 | var context = getContext();
40 | var response = context.getResponse();
41 | response.setBody("Hello, World");
42 | }`;
43 |
44 | cosmosdb:StoredProcedureResponse storedProcedureCreateResult = check azureCosmosClient->createStoredProcedure(
45 | databaseId, containerId, storedProcedureId, storedProcedureBody);
46 |
47 | log:printInfo(storedProcedureCreateResult.toString());
48 |
49 | log:printInfo("List stored procedures");
50 | stream spList = check azureCosmosClient->listStoredProcedures(databaseId,
51 | containerId);
52 | error? e = spList.forEach(function(cosmosdb:StoredProcedure storedPrcedure) {
53 | log:printInfo(storedPrcedure.toString());
54 | });
55 | log:printInfo("Success!");
56 |
57 | log:printInfo("Executing stored procedure");
58 | cosmosdb:StoredProcedureExecuteOptions options = {
59 | parameters: ["Sachi"]
60 | };
61 |
62 | cosmosdb:StoredProcedureResponse result = check azureCosmosClient->executeStoredProcedure(databaseId, containerId,
63 | storedProcedureId, primaryKey, options);
64 |
65 | log:printInfo(result.toString());
66 |
67 | log:printInfo("Deleting stored procedure");
68 | cosmosdb:StoredProcedureResponse deletionResult = check azureCosmosClient->deleteStoredProcedure(databaseId,
69 | containerId, storedProcedureId);
70 |
71 | log:printInfo(deletionResult.toString());
72 |
73 | log:printInfo("End!");
74 | }
75 |
76 | function createRandomUUIDWithoutHyphens() returns string {
77 | string? stringUUID = java:toString(createRandomUUID());
78 | if (stringUUID is string) {
79 | stringUUID = 'string:substring(regex:replaceAll(stringUUID, "-", ""), 1, 4);
80 | return stringUUID ?: "";
81 | } else {
82 | return "";
83 | }
84 | }
85 |
86 | function createRandomUUID() returns handle = @java:Method {
87 | name: "randomUUID",
88 | 'class: "java.util.UUID"
89 | } external;
90 |
--------------------------------------------------------------------------------
/native/src/main/java/io/ballerinax/cosmosdb/RecordIteratorUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3 | *
4 | * WSO2 Inc. licenses this file to you under the Apache License,
5 | * Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing,
12 | * software distributed under the License is distributed on an
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | * KIND, either express or implied. See the License for the
15 | * specific language governing permissions and limitations
16 | * under the License.
17 | */
18 |
19 | package io.ballerinax.cosmosdb;
20 |
21 | import com.azure.cosmos.models.CosmosStoredProcedureProperties;
22 | import com.fasterxml.jackson.databind.ObjectMapper;
23 | import io.ballerina.runtime.api.PredefinedTypes;
24 | import io.ballerina.runtime.api.creators.TypeCreator;
25 | import io.ballerina.runtime.api.creators.ValueCreator;
26 | import io.ballerina.runtime.api.types.RecordType;
27 | import io.ballerina.runtime.api.types.UnionType;
28 | import io.ballerina.runtime.api.values.BObject;
29 | import io.ballerina.runtime.api.values.BTypedesc;
30 | import org.ballerinalang.langlib.value.FromJsonStringWithType;
31 |
32 | import java.util.HashMap;
33 | import java.util.Iterator;
34 | import java.util.Map;
35 |
36 | import static io.ballerina.runtime.api.utils.StringUtils.fromString;
37 | import static io.ballerinax.cosmosdb.Constants.STORED_PROCEDURE;
38 |
39 | /**
40 | * This class provides functionality for the `RecordIterator` to iterate.
41 | */
42 | public class RecordIteratorUtils {
43 |
44 | public static Object nextResult(BObject recordIterator) {
45 | RecordType targetType = (RecordType) recordIterator.getNativeData(Constants.RECORD_TYPE);
46 | if (targetType.getName().equals(STORED_PROCEDURE)) {
47 | Iterator results = (Iterator)
48 | recordIterator.getNativeData(Constants.OBJECT_ITERATOR);
49 | if (results.hasNext()) {
50 | Map objectMap = new HashMap<>();
51 | CosmosStoredProcedureProperties next = results.next();
52 | objectMap.put("storedProcedure", fromString(next.getBody()));
53 | objectMap.put("id", fromString(next.getId()));
54 | objectMap.put("eTag", next.getETag());
55 | return ValueCreator.createRecordValue(ModuleUtils.getModule(), targetType.getName(), objectMap);
56 | }
57 | return null;
58 | } else {
59 | Iterator