/
deploy.json
218 lines (218 loc) · 12.4 KB
/
deploy.json
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
},
"variables": {
"unique suffix": "[uniqueString(resourceGroup().id)]",
"event hub namespace": "[concat('event-hub-', variables('unique suffix'))]",
"event hub name": "department-hub",
"topic name": "[concat('topic-', variables('unique suffix'))]",
"event subscription name": "event-hub-subscription",
"storage name": "[concat('storage', variables('unique suffix'))]",
"app plan name": "demo-app-plan",
"function app name": "[concat('function-app-', variables('unique suffix'))]",
"function name": "generate-events",
"event count": 50
},
"resources": [
{
"type": "Microsoft.EventHub/namespaces",
"apiVersion": "2017-04-01",
"name": "[variables('event hub namespace')]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Standard",
"tier": "Standard",
"capacity": 1
},
"properties": {
"zoneRedundant": false,
"isAutoInflateEnabled": false,
"maximumThroughputUnits": 0,
"kafkaEnabled": true
},
"resources": [
{
"type": "eventhubs",
"apiVersion": "2017-04-01",
"name": "[variables('event hub name')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.EventHub/namespaces', variables('event hub namespace'))]"
],
"properties": {
"messageRetentionInDays": 1,
"partitionCount": 1
}
},
{
"type": "AuthorizationRules",
"apiVersion": "2017-04-01",
"name": "RootManageSharedAccessKey",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.EventHub/namespaces', variables('event hub namespace'))]"
],
"properties": {
"rights": [
"Listen",
"Manage",
"Send"
]
}
}
]
},
{
"type": "Microsoft.EventGrid/topics",
"apiVersion": "2019-06-01",
"name": "[variables('topic name')]",
"location": "[resourceGroup().location]",
"properties": {
}
},
{
"type": "Microsoft.EventGrid/topics/providers/eventSubscriptions",
"apiVersion": "2019-06-01",
"name": "[concat(variables('topic name'), '/Microsoft.EventGrid/', variables('event subscription name'))]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.EventGrid/topics', variables('topic name'))]",
"[resourceId('Microsoft.EventHub/namespaces/eventhubs', variables('event hub namespace'), variables('event hub name'))]"
],
"properties": {
"destination": {
"endpointType": "EventHub",
"properties": {
"resourceId": "[resourceId('Microsoft.EventHub/namespaces/eventhubs', variables('event hub namespace'), variables('event hub name'))]"
}
},
"filter": {
"isSubjectCaseSensitive": false
}
}
},
{
"apiVersion": "2019-04-01",
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storage name')]",
"location": "[resourceGroup().location]",
"kind": "StorageV2",
"sku": {
"name": "Standard_LRS"
},
"properties": {
"supportsHttpsTrafficOnly": true
}
},
{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2019-08-01",
"name": "[variables('app plan name')]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Y1",
"tier": "Dynamic"
},
"properties": {
"name": "[variables('app plan name')]",
"computeMode": "Dynamic"
},
"kind": "linux"
},
{
"apiVersion": "2019-08-01",
"type": "Microsoft.Web/sites",
"name": "[variables('function app name')]",
"location": "[resourceGroup().location]",
"kind": "functionapp",
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts', variables('storage name'))]",
"[resourceId('Microsoft.Web/serverfarms', variables('app plan name'))]",
"[resourceId('Microsoft.EventGrid/topics', variables('topic name'))]"
],
"identity": {
"type": "SystemAssigned"
},
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('app plan name'))]",
"siteConfig": {
"appSettings": [
{
"name": "AzureWebJobsSecretStorageType",
"value": "Files"
},
{
"name": "FUNCTIONS_EXTENSION_VERSION",
"value": "~3"
},
{
"name": "FUNCTIONS_WORKER_RUNTIME",
"value": "dotnet"
},
{
"name": "AzureWebJobsStorage",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=',variables('storage name'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storage name')), '2019-04-01').keys[0].value)]"
},
{
"name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storage name'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storage name')),'2019-04-01').keys[0].value)]"
},
{
"name": "WEBSITE_CONTENTSHARE",
"value": "[toLower(variables('function app name'))]"
},
{
"name": "ApplicationInsightsAgent_EXTENSION_VERSION",
"value": "~2"
},
{
"name": "TOPIC_ENDPOINT",
"value": "[reference(variables('topic name')).endpoint]"
},
{
"name": "TOPIC_KEY",
"value": "[listKeys(variables('topic name'), '2019-06-01').key1]"
},
{
"name": "EVENT_COUNT",
"value": "[variables('event count')]"
}
]
}
},
"resources": [
{
"name": "[variables('function name')]",
"type": "functions",
"apiVersion": "2015-08-01",
"dependsOn": [
"[resourceId('Microsoft.Web/sites', variables('function app name'))]"
],
"properties": {
"config": {
"bindings": [
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
]
}
],
"disabled": false
},
"files": {
"run.csx": "#r \"System.Text.Json\"\n\nusing System;\nusing System.Linq;\nusing System.Net;\nusing System.Net.Http;\nusing System.Text.Json;\nusing System.Threading.Tasks;\nusing Microsoft.AspNetCore.Mvc;\nusing Microsoft.Extensions.Primitives;\n\n// Using https://codebeautify.org/csharp-escape-unescape/ to compress\n// into a one-liner for ARM Template\n// Post process: need to replace \\' by ' as it fails in ARM Templates\npublic static async Task<IActionResult> Run(\n HttpRequest req,\n ILogger log)\n{\n log.LogInformation(\"Event Generation triggered\");\n\n\n var endpoint = Environment.GetEnvironmentVariable(\"TOPIC_ENDPOINT\");\n var key = Environment.GetEnvironmentVariable(\"TOPIC_KEY\");\n var eventCountText = Environment.GetEnvironmentVariable(\"EVENT_COUNT\");\n\n if (string.IsNullOrWhiteSpace(endpoint))\n {\n throw new ArgumentNullException(\"TOPIC_ENDPOINT\");\n }\n if (string.IsNullOrWhiteSpace(key))\n {\n throw new ArgumentNullException(\"TOPIC_KEY\");\n }\n if (string.IsNullOrWhiteSpace(eventCountText))\n {\n throw new ArgumentNullException(\"EVENT_COUNT\");\n }\n\n var eventCount = int.Parse(eventCountText);\n var client = new HttpClient();\n // Generate random events\n var events = GenerateEvents(eventCount);\n // Serialize events in JSON\n var options = new JsonSerializerOptions\n {\n PropertyNamingPolicy = JsonNamingPolicy.CamelCase\n };\n var eventsText = JsonSerializer.Serialize(events, options);\n var content = new StringContent(eventsText);\n\n // Include Topic Key as HTTP header for authentication\n client.DefaultRequestHeaders.Add(\"aeg-sas-key\", key);\n\n // Send events to Event Grid Topic\n var response = await client.PostAsync(endpoint, content);\n\n if (response.StatusCode != HttpStatusCode.OK)\n {\n var output = await response.Content.ReadAsStringAsync();\n\n return new BadRequestObjectResult($\"Error sending events: {output}\");\n }\n else\n {\n log.LogInformation($\"{eventCount} events were sent\");\n\n return new OkObjectResult($\"{eventCount} events were sent\");\n }\n}\n\nprivate class EventData\n{\n public string Id { get; set; }\n\n public string EventType { get; set; }\n\n public string Subject { get; set; }\n\n public DateTime EventTime { get; set; }\n\n public object Data { get; set; }\n\n public string DataVersion { get; set; }\n}\n\nprivate static EventData[] GenerateEvents(int eventCount)\n{\n var random = new Random();\n var data = from i in Enumerable.Range(0, eventCount)\n select new EventData\n {\n Id = Guid.NewGuid().ToString(),\n EventType = random.Next(0, 10) < 4 ? \"news\" : \"updates\",\n Subject = random.Next(0, 10) < 7 ? \"hr\" : \"finance\",\n EventTime = DateTime.Now,\n Data = new\n {\n RecordId = random.Next(1000, 100000),\n Name = new string((from j in Enumerable.Range(0, 25)\n select (char)random.Next((int)'A', (int)'Z')).ToArray()),\n Amount = random.NextDouble() * 15000 + 5000\n },\n DataVersion = \"1.1\"\n };\n\n return data.ToArray();\n}\n"
}
}
}
]
}
],
"outputs": {
}
}