Skip to content

Commit

Permalink
change validate
Browse files Browse the repository at this point in the history
  • Loading branch information
teowa committed Oct 13, 2023
1 parent 031d629 commit 4e3e412
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-10-01/deploymentscripts"
"github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
resourceValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)
Expand Down Expand Up @@ -225,14 +226,14 @@ func getDeploymentScriptArguments(kind DeploymentScriptKind) map[string]*plugins
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: resourceValidate.ResourceDeploymentScriptAzurePowerShellVersion,
}
} else {
result["version"] = &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: resourceValidate.ResourceDeploymentScriptAzureCliVersion,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package validate

import (
"fmt"
"regexp"
)

func ResourceDeploymentScriptAzureCliVersion(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)}
}

var errors []error
if matched := regexp.MustCompile(`^\d+\.\d+\.\d+$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q should be in format x.y.z", k))
}

return nil, errors
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package validate

import "testing"

func TestResourceDeploymentScriptAzureCliVersion(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{

{
Input: "",
Valid: false,
},

{
Input: "2",
Valid: false,
},

{
Input: "1.1",
Valid: false,
},

{
Input: "3..",
Valid: false,
},

{
Input: "2.9.0.1",
Valid: true,
},

{
Input: "2.9.0",
Valid: true,
},

{
Input: "2.0.77",
Valid: true,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := ResourceDeploymentScriptAzureCliVersion(tc.Input, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package validate

import (
"fmt"
"regexp"
)

func ResourceDeploymentScriptAzurePowerShellVersion(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)}
}

var errors []error
if matched := regexp.MustCompile(`^\d+\.\d+$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q should be in format x.y", k))
}

return nil, errors
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package validate

import "testing"

func TestResourceDeploymentScriptAzurePowerShellVersion(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{

{
Input: "",
Valid: false,
},

{
Input: "2",
Valid: false,
},

{
Input: "1.1.1",
Valid: false,
},

{
Input: "3.",
Valid: false,
},

{
Input: "9.7",
Valid: true,
},

{
Input: "10.3",
Valid: true,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := ResourceDeploymentScriptAzurePowerShellVersion(tc.Input, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}

0 comments on commit 4e3e412

Please sign in to comment.