diff --git a/internal/services/resource/resource_deployment_script_common.go b/internal/services/resource/resource_deployment_script_common.go index 050d4eac60bfb..00d960f4ffd24 100644 --- a/internal/services/resource/resource_deployment_script_common.go +++ b/internal/services/resource/resource_deployment_script_common.go @@ -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" ) @@ -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, } } diff --git a/internal/services/resource/validate/resource_deployment_script_azure_cli_version.go b/internal/services/resource/validate/resource_deployment_script_azure_cli_version.go new file mode 100644 index 0000000000000..94576db7a9086 --- /dev/null +++ b/internal/services/resource/validate/resource_deployment_script_azure_cli_version.go @@ -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 +} diff --git a/internal/services/resource/validate/resource_deployment_script_azure_cli_version_test.go b/internal/services/resource/validate/resource_deployment_script_azure_cli_version_test.go new file mode 100644 index 0000000000000..3b88f76c85f22 --- /dev/null +++ b/internal/services/resource/validate/resource_deployment_script_azure_cli_version_test.go @@ -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) + } + } +} diff --git a/internal/services/resource/validate/resource_deployment_script_azure_power_shell_version.go b/internal/services/resource/validate/resource_deployment_script_azure_power_shell_version.go new file mode 100644 index 0000000000000..78ae11cecdbd6 --- /dev/null +++ b/internal/services/resource/validate/resource_deployment_script_azure_power_shell_version.go @@ -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 +} diff --git a/internal/services/resource/validate/resource_deployment_script_azure_power_shell_version_test.go b/internal/services/resource/validate/resource_deployment_script_azure_power_shell_version_test.go new file mode 100644 index 0000000000000..9b627d697a00d --- /dev/null +++ b/internal/services/resource/validate/resource_deployment_script_azure_power_shell_version_test.go @@ -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) + } + } +}