Skip to content

Commit

Permalink
azurerm_windows_web_app, azurerm_linux_web_app, `azurerm_windows_…
Browse files Browse the repository at this point in the history
…function_app`, `azurerm_linux_function_app` - Terraform will no longer plan when `site_config.0.cors` are the default values(#28703)
  • Loading branch information
mbfrahry authored Feb 10, 2025
1 parent 3b5e43a commit c4b300a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
31 changes: 31 additions & 0 deletions internal/services/appservice/helpers/shared_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/web/2023-12-01/webapps"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -285,6 +286,36 @@ func CorsSettingsSchema() *pluginsdk.Schema {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: func(k, _, _ string, d *schema.ResourceData) bool {
stateCors, planCors := d.GetChange("site_config.0.cors")
if stateCors == nil || planCors == nil {
return false
}
stateAttrs := stateCors.([]interface{})
planAttrs := planCors.([]interface{})

// Fixes https://github.com/hashicorp/terraform-provider-azurerm/issues/22879
// If the plan wants to set default values and the state is empty; suppress diff
if len(stateAttrs) == 0 && len(planAttrs) > 0 && planAttrs[0] != nil {
planAttr := planAttrs[0].(map[string]interface{})

newAllowedOrigins, ok := planAttr["allowed_origins"].(*schema.Set)
if !ok {
return false
}

newSupportCreds, ok := planAttr["support_credentials"].(bool)
if !ok {
return false
}

if newAllowedOrigins.Len() == 0 && !newSupportCreds {
return true
}
}

return false
},
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"allowed_origins": {
Expand Down
16 changes: 12 additions & 4 deletions internal/services/appservice/linux_function_app_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,15 @@ func TestAccLinuxFunctionApp_corsUpdate(t *testing.T) {
},
data.ImportStep("site_credential.0.password"),
{
Config: r.withCorsSupportCredentialsOnly(data, SkuStandardPlan),
Config: r.withCorsSupportCredentialsOnly(data, SkuStandardPlan, true),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("kind").HasValue("functionapp,linux"),
),
},
data.ImportStep("site_credential.0.password"),
{
Config: r.withCorsSupportCredentialsOnly(data, SkuStandardPlan, false),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("kind").HasValue("functionapp,linux"),
Expand Down Expand Up @@ -1929,7 +1937,7 @@ resource "azurerm_linux_function_app" "test" {
`, r.template(data, planSku), data.RandomInteger)
}

func (r LinuxFunctionAppResource) withCorsSupportCredentialsOnly(data acceptance.TestData, planSku string) string {
func (r LinuxFunctionAppResource) withCorsSupportCredentialsOnly(data acceptance.TestData, planSku string, enabled bool) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
Expand All @@ -1948,11 +1956,11 @@ resource "azurerm_linux_function_app" "test" {
site_config {
cors {
support_credentials = true
support_credentials = %t
}
}
}
`, r.template(data, planSku), data.RandomInteger)
`, r.template(data, planSku), data.RandomInteger, enabled)
}

func (r LinuxFunctionAppResource) runtimeScaleCheck(data acceptance.TestData, planSku string) string {
Expand Down

0 comments on commit c4b300a

Please sign in to comment.