Skip to content

Commit

Permalink
Add/update CosmosDB examples for native Azure (pulumi#923)
Browse files Browse the repository at this point in the history
* Add/update CosmosDB examples for native Azure

* Python

* C# WIP

* Fixed C#

* One more fix
  • Loading branch information
lblackstone committed Mar 1, 2021
1 parent 49d1ae9 commit d2a9ee3
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 27 deletions.
16 changes: 16 additions & 0 deletions azure-nextgen-cs-cosmosdb-logicapp/CosmosDBLogicApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pulumi" Version="2.*" />
<PackageReference Include="Pulumi.AzureNextGen" Version="0.*" />
<PackageReference Include="Pulumi.Docker" Version="2.*" />
<PackageReference Include="Pulumi.Random" Version="3.*" />
</ItemGroup>

</Project>
216 changes: 216 additions & 0 deletions azure-nextgen-cs-cosmosdb-logicapp/MyStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.

using Pulumi;
using System.Collections.Generic;
using Authorization = Pulumi.AzureNextGen.Authorization.Latest;
using DocumentDB = Pulumi.AzureNextGen.DocumentDB.Latest;
using Logic = Pulumi.AzureNextGen.Logic.Latest;
using Resources = Pulumi.AzureNextGen.Resources.Latest;
using Storage = Pulumi.AzureNextGen.Storage.Latest;
using Web = Pulumi.AzureNextGen.Web.Latest;

class MyStack : Stack
{
public MyStack()
{
// Create an Azure Resource Group
var resourceGroup = new Resources.ResourceGroup("resourceGroup", new Resources.ResourceGroupArgs
{
ResourceGroupName = "logicappdemo-rg",
});

// Create an Azure resource (Storage Account)
var storageAccount = new Storage.StorageAccount("storageAccount", new Storage.StorageAccountArgs
{
AccountName = "logicappdemosa21",
ResourceGroupName = resourceGroup.Name,
Sku = new Storage.Inputs.SkuArgs
{
Name = Storage.SkuName.Standard_LRS,
},
Kind = Storage.Kind.StorageV2,
});

// Cosmos DB Account
var cosmosdbAccount = new DocumentDB.DatabaseAccount("cosmosdbAccount", new DocumentDB.DatabaseAccountArgs
{
AccountName = "logicappdemo-cdb",
ResourceGroupName = resourceGroup.Name,
DatabaseAccountOfferType = DocumentDB.DatabaseAccountOfferType.Standard,
Locations =
{
new DocumentDB.Inputs.LocationArgs
{
LocationName = resourceGroup.Location,
FailoverPriority = 0,
},
},
ConsistencyPolicy = new DocumentDB.Inputs.ConsistencyPolicyArgs
{
DefaultConsistencyLevel = DocumentDB.DefaultConsistencyLevel.Session,
},
});

// Cosmos DB Database
var db = new DocumentDB.SqlResourceSqlDatabase("db", new DocumentDB.SqlResourceSqlDatabaseArgs
{
DatabaseName = "sqldb",
ResourceGroupName = resourceGroup.Name,
AccountName = cosmosdbAccount.Name,
Resource = new DocumentDB.Inputs.SqlDatabaseResourceArgs
{
Id = "sqldb",
},
});

// Cosmos DB SQL Container
var dbContainer = new DocumentDB.SqlResourceSqlContainer("dbContainer", new DocumentDB.SqlResourceSqlContainerArgs
{
ContainerName = "container",
ResourceGroupName = resourceGroup.Name,
AccountName = cosmosdbAccount.Name,
DatabaseName = db.Name,
Resource = new DocumentDB.Inputs.SqlContainerResourceArgs
{
Id = "container",
},
});

var accountKeys = Output.Tuple(cosmosdbAccount.Name, resourceGroup.Name).Apply(values =>
{
var cosmosdbAccountName = values.Item1;
var resourceGroupName = values.Item2;
return DocumentDB.ListDatabaseAccountKeys.InvokeAsync(new DocumentDB.ListDatabaseAccountKeysArgs
{
AccountName = cosmosdbAccountName,
ResourceGroupName = resourceGroupName,
});
});

var clientConfig = Output.Create(Authorization.GetClientConfig.InvokeAsync());

var apiId = Output.Tuple(clientConfig, resourceGroup.Location).Apply(values =>
{
var clientConfig = values.Item1;
var location = values.Item2;
return $"/subscriptions/{clientConfig.SubscriptionId}/providers/Microsoft.Web/locations/{location}/managedApis/documentdb";
});

// API Connection to be used in a Logic App
var connection = new Web.Connection("connection", new Web.ConnectionArgs
{
ConnectionName = "cosmosdbConnection",
ResourceGroupName = resourceGroup.Name,
Properties = new Web.Inputs.ApiConnectionDefinitionPropertiesArgs
{
DisplayName = "cosmosdb_connection",
Api = new Web.Inputs.ApiReferenceArgs
{
Id = apiId,
},
ParameterValues =
{
{ "databaseAccount", cosmosdbAccount.Name },
{ "accessKey", accountKeys.Apply(accountKeys => accountKeys.PrimaryMasterKey) },
},
},
});

// Logic App with an HTTP trigger and Cosmos DB action
var workflow = new Logic.Workflow("workflow", new Logic.WorkflowArgs
{
WorkflowName = "httpToCosmos",
ResourceGroupName = resourceGroup.Name,
Definition = new Dictionary<string, object>
{
{ "$schema", "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#" },
{ "contentVersion", "1.0.0.0" },
{ "parameters", new Dictionary<string, object>
{
{ "$connections",new Dictionary<string, object>
{
{ "defaultValue", new Dictionary<string, object>() },
{ "type", "Object" },
} },
} },
{ "triggers", new Dictionary<string, object>
{
{ "Receive_post", new Dictionary<string, object>
{
{ "type", "Request" },
{ "kind", "Http" },
{ "inputs", new Dictionary<string, object>
{
{ "method", "POST" },
{ "schema", new Dictionary<string, object>
{
{ "properties", new Dictionary<string, object>() },
{ "type", "object" },
} },
} },
} },
} },
{ "actions", new Dictionary<string, object>
{
{ "write_body", new Dictionary<string, object>
{
{ "type", "ApiConnection" },
{ "inputs", new Dictionary<string, object>
{
{ "body", new Dictionary<string, object>
{
{ "data", "@triggerBody()" },
{ "id", "@utcNow()" },
} },
{ "host", new Dictionary<string, object>
{
{ "$connection",new Dictionary<string, object>
{
{ "name", "@parameters('$connections')['documentdb']['connectionId']" },
} },
} },
{ "method", "post" },
{ "path", Output.Tuple(db.Name, dbContainer.Name).Apply(values =>
{
var dbName = values.Item1;
var dbContainerName = values.Item2;
return $"/dbs/{dbName}/colls/{dbContainerName}/docs";
}) },
} },
} },
} },
},
Parameters =
{
{ "$connections", new Logic.Inputs.WorkflowParameterArgs
{
Value = new Dictionary<string, object>
{
{ "documentdb", new Dictionary<string, object>
{
{ "connectionId", connection.Id },
{ "connectionName", "logicapp-cosmosdb-connection" },
{ "id", apiId },
} },
},
} },
},
});

var callbackUrls = Output.Tuple(resourceGroup.Name, workflow.Name).Apply(values =>
{
var resourceGroupName = values.Item1;
var workflowName = values.Item2;
return Logic.ListWorkflowTriggerCallbackUrl.InvokeAsync(new Logic.ListWorkflowTriggerCallbackUrlArgs
{
ResourceGroupName = resourceGroupName,
WorkflowName = workflowName,
TriggerName = "Receive_post",
});
});
this.Endpoint = callbackUrls.Apply(callbackUrls => callbackUrls.Value);
}

[Output("endpoint")]
public Output<string> Endpoint { get; set; }
}
8 changes: 8 additions & 0 deletions azure-nextgen-cs-cosmosdb-logicapp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.
using System.Threading.Tasks;
using Pulumi;

class Program
{
static Task<int> Main() => Deployment.RunAsync<MyStack>();
}
3 changes: 3 additions & 0 deletions azure-nextgen-cs-cosmosdb-logicapp/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: azure-nextgen-cosmosdb-logicapp
runtime: dotnet
description: An example of creating a CosmosDB container through a Azure's Cosmos SDK and deploying a Logic App and an API Connection
61 changes: 61 additions & 0 deletions azure-nextgen-cs-cosmosdb-logicapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# 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.

## Prerequisites

1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
2. [Install .NET Core 3.1+](https://dotnet.microsoft.com/download)

## Running the App

1. Create a new stack:

```sh
$ pulumi stack init dev
```

2. Set the required configuration variables for this program, and log into Azure:

```bash
$ pulumi config set azure:location westeurope
$ az login
```

3. Perform the deployment:

```sh
$ pulumi up

Type Name Status
+ pulumi:pulumi:Stack azure-cosmosdb-logicapp-dev created
+ ├─ azure-nextgen:resources/latest:ResourceGroup logicappdemo-rg created
+ ├─ azure-nextgen:storage/latest:StorageAccount logicappdemosa created
+ ├─ azure-nextgen:documentdb/latest:DatabaseAccount logicappdemo-cdb created
+ ├─ azure-nextgen:documentdb/latest:SqlResourceSqlDatabase db created
+ ├─ azure-nextgen:web/latest:Connection cosmosdbConnection created
+ ├─ azure-nextgen:documentdb/latest:SqlResourceSqlContainer container created
+ └─ azure-nextgen:logic/latest:Workflow workflow created

Resources:
+ 8 created

Duration: 3m16s
```

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:

```
$ 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. Once you are done, you can destroy all of the resources, and the stack:

```bash
$ pulumi destroy
$ pulumi stack rm
```
6 changes: 6 additions & 0 deletions azure-nextgen-py-cosmosdb-logicapp/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: azure-nextgen-cosmosdb-logicapp
runtime:
name: python
options:
virtualenv: venv
description: An example of creating a CosmosDB container through a Azure's Cosmos SDK and deploying a Logic App and an API Connection
62 changes: 62 additions & 0 deletions azure-nextgen-py-cosmosdb-logicapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# 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.

## Prerequisites

1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
1. [Configure Pulumi for Azure](https://www.pulumi.com/docs/intro/cloud-providers/azure/setup/)
1. [Configure Pulumi for Python](https://www.pulumi.com/docs/intro/languages/python/)

## Running the App

1. Create a new stack:

```sh
$ pulumi stack init dev
```

2. Set the required configuration variables for this program, and log into Azure:

```bash
$ pulumi config set azure:location westeurope
$ az login
```

3. Perform the deployment:

```sh
$ pulumi up

Type Name Status
+ pulumi:pulumi:Stack azure-cosmosdb-logicapp-dev created
+ ├─ azure-nextgen:resources/latest:ResourceGroup logicappdemo-rg created
+ ├─ azure-nextgen:storage/latest:StorageAccount logicappdemosa created
+ ├─ azure-nextgen:documentdb/latest:DatabaseAccount logicappdemo-cdb created
+ ├─ azure-nextgen:documentdb/latest:SqlResourceSqlDatabase db created
+ ├─ azure-nextgen:web/latest:Connection cosmosdbConnection created
+ ├─ azure-nextgen:documentdb/latest:SqlResourceSqlContainer container created
+ └─ azure-nextgen:logic/latest:Workflow workflow created

Resources:
+ 8 created

Duration: 3m16s
```

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:

```
$ 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. Once you are done, you can destroy all of the resources, and the stack:

```bash
$ pulumi destroy
$ pulumi stack rm
```
Loading

0 comments on commit d2a9ee3

Please sign in to comment.