Skip to content

Commit

Permalink
Issue #691, adds always_on attribute to the function_app resource
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudify committed Jan 13, 2018
1 parent c30d46c commit da0d72d
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
73 changes: 73 additions & 0 deletions azurerm/resource_arm_function_app.go
Expand Up @@ -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,
},
},
},
},
},
}
}
Expand All @@ -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,
Expand All @@ -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,
},
},
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
60 changes: 60 additions & 0 deletions azurerm/resource_arm_function_app_test.go
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}

0 comments on commit da0d72d

Please sign in to comment.