Skip to content

Latest commit

 

History

History
79 lines (58 loc) · 4.07 KB

validate-configuration.mdx

File metadata and controls

79 lines (58 loc) · 4.07 KB
page_title description
Plugin Development - Framework: Validate Data Source Configurations
How to validate data source configurations with the provider development framework.

Validate Configuration

Data sources support validating an entire practitioner configuration in either declarative or imperative logic. Feedback, such as required syntax or acceptable combinations of values, is returned via diagnostics.

This page describes implementation details for validating entire data source configurations, typically referencing multiple attributes. Further documentation is available for other configuration validation concepts:

  • Single attribute validation is a schema-based mechanism for implementing attribute-specific validation logic.
  • Type validation is a schema-based mechanism for implementing reusable validation logic for any attribute using the type.

ConfigValidators Method

The datasource.DataSourceWithConfigValidators interface follows a similar pattern to attribute validation and allows for a more declarative approach. This enables consistent validation logic across multiple data sources. Each validator intended for this interface must implement the datasource.ConfigValidator interface.

The terraform-plugin-framework-validators Go module has a collection of common use case data source configuration validators in the datasourcevalidator package. These use path expressions for matching attributes.

This example will raise an error if a practitioner attempts to configure both attribute_one and attribute_two:

// Other methods to implement the datasource.DataSource interface are omitted for brevity
type ThingDataSource struct {}

func (d ThingDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
    return []datasource.ConfigValidator{
        datasourcevalidator.Conflicting(
            path.MatchRoot("attribute_one"),
            path.MatchRoot("attribute_two"),
        ),
    }
}

ValidateConfig Method

The datasource.DataSourceWithValidateConfig interface is more imperative in design and is useful for validating unique functionality across multiple attributes that typically applies to a single data source.

This example will raise a warning if a practitioner attempts to configure attribute_one, but not attribute_two:

// Other methods to implement the datasource.DataSource interface are omitted for brevity
type ThingDataSource struct {}

type ThingDataSourceModel struct {
    AttributeOne types.String `tfsdk:"attribute_one"`
    AttributeTwo types.String `tfsdk:"attribute_two"`
}

func (d ThingDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
    var data ThingDataSourceModel

    resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

    if resp.Diagnostics.HasError() {
        return
    }

    // If attribute_one is not configured, return without warning.
    if data.AttributeOne.IsNull() || data.AttributeOne.IsUnknown() {
        return
    }

    // If attribute_two is not null, return without warning.
    if !data.AttributeTwo.IsNull() {
        return
    }

    resp.Diagnostics.AddAttributeWarning(
        path.Root("attribute_two"),
        "Missing Attribute Configuration",
        "Expected attribute_two to be configured with attribute_one. "+
            "The data source may return unexpected results.",
    )
}