Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

An example of azure-cosmosdb-logicapp #520

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/azure-cosmosdb-logicapp/.gitignore
Original file line number Diff line number Diff line change
@@ -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
171 changes: 171 additions & 0 deletions examples/azure-cosmosdb-logicapp/Main.scala
Original file line number Diff line number Diff line change
@@ -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
)
}
3 changes: 3 additions & 0 deletions examples/azure-cosmosdb-logicapp/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions examples/azure-cosmosdb-logicapp/README.md
Original file line number Diff line number Diff line change
@@ -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
```
6 changes: 6 additions & 0 deletions examples/azure-cosmosdb-logicapp/project.scala
Original file line number Diff line number Diff line change
@@ -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
Loading