Skip to content

Commit

Permalink
refactor: removing (some) fields from the top level (#2257)
Browse files Browse the repository at this point in the history
* refactor: moving resource group name into the azure folder

* refactor: moving location into the common schema/azure

* refactor: moving zones into the common schema/azure directory

* removing the unused test_utils function
  • Loading branch information
tombuildsstuff authored Nov 7, 2018
1 parent bef6ec5 commit 984662e
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 75 deletions.
58 changes: 58 additions & 0 deletions azurerm/common_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package azurerm

// NOTE: methods in this file will be moved to `./helpers/azurerm` in time
// new methods should be added to this directory instead.
// This file exists to be able to consolidate files in the root

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

func locationSchema() *schema.Schema {
return azure.SchemaLocation()
}

func locationForDataSourceSchema() *schema.Schema {
return azure.SchemaLocationForDataSource()
}

func deprecatedLocationSchema() *schema.Schema {
return azure.SchemaLocationDeprecated()
}

func azureRMNormalizeLocation(location interface{}) string {
return azure.NormalizeLocation(location)
}

func azureRMSuppressLocationDiff(k, old, new string, d *schema.ResourceData) bool {
return azure.SuppressLocationDiff(k, old, new, d)
}

func resourceGroupNameSchema() *schema.Schema {
return azure.SchemaResourceGroupName()
}

func resourceGroupNameDiffSuppressSchema() *schema.Schema {
return azure.SchemaResourceGroupNameDiffSuppress()
}

func resourceGroupNameForDataSourceSchema() *schema.Schema {
return azure.SchemaResourceGroupNameForDataSource()
}

func zonesSchema() *schema.Schema {
return azure.SchemaZones()
}

func singleZonesSchema() *schema.Schema {
return azure.SchemaSingleZone()
}

func zonesSchemaComputed() *schema.Schema {
return azure.SchemaZonesComputed()
}

func expandZones(v []interface{}) *[]string {
return azure.ExpandZones(v)
}
22 changes: 11 additions & 11 deletions azurerm/location.go → azurerm/helpers/azure/location.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package azurerm
package azure

import (
"strings"

"github.com/hashicorp/terraform/helper/schema"
)

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

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

func deprecatedLocationSchema() *schema.Schema {
func SchemaLocationDeprecated() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Optional: true,
StateFunc: azureRMNormalizeLocation,
DiffSuppressFunc: azureRMSuppressLocationDiff,
StateFunc: NormalizeLocation,
DiffSuppressFunc: SuppressLocationDiff,
Deprecated: "location is no longer used",
}
}
Expand All @@ -38,11 +38,11 @@ func deprecatedLocationSchema() *schema.Schema {
// 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 azureRMNormalizeLocation(location interface{}) string {
func NormalizeLocation(location interface{}) string {
input := location.(string)
return strings.Replace(strings.ToLower(input), " ", "", -1)
}

func azureRMSuppressLocationDiff(k, old, new string, d *schema.ResourceData) bool {
return azureRMNormalizeLocation(old) == azureRMNormalizeLocation(new)
func SuppressLocationDiff(k, old, new string, d *schema.ResourceData) bool {
return NormalizeLocation(old) == NormalizeLocation(new)
}
30 changes: 30 additions & 0 deletions azurerm/helpers/azure/location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package azure

import "testing"

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 := NormalizeLocation(v.input)
if v.expected != actual {
t.Fatalf("Expected %q but got %q", v.expected, actual)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
package azurerm
package azure

import (
"fmt"
"regexp"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
)

func resourceGroupNameSchema() *schema.Schema {
func SchemaResourceGroupName() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmResourceGroupName,
ValidateFunc: validateResourceGroupName,
}
}

func resourceGroupNameDiffSuppressSchema() *schema.Schema {
func SchemaResourceGroupNameDiffSuppress() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: resourceAzurermResourceGroupNameDiffSuppress,
ValidateFunc: validateArmResourceGroupName,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validateResourceGroupName,
}
}

func resourceGroupNameForDataSourceSchema() *schema.Schema {
func SchemaResourceGroupNameForDataSource() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
}
}

func validateArmResourceGroupName(v interface{}, k string) (ws []string, es []error) {
func validateResourceGroupName(v interface{}, k string) (ws []string, es []error) {
value := v.(string)

if len(value) > 80 {
Expand All @@ -52,9 +53,3 @@ func validateArmResourceGroupName(v interface{}, k string) (ws []string, es []er

return ws, es
}

// Resource group names can be capitalised, but we store them in lowercase.
// Use a custom diff function to avoid creation of new resources.
func resourceAzurermResourceGroupNameDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(old) == strings.ToLower(new)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package azurerm
package azure

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
)

func TestValidateArmResourceGroupName(t *testing.T) {
func TestValidateResourceGroupName(t *testing.T) {
cases := []struct {
Value string
ErrCount int
Expand Down Expand Up @@ -58,10 +58,10 @@ func TestValidateArmResourceGroupName(t *testing.T) {
}

for _, tc := range cases {
_, errors := validateArmResourceGroupName(tc.Value, "azurerm_resource_group")
_, errors := validateResourceGroupName(tc.Value, "azurerm_resource_group")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected validateArmResourceGroupName to trigger '%d' errors for '%s' - got '%d'", tc.ErrCount, tc.Value, len(errors))
t.Fatalf("Expected validateResourceGroupName to trigger '%d' errors for '%s' - got '%d'", tc.ErrCount, tc.Value, len(errors))
}
}
}
14 changes: 6 additions & 8 deletions azurerm/zones.go → azurerm/helpers/azure/zones.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package azurerm
package azure

import (
"github.com/hashicorp/terraform/helper/schema"
)
import "github.com/hashicorp/terraform/helper/schema"

func zonesSchema() *schema.Schema {
func SchemaZones() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Expand All @@ -16,7 +14,7 @@ func zonesSchema() *schema.Schema {
}
}

func singleZonesSchema() *schema.Schema {
func SchemaSingleZone() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Expand All @@ -28,7 +26,7 @@ func singleZonesSchema() *schema.Schema {
}
}

func zonesSchemaComputed() *schema.Schema {
func SchemaZonesComputed() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Expand All @@ -39,7 +37,7 @@ func zonesSchemaComputed() *schema.Schema {
}
}

func expandZones(v []interface{}) *[]string {
func ExpandZones(v []interface{}) *[]string {
zones := make([]string, 0)
for _, zone := range v {
zones = append(zones, zone.(string))
Expand Down
10 changes: 0 additions & 10 deletions azurerm/location_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,14 @@ func TestAccAzureRMVirtualNetworkGatewayConnection_updatingSharedKey(t *testing.

func testCheckAzureRMVirtualNetworkGatewayConnectionExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
connectionName, resourceGroup, err := getArmResourceNameAndGroup(s, name)
if err != nil {
return err
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

connectionName := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]

client := testAccProvider.Meta().(*ArmClient).vnetGatewayConnectionsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

Expand Down
9 changes: 6 additions & 3 deletions azurerm/resource_arm_virtual_network_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,14 @@ func TestAccAzureRMVirtualNetworkGateway_vpnClientConfigOpenVPN(t *testing.T) {

func testCheckAzureRMVirtualNetworkGatewayExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
gatewayName, resourceGroup, err := getArmResourceNameAndGroup(s, name)
if err != nil {
return err
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

gatewayName := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]

client := testAccProvider.Meta().(*ArmClient).vnetGatewayClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

Expand Down
22 changes: 0 additions & 22 deletions azurerm/test_utils.go

This file was deleted.

0 comments on commit 984662e

Please sign in to comment.