Skip to content

Commit

Permalink
Provider function to replace validation.NoZeroValues (#2467)
Browse files Browse the repository at this point in the history
* Fix for name with only spaces

* Removed regex and replaced with TrimSpace

* Updated test for new behavior

* Saved as LF only

* Fix lint error

* Update azurerm/data_source_route_table.go

Co-Authored-By: jeffreyCline <[email protected]>

* Update azurerm/resource_arm_route_table.go

Co-Authored-By: jeffreyCline <[email protected]>

* Update azurerm/resource_arm_route_table.go

Co-Authored-By: jeffreyCline <[email protected]>

* Update azurerm/resource_arm_route_table.go

Co-Authored-By: jeffreyCline <[email protected]>

* Update azurerm/resource_arm_route_table.go

Co-Authored-By: jeffreyCline <[email protected]>

* Update signature

* Added test case

* Added special character test cases

* Permission to merge per KT
  • Loading branch information
WodansSon authored Dec 7, 2018
1 parent 379b6ca commit 0cc82c2
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 6 deletions.
4 changes: 2 additions & 2 deletions azurerm/data_source_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-04-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -17,7 +17,7 @@ func dataSourceArmRouteTable() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings,
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),
Expand Down
20 changes: 20 additions & 0 deletions azurerm/helpers/validate/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package validate

import (
"fmt"
"strings"
)

// NoEmptyStrings validates that the string is not just whitespace characters (equal to [\r\n\t\f\v ])
func NoEmptyStrings(i interface{}, k string) ([]string, []error) {
v, ok := i.(string)
if !ok {
return nil, []error{fmt.Errorf("expected type of %q to be string", k)}
}

if strings.TrimSpace(v) == "" {
return nil, []error{fmt.Errorf("%q must not be empty", k)}
}

return nil, nil
}
99 changes: 99 additions & 0 deletions azurerm/helpers/validate/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package validate

import (
"testing"
)

func TestNoEmptyStrings(t *testing.T) {
cases := []struct {
Value string
TestName string
ErrCount int
}{
{
Value: "!",
TestName: "Exclamation",
ErrCount: 0,
},
{
Value: ".",
TestName: "Period",
ErrCount: 0,
},
{
Value: "-",
TestName: "Hyphen",
ErrCount: 0,
},
{
Value: "_",
TestName: "Underscore",
ErrCount: 0,
},
{
Value: "10.1.0.0/16",
TestName: "IP",
ErrCount: 0,
},
{
Value: "",
TestName: "Empty",
ErrCount: 1,
},
{
Value: " ",
TestName: "Space",
ErrCount: 1,
},
{
Value: " ",
TestName: "FiveSpaces",
ErrCount: 1,
},
{
Value: " 1",
TestName: "DoubleSpaceOne",
ErrCount: 0,
},
{
Value: "1 ",
TestName: "OneSpace",
ErrCount: 0,
},
{
Value: "\r",
TestName: "CarriageReturn",
ErrCount: 1,
},
{
Value: "\n",
TestName: "NewLine",
ErrCount: 1,
},
{
Value: "\t",
TestName: "HorizontalTab",
ErrCount: 1,
},
{
Value: "\f",
TestName: "FormFeed",
ErrCount: 1,
},
{
Value: "\v",
TestName: "VerticalTab",
ErrCount: 1,
},
}

for _, tc := range cases {
t.Run(tc.TestName, func(t *testing.T) {
_, errors := NoEmptyStrings(tc.Value, tc.TestName)

if len(errors) != tc.ErrCount {
t.Fatalf("Expected NoEmptyStrings to have %d not %d errors for %q", tc.ErrCount, len(errors), tc.TestName)
}
})
}
}
9 changes: 5 additions & 4 deletions azurerm/resource_arm_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"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/utils"
)

Expand All @@ -30,7 +31,7 @@ func resourceArmRouteTable() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings,
},

"location": locationSchema(),
Expand All @@ -46,13 +47,13 @@ func resourceArmRouteTable() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings,
},

"address_prefix": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings,
},

"next_hop_type": {
Expand All @@ -71,7 +72,7 @@ func resourceArmRouteTable() *schema.Resource {
"next_hop_in_ip_address": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings,
},
},
},
Expand Down

0 comments on commit 0cc82c2

Please sign in to comment.