Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix app service logging with app settings #4243

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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