Skip to content

feat: Add validate_path for pre-flight API validation during terraform plan - #364

Open
johnsolomonj wants to merge 1 commit into
Mastercard:mainfrom
johnsolomonj:feature/validate-path-v2
Open

feat: Add validate_path for pre-flight API validation during terraform plan#364
johnsolomonj wants to merge 1 commit into
Mastercard:mainfrom
johnsolomonj:feature/validate-path-v2

Conversation

@johnsolomonj

Copy link
Copy Markdown

Summary

Adds two new optional restapi_object resource attributes:

  • validate_path — an endpoint to call during terraform plan with the object's data. A non-2xx response immediately fails the plan with the API's error message, surfacing configuration problems before apply.
  • validate_method — HTTP method for the validate call (default: POST).

Closes #362.

Motivation

Many REST APIs expose a dedicated validation endpoint (e.g. POST /resource/validate) that performs a dry-run check without persisting anything. Without this feature, validation only surfaces at apply time when the API rejects the POST/PUT, which can cause partial applies where unrelated resources succeed but the invalid one fails.

Usage

resource "restapi_object" "example" {
  path            = "/tables"
  data            = jsonencode({ ... })
  validate_path   = "/tables/validate"
  validate_method = "POST"   # optional, defaults to POST
}

Behaviour

  • When validate_path is not set, the feature is a no-op — fully backward compatible.
  • When data is unknown at plan time (e.g. depends on another resource), the validate call is skipped to avoid false failures.
  • A non-2xx response produces a clear API pre-flight validation failed diagnostic that includes the validate path and the API's error body.

Changes

File Change
internal/apiclient/object.go Added ValidatePath/ValidateMethod to APIObjectOpts and APIObject; added ValidateObject() method; added GetValidatePath()/GetValidateMethod() accessors
internal/provider/resource_api_object.go Added validate_path/validate_method schema attributes and model fields; implemented resource.ResourceWithConfigValidators via validatePathValidator
internal/apiclient/object_validate_test.go 6 unit tests covering no-op, success, custom method, 4xx/5xx rejection, and accessors
internal/provider/resource_api_object_validate_test.go 3 acceptance tests: success (validate called ≥1×), failure (plan error), no validate_path (no-op)

Test results

ok  github.com/Mastercard/terraform-provider-restapi/internal/apiclient
ok  github.com/Mastercard/terraform-provider-restapi/internal/provider

All existing tests pass. go vet and gofmt clean.

…m plan

Adds two new optional resource attributes:
- validate_path: An endpoint to call during plan with the object's data.
  A non-2xx response fails the plan immediately with the API's error message.
- validate_method: HTTP method for the validate call (default: POST).

This surfaces configuration errors before apply, prevents partial applies
caused by invalid resource configs, and gives faster CI feedback when the
API has richer validation logic than Terraform schema alone can express.

Unit tests added in internal/apiclient/object_validate_test.go.
Acceptance tests added in internal/provider/resource_api_object_validate_test.go.
@johnsolomonj

Copy link
Copy Markdown
Author

Hi @DRuggeri — would love your review on this when you get a chance! Happy to make any changes needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Support pre-flight validation via configurable validate endpoint during terraform plan

2 participants