Skip to content

Commit b8f76c6

Browse files
committed
tfsdk: Initial support for Data Source, Provider, and Resource validation
Reference: #17 Reference: #65
1 parent dacb915 commit b8f76c6

14 files changed

+1574
-43
lines changed

Diff for: tfsdk/data_source_validation.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tfsdk
2+
3+
import (
4+
"context"
5+
)
6+
7+
// DataSourceConfigValidator describes reusable DataSource configuration validation functionality.
8+
type DataSourceConfigValidator interface {
9+
// Description describes the validation in plain text formatting.
10+
Description(context.Context) string
11+
12+
// MarkdownDescription describes the validation in Markdown formatting.
13+
MarkdownDescription(context.Context) string
14+
15+
// Validate performs the validation.
16+
Validate(context.Context, ValidateDataSourceConfigRequest, *ValidateDataSourceConfigResponse)
17+
}
18+
19+
// DataSourceWithConfigValidators is an interface type that extends DataSource to include declarative validations.
20+
type DataSourceWithConfigValidators interface {
21+
DataSource
22+
23+
// ConfigValidators returns a list of functions which will all be performed during validation.
24+
ConfigValidators(context.Context) []DataSourceConfigValidator
25+
}
26+
27+
// DataSourceWithValidateConfig is an interface type that extends DataSource to include imperative validation.
28+
type DataSourceWithValidateConfig interface {
29+
DataSource
30+
31+
// ValidateConfig performs the validation.
32+
ValidateConfig(context.Context, ValidateDataSourceConfigRequest, *ValidateDataSourceConfigResponse)
33+
}

Diff for: tfsdk/provider_validation.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tfsdk
2+
3+
import (
4+
"context"
5+
)
6+
7+
// ProviderConfigValidator describes reusable Provider configuration validation functionality.
8+
type ProviderConfigValidator interface {
9+
// Description describes the validation in plain text formatting.
10+
Description(context.Context) string
11+
12+
// MarkdownDescription describes the validation in Markdown formatting.
13+
MarkdownDescription(context.Context) string
14+
15+
// Validate performs the validation.
16+
Validate(context.Context, ValidateProviderConfigRequest, *ValidateProviderConfigResponse)
17+
}
18+
19+
// ProviderWithConfigValidators is an interface type that extends Provider to include declarative validations.
20+
type ProviderWithConfigValidators interface {
21+
Provider
22+
23+
// ConfigValidators returns a list of functions which will all be performed during validation.
24+
ConfigValidators(context.Context) []ProviderConfigValidator
25+
}
26+
27+
// ProviderWithValidateConfig is an interface type that extends Provider to include imperative validation.
28+
type ProviderWithValidateConfig interface {
29+
Provider
30+
31+
// ValidateConfig performs the validation.
32+
ValidateConfig(context.Context, ValidateProviderConfigRequest, *ValidateProviderConfigResponse)
33+
}

Diff for: tfsdk/request_validation.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package tfsdk
2+
3+
// ValidateDataSourceConfigRequest represents a request to validate the
4+
// configuration of a data source. An instance of this request struct is
5+
// supplied as an argument to the DataSource ValidateConfig receiver method
6+
// or automatically passed through to each ConfigValidator.
7+
type ValidateDataSourceConfigRequest struct {
8+
// Config is the configuration the user supplied for the data source.
9+
//
10+
// This configuration may contain unknown values if a user uses
11+
// interpolation or other functionality that would prevent Terraform
12+
// from knowing the value at request time.
13+
Config Config
14+
15+
// TypeName is the data source type name.
16+
TypeName string
17+
}
18+
19+
// ValidateProviderConfigRequest represents a request to validate the
20+
// configuration of a provider. An instance of this request struct is
21+
// supplied as an argument to the Provider ValidateConfig receiver method
22+
// or automatically passed through to each ConfigValidator.
23+
type ValidateProviderConfigRequest struct {
24+
// Config is the configuration the user supplied for the provider.
25+
//
26+
// This configuration may contain unknown values if a user uses
27+
// interpolation or other functionality that would prevent Terraform
28+
// from knowing the value at request time.
29+
Config Config
30+
}
31+
32+
// ValidateResourceConfigRequest represents a request to validate the
33+
// configuration of a resource. An instance of this request struct is
34+
// supplied as an argument to the Resource ValidateConfig receiver method
35+
// or automatically passed through to each ConfigValidator.
36+
type ValidateResourceConfigRequest struct {
37+
// Config is the configuration the user supplied for the resource.
38+
//
39+
// This configuration may contain unknown values if a user uses
40+
// interpolation or other functionality that would prevent Terraform
41+
// from knowing the value at request time.
42+
Config Config
43+
44+
// TypeName is the resource type name.
45+
TypeName string
46+
}

Diff for: tfsdk/resource_validation.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tfsdk
2+
3+
import (
4+
"context"
5+
)
6+
7+
// ResourceConfigValidator describes reusable Resource configuration validation functionality.
8+
type ResourceConfigValidator interface {
9+
// Description describes the validation in plain text formatting.
10+
Description(context.Context) string
11+
12+
// MarkdownDescription describes the validation in Markdown formatting.
13+
MarkdownDescription(context.Context) string
14+
15+
// Validate performs the validation.
16+
Validate(context.Context, ValidateResourceConfigRequest, *ValidateResourceConfigResponse)
17+
}
18+
19+
// ResourceWithConfigValidators is an interface type that extends Resource to include declarative validations.
20+
type ResourceWithConfigValidators interface {
21+
Resource
22+
23+
// ConfigValidators returns a list of functions which will all be performed during validation.
24+
ConfigValidators(context.Context) []ResourceConfigValidator
25+
}
26+
27+
// ResourceWithValidateConfig is an interface type that extends Resource to include imperative validation.
28+
type ResourceWithValidateConfig interface {
29+
Resource
30+
31+
// ValidateConfig performs the validation.
32+
ValidateConfig(context.Context, ValidateResourceConfigRequest, *ValidateResourceConfigResponse)
33+
}

Diff for: tfsdk/response_validation.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tfsdk
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
5+
)
6+
7+
// ValidateDataSourceConfigResponse represents a response to a
8+
// ValidateDataSourceConfigRequest. An instance of this response struct is
9+
// supplied as an argument to the DataSource ValidateConfig receiver method
10+
// or automatically passed through to each ConfigValidator.
11+
type ValidateDataSourceConfigResponse struct {
12+
// Diagnostics report errors or warnings related to validating the data
13+
// source configuration. An empty slice indicates success, with no warnings
14+
// or errors generated.
15+
Diagnostics []*tfprotov6.Diagnostic
16+
}
17+
18+
// ValidateResourceConfigResponse represents a response to a
19+
// ValidateResourceConfigRequest. An instance of this response struct is
20+
// supplied as an argument to the Resource ValidateConfig receiver method
21+
// or automatically passed through to each ConfigValidator.
22+
type ValidateResourceConfigResponse struct {
23+
// Diagnostics report errors or warnings related to validating the resource
24+
// configuration. An empty slice indicates success, with no warnings or
25+
// errors generated.
26+
Diagnostics []*tfprotov6.Diagnostic
27+
}
28+
29+
// ValidateProviderConfigResponse represents a response to a
30+
// ValidateProviderConfigRequest. An instance of this response struct is
31+
// supplied as an argument to the Provider ValidateConfig receiver method
32+
// or automatically passed through to each ConfigValidator.
33+
type ValidateProviderConfigResponse struct {
34+
// Diagnostics report errors or warnings related to validating the provider
35+
// configuration. An empty slice indicates success, with no warnings or
36+
// errors generated.
37+
Diagnostics []*tfprotov6.Diagnostic
38+
}

0 commit comments

Comments
 (0)