From 1d710f84ff8b47e3b37db36061de2ea0ffa87586 Mon Sep 17 00:00:00 2001 From: polkx <82140068+polkx@users.noreply.github.com> Date: Tue, 18 Jun 2024 14:08:14 +0200 Subject: [PATCH 1/2] An example of creating a CosmosDB container through a Azure's Cosmos SDK and deploying a Logic App and an API Connection --- examples/azure-cosmosdb-logicapp/.gitignore | 10 + examples/azure-cosmosdb-logicapp/Main.scala | 171 ++++++++++++++++++ examples/azure-cosmosdb-logicapp/Pulumi.yaml | 3 + examples/azure-cosmosdb-logicapp/README.md | 51 ++++++ .../azure-cosmosdb-logicapp/project.scala | 6 + 5 files changed, 241 insertions(+) create mode 100644 examples/azure-cosmosdb-logicapp/.gitignore create mode 100644 examples/azure-cosmosdb-logicapp/Main.scala create mode 100644 examples/azure-cosmosdb-logicapp/Pulumi.yaml create mode 100644 examples/azure-cosmosdb-logicapp/README.md create mode 100644 examples/azure-cosmosdb-logicapp/project.scala diff --git a/examples/azure-cosmosdb-logicapp/.gitignore b/examples/azure-cosmosdb-logicapp/.gitignore new file mode 100644 index 00000000..7da74da6 --- /dev/null +++ b/examples/azure-cosmosdb-logicapp/.gitignore @@ -0,0 +1,10 @@ +### Scala an JVM +*.class +*.log +.bsp +.scala-build + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +kubeconfig.json diff --git a/examples/azure-cosmosdb-logicapp/Main.scala b/examples/azure-cosmosdb-logicapp/Main.scala new file mode 100644 index 00000000..3b5796c1 --- /dev/null +++ b/examples/azure-cosmosdb-logicapp/Main.scala @@ -0,0 +1,171 @@ +import besom.* +import besom.api.azurenative +import besom.json.* + +@main def main = Pulumi.run { + // Create an Azure Resource Group + val resourceGroup = azurenative.resources.ResourceGroup("logicappdemo-rg") + + // Create an Azure resource (Storage Account) + val storageAccount = azurenative.storage.StorageAccount( + name = "logicappdemosa", + azurenative.storage.StorageAccountArgs( + resourceGroupName = resourceGroup.name, + sku = azurenative.storage.inputs.SkuArgs( + name = azurenative.storage.enums.SkuName.Standard_LRS + ), + kind = azurenative.storage.enums.Kind.StorageV2 + ) + ) + + // Cosmos DB Account + val cosmosdbAccount = azurenative.documentdb.DatabaseAccount( + "logicappdemo-cdb", + azurenative.documentdb.DatabaseAccountArgs( + resourceGroupName = resourceGroup.name, + databaseAccountOfferType = azurenative.documentdb.enums.DatabaseAccountOfferType.Standard, + locations = List( + azurenative.documentdb.inputs.LocationArgs( + locationName = resourceGroup.location, + failoverPriority = 0 + ) + ), + consistencyPolicy = azurenative.documentdb.inputs.ConsistencyPolicyArgs( + defaultConsistencyLevel = azurenative.documentdb.enums.DefaultConsistencyLevel.Session + ) + ) + ) + + // Cosmos DB Database + val db = azurenative.documentdb.SqlResourceSqlDatabase( + "sqldb", + azurenative.documentdb.SqlResourceSqlDatabaseArgs( + resourceGroupName = resourceGroup.name, + accountName = cosmosdbAccount.name, + resource = azurenative.documentdb.inputs.SqlDatabaseResourceArgs( + id = "sqldb" + ) + ) + ) + +// Cosmos DB SQL Container + val dbContainer = azurenative.documentdb.SqlResourceSqlContainer( + "container", + azurenative.documentdb.SqlResourceSqlContainerArgs( + resourceGroupName = resourceGroup.name, + accountName = cosmosdbAccount.name, + databaseName = db.name, + resource = azurenative.documentdb.inputs.SqlContainerResourceArgs( + id = "container", + partitionKey = azurenative.documentdb.inputs.ContainerPartitionKeyArgs( + paths = List("/myPartitionKey"), + kind = "Hash" + ) + ) + ) + ) + + val accountKeys = azurenative.documentdb.listDatabaseAccountKeys( + azurenative.documentdb.ListDatabaseAccountKeysArgs( + accountName = cosmosdbAccount.name, + resourceGroupName = resourceGroup.name + ) + ) + val clientConfig = azurenative.authorization.getClientConfig() + + val apiId = + p"/subscriptions/${clientConfig.subscriptionId}/providers/Microsoft.Web/locations/${resourceGroup.location}/managedApis/documentdb" + +// API Connection to be used in a Logic App + val connection = azurenative.web.Connection( + "cosmosdbConnection", + azurenative.web.ConnectionArgs( + resourceGroupName = resourceGroup.name, + properties = azurenative.web.inputs.ApiConnectionDefinitionPropertiesArgs( + displayName = "cosmosdb_connection", + api = azurenative.web.inputs.ApiReferenceArgs( + id = apiId + ), + parameterValues = Map( + "databaseAccount" -> cosmosdbAccount.name, + "accessKey" -> accountKeys.primaryMasterKey + ) + ) + ) + ) + + val path = p"/dbs/${db.name}/colls/${dbContainer.name}/docs" + +// Logic App with an HTTP trigger and Cosmos DB action + val workflow = azurenative.logic.Workflow( + "httpToCosmos", + azurenative.logic.WorkflowArgs( + resourceGroupName = resourceGroup.name, + definition = json"""{ + "$$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "$$connections": { + "defaultValue": {}, + "type": "Object" + } + }, + "triggers": { + "Receive_post": { + "type": "Request", + "kind": "Http", + "inputs": { + "method": "POST", + "schema": { + "properties": {}, + "type": "object" + } + } + } + }, + "actions": { + "write_body": { + "type": "ApiConnection", + "inputs": { + "body": { + "data": "@triggerBody()", + "id": "@utcNow()" + }, + "host": { + "connection": { + "name": "@parameters('$$connections')['documentdb']['connectionId']" + } + }, + "method": "post", + "path": $path + } + } + } + }""", + parameters = Map( + "$connections" -> azurenative.logic.inputs.WorkflowParameterArgs( + value = json"""{ + "documentdb": { + "connectionId": ${connection.id}, + "connectionName": "logicapp-cosmosdb-connection", + "id": $apiId + } + }""" + ) + ) + ) + ) + + val callbackUrls = azurenative.logic.listWorkflowTriggerCallbackUrl( + azurenative.logic.ListWorkflowTriggerCallbackUrlArgs( + resourceGroupName = resourceGroup.name, + workflowName = workflow.name, + triggerName = "Receive_post" + ) + ) + +// Export the HTTP endpoint + Stack(storageAccount).exports( + endpoint = callbackUrls.value + ) +} diff --git a/examples/azure-cosmosdb-logicapp/Pulumi.yaml b/examples/azure-cosmosdb-logicapp/Pulumi.yaml new file mode 100644 index 00000000..3172e3c5 --- /dev/null +++ b/examples/azure-cosmosdb-logicapp/Pulumi.yaml @@ -0,0 +1,3 @@ +name: azure-cosmosdb-logicapp +description: An example of creating a CosmosDB container through a Azure's Cosmos SDK and deploying a Logic App and an API Connection +runtime: scala diff --git a/examples/azure-cosmosdb-logicapp/README.md b/examples/azure-cosmosdb-logicapp/README.md new file mode 100644 index 00000000..0fab8491 --- /dev/null +++ b/examples/azure-cosmosdb-logicapp/README.md @@ -0,0 +1,51 @@ +# Azure Cosmos DB, an API Connection, and a Logic App + +With the native Azure provider we can directly use the Azure resource manager API to define API connections and linking +it to a logic app. The resulting experience is much faster in comparison to performing the same operation through ARM +templates. + +## Deploying the App + +To deploy your infrastructure, follow the below steps. + +### Prerequisites + +1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/) +2. [Configure Azure Credentials](https://www.pulumi.com/docs/intro/cloud-providers/azure/setup/) + +### Steps + +1. Create a new stack, which is an isolated deployment target for this example: + + ```bash + $ pulumi stack init dev + ``` + +2. Set the Azure region location to use: + + ```bash + $ pulumi config set azure-native:location westus2 + ``` + +3. Stand up the cluster by invoking pulumi + ```bash + $ pulumi up + ``` + +4. At this point, you have a Cosmos DB collection and a Logic App listening to HTTP requests. You can trigger the Logic + App with a `curl` command: + + ```bash + $ curl -X POST "$(pulumi stack output endpoint)" -d '"Hello World"' -H 'Content-Type: application/json' + ```` + The POST body will be saved into a new document in the Cosmos DB collection. + +5. From there, feel free to experiment. Simply making edits and running `pulumi up` will incrementally update your + stack. + +6. Once you've finished experimenting, tear down your stack's resources by destroying and removing it: + + ```bash + $ pulumi destroy --yes + $ pulumi stack rm --yes + ``` diff --git a/examples/azure-cosmosdb-logicapp/project.scala b/examples/azure-cosmosdb-logicapp/project.scala new file mode 100644 index 00000000..177dab26 --- /dev/null +++ b/examples/azure-cosmosdb-logicapp/project.scala @@ -0,0 +1,6 @@ +//> using scala "3.3.1" +//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement +//> using dep "org.virtuslab::besom-core:0.4.0-SNAPSHOT" +//> using dep "org.virtuslab::besom-azure-native:2.38.0-core.0.4-SNAPSHOT" + +//> using repository sonatype:snapshots From 8ad93c5582a755ddb95b31fbb3745344bac27b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Bia=C5=82y?= Date: Thu, 22 Aug 2024 17:45:11 +0200 Subject: [PATCH 2/2] bumped version of library solves #521, good to merge --- examples/azure-cosmosdb-logicapp/project.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/azure-cosmosdb-logicapp/project.scala b/examples/azure-cosmosdb-logicapp/project.scala index 177dab26..5d369330 100644 --- a/examples/azure-cosmosdb-logicapp/project.scala +++ b/examples/azure-cosmosdb-logicapp/project.scala @@ -1,6 +1,6 @@ //> using scala "3.3.1" //> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement //> using dep "org.virtuslab::besom-core:0.4.0-SNAPSHOT" -//> using dep "org.virtuslab::besom-azure-native:2.38.0-core.0.4-SNAPSHOT" +//> using dep "org.virtuslab::besom-azure-native:2.56.0-core.0.4-SNAPSHOT" //> using repository sonatype:snapshots