-
Notifications
You must be signed in to change notification settings - Fork 878
/
Copy pathindex.ts
147 lines (134 loc) · 4.54 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2016-2025, Pulumi Corporation. All rights reserved.
import * as authorization from "@pulumi/azure-native/authorization";
import * as documentdb from "@pulumi/azure-native/documentdb";
import * as logic from "@pulumi/azure-native/logic";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";
import * as web from "@pulumi/azure-native/web";
import * as pulumi from "@pulumi/pulumi";
// Create an Azure Resource Group
const resourceGroup = new resources.ResourceGroup("logicappdemo-rg");
// Create an Azure resource (Storage Account)
const storageAccount = new storage.StorageAccount("logicappdemosa", {
resourceGroupName: resourceGroup.name,
sku: {
name: storage.SkuName.Standard_LRS,
},
kind: storage.Kind.StorageV2,
});
// Cosmos DB Account
const cosmosdbAccount = new documentdb.DatabaseAccount("logicappdemo-cdb", {
resourceGroupName: resourceGroup.name,
databaseAccountOfferType: documentdb.DatabaseAccountOfferType.Standard,
locations: [{
locationName: resourceGroup.location,
failoverPriority: 0,
}],
consistencyPolicy: {
defaultConsistencyLevel: documentdb.DefaultConsistencyLevel.Session,
},
});
// Cosmos DB Database
const db = new documentdb.SqlResourceSqlDatabase("sqldb", {
resourceGroupName: resourceGroup.name,
accountName: cosmosdbAccount.name,
resource: {
id: "sqldb",
},
});
// Cosmos DB SQL Container
const dbContainer = new documentdb.SqlResourceSqlContainer("container", {
resourceGroupName: resourceGroup.name,
accountName: cosmosdbAccount.name,
databaseName: db.name,
resource: {
id: "container",
partitionKey: {
paths: ["/myPartitionKey"],
kind: "Hash",
},
},
});
const accountKeys = documentdb.listDatabaseAccountKeysOutput({
accountName: cosmosdbAccount.name,
resourceGroupName: resourceGroup.name,
});
const clientConfig = pulumi.output(authorization.getClientConfig());
const apiId = pulumi.interpolate`/subscriptions/${clientConfig.subscriptionId}/providers/Microsoft.Web/locations/${resourceGroup.location}/managedApis/documentdb`;
// API Connection to be used in a Logic App
const connection = new web.Connection("cosmosdbConnection", {
resourceGroupName: resourceGroup.name,
properties: {
displayName: "cosmosdb_connection",
api: {
id: apiId,
},
parameterValues: {
databaseAccount: cosmosdbAccount.name,
accessKey: accountKeys.primaryMasterKey,
},
},
});
// Logic App with an HTTP trigger and Cosmos DB action
const workflow = new logic.Workflow("httpToCosmos", {
resourceGroupName: resourceGroup.name,
definition: {
$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: pulumi.interpolate`/dbs/${db.name}/colls/${dbContainer.name}/docs`,
},
},
},
},
parameters: {
$connections: {
value: {
documentdb: {
connectionId: connection.id,
connectionName: "logicapp-cosmosdb-connection",
id: apiId,
},
},
},
},
});
const callbackUrls = logic.listWorkflowTriggerCallbackUrlOutput({
resourceGroupName: resourceGroup.name,
workflowName: workflow.name,
triggerName: "Receive_post",
});
// Export the HTTP endpoint
export const endpoint = callbackUrls.value;