Skip to content

Commit

Permalink
Validate shared image identifier attributes
Browse files Browse the repository at this point in the history
To fail fast during the plan stage if there are some violations.
  • Loading branch information
Alexander Pykavy authored and Alexander Pykavy committed Jul 15, 2022
1 parent fe97b93 commit f8c304d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
21 changes: 12 additions & 9 deletions internal/services/compute/shared_image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,22 @@ func resourceSharedImage() *pluginsdk.Resource {
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"publisher": {
Type: pluginsdk.TypeString,
ForceNew: true,
Required: true,
Type: pluginsdk.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: validate.SharedImageIdentifierAttribute,
},
"offer": {
Type: pluginsdk.TypeString,
ForceNew: true,
Required: true,
Type: pluginsdk.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: validate.SharedImageIdentifierAttribute,
},
"sku": {
Type: pluginsdk.TypeString,
ForceNew: true,
Required: true,
Type: pluginsdk.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: validate.SharedImageIdentifierAttribute,
},
},
},
Expand Down
20 changes: 20 additions & 0 deletions internal/services/compute/validate/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package validate
import (
"fmt"
"regexp"
"strings"

"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -40,6 +41,25 @@ func SharedImageName(v interface{}, k string) (warnings []string, errors []error
return warnings, errors
}

func SharedImageIdentifierAttribute(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

length := len(value)
if length > 128 {
errors = append(errors, fmt.Errorf("%s can be up to 128 characters, currently %d.", k, length))
}

if strings.HasSuffix(value, ".") {
errors = append(errors, fmt.Errorf("%q can not end with a '.', got %q", k, value))
}

if !regexp.MustCompile(`^[A-Za-z0-9._-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%s can only contain alphanumeric, full stops, dashes and underscores. Got %q.", k, value))
}

return warnings, errors
}

func SharedImageVersionName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

Expand Down
63 changes: 63 additions & 0 deletions internal/services/compute/validate/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,69 @@ func TestSharedImageName(t *testing.T) {
}
}

func TestSharedImageIdentifierAttribute(t *testing.T) {
cases := []struct {
Input string
ShouldError bool
}{
{
Input: "",
ShouldError: true,
},
{
Input: "hello",
ShouldError: false,
},
{
Input: "hello.",
ShouldError: true,
},
{
Input: "hello123",
ShouldError: false,
},
{
Input: "hello.123",
ShouldError: false,
},
{
Input: "hello,123",
ShouldError: true,
},
{
Input: "hello_123",
ShouldError: false,
},
{
Input: "hello-123",
ShouldError: false,
},
{
Input: strings.Repeat("a", 128),
ShouldError: false,
},
{
Input: strings.Repeat("a", 129),
ShouldError: true,
},
}

for _, tc := range cases {
t.Run(tc.Input, func(t *testing.T) {
_, errors := SharedImageIdentifierAttribute(tc.Input, "test")

hasErrors := len(errors) > 0
if !hasErrors && tc.ShouldError {
t.Fatalf("Expected an error but didn't get one for %q", tc.Input)
}

if hasErrors && !tc.ShouldError {
t.Fatalf("Expected to get no errors for %q but got %d", tc.Input, len(errors))
}
})
}
}

func TestSharedImageVersionName(t *testing.T) {
cases := []struct {
Input string
Expand Down

0 comments on commit f8c304d

Please sign in to comment.