Skip to content

Commit

Permalink
add validation funcs in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
saravanan30erd committed Oct 26, 2018
1 parent 22c8cc5 commit 0a17b4d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
2 changes: 2 additions & 0 deletions aws/resource_aws_secretsmanager_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ func resourceAwsSecretsManagerSecret() *schema.Resource {
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateSecretManagerSecretName,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name"},
ValidateFunc: validateSecretManagerSecretNamePrefix,
},
"policy": {
Type: schema.TypeString,
Expand Down
27 changes: 27 additions & 0 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,19 @@ func validateLbTargetGroupName(v interface{}, k string) (ws []string, errors []e
return
}

func validateSecretManagerSecretName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9A-Za-z/_+=.@-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only alphanumeric characters and /_+=.@- special characters are allowed in %q", k))
}
if len(value) > 512 {
errors = append(errors, fmt.Errorf(
"%q cannot be greater than 512 characters", k))
}
return
}

func validateLbTargetGroupNamePrefix(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
prefixMaxLength := 32 - resource.UniqueIDSuffixLength
Expand All @@ -2031,3 +2044,17 @@ func validateLbTargetGroupNamePrefix(v interface{}, k string) (ws []string, erro
}
return
}

func validateSecretManagerSecretNamePrefix(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9A-Za-z/_+=.@-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only alphanumeric characters and /_+=.@- special characters are allowed in %q", k))
}
prefixMaxLength := 512 - resource.UniqueIDSuffixLength
if len(value) > prefixMaxLength {
errors = append(errors, fmt.Errorf(
"%q cannot be greater than %d characters", k, prefixMaxLength))
}
return
}
54 changes: 52 additions & 2 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2890,7 +2890,6 @@ func TestValidateLbTargetGroupName(t *testing.T) {
ErrCount: 1,
},
}

for _, tc := range cases {
_, errors := validateLbTargetGroupName(tc.Value, "aws_lb_target_group")
if len(errors) != tc.ErrCount {
Expand All @@ -2917,11 +2916,62 @@ func TestValidateLbTargetGroupNamePrefix(t *testing.T) {
ErrCount: 1,
},
}

for _, tc := range cases {
_, errors := validateLbTargetGroupNamePrefix(tc.Value, "aws_lb_target_group")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected the AWS LB Target Group Name to trigger a validation error for %q", tc.Value)
}
}
}

func TestValidateSecretManagerSecretName(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "testing123!",
ErrCount: 1,
},
{
Value: "testing 123",
ErrCount: 1,
},
{
Value: randomString(513),
ErrCount: 1,
},
}
for _, tc := range cases {
_, errors := validateSecretManagerSecretName(tc.Value, "aws_secretsmanager_secret")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected the AWS Secretsmanager Secret Name to not trigger a validation error for %q", tc.Value)
}
}
}

func TestValidateSecretManagerSecretNamePrefix(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "testing123!",
ErrCount: 1,
},
{
Value: "testing 123",
ErrCount: 1,
},
{
Value: randomString(512),
ErrCount: 1,
},
}
for _, tc := range cases {
_, errors := validateSecretManagerSecretNamePrefix(tc.Value, "aws_secretsmanager_secret")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected the AWS Secretsmanager Secret Name to not trigger a validation error for %q", tc.Value)
}
}
}

0 comments on commit 0a17b4d

Please sign in to comment.