-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6242 from terraform-providers/f/location-attr
location: moving to it's own package/making this strongly typed
- Loading branch information
Showing
13 changed files
with
170 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters