-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodules.apiManagement.bicep
334 lines (309 loc) · 10.2 KB
/
modules.apiManagement.bicep
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
param resourceLocation string = resourceGroup().location
param apiMgmtName string
param apiMgmtSku string
param apiMgmtPublisherEmail string
param apiMgmtPublisherName string
param apiMgmtVersionDescription string = 'Release Date: ${utcNow('d')}'
@secure()
param apiMgmtFuncKeySecret string
param templateFuncId string
param templateFuncName string
param appInsightsResId string
param logAnalyticsWsId string
@secure()
param monitoringSubscriptionKey string = base64(newGuid())
var appInsightsResIdParts = split(appInsightsResId, '/') // Indexes: 2 = SubscriptionId, 4 = ResourceGroupName, 8 = ResourceName
var apiMgmtInternalApiName = 'template'
var apiMgmtTemplateApi = 'test-api'
var apiMgmtTemplateApiDisplayName = 'Test API'
var apiMgmtTemplateApiPath = 'test'
var apiMgmtTemplateProduct = 'test-product'
var apiMgmtTemplateProductDisplayName = 'Test API Integration'
resource appInsightsRes 'Microsoft.Insights/components@2020-02-02' existing = {
name: appInsightsResIdParts[8]
}
resource apiMgmtRes 'Microsoft.ApiManagement/service@2021-08-01' = {
name: apiMgmtName
location: resourceLocation
sku: {
name: apiMgmtSku
capacity: 1
}
properties: {
publisherEmail: apiMgmtPublisherEmail
publisherName: apiMgmtPublisherName
}
identity: {
type: 'SystemAssigned'
}
}
resource apiMgmtServiceLoggerRes 'Microsoft.ApiManagement/service/loggers@2021-08-01' = {
parent: apiMgmtRes
name: appInsightsResIdParts[8]
properties: {
loggerType: 'applicationInsights'
credentials: {
instrumentationKey: appInsightsRes.properties.InstrumentationKey // Despite migrated to connection-string in other places, this is still documented to require the key - but may change in future
}
isBuffered: true
resourceId: appInsightsResId
}
}
resource apiMgmtServiceDiagnosticsRes 'Microsoft.ApiManagement/service/diagnostics@2021-08-01' = {
parent: apiMgmtRes
name: 'applicationinsights'
properties: {
alwaysLog: 'allErrors'
loggerId: apiMgmtServiceLoggerRes.id
sampling: {
samplingType: 'fixed'
percentage: 100
}
}
}
resource apiMgmtDiagnosticsRes 'Microsoft.Insights/diagnosticSettings@2017-05-01-preview' = {
name: 'LogAnalytics'
scope: apiMgmtRes
properties: {
workspaceId: logAnalyticsWsId
logs: [
{
category: 'GatewayLogs'
enabled: true
}
]
metrics: [
{
category: 'AllMetrics'
enabled: true
}
]
}
}
resource apiMgmtBasePolicyRes 'Microsoft.ApiManagement/service/policies@2021-08-01' = {
parent: apiMgmtRes
name: 'policy'
properties: {
value: '<policies>\r\n <inbound>\r\n <set-query-parameter name="subscriptionId" exists-action="override">\r\n <value>@(context.Subscription.Id)</value>\r\n </set-query-parameter>\r\n <set-query-parameter name="userId" exists-action="override">\r\n <value>@(context.User.Id)</value>\r\n </set-query-parameter>\r\n <cors>\r\n <allowed-origins>\r\n <origin>*</origin>\r\n </allowed-origins>\r\n <allowed-methods>\r\n <method>*</method>\r\n </allowed-methods>\r\n <allowed-headers>\r\n <header>*</header>\r\n </allowed-headers>\r\n <expose-headers>\r\n <header>*</header>\r\n </expose-headers>\r\n </cors>\r\n </inbound>\r\n <backend>\r\n <forward-request />\r\n </backend>\r\n <outbound />\r\n <on-error />\r\n</policies>'
format: 'xml'
}
}
resource apiMgmtTemplateBackendRes 'Microsoft.ApiManagement/service/backends@2021-08-01' = {
parent: apiMgmtRes
name: '${apiMgmtInternalApiName}-backend'
properties: {
description: templateFuncName
url: 'https://${templateFuncName}.azurewebsites.net/api'
protocol: 'http'
resourceId: '${environment().resourceManager}${skip(templateFuncId, 1)}'
credentials: {
header: {
'x-functions-key': [
apiMgmtFuncKeySecret
]
}
}
}
}
resource apiMgmtTemplateApiRes 'Microsoft.ApiManagement/service/apis@2021-08-01' = {
parent: apiMgmtRes
name: apiMgmtTemplateApi
properties: {
displayName: apiMgmtTemplateApiDisplayName
apiRevision: '1'
apiVersionDescription: apiMgmtVersionDescription
subscriptionRequired: true
path: apiMgmtTemplateApiPath
protocols: [
'https'
]
isCurrent: true
}
}
resource apiMgmtTemplateApiPolicyRes 'Microsoft.ApiManagement/service/apis/policies@2021-08-01' = {
parent: apiMgmtTemplateApiRes
name: 'policy'
properties: {
value: '<policies>\r\n <inbound>\r\n <base />\r\n <set-backend-service backend-id="${apiMgmtInternalApiName}-backend" />\r\n </inbound>\r\n <backend>\r\n <base />\r\n </backend>\r\n <outbound>\r\n <base />\r\n </outbound>\r\n <on-error>\r\n <base />\r\n </on-error>\r\n</policies>'
format: 'xml'
}
dependsOn: [
apiMgmtTemplateBackendRes
]
}
resource apiMgmtTemplateApiSchemaRes 'Microsoft.ApiManagement/service/apis/schemas@2021-08-01' = {
parent: apiMgmtTemplateApiRes
name: '${apiMgmtInternalApiName}-schema'
properties: {
contentType: 'application/vnd.oai.openapi.components+json'
document: {
components: {
schemas: {
RequestObject: {
required: [
'intProperty'
]
type: 'object'
properties: {
intProperty: {
maximum: 2
minimum: 1
type: 'integer'
description: 'The value must be either 1 or 2'
}
stringProperty: {
type: 'string'
description: 'Any additional text informing about content or purpose of the message'
}
}
example: {
intProperty: 2
messageTypeInfo: 'Message XYZ'
}
}
ResponseObject: {
required: [
'transferStatusCode'
]
type: 'object'
properties: {
transferId: {
type: 'string'
}
transferTimestamp: {
type: 'string'
format: 'date-time'
}
transferStatusCode: {
type: 'integer'
minimum: 0
maximum: 4
}
transferStatusInfo: {
type: 'string'
}
}
example: {
transferId: '1aa41d46-1edb-487c-acc8-9e3d6151da1e'
transferTimestamp: '2020-01-01T23:28:56Z'
transferStatusCode: 0
transferStatusInfo: 'Request accepted'
}
}
}
securitySchemes: {
apiKeyHeader: {
type: 'apiKey'
name: 'Ocp-Apim-Subscription-Key'
in: 'header'
}
apiKeyQuery: {
type: 'apiKey'
name: 'subscription-key'
in: 'query'
}
}
}
}
}
}
resource apiMgmtTemplateApiOperationsRes 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = {
parent: apiMgmtTemplateApiRes
name: 'test'
properties: {
displayName: 'Test'
method: 'POST'
urlTemplate: '/testapi'
templateParameters: []
description: 'An example to show setup of API Management with Azure Functions'
request: {
description: 'Test API with some values'
queryParameters: [
{
name: 'testParam'
description: 'Test only'
type: 'boolean'
required: false
values: []
}
]
headers: []
representations: [
{
contentType: 'application/json'
schemaId: '${apiMgmtInternalApiName}-schema'
typeName: 'RequestObject'
}
]
}
responses: [
{
statusCode: 200
description: 'Successfully processed'
representations: [
{
contentType: 'application/json'
schemaId: '${apiMgmtInternalApiName}-schema'
typeName: 'ResponseObject'
}
]
headers: []
}
{
statusCode: 400
description: 'Failure'
representations: [
{
contentType: 'application/json'
}
]
headers: []
}
]
}
}
resource apiMgmtTemplateApiOperationsPolicyRes 'Microsoft.ApiManagement/service/apis/operations/policies@2021-08-01' = {
parent: apiMgmtTemplateApiOperationsRes
name: 'policy'
properties: {
value: '<policies>\r\n <inbound>\r\n <base />\r\n <rewrite-uri template="/testfunction" copy-unmatched-params="true" />\r\n </inbound>\r\n <backend>\r\n <base />\r\n </backend>\r\n <outbound>\r\n <base />\r\n </outbound>\r\n <on-error>\r\n <base />\r\n </on-error>\r\n</policies>'
format: 'xml'
}
}
resource apiMgmtTemplateProductRes 'Microsoft.ApiManagement/service/products@2021-08-01' = {
parent: apiMgmtRes
name: apiMgmtTemplateProduct
properties: {
displayName: apiMgmtTemplateProductDisplayName
description: 'Provide Azure Function endpoints'
subscriptionRequired: true
approvalRequired: true
state: 'published'
}
}
resource apiMgmtTemplateProductApiRes 'Microsoft.ApiManagement/service/products/apis@2021-08-01' = {
parent: apiMgmtTemplateProductRes
name: apiMgmtTemplateApi
dependsOn: [
apiMgmtTemplateApiRes
]
}
resource apiMgmtTemplateProductGroupRes 'Microsoft.ApiManagement/service/products/groups@2021-08-01' = {
parent: apiMgmtTemplateProductRes
name: 'developers'
}
resource apiMgmtMonitoringSubscriptionRes 'Microsoft.ApiManagement/service/subscriptions@2021-08-01' = if (!empty(monitoringSubscriptionKey)) {
name: 'monitoring'
parent: apiMgmtRes
properties: {
allowTracing: false
displayName: 'Monitoring (internal)'
ownerId: '/users/1' // Predefined administrator account (if not deleted manually)
primaryKey: monitoringSubscriptionKey
scope: '/products/${apiMgmtTemplateProduct}' // Predefined product (if not deleted manually) : '/products/unlimited' -> Not used here to not expose security risks
secondaryKey: null // Will be generated automatically
state: 'active'
}
}
output apiMgmtResId string = apiMgmtRes.id
// output monitoringSubscriptionKey string = apiMgmtMonitoringSubscriptionRes.listSecrets().primaryKey