From da0d72d99d5fc7018183d85cd23e867a09ff72d6 Mon Sep 17 00:00:00 2001 From: Federico Feroldi Date: Sat, 13 Jan 2018 08:44:21 +0100 Subject: [PATCH] Issue #691, adds always_on attribute to the function_app resource --- azurerm/resource_arm_function_app.go | 73 +++++++++++++++++++++++ azurerm/resource_arm_function_app_test.go | 60 +++++++++++++++++++ 2 files changed, 133 insertions(+) diff --git a/azurerm/resource_arm_function_app.go b/azurerm/resource_arm_function_app.go index d16cc294a9f06..1f02456f03753 100644 --- a/azurerm/resource_arm_function_app.go +++ b/azurerm/resource_arm_function_app.go @@ -80,6 +80,22 @@ func resourceArmFunctionApp() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "site_config": { + Type: schema.TypeList, + Optional: true, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "always_on": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + }, + }, + }, }, } } @@ -97,6 +113,7 @@ func resourceArmFunctionAppCreate(d *schema.ResourceData, meta interface{}) erro enabled := d.Get("enabled").(bool) tags := d.Get("tags").(map[string]interface{}) basicAppSettings := getBasicFunctionAppAppSettings(d) + siteConfig := expandFunctionAppSiteConfig(d) siteEnvelope := web.Site{ Kind: &kind, @@ -106,6 +123,7 @@ func resourceArmFunctionAppCreate(d *schema.ResourceData, meta interface{}) erro ServerFarmID: utils.String(appServicePlanID), Enabled: utils.Bool(enabled), SiteConfig: &web.SiteConfig{ + AlwaysOn: siteConfig.AlwaysOn, AppSettings: &basicAppSettings, }, }, @@ -157,6 +175,17 @@ func resourceArmFunctionAppUpdate(d *schema.ResourceData, meta interface{}) erro } } + if d.HasChange("site_config") { + siteConfig := expandFunctionAppSiteConfig(d) + siteConfigResource := web.SiteConfigResource{ + SiteConfig: &siteConfig, + } + _, err := client.CreateOrUpdateConfiguration(resGroup, name, siteConfigResource) + if err != nil { + return fmt.Errorf("Error updating Configuration for Function App %q: %+v", name, err) + } + } + return resourceArmFunctionAppRead(d, meta) } @@ -211,6 +240,16 @@ func resourceArmFunctionAppRead(d *schema.ResourceData, meta interface{}) error return err } + configResp, err := client.GetConfiguration(resGroup, name) + if err != nil { + return fmt.Errorf("Error making Read request on AzureRM Function App Configuration %q: %+v", name, err) + } + + siteConfig := flattenFunctionAppSiteConfig(configResp.SiteConfig) + if err := d.Set("site_config", siteConfig); err != nil { + return err + } + flattenAndSetTags(d, resp.Tags) return nil @@ -271,3 +310,37 @@ func expandFunctionAppAppSettings(d *schema.ResourceData) *map[string]*string { return output } + +func expandFunctionAppSiteConfig(d *schema.ResourceData) web.SiteConfig { + configs := d.Get("site_config").([]interface{}) + siteConfig := web.SiteConfig{} + + if len(configs) == 0 { + return siteConfig + } + + config := configs[0].(map[string]interface{}) + + if v, ok := config["always_on"]; ok { + siteConfig.AlwaysOn = utils.Bool(v.(bool)) + } + + return siteConfig +} + +func flattenFunctionAppSiteConfig(input *web.SiteConfig) []interface{} { + results := make([]interface{}, 0) + result := make(map[string]interface{}, 0) + + if input == nil { + log.Printf("[DEBUG] SiteConfig is nil") + return results + } + + if input.AlwaysOn != nil { + result["always_on"] = *input.AlwaysOn + } + + results = append(results, result) + return results +} diff --git a/azurerm/resource_arm_function_app_test.go b/azurerm/resource_arm_function_app_test.go index 751da2c73c3ed..440d195083b49 100644 --- a/azurerm/resource_arm_function_app_test.go +++ b/azurerm/resource_arm_function_app_test.go @@ -94,6 +94,28 @@ func TestAccAzureRMFunctionApp_appSettings(t *testing.T) { }) } +func TestAccAzureRMFunctionApp_siteConfig(t *testing.T) { + resourceName := "azurerm_function_app.test" + ri := acctest.RandInt() + rs := strings.ToLower(acctest.RandString(11)) + config := testAccAzureRMFunctionApp_alwaysOn(ri, rs, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMFunctionAppDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMFunctionAppExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "site_config.0.always_on", "true"), + ), + }, + }, + }) +} + func TestAccAzureRMFunctionApp_updateVersion(t *testing.T) { resourceName := "azurerm_function_app.test" ri := acctest.RandInt() @@ -323,3 +345,41 @@ resource "azurerm_function_app" "test" { } `, rInt, location, rString) } + +func testAccAzureRMFunctionApp_alwaysOn(rInt int, rString, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%[1]d" + location = "%[2]s" +} + +resource "azurerm_storage_account" "test" { + name = "acctestsa%[3]s" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "${azurerm_resource_group.test.location}" + account_tier = "Standard" + account_replication_type = "LRS" +} + +resource "azurerm_app_service_plan" "test" { + name = "acctestASP-%[1]d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + sku { + tier = "Standard" + size = "S1" + } +} + +resource "azurerm_function_app" "test" { + name = "acctest-%[1]d-func" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + app_service_plan_id = "${azurerm_app_service_plan.test.id}" + storage_connection_string = "${azurerm_storage_account.test.primary_connection_string}" + site_config { + always_on = true + } +} +`, rInt, location, rString) +}