public async Task Create(
            string name,
            string storageAccountName,
            string storageAccountKey,
            string serverFarmName,
            FunctionAppLocation location = FunctionAppLocation.WestUs2,
            FunctionAppSku sku           = FunctionAppSku.Consumption,
            FunctionAppOs os             = FunctionAppOs.Windows,
            FunctionAppRuntime runtime   = FunctionAppRuntime.DotNet)
        {
            string resourceGroup = GetResourceGroupName(os);
            Uri    uri           = new Uri($"{ManagementURL}subscriptions/{SubscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{name}?api-version=2018-11-01");

            string storageAccountConnectionString = $"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey};EndpointSuffix=core.windows.net";

            List <Dictionary <string, string> > appSettings = new List <Dictionary <string, string> >();

            appSettings.Add(new Dictionary <string, string> {
                { "name", "FUNCTIONS_WORKER_RUNTIME" }, { "value", runtime.ToFunctionWorkerRuntime() }
            });
            appSettings.Add(new Dictionary <string, string> {
                { "name", "FUNCTIONS_EXTENSION_VERSION" }, { "value", "~2" }
            });
            appSettings.Add(new Dictionary <string, string> {
                { "name", "AzureWebJobsStorage" }, { "value", storageAccountConnectionString }
            });

            object payload = new
            {
                kind       = os.GetFunctionAppKindLabel(),
                location   = location.ToRegion(),
                properties = new
                {
                    siteConfig = new
                    {
                        appSettings = appSettings
                    },
                    serverFarmId         = $"/subscriptions/{SubscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/serverfarms/{serverFarmName}",
                    hostingEnvironment   = "",
                    clientAffinityEnable = false
                }
            };

            var response = await ArmClient.HttpInvoke("PUT", uri, AccessToken, payload);

            if (response.IsSuccessStatusCode)
            {
                AddToResources(name, os);
            }
            else
            {
                string statusCode = response.StatusCode.ToString();
                string message    = await response.Content.ReadAsStringAsync();

                throw new Exception($"Failed to create function app {name}: ({statusCode}) {message}");
            }
        }
Exemple #2
0
 public static string ToFunctionWorkerRuntime(this FunctionAppRuntime runtime)
 {
     return(runtime.ToString().ToLower());
 }