Skip to content

Commit

Permalink
Fix app service logging with app settings
Browse files Browse the repository at this point in the history
- Update the log settings after updating the app settings and whenever
the app settings change if there's a `logs` block. Azure adds app settings
for the logging configuration, so changing the app settings would otherwise
overwrite them
- Update test case for above
  • Loading branch information
phekmat committed Sep 4, 2019
1 parent d97960d commit 8de30b8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
33 changes: 20 additions & 13 deletions azurerm/resource_arm_app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,6 @@ func resourceArmAppServiceUpdate(d *schema.ResourceData, meta interface{}) error
}
}

if d.HasChange("logs") {
logs := azure.ExpandAppServiceLogs(d.Get("logs"))
id := d.Id()
logsResource := web.SiteLogsConfig{
ID: &id,
SiteLogsConfigProperties: &logs,
}

if _, err := client.UpdateDiagnosticLogsConfig(ctx, resGroup, name, logsResource); err != nil {
return fmt.Errorf("Error updating Diagnostics Logs for App Service %q: %+v", name, err)
}
}

if d.HasChange("backup") {
backupRaw := d.Get("backup").([]interface{})
if backup := azure.ExpandAppServiceBackup(backupRaw); backup != nil {
Expand Down Expand Up @@ -422,6 +409,7 @@ func resourceArmAppServiceUpdate(d *schema.ResourceData, meta interface{}) error
}
}

// app settings updates have a side effect on logging settings. See the note below
if d.HasChange("app_settings") {
// update the AppSettings
appSettings := expandAppServiceAppSettings(d)
Expand All @@ -434,6 +422,25 @@ func resourceArmAppServiceUpdate(d *schema.ResourceData, meta interface{}) error
}
}

// the logging configuration has a dependency on the app settings in Azure
// e.g. configuring logging to blob storage will add the DIAGNOSTICS_AZUREBLOBCONTAINERSASURL
// and DIAGNOSTICS_AZUREBLOBRETENTIONINDAYS app settings to the app service.
// If the app settings are updated, also update the logging configuration if it exists, otherwise
// updating the former will clobber the log settings
_, hasLogs := d.GetOkExists("logs")
if d.HasChange("logs") || (hasLogs && d.HasChange("app_settings")) {
logs := azure.ExpandAppServiceLogs(d.Get("logs"))
id := d.Id()
logsResource := web.SiteLogsConfig{
ID: &id,
SiteLogsConfigProperties: &logs,
}

if _, err := client.UpdateDiagnosticLogsConfig(ctx, resGroup, name, logsResource); err != nil {
return fmt.Errorf("Error updating Diagnostics Logs for App Service %q: %+v", name, err)
}
}

if d.HasChange("storage_account") {
storageAccounts := azure.ExpandAppServiceStorageAccounts(d)
properties := web.AzureStoragePropertyDictionaryResource{
Expand Down
48 changes: 48 additions & 0 deletions azurerm/resource_arm_app_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ func TestAccAzureRMAppService_applicationBlobStorageLogs(t *testing.T) {
resourceName := "azurerm_app_service.test"
ri := tf.AccRandTimeInt()
config := testAccAzureRMAppService_applicationBlobStorageLogs(ri, testLocation())
updated := testAccAzureRMAppService_applicationBlobStorageLogsWithAppSettings(ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -972,6 +973,16 @@ func TestAccAzureRMAppService_applicationBlobStorageLogs(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "logs.0.application_logs.0.azure_blob_storage.0.retention_in_days", "3"),
),
},
{
Config: updated,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "logs.0.application_logs.0.azure_blob_storage.0.level", "Information"),
resource.TestCheckResourceAttr(resourceName, "logs.0.application_logs.0.azure_blob_storage.0.sas_url", "http://x.com/"),
resource.TestCheckResourceAttr(resourceName, "logs.0.application_logs.0.azure_blob_storage.0.retention_in_days", "3"),
resource.TestCheckResourceAttr(resourceName, "app_settings.foo", "bar"),
),
},
{
ResourceName: resourceName,
ImportState: true,
Expand Down Expand Up @@ -3307,7 +3318,44 @@ resource "azurerm_app_service" "test" {
}
`, rInt, location, rInt, rInt)
}
func testAccAzureRMAppService_applicationBlobStorageLogsWithAppSettings(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_app_service_plan" "test" {
name = "acctestASP-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku {
tier = "Standard"
size = "S1"
}
}
resource "azurerm_app_service" "test" {
name = "acctestAS-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_plan_id = "${azurerm_app_service_plan.test.id}"
app_settings = {
foo = "bar"
}
logs {
application_logs {
azure_blob_storage {
level = "Information"
sas_url = "http://x.com/"
retention_in_days = 3
}
}
}
}
`, rInt, location, rInt, rInt)
}
func testAccAzureRMAppService_httpFileSystemLogs(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down

0 comments on commit 8de30b8

Please sign in to comment.