Example #1
0
        public async Task Create(
            string name,
            FunctionAppLocation location = FunctionAppLocation.WestUs2,
            FunctionAppOs os             = FunctionAppOs.Windows)
        {
            string resourceGroup = GetResourceGroupName(os);
            Uri    uri           = new Uri($"{ManagementURL}subscriptions/{SubscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Storage/storageAccounts/{name}?api-version=2019-06-01");
            object payload       = new
            {
                kind     = "StorageV2",
                location = location.ToRegion(),
                sku      = new
                {
                    name = "Standard_LRS"
                }
            };
            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 storage account {name}: ({statusCode}) {message}");
            }
        }
        public async Task Create(
            string name,
            FunctionAppLocation location = FunctionAppLocation.WestUs2,
            FunctionAppSku sku           = FunctionAppSku.Consumption,
            FunctionAppOs os             = FunctionAppOs.Windows)
        {
            string resourceGroup = GetResourceGroupName(os);
            Uri    uri           = new Uri($"{ManagementURL}subscriptions/{SubscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/serverfarms/{name}?api-version=2019-08-01");
            object payload       = new
            {
                kind       = os.GetServerFarmKindLabel(),
                location   = location.ToRegion(),
                properties = sku.GetServerFarmProperties(os),
                sku        = sku.GetServerFarmSku(os)
            };
            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 server farm {name}: ({statusCode}) {message}");
            }
        }
        public async Task <HttpResponseMessage> Get(
            string name)
        {
            FunctionAppOs os            = GetOsFromResourceLabel(name);
            string        resourceGroup = GetResourceGroupName(os);
            Uri           uri           = new Uri($"{ManagementURL}subscriptions/{SubscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/serverfarms/{name}?api-version=2019-08-01");

            return(await ArmClient.HttpInvoke("GET", uri, AccessToken));
        }
        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}");
            }
        }
        protected void RemoveFromResources(ResourceLabel resource, FunctionAppOs os)
        {
            if (os == FunctionAppOs.Windows)
            {
                _windowsResources.Remove(resource);
            }

            if (os == FunctionAppOs.Linux)
            {
                _linuxResources.Remove(resource);
            }
        }
        protected void AddToResources(ResourceLabel resource, FunctionAppOs os)
        {
            if (os == FunctionAppOs.Windows)
            {
                _windowsResources.Add(resource);
            }

            if (os == FunctionAppOs.Linux)
            {
                _linuxResources.Add(resource);
            }
        }
        public string GetResourceGroupName(FunctionAppOs os)
        {
            switch (os)
            {
            case FunctionAppOs.Windows:
                return(WindowsResourceGroupName);

            case FunctionAppOs.Linux:
                return(LinuxResourceGroupName);

            default:
                return(string.Empty);
            }
        }
Example #8
0
        public static string GetServerFarmKindLabel(this FunctionAppOs os)
        {
            switch (os)
            {
            case FunctionAppOs.Windows:
                return("windows");

            case FunctionAppOs.Linux:
                return("linux");

            default:
                return(string.Empty);
            }
        }
Example #9
0
        public static string GetFunctionAppKindLabel(this FunctionAppOs os)
        {
            switch (os)
            {
            case FunctionAppOs.Windows:
                return("functionapp");

            case FunctionAppOs.Linux:
                return("functionapp,linux");

            default:
                return(string.Empty);
            }
        }
Example #10
0
        public static ServerFarmSkuObject GetServerFarmSku(this FunctionAppSku sku, FunctionAppOs os)
        {
            // Consumption
            if (sku == FunctionAppSku.Consumption)
            {
                return(new ServerFarmSkuObject
                {
                    Tier = "Dynamic",
                    Name = "Y1"
                });
            }

            // Dedicated
            if (sku == FunctionAppSku.Dedicated && os == FunctionAppOs.Windows)
            {
                return(new ServerFarmSkuObject
                {
                    Tier = "Standard",
                    Name = "S1"
                });
            }
            else if (sku == FunctionAppSku.Dedicated && os == FunctionAppOs.Linux)
            {
                return(new ServerFarmSkuObject
                {
                    Tier = "PremiumV2",
                    Name = "P1v2"
                });
            }

            // ElasticPremium
            if (sku == FunctionAppSku.ElasticPremium)
            {
                return(new ServerFarmSkuObject
                {
                    Tier = "ElasticPremium",
                    Name = "EP1"
                });
            }

            return(null);
        }
Example #11
0
        public async Task Delete(
            string name)
        {
            FunctionAppOs os            = GetOsFromResourceLabel(name);
            string        resourceGroup = GetResourceGroupName(os);
            Uri           uri           = new Uri($"{ManagementURL}subscriptions/{SubscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Storage/storageAccounts/{name}?api-version=2019-06-01");
            var           response      = await ArmClient.HttpInvoke("DELETE", uri, AccessToken);

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

                throw new Exception($"Failed to remove storage account {name}: ({statusCode}) {message}");
            }
        }
Example #12
0
        public async Task <ListKeysResponse> ListKeys(
            string name)
        {
            FunctionAppOs os            = GetOsFromResourceLabel(name);
            string        resourceGroup = GetResourceGroupName(os);
            Uri           uri           = new Uri($"{ManagementURL}subscriptions/{SubscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Storage/storageAccounts/{name}/listkeys?api-version=2019-06-01");
            var           response      = await ArmClient.HttpInvoke("POST", uri, AccessToken);

            if (response.IsSuccessStatusCode)
            {
                string json = await response.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <ListKeysResponse>(json));
            }
            else
            {
                string statusCode = response.StatusCode.ToString();
                string message    = await response.Content.ReadAsStringAsync();

                throw new Exception($"Failed to list keys for storage account {name}: ({statusCode}) {message}");
            }
        }
Example #13
0
        public static ServerFarmPropertiesObject GetServerFarmProperties(this FunctionAppSku sku, FunctionAppOs os)
        {
            // Consumption
            if (sku == FunctionAppSku.Consumption)
            {
                return(new ServerFarmPropertiesObject
                {
                    workerSize = 0,
                    workerSizeId = 0,
                    numberOfWorkers = 1,
                    maximumElasticWorkerCount = 0,
                    hostingEnvironment = string.Empty,
                    reserved = os == FunctionAppOs.Linux
                });
            }

            // Dedicated
            if (sku == FunctionAppSku.Dedicated)
            {
                return(new ServerFarmPropertiesObject
                {
                    workerSize = 0,
                    workerSizeId = 0,
                    numberOfWorkers = 1,
                    maximumElasticWorkerCount = 0,
                    hostingEnvironment = string.Empty,
                    reserved = os == FunctionAppOs.Linux
                });
            }

            // ElasticPremium
            if (sku == FunctionAppSku.ElasticPremium)
            {
                return(new ServerFarmPropertiesObject
                {
                    workerSize = 3,
                    workerSizeId = 3,
                    numberOfWorkers = 1,
                    maximumElasticWorkerCount = 20,
                    hostingEnvironment = string.Empty,
                    reserved = os == FunctionAppOs.Linux
                });
            }

            return(null);
        }