Skip to content

Commit

Permalink
refactor helpers for pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
jackofallops committed Mar 22, 2022
1 parent 85dc4c0 commit 8bdb9c3
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 48 deletions.
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
36 changes: 24 additions & 12 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 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
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ func (r LinuxFunctionAppResource) Update() sdk.ResourceFunc {
}

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

// Some service plan updates are allowed - see customiseDiff for exceptions
if metadata.ResourceData.HasChange("service_plan_id") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ func (r LinuxFunctionAppSlotResource) Update() sdk.ResourceFunc {
return err
}

sendContentSettings := !helpers.PlanIsElastic(*planSKU)
sendContentSettings := !helpers.PlanIsElastic(planSKU)

if metadata.ResourceData.HasChange("enabled") {
existing.SiteProperties.Enabled = utils.Bool(state.Enabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (r WebAppHybridConnectionResource) CustomImporter() sdk.ResourceRunFunc {
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
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func (r WindowsFunctionAppResource) Update() sdk.ResourceFunc {
if err != nil {
return err
}
sendContentSettings := !helpers.PlanIsAppPlan(*planSKU)
sendContentSettings := !helpers.PlanIsAppPlan(planSKU)

// Some service plan updates are allowed - see customiseDiff for exceptions
if metadata.ResourceData.HasChange("service_plan_id") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func (r WindowsFunctionAppSlotResource) Update() sdk.ResourceFunc {
if err != nil {
return err
}
sendContentSettings := !helpers.PlanIsAppPlan(*planSKU)
sendContentSettings := !helpers.PlanIsAppPlan(planSKU)

// Some service plan updates are allowed - see customiseDiff for exceptions
if metadata.ResourceData.HasChange("enabled") {
Expand Down

0 comments on commit 8bdb9c3

Please sign in to comment.