Skip to content

Commit

Permalink
add ARN and OnceAWeek validators
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonaj committed Feb 9, 2023
1 parent 09d119a commit db391f9
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 74 deletions.
78 changes: 78 additions & 0 deletions internal/framework/validators/arn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package validators

import (
"context"
"fmt"
"regexp"

"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

type arnValidator struct{}

func (validator arnValidator) Description(_ context.Context) string {
return "value must be a valid ARN"
}

func (validator arnValidator) MarkdownDescription(ctx context.Context) string {
return validator.Description(ctx)
}

func (validator arnValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}

if err := validateARN(request.ConfigValue.ValueString()); err != nil {
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
request.Path,
validator.Description(ctx),
err.Error(),
))
return
}
}

func ARN() validator.String {
return arnValidator{}
}

var (
accountIDRegexp = regexp.MustCompile(`^(aws|aws-managed|\d{12})$`)
partitionRegexp = regexp.MustCompile(`^aws(-[a-z]+)*$`)
regionRegexp = regexp.MustCompile(`^[a-z]{2}(-[a-z]+)+-\d$`)
)

func validateARN(value string) error {
if value == "" {
return nil
}

parsedARN, err := arn.Parse(value)

if err != nil {
return fmt.Errorf("(%s) is an invalid ARN: %s", value, err)
}

if parsedARN.Partition == "" {
return fmt.Errorf("(%s) is an invalid ARN: missing partition value", value)
} else if !partitionRegexp.MatchString(parsedARN.Partition) {
return fmt.Errorf("(%s) is an invalid ARN: invalid partition value (expecting to match regular expression: %s)", value, partitionRegexp)
}

if parsedARN.Region != "" && !regionRegexp.MatchString(parsedARN.Region) {
return fmt.Errorf("(%s) is an invalid ARN: invalid region value (expecting to match regular expression: %s)", value, regionRegexp)
}

if parsedARN.AccountID != "" && !accountIDRegexp.MatchString(parsedARN.AccountID) {
return fmt.Errorf("(%s) is an invalid ARN: invalid account ID value (expecting to match regular expression: %s)", value, accountIDRegexp)
}

if parsedARN.Resource == "" {
return fmt.Errorf("(%s) is an invalid ARN: missing resource value", value)
}

return nil
}
84 changes: 84 additions & 0 deletions internal/framework/validators/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package validators
import (
"context"
"fmt"
"regexp"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -46,3 +48,85 @@ func validateUTCTimestamp(value string) error {

return nil
}

type onceADayWindowFormatValidator struct{}

func (validator onceADayWindowFormatValidator) Description(_ context.Context) string {
return "value must be a valid time format"
}

func (validator onceADayWindowFormatValidator) MarkdownDescription(ctx context.Context) string {
return validator.Description(ctx)
}

func (validator onceADayWindowFormatValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}

if err := validateOnceADayWindowFormat(request.ConfigValue.ValueString()); err != nil {
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
request.Path,
validator.Description(ctx),
err.Error(),
))
return
}
}

func OnceADayWindowFormat() validator.String {
return onceADayWindowFormatValidator{}
}

func validateOnceADayWindowFormat(value string) error {
// valid time format is "hh24:mi"
validTimeFormat := "([0-1][0-9]|2[0-3]):([0-5][0-9])"
validTimeFormatConsolidated := "^(" + validTimeFormat + "-" + validTimeFormat + "|)$"

if !regexp.MustCompile(validTimeFormatConsolidated).MatchString(value) {
return fmt.Errorf("(%s) must satisfy the format of \"hh24:mi-hh24:mi\"", value)
}

return nil
}

type onceAWeekWindowFormatValidator struct{}

func (validator onceAWeekWindowFormatValidator) Description(_ context.Context) string {
return "value must be a valid time format"
}

func (validator onceAWeekWindowFormatValidator) MarkdownDescription(ctx context.Context) string {
return validator.Description(ctx)
}

func (validator onceAWeekWindowFormatValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}

if err := validateOnceAWeekWindowFormat(request.ConfigValue.ValueString()); err != nil {
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
request.Path,
validator.Description(ctx),
err.Error(),
))
return
}
}

func OnceAWeekWindowFormat() validator.String {
return onceAWeekWindowFormatValidator{}
}

func validateOnceAWeekWindowFormat(value string) error {
// valid time format is "ddd:hh24:mi"
validTimeFormat := "(sun|mon|tue|wed|thu|fri|sat):([0-1][0-9]|2[0-3]):([0-5][0-9])"
validTimeFormatConsolidated := "^(" + validTimeFormat + "-" + validTimeFormat + "|)$"

val := strings.ToLower(value)
if !regexp.MustCompile(validTimeFormatConsolidated).MatchString(val) {
return fmt.Errorf("(%s) must satisfy the format of \"ddd:hh24:mi-ddd:hh24:mi\"", value)
}
return nil
}
103 changes: 35 additions & 68 deletions internal/service/rds/cluster_fw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit db391f9

Please sign in to comment.