forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
182 lines (166 loc) · 6.31 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import * as azure from "@pulumi/azure";
import * as pulumi from "@pulumi/pulumi";
// Create an Azure Resource Group
const resourceGroup = new azure.core.ResourceGroup("logicappdemo-rg");
// Create an Azure resource (Storage Account)
const storageAccount = new azure.storage.Account("logicappdemosa", {
resourceGroupName: resourceGroup.name,
accountReplicationType: "LRS",
accountTier: "Standard",
accountKind: "StorageV2",
});
// Cosmos DB Account
const cosmosdbAccount = new azure.cosmosdb.Account("logicappdemo-cdb", {
resourceGroupName: resourceGroup.name,
location: resourceGroup.location,
offerType: "Standard",
geoLocations: [{ location: resourceGroup.location, failoverPriority: 0 }],
consistencyPolicy: {
consistencyLevel: "Session",
},
});
// Cosmos DB Database
const db = new azure.cosmosdb.SqlDatabase("db", {
resourceGroupName: resourceGroup.name,
accountName: cosmosdbAccount.name,
});
// Cosmos DB SQL Container
const dbContainer = new azure.cosmosdb.SqlContainer("container", {
resourceGroupName: resourceGroup.name,
accountName: cosmosdbAccount.name,
databaseName: db.name,
});
/*
* API Connection to be used in a Logic App
*/
// Calculate the subscription path
const subscriptionId = resourceGroup.id.apply(id => {
const splitId = id.split("/");
return `/${splitId[1]}/${splitId[2]}`;
});
// ARM Template with a connection
const connectionTemplate = {
$schema: "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
contentVersion: "1.0.0.0",
resources: [{
type: "Microsoft.Web/connections",
apiVersion: "2016-06-01",
name: "cosmosdb-connection",
location: resourceGroup.location,
properties: {
displayName: "cosmosdb_connection",
api: {
id: pulumi.interpolate`${subscriptionId}/providers/Microsoft.Web/locations/${resourceGroup.location}/managedApis/documentdb`,
},
parameterValues: {
databaseAccount: db.accountName,
accessKey: cosmosdbAccount.primaryMasterKey,
},
},
}],
};
// Template deployment
const cosmosdbConnection = new azure.core.TemplateDeployment("db-connection", {
resourceGroupName: resourceGroup.name,
templateBody: pulumi.output(connectionTemplate).apply(JSON.stringify),
deploymentMode: "Incremental",
}, { dependsOn: [cosmosdbAccount, db, dbContainer] });
/*
* Logic App with an HTTP trigger and Cosmos DB action
*/
// ARM Template with a Logic App listening to an HTTP endpoint
const processUrlTemplate = {
$schema: "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
contentVersion: "1.0.0.0",
parameters: {
connections_cosmosdb_externalid: {
defaultValue: pulumi.interpolate`${resourceGroup.id}/providers/Microsoft.Web/connections/${connectionTemplate.resources[0].name}`,
type: "String",
},
},
outputs: {
endpoint: {
type: "String",
value: pulumi.interpolate`[listCallbackUrl(resourceId('${resourceGroup.name}','Microsoft.Logic/workflows/triggers', 'http-to-cosmos', 'Receive_post'), '2016-06-01').value]`,
},
},
variables: {},
resources: [
{
type: "Microsoft.Logic/workflows",
apiVersion: "2017-07-01",
name: "http-to-cosmos",
location: resourceGroup.location,
properties: {
state: "Enabled",
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: {
},
},
parameters: {
$connections: {
value: {
documentdb: {
connectionId: "[parameters('connections_cosmosdb_externalid')]",
connectionName: "logicapp-cosmosdb-connection",
id: pulumi.interpolate`${connectionTemplate.resources[0].properties.api.id}`,
},
},
},
},
},
},
],
};
// Template deployment
const procesUrlWorkflow = new azure.core.TemplateDeployment("logic-app", {
resourceGroupName: resourceGroup.name,
templateBody: pulumi.output(processUrlTemplate).apply(JSON.stringify),
deploymentMode: "Incremental",
}, { dependsOn: [cosmosdbConnection] });
// Definition of an action to insert a document into the Cosmos DB collection
const insertActionBody = {
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`,
},
};
const insertAction = new azure.logicapps.ActionCustom("Create_or_update_document", {
logicAppId: pulumi.interpolate`${resourceGroup.id}/providers/Microsoft.Logic/workflows/${processUrlTemplate.resources[0].name}`,
name: "Create_or_update_document",
body: pulumi.output(insertActionBody).apply(JSON.stringify),
}, { dependsOn: [procesUrlWorkflow] });
// Export the HTTP endpoint
export const endpoint = procesUrlWorkflow.outputs["endpoint"];