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

location: moving to it's own package/making this strongly typed #6242

Merged
merged 3 commits into from
Mar 25, 2020
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
3 changes: 2 additions & 1 deletion azurerm/helpers/azure/hdinsight.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -41,7 +42,7 @@ func SchemaHDInsightTier() *schema.Schema {
string(hdinsight.Premium),
}, false),
// TODO: file a bug about this
DiffSuppressFunc: SuppressLocationDiff,
DiffSuppressFunc: location.DiffSuppressFunc,
}
}

Expand Down
40 changes: 7 additions & 33 deletions azurerm/helpers/azure/location.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,27 @@
package azure

import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
)

func SchemaLocation() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: NormalizeLocation,
DiffSuppressFunc: SuppressLocationDiff,
}
return location.Schema()
}

func SchemaLocationOptional() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
StateFunc: NormalizeLocation,
DiffSuppressFunc: SuppressLocationDiff,
}
return location.SchemaOptional()
}

// todo should we change this to SchemaLocationComputed
func SchemaLocationForDataSource() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Computed: true,
}
return location.SchemaComputed()
}

// azure.NormalizeLocation is a function which normalises human-readable region/location
// names (e.g. "West US") to the values used and returned by the Azure API (e.g. "westus").
// In state we track the API internal version as it is easier to go from the human form
// to the canonical form than the other way around.
func NormalizeLocation(location interface{}) string {
input := location.(string)
return strings.Replace(strings.ToLower(input), " ", "", -1)
}

func SuppressLocationDiff(_, old, new string, _ *schema.ResourceData) bool {
return NormalizeLocation(old) == NormalizeLocation(new)
}

func HashAzureLocation(location interface{}) int {
return hashcode.String(NormalizeLocation(location.(string)))
func NormalizeLocation(input interface{}) string {
loc := input.(string)
return location.Normalize(loc)
}
19 changes: 19 additions & 0 deletions azurerm/internal/location/normalize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package location

import "strings"

// Normalize transforms the human readable Azure Region/Location names (e.g. `West US`)
// into the canonical value to allow comparisons between user-code and API Responses
func Normalize(input string) string {
return strings.Replace(strings.ToLower(input), " ", "", -1)
}

// NormalizeNilable normalizes the Location field even if it's nil to ensure this field
// can always have a value
func NormalizeNilable(input *string) string {
if input == nil {
return ""
}

return Normalize(*input)
}
65 changes: 65 additions & 0 deletions azurerm/internal/location/normalize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package location

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func TestNormalizeLocation(t *testing.T) {
cases := []struct {
input string
expected string
}{
{
input: "West US",
expected: "westus",
},
{
input: "South East Asia",
expected: "southeastasia",
},
{
input: "southeastasia",
expected: "southeastasia",
},
}

for _, v := range cases {
actual := Normalize(v.input)
if v.expected != actual {
t.Fatalf("Expected %q but got %q", v.expected, actual)
}
}
}

func TestNormalizeNilableLocation(t *testing.T) {
cases := []struct {
input *string
expected string
}{
{
input: utils.String("West US"),
expected: "westus",
},
{
input: utils.String("South East Asia"),
expected: "southeastasia",
},
{
input: utils.String("southeastasia"),
expected: "southeastasia",
},
{
input: nil,
expected: "",
},
}

for _, v := range cases {
actual := NormalizeNilable(v.input)
if v.expected != actual {
t.Fatalf("Expected %q but got %q", v.expected, actual)
}
}
}
52 changes: 52 additions & 0 deletions azurerm/internal/location/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package location

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate"
)

// Schema returns the default Schema which should be used for Location fields
// where these are Required and Cannot be Changed
func Schema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
StateFunc: StateFunc,
DiffSuppressFunc: DiffSuppressFunc,
}
}

// SchemaOptional returns the Schema for a Location field where this can be optionally specified
func SchemaOptional() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
StateFunc: StateFunc,
DiffSuppressFunc: DiffSuppressFunc,
}
}

func SchemaComputed() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Computed: true,
}
}

func DiffSuppressFunc(_, old, new string, _ *schema.ResourceData) bool {
return Normalize(old) == Normalize(new)
}

func HashCode(location interface{}) int {
loc := location.(string)
return hashcode.String(Normalize(loc))
}

func StateFunc(location interface{}) string {
input := location.(string)
return Normalize(input)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -100,8 +101,8 @@ func resourceArmApplicationInsightsWebTests() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
StateFunc: azure.NormalizeLocation,
DiffSuppressFunc: azure.SuppressLocationDiff,
StateFunc: location.StateFunc,
DiffSuppressFunc: location.DiffSuppressFunc,
},
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -75,8 +76,8 @@ func resourceArmSharedImageVersion() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
StateFunc: azure.NormalizeLocation,
DiffSuppressFunc: azure.SuppressLocationDiff,
StateFunc: location.StateFunc,
DiffSuppressFunc: location.DiffSuppressFunc,
},

"regional_replica_count": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -80,7 +81,7 @@ func resourceArmContainerRegistry() *schema.Resource {
Type: schema.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
Set: azure.HashAzureLocation,
Set: location.HashCode,
},

"storage_account_id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -110,7 +111,7 @@ func resourceArmMonitorActionGroup() *schema.Resource {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
DiffSuppressFunc: azure.SuppressLocationDiff,
DiffSuppressFunc: location.DiffSuppressFunc,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -60,8 +61,8 @@ func resourceArmMonitorLogProfile() *schema.Resource {
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
StateFunc: azure.NormalizeLocation,
DiffSuppressFunc: azure.SuppressLocationDiff,
StateFunc: location.StateFunc,
DiffSuppressFunc: location.DiffSuppressFunc,
},
Set: schema.HashString,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -132,8 +133,8 @@ func resourceArmNetworkWatcherFlowLog() *schema.Resource {
"workspace_region": {
Type: schema.TypeString,
Required: true,
StateFunc: azure.NormalizeLocation,
DiffSuppressFunc: azure.SuppressLocationDiff,
StateFunc: location.StateFunc,
DiffSuppressFunc: location.DiffSuppressFunc,
},

"workspace_resource_id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func resourceArmResourceGroupCreateUpdate(d *schema.ResourceData, meta interface
defer cancel()

name := d.Get("name").(string)
location := azure.NormalizeLocation(d.Get("location").(string))
location := location.Normalize(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

if features.ShouldResourcesBeImported() && d.IsNewResource() {
Expand Down Expand Up @@ -110,9 +110,7 @@ func resourceArmResourceGroupRead(d *schema.ResourceData, meta interface{}) erro
}

d.Set("name", resp.Name)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
d.Set("location", location.NormalizeNilable(resp.Location))
return tags.FlattenAndSet(d, resp.Tags)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -106,8 +107,8 @@ func resourceArmTrafficManagerEndpoint() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
StateFunc: azure.NormalizeLocation,
DiffSuppressFunc: azure.SuppressLocationDiff,
StateFunc: location.StateFunc,
DiffSuppressFunc: location.DiffSuppressFunc,
},

"min_child_endpoints": {
Expand Down