Skip to content

Commit

Permalink
Merge pull request #64 from jfrog/add-project-key-validator
Browse files Browse the repository at this point in the history
Add project_key validator for Framework
  • Loading branch information
alexhung authored May 9, 2024
2 parents d592984 + 9548e02 commit baed78c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.25.2 (May 10, 2024)

IMPROVEMENTS:

* Add project_key validator for Framework
* Update CheckPlan func to improve text output and filtering

## 1.25.1 (May 2, 2024)

IMPROVEMENTS:
Expand Down
38 changes: 25 additions & 13 deletions testutil/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,22 @@ func MkNames(name, resource string) (int, string, string) {
return id, fmt.Sprintf("%s.%s", resource, n), n
}

var ConfigPlanChecks = resource.ConfigPlanChecks{
PostApplyPreRefresh: []plancheck.PlanCheck{
DebugPlan("PostApplyPreRefresh"),
},
PostApplyPostRefresh: []plancheck.PlanCheck{
DebugPlan("PostApplyPostRefresh"),
},
var ConfigPlanChecks = func(resourceName string) resource.ConfigPlanChecks {
return resource.ConfigPlanChecks{
PostApplyPreRefresh: []plancheck.PlanCheck{
DebugPlan(resourceName, "PostApplyPreRefresh"),
},
PostApplyPostRefresh: []plancheck.PlanCheck{
DebugPlan(resourceName, "PostApplyPostRefresh"),
},
}
}

var _ plancheck.PlanCheck = PlanCheck{}

type PlanCheck struct {
Stage string
Stage string
ResourceName string
}

func (p PlanCheck) CheckPlan(ctx context.Context, req plancheck.CheckPlanRequest, resp *plancheck.CheckPlanResponse) {
Expand Down Expand Up @@ -138,10 +141,18 @@ func (p PlanCheck) CheckPlan(ctx context.Context, req plancheck.CheckPlanRequest
})

if len(req.Plan.ResourceDrift) > 0 {
drifts := lo.Map(req.Plan.ResourceDrift, func(c *tfjson.ResourceChange, index int) string {
return fmt.Sprintf("Name: %s, Before: %v, After: %v", c.Name, c.Change.Before, c.Change.After)
drifts := lo.FilterMap(req.Plan.ResourceDrift, func(c *tfjson.ResourceChange, index int) (string, bool) {
tflog.Debug(ctx, "CheckPlan", map[string]interface{}{
"p.ResourceName": p.ResourceName,
"fmt.Sprintf(\"%s.%s\", c.Type, c.Name)": fmt.Sprintf("%s.%s", c.Type, c.Name),
})
driftsMessage := fmt.Sprintf("Name: %s.%s\n\nBefore: %v\n\nAfter: %v\n", c.Type, c.Name, c.Change.Before, c.Change.After)
shouldInclude := p.ResourceName == "" || p.ResourceName == fmt.Sprintf("%s.%s", c.Type, c.Name)
return driftsMessage, shouldInclude
})
resp.Error = fmt.Errorf("expected empty plan, but has resouce drifts(s): %v", strings.Join(drifts, ", "))
if len(drifts) > 0 {
resp.Error = fmt.Errorf("expected empty plan, but has resource drift(s):\n\n%v", strings.Join(drifts, "\n\n"))
}
return
}

Expand All @@ -158,8 +169,9 @@ func (p PlanCheck) CheckPlan(ctx context.Context, req plancheck.CheckPlanRequest
}
}

func DebugPlan(stage string) plancheck.PlanCheck {
func DebugPlan(resourceName, stage string) plancheck.PlanCheck {
return PlanCheck{
Stage: stage,
ResourceName: resourceName,
Stage: stage,
}
}
42 changes: 42 additions & 0 deletions validator/fw/string/project_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package string

import (
"context"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// Ensure our implementation satisfies the validator.String interface.
var _ validator.String = &projectKeyValidator{}

type projectKeyValidator struct{}

func (v projectKeyValidator) Description(_ context.Context) string {
return "value must be a valid email address"
}

func (v projectKeyValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}

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

value := request.ConfigValue.ValueString()
re := regexp.MustCompile(`^[a-z][a-z0-9\-]{1,31}$`)
if !re.MatchString(value) {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueMatchDiagnostic(
request.Path,
"must be 2 - 32 lowercase alphanumeric and hyphen characters",
value,
))
}
}

func ProjectKey() validator.String {
return projectKeyValidator{}
}
2 changes: 1 addition & 1 deletion validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestProjectKey_invalidKeys(t *testing.T) {
t.Errorf("ProjectKey '%s' should fail", projectKey)
}

errorRegex := regexp.MustCompile(`.*project_key must be 2 - 32 lowercase alphanumeric and hyphen characters.*`)
errorRegex := regexp.MustCompile(`.*key must be 2 - 32 lowercase alphanumeric and hyphen characters.*`)
if !errorRegex.MatchString(diag[0].Summary) {
t.Fail()
}
Expand Down

0 comments on commit baed78c

Please sign in to comment.