Skip to content

Instantly share code, notes, and snippets.

@johndowns
Created December 5, 2018 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johndowns/6ffe69cdd073364e1dd16c8e11afbfa7 to your computer and use it in GitHub Desktop.
Save johndowns/6ffe69cdd073364e1dd16c8e11afbfa7 to your computer and use it in GitHub Desktop.
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"apiManagementPublisherEmailAddress": {
"type": "string",
"minLength": 1,
"metadata": {
"description": "The email address of the owner of the API Management service."
}
},
"apiManagementPublisherName": {
"type": "string",
"minLength": 1,
"metadata": {
"description": "The name of the owner of the API Management service."
}
},
"apiManagementSkuCount": {
"type": "string",
"allowedValues": [
"1",
"2"
],
"defaultValue": "1",
"metadata": {
"description": "The instance size of the API Management service."
}
},
"uniqueResourceNameSuffix": {
"type": "string",
"defaultValue": "[uniqueString(subscription().subscriptionId, resourceGroup().id)]",
"metadata": {
"description": "The suffix to add to resource names that require global uniqueness."
}
}
},
"variables": {
"apiManagementServiceName": "[concat('apiservice', parameters('uniqueResourceNameSuffix'))]",
"functionsStorageAccountName": "[concat('fn', parameters('uniqueResourceNameSuffix'))]",
"functionsStorageAccountResourceId": "[resourceId('Microsoft.Storage/storageAccounts', variables('functionsStorageAccountName'))]",
"functionsAppServicePlanName": "FunctionsApps",
"functionsAppName": "[concat('fn', parameters('uniqueResourceNameSuffix'))]"
},
"resources": [
{
"name": "[variables('apiManagementServiceName')]",
"type": "Microsoft.ApiManagement/service",
"apiVersion": "2018-06-01-preview",
"location": "[resourceGroup().location]",
"sku": {
"name": "Consumption"
},
"properties": {
"publisherEmail": "[parameters('apiManagementPublisherEmailAddress')]",
"publisherName": "[parameters('apiManagementPublisherName')]"
},
"resources": [
{
"type": "apis",
"name": "shipping-calculator",
"apiVersion": "2018-06-01-preview",
"properties": {
"displayName": "Shipping API",
"description": "Provides shipping cost calculations.",
"serviceUrl": "[listSecrets(resourceId('Microsoft.Web/sites/functions', variables('functionsAppName'), 'CalculateShipping'), '2018-02-01').trigger_url]",
"path": "shipping",
"protocols": [
"https"
]
},
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service', variables('apiManagementServiceName'))]",
"[resourceId('Microsoft.Web/sites/functions', variables('functionsAppName'), 'CalculateShipping')]"
],
"resources": [
{
"type": "operations",
"name": "calculateShipping",
"apiVersion": "2018-06-01-preview",
"properties": {
"displayName": "Calculate shipping cost",
"method": "POST",
"urlTemplate": "/calculateShipping",
"description": "Calculates the cost of a shipment.",
"responses": [
{
"statusCode": 200,
"description": "OK"
}
]
},
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service/apis', variables('apiManagementServiceName'), 'shipping-calculator')]",
"[resourceId('Microsoft.Web/sites', variables('functionsAppName'))]"
],
"resources": [
{
"name": "policy",
"type": "policies",
"apiVersion": "2017-03-01",
"properties": {
"policyContent": "[concat('<policies>\r\n <inbound>\r\n <set-header name=\"x-functions-key\" exists-action=\"override\">\r\n <value>', listSecrets(resourceId('Microsoft.Web/sites/functions', variables('functionsAppName'), 'CalculateShipping'), '2018-02-01').key, '<\/value>\r\n <\/set-header>\r\n <base \/>\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>')]"
},
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service/apis/operations', variables('apiManagementServiceName'), 'shipping-calculator', 'calculateShipping')]",
"[resourceId('Microsoft.Web/sites/functions', variables('functionsAppName'), 'CalculateShipping')]"
]
}
]
}
]
},
{
"type": "products",
"name": "Free",
"apiVersion": "2018-06-01-preview",
"properties": {
"displayName": "Free",
"description": "Product that grants limited free access to APIs.",
"subscriptionRequired": false,
"state": "published"
},
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service', variables('apiManagementServiceName'))]"
],
"resources": [
{
"type": "apis",
"name": "shipping-calculator",
"apiVersion": "2018-06-01-preview",
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service/products', variables('apiManagementServiceName'), 'Free')]",
"[resourceId('Microsoft.ApiManagement/service/apis', variables('apiManagementServiceName'), 'shipping-calculator')]"
]
},
{
"name": "policy",
"type": "policies",
"apiVersion": "2018-06-01-preview",
"properties": {
"policyContent": "[concat('<policies>\r\n <inbound>\r\n <rate-limit-by-key calls=\"3\" renewal-period=\"15\" counter-key=\"@(context.Request.IpAddress)\" \/>\r\n <base \/>\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>')]"
},
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service/products', variables('apiManagementServiceName'), 'Free')]"
]
}
]
}
]
},
{
"name": "[variables('functionsStorageAccountName')]",
"type": "Microsoft.Storage/storageAccounts",
"location": "[resourceGroup().location]",
"apiVersion": "2018-02-01",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"accessTier": "Hot",
"supportsHttpsTrafficOnly": true,
"encryption": {
"services": {
"blob": {
"enabled": true
}
},
"keySource": "Microsoft.Storage"
}
}
},
{
"name": "[variables('functionsAppServicePlanName')]",
"type": "Microsoft.Web/serverfarms",
"location": "[resourceGroup().location]",
"apiVersion": "2016-09-01",
"sku": {
"name": "Y1",
"tier": "Dynamic",
"size": "Y1",
"family": "Y",
"capacity": 0
},
"kind": "functionapp",
"properties": {
"workerTierName": null,
"adminSiteName": null,
"hostingEnvironmentProfile": null,
"perSiteScaling": false,
"reserved": false,
"targetWorkerCount": 0,
"targetWorkerSizeId": 0
}
},
{
"name": "[variables('functionsAppName')]",
"type": "Microsoft.Web/sites",
"location": "[resourceGroup().location]",
"apiVersion": "2016-08-01",
"kind": "functionapp",
"properties": {
"enabled": true,
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('functionsAppServicePlanName'))]",
"reserved": false,
"siteConfig": {
"ftpsState": "Disabled"
}
},
"resources": [
{
"name": "appsettings",
"type": "config",
"apiVersion": "2014-11-01",
"properties": {
"AzureWebJobsDashboard": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('functionsStorageAccountName'), ';AccountKey=', listKeys(variables('functionsStorageAccountResourceId'),'2015-05-01-preview').key1)]",
"AzureWebJobsStorage": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('functionsStorageAccountName'), ';AccountKey=', listKeys(variables('functionsStorageAccountResourceId'),'2015-05-01-preview').key1)]",
"AzureWebJobsSecretStorageType": "files"
},
"dependsOn": [
"[resourceId('Microsoft.Web/sites', variables('functionsAppName'))]",
"[variables('functionsStorageAccountResourceId')]"
]
},
{
"name": "CalculateShipping",
"type": "functions",
"apiVersion": "2015-08-01",
"properties": {
"config": {
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"authLevel": "function",
"name": "req",
"methods": [
"post"
]
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
],
"disabled": false
},
"files": {
"run.csx": "#r \"Newtonsoft.Json\"\r\n\r\nusing System.Net;\r\nusing Microsoft.AspNetCore.Mvc;\r\nusing Microsoft.Extensions.Primitives;\r\nusing Newtonsoft.Json;\r\n\r\nstatic Random Random = new Random();\r\n\r\npublic static async Task<IActionResult> Run(HttpRequest req, ILogger log)\r\n{\r\n string requestBody = await new StreamReader(req.Body).ReadToEndAsync();\r\n var shippingDetails = JsonConvert.DeserializeObject<ShippingDetails>(requestBody);\r\n\r\n var shippingCost = CalculateShippingCost(shippingDetails);\r\n\r\n return (ActionResult)new OkObjectResult(shippingCost);\r\n}\r\n\r\npublic static ShippingCost CalculateShippingCost(ShippingDetails shippingDetails)\r\n{\r\n \/\/ simulating doing important calculations...\r\n return new ShippingCost\r\n {\r\n Cost = (decimal)Random.NextDouble() * 50\r\n };\r\n}\r\n\r\npublic class ShippingDetails\r\n{\r\n public decimal PackageWidth { get; set; }\r\n public decimal PackageHeight { get; set; }\r\n public decimal PackageDepth { get; set; }\r\n public decimal PackageWeight { get; set; }\r\n public string RecipientPostCode { get; set; }\r\n}\r\n\r\npublic class ShippingCost\r\n{\r\n public decimal Cost { get; set; }\r\n}"
}
},
"dependsOn": [
"[resourceId('Microsoft.Web/sites', variables('functionsAppName'))]"
]
}
],
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms', variables('functionsAppServicePlanName'))]"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment