Skip to content

Commit

Permalink
add validation that required won't be false
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomer-Buhadana-Port committed Jan 29, 2025
1 parent 14f639f commit 255c770
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
40 changes: 40 additions & 0 deletions port/action/isTrueValidator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package action

import (
"context"

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

type isTrueValidator struct{}

var _ validator.Bool = isTrueValidator{}

// Description describes the validation in plain text formatting.
func (v isTrueValidator) Description(ctx context.Context) string {
return "Value must be true"
}

// MarkdownDescription describes the validation in Markdown formatting.
func (v isTrueValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}

// ValidateBool performs the validation.
func (v isTrueValidator) ValidateBool(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}
value := req.ConfigValue
if value.Equal(types.BoolValue(true)) {
return
}
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueMatchDiagnostic(
req.Path,
v.Description(ctx),
value.String(),
))

}
38 changes: 38 additions & 0 deletions port/action/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,44 @@ func TestAccPortActionImport(t *testing.T) {
})
}

func TestPortActionFalseRequiredPropShouldNotWork(t *testing.T) {
blueprintIdentifier := utils.GenID()
actionIdentifier := utils.GenID()
var testAccActionConfigCreate = testAccCreateBlueprintConfig(blueprintIdentifier) + fmt.Sprintf(`
resource "port_action" "create_microservice" {
title = "TF Provider Test"
identifier = "%s"
icon = "Terraform"
self_service_trigger = {
operation = "DAY-2"
blueprint_identifier = port_blueprint.microservice.identifier
user_properties = {
"string_props" = {
"myStringIdentifier" = {
"title" = "My String Identifier"
"required" = false
}
}
}
}
webhook_method = {
url = "https://getport.io"
}
}`, actionIdentifier)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,

Steps: []resource.TestStep{
{
Config: acctest.ProviderConfig + testAccActionConfigCreate,
ExpectError: regexp.MustCompile(`.*Invalid Attribute Value Match*`),
},
},
})
}

func TestAccPortActionUpdate(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
Expand Down
1 change: 1 addition & 0 deletions port/action/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func MetadataProperties() map[string]schema.Attribute {
MarkdownDescription: "Whether the property is required, by default not required, this property can't be set at the same time if `required_jq_query` is set, and only supports true as value",
Optional: true,
Validators: []validator.Bool{
isTrueValidator{},
boolvalidator.ConflictsWith(path.MatchRoot("self_service_trigger").AtName("required_jq_query")),
},
},
Expand Down

0 comments on commit 255c770

Please sign in to comment.