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

update function apps and app slots to correctly maintain content settings #15907

Merged
merged 4 commits into from
Mar 23, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (r FunctionAppHybridConnectionResource) CustomImporter() sdk.ResourceRunFun
return err
}

if helpers.PlanIsConsumption(*sku) || helpers.PlanIsElastic(*sku) {
if helpers.PlanIsConsumption(sku) || helpers.PlanIsElastic(sku) {
return fmt.Errorf("unsupported plan type. Hybrid Connections are not supported on Consumption or Elastic service plans")
}

Expand Down
19 changes: 19 additions & 0 deletions internal/services/appservice/helpers/function_app_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2153,3 +2153,22 @@ func FlattenFunctionAppAppServiceLogs(input web.SiteLogsConfig) []FunctionAppApp

return []FunctionAppAppServiceLogs{}
}

func ParseContentSettings(input web.StringDictionary, existing map[string]string) map[string]string {
if input.Properties == nil {
return nil
}

out := existing
for k, v := range input.Properties {
switch k {
case "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING":
out[k] = utils.NormalizeNilableString(v)

case "WEBSITE_CONTENTSHARE":
out[k] = utils.NormalizeNilableString(v)
}
}

return out
}
49 changes: 33 additions & 16 deletions internal/services/appservice/helpers/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,51 @@ func AllKnownServicePlanSkus() []string {
return allSkus
}

func PlanIsConsumption(input string) bool {
func PlanIsConsumption(input *string) bool {
if input == nil {
return false
}
for _, v := range consumptionSkus {
if strings.EqualFold(input, v) {
if strings.EqualFold(*input, v) {
return true
}
}

return false
}

func PlanIsElastic(input string) bool {
func PlanIsElastic(input *string) bool {
if input == nil {
return false
}
for _, v := range elasticSkus {
if strings.EqualFold(input, v) {
if strings.EqualFold(*input, v) {
return true
}
}

return false
}

func PlanIsIsolated(input string) bool {
func PlanIsIsolated(input *string) bool {
if input == nil {
return false
}
for _, v := range isolatedSkus {
if strings.EqualFold(input, v) {
if strings.EqualFold(*input, v) {
return true
}
}

return false
}

func PlanIsAppPlan(input string) bool {
func PlanIsAppPlan(input *string) bool {
if input == nil {
return false
}
for _, v := range appServicePlanSkus {
if strings.EqualFold(input, v) {
if strings.EqualFold(*input, v) {
return true
}
}
Expand All @@ -106,19 +118,19 @@ func PlanIsAppPlan(input string) bool {
}

func PlanTypeFromSku(input string) string {
if PlanIsConsumption(input) {
if PlanIsConsumption(&input) {
return ServicePlanTypeConsumption
}

if PlanIsElastic(input) {
if PlanIsElastic(&input) {
return ServicePlanTypeElastic
}

if PlanIsIsolated(input) {
if PlanIsIsolated(&input) {
return ServicePlanTypeIsolated
}

if PlanIsAppPlan(input) {
if PlanIsAppPlan(&input) {
return ServicePlanTypeAppPlan
}

Expand All @@ -131,12 +143,17 @@ func ServicePlanInfoForApp(ctx context.Context, metadata sdk.ResourceMetaData, i
servicePlanClient := metadata.Client.AppService.ServicePlanClient
var rg, siteName string

if appId, ok := id.(parse.WebAppId); ok {
switch appId := id.(type) {
case parse.WebAppId:
rg = appId.ResourceGroup
siteName = appId.SiteName
}

if appId, ok := id.(parse.FunctionAppId); ok {
case parse.WebAppSlotId:
rg = appId.ResourceGroup
siteName = appId.SiteName
case parse.FunctionAppId:
rg = appId.ResourceGroup
siteName = appId.SiteName
case parse.FunctionAppSlotId:
rg = appId.ResourceGroup
siteName = appId.SiteName
}
Expand Down
61 changes: 31 additions & 30 deletions internal/services/appservice/helpers/service_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,148 +4,149 @@ import (
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/helpers"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func TestPlanIsConsumption(t *testing.T) {
input := []struct {
name string
name *string
isConsumption bool
}{
{
name: "",
name: utils.String(""),
isConsumption: false,
},
{
name: "Y1",
name: utils.String("Y1"),
isConsumption: true,
},
{
name: "EP1",
name: utils.String("EP1"),
isConsumption: false,
},
{
name: "S1",
name: utils.String("S1"),
isConsumption: false,
},
}

for _, v := range input {
if actual := helpers.PlanIsConsumption(v.name); actual != v.isConsumption {
t.Fatalf("expected %s to be %t, got %t", v.name, v.isConsumption, actual)
t.Fatalf("expected %s to be %t, got %t", *v.name, v.isConsumption, actual)
}
}
}

func TestPlanIsElastic(t *testing.T) {
input := []struct {
name string
name *string
isElastic bool
}{
{
name: "",
name: utils.String(""),
isElastic: false,
},
{
name: "Y1",
name: utils.String("Y1"),
isElastic: false,
},
{
name: "EP1",
name: utils.String("EP1"),
isElastic: true,
},
{
name: "S1",
name: utils.String("S1"),
isElastic: false,
},
}

for _, v := range input {
if actual := helpers.PlanIsElastic(v.name); actual != v.isElastic {
t.Fatalf("expected %s to be %t, got %t", v.name, v.isElastic, actual)
t.Fatalf("expected %s to be %t, got %t", *v.name, v.isElastic, actual)
}
}
}

func TestPlanIsIsolated(t *testing.T) {
input := []struct {
name string
name *string
isIsolated bool
}{
{
name: "",
name: utils.String(""),
isIsolated: false,
},
{
name: "Y1",
name: utils.String("Y1"),
isIsolated: false,
},
{
name: "EP1",
name: utils.String("EP1"),
isIsolated: false,
},
{
name: "S1",
name: utils.String("S1"),
isIsolated: false,
},
{
name: "I1",
name: utils.String("I1"),
isIsolated: true,
},
{
name: "I1v2",
name: utils.String("I1v2"),
isIsolated: true,
},
}

for _, v := range input {
if actual := helpers.PlanIsIsolated(v.name); actual != v.isIsolated {
t.Fatalf("expected %s to be %t, got %t", v.name, v.isIsolated, actual)
t.Fatalf("expected %s to be %t, got %t", *v.name, v.isIsolated, actual)
}
}
}

func TestPlanIsAppPlan(t *testing.T) {
input := []struct {
name string
name *string
isAppPlan bool
}{
{
name: "",
name: utils.String(""),
isAppPlan: false,
},
{
name: "Y1",
name: utils.String("Y1"),
isAppPlan: false,
},
{
name: "EP1",
name: utils.String("EP1"),
isAppPlan: false,
},
{
name: "B1",
name: utils.String("B1"),
isAppPlan: true,
},
{
name: "S1",
name: utils.String("S1"),
isAppPlan: true,
},
{
name: "P1v3",
name: utils.String("P1v3"),
isAppPlan: true,
},
{
name: "I1",
name: utils.String("I1"),
isAppPlan: false,
},
{
name: "I1v2",
name: utils.String("I1v2"),
isAppPlan: false,
},
}

for _, v := range input {
if actual := helpers.PlanIsAppPlan(v.name); actual != v.isAppPlan {
t.Fatalf("expected %s to be %t, got %t", v.name, v.isAppPlan, actual)
t.Fatalf("expected %s to be %t, got %t", *v.name, v.isAppPlan, actual)
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion internal/services/appservice/linux_function_app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,14 @@ func (r LinuxFunctionAppResource) Update() sdk.ResourceFunc {
return fmt.Errorf("reading Linux %s: %v", id, err)
}

_, planSKU, err := helpers.ServicePlanInfoForApp(ctx, metadata, *id)
if err != nil {
return err
}

// Only send for ElasticPremium
sendContentSettings := helpers.PlanIsElastic(planSKU)

// Some service plan updates are allowed - see customiseDiff for exceptions
if metadata.ResourceData.HasChange("service_plan_id") {
existing.SiteProperties.ServerFarmID = utils.String(state.ServicePlanId)
Expand Down Expand Up @@ -704,13 +712,28 @@ func (r LinuxFunctionAppResource) Update() sdk.ResourceFunc {
}
}

if sendContentSettings {
appSettingsResp, err := client.ListApplicationSettings(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
return fmt.Errorf("reading App Settings for Windows %s: %+v", id, err)
}
if state.AppSettings == nil {
state.AppSettings = make(map[string]string)
}
state.AppSettings = helpers.ParseContentSettings(appSettingsResp, state.AppSettings)
}

// Note: We process this regardless to give us a "clean" view of service-side app_settings, so we can reconcile the user-defined entries later
siteConfig, err := helpers.ExpandSiteConfigLinuxFunctionApp(state.SiteConfig, existing.SiteConfig, metadata, state.FunctionExtensionsVersion, storageString, state.StorageUsesMSI)
if state.BuiltinLogging {
if state.AppSettings == nil && !state.StorageUsesMSI {
state.AppSettings = make(map[string]string)
}
state.AppSettings["AzureWebJobsDashboard"] = storageString
if !state.StorageUsesMSI {
state.AppSettings["AzureWebJobsDashboard"] = storageString
} else {
state.AppSettings["AzureWebJobsDashboard__accountName"] = state.StorageAccountName
}
}

if metadata.ResourceData.HasChange("site_config") {
Expand Down
Loading