Skip to content

tfsdk: Initial support for Attribute, Data Source, Provider, and Resource validation #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tfsdk/data_source_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tfsdk

import (
"context"
)

// DataSourceConfigValidator describes reusable DataSource configuration validation functionality.
type DataSourceConfigValidator interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hot take: is Config necessary here, or can we save some typing by removing it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm certainly amenable to updating the name, I'm just not sure if there's any concerns with keeping the names and types relatively close to the RPCs similar to as has been done elsewhere. Should there be any future other implementations with differing semantics, it could be weird to have DataSourceValidator vs DataSourceStateValidator for example.

// Description describes the validation in plain text formatting.
Description(context.Context) string

// MarkdownDescription describes the validation in Markdown formatting.
MarkdownDescription(context.Context) string

// Validate performs the validation.
Validate(context.Context, ValidateDataSourceConfigRequest, *ValidateDataSourceConfigResponse)
}

// DataSourceWithConfigValidators is an interface type that extends DataSource to include declarative validations.
type DataSourceWithConfigValidators interface {
DataSource

// ConfigValidators returns a list of functions which will all be performed during validation.
ConfigValidators(context.Context) []DataSourceConfigValidator
}

// DataSourceWithValidateConfig is an interface type that extends DataSource to include imperative validation.
type DataSourceWithValidateConfig interface {
DataSource

// ValidateConfig performs the validation.
ValidateConfig(context.Context, ValidateDataSourceConfigRequest, *ValidateDataSourceConfigResponse)
}
33 changes: 33 additions & 0 deletions tfsdk/provider_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tfsdk

import (
"context"
)

// ProviderConfigValidator describes reusable Provider configuration validation functionality.
type ProviderConfigValidator interface {
// Description describes the validation in plain text formatting.
Description(context.Context) string

// MarkdownDescription describes the validation in Markdown formatting.
MarkdownDescription(context.Context) string

// Validate performs the validation.
Validate(context.Context, ValidateProviderConfigRequest, *ValidateProviderConfigResponse)
}

// ProviderWithConfigValidators is an interface type that extends Provider to include declarative validations.
type ProviderWithConfigValidators interface {
Provider

// ConfigValidators returns a list of functions which will all be performed during validation.
ConfigValidators(context.Context) []ProviderConfigValidator
}

// ProviderWithValidateConfig is an interface type that extends Provider to include imperative validation.
type ProviderWithValidateConfig interface {
Provider

// ValidateConfig performs the validation.
ValidateConfig(context.Context, ValidateProviderConfigRequest, *ValidateProviderConfigResponse)
}
46 changes: 46 additions & 0 deletions tfsdk/request_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package tfsdk

// ValidateDataSourceConfigRequest represents a request to validate the
// configuration of a data source. An instance of this request struct is
// supplied as an argument to the DataSource ValidateConfig receiver method
// or automatically passed through to each ConfigValidator.
type ValidateDataSourceConfigRequest struct {
// Config is the configuration the user supplied for the data source.
//
// This configuration may contain unknown values if a user uses
// interpolation or other functionality that would prevent Terraform
// from knowing the value at request time.
Config Config

// TypeName is the data source type name.
TypeName string
}

// ValidateProviderConfigRequest represents a request to validate the
// configuration of a provider. An instance of this request struct is
// supplied as an argument to the Provider ValidateConfig receiver method
// or automatically passed through to each ConfigValidator.
type ValidateProviderConfigRequest struct {
// Config is the configuration the user supplied for the provider.
//
// This configuration may contain unknown values if a user uses
// interpolation or other functionality that would prevent Terraform
// from knowing the value at request time.
Config Config
}

// ValidateResourceConfigRequest represents a request to validate the
// configuration of a resource. An instance of this request struct is
// supplied as an argument to the Resource ValidateConfig receiver method
// or automatically passed through to each ConfigValidator.
type ValidateResourceConfigRequest struct {
// Config is the configuration the user supplied for the resource.
//
// This configuration may contain unknown values if a user uses
// interpolation or other functionality that would prevent Terraform
// from knowing the value at request time.
Config Config

// TypeName is the resource type name.
TypeName string
}
33 changes: 33 additions & 0 deletions tfsdk/resource_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tfsdk

import (
"context"
)

// ResourceConfigValidator describes reusable Resource configuration validation functionality.
type ResourceConfigValidator interface {
// Description describes the validation in plain text formatting.
Description(context.Context) string

// MarkdownDescription describes the validation in Markdown formatting.
MarkdownDescription(context.Context) string

// Validate performs the validation.
Validate(context.Context, ValidateResourceConfigRequest, *ValidateResourceConfigResponse)
}

// ResourceWithConfigValidators is an interface type that extends Resource to include declarative validations.
type ResourceWithConfigValidators interface {
Resource

// ConfigValidators returns a list of functions which will all be performed during validation.
ConfigValidators(context.Context) []ResourceConfigValidator
}

// ResourceWithValidateConfig is an interface type that extends Resource to include imperative validation.
type ResourceWithValidateConfig interface {
Resource

// ValidateConfig performs the validation.
ValidateConfig(context.Context, ValidateResourceConfigRequest, *ValidateResourceConfigResponse)
}
38 changes: 38 additions & 0 deletions tfsdk/response_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tfsdk

import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

// ValidateDataSourceConfigResponse represents a response to a
// ValidateDataSourceConfigRequest. An instance of this response struct is
// supplied as an argument to the DataSource ValidateConfig receiver method
// or automatically passed through to each ConfigValidator.
type ValidateDataSourceConfigResponse struct {
// Diagnostics report errors or warnings related to validating the data
// source configuration. An empty slice indicates success, with no warnings
// or errors generated.
Diagnostics []*tfprotov6.Diagnostic
}

// ValidateResourceConfigResponse represents a response to a
// ValidateResourceConfigRequest. An instance of this response struct is
// supplied as an argument to the Resource ValidateConfig receiver method
// or automatically passed through to each ConfigValidator.
type ValidateResourceConfigResponse struct {
// Diagnostics report errors or warnings related to validating the resource
// configuration. An empty slice indicates success, with no warnings or
// errors generated.
Diagnostics []*tfprotov6.Diagnostic
}

// ValidateProviderConfigResponse represents a response to a
// ValidateProviderConfigRequest. An instance of this response struct is
// supplied as an argument to the Provider ValidateConfig receiver method
// or automatically passed through to each ConfigValidator.
type ValidateProviderConfigResponse struct {
// Diagnostics report errors or warnings related to validating the provider
// configuration. An empty slice indicates success, with no warnings or
// errors generated.
Diagnostics []*tfprotov6.Diagnostic
}
Loading