Skip to content

Commit 2f341cb

Browse files
committed
external interface
1 parent 43f5717 commit 2f341cb

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

action/action.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,33 @@ type ActionWithModifyPlan interface {
4949
// diagnostics to practitioners, such as validation errors.
5050
ModifyPlan(context.Context, ModifyPlanRequest, *ModifyPlanResponse)
5151
}
52+
53+
// ActionWithConfigValidators is an interface type that extends Action to include declarative validations.
54+
//
55+
// Declaring validation using this methodology simplifies implementation of
56+
// reusable functionality. These also include descriptions, which can be used
57+
// for automating documentation.
58+
//
59+
// Validation will include ConfigValidators and ValidateConfig, if both are
60+
// implemented, in addition to any Attribute or Type validation.
61+
type ActionWithConfigValidators interface {
62+
Action
63+
64+
// ConfigValidators returns a list of functions which will all be performed during validation.
65+
ConfigValidators(context.Context) []ConfigValidator
66+
}
67+
68+
// ActionWithValidateConfig is an interface type that extends Action to include imperative validation.
69+
//
70+
// Declaring validation using this methodology simplifies one-off
71+
// functionality that typically applies to a single action. Any documentation
72+
// of this functionality must be manually added into schema descriptions.
73+
//
74+
// Validation will include ConfigValidators and ValidateConfig, if both are
75+
// implemented, in addition to any Attribute or Type validation.
76+
type ActionWithValidateConfig interface {
77+
Action
78+
79+
// ValidateConfig performs the validation.
80+
ValidateConfig(context.Context, ValidateConfigRequest, *ValidateConfigResponse)
81+
}

action/config_validator.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package action
5+
6+
import "context"
7+
8+
// ConfigValidator describes reusable Action configuration validation functionality.
9+
type ConfigValidator interface {
10+
// Description describes the validation in plain text formatting.
11+
//
12+
// This information may be automatically added to action plain text
13+
// descriptions by external tooling.
14+
Description(context.Context) string
15+
16+
// MarkdownDescription describes the validation in Markdown formatting.
17+
//
18+
// This information may be automatically added to action Markdown
19+
// descriptions by external tooling.
20+
MarkdownDescription(context.Context) string
21+
22+
// ValidateAction performs the validation.
23+
//
24+
// This method name is separate from the datasource.ConfigValidator
25+
// interface ValidateDataSource method name, provider.ConfigValidator
26+
// interface ValidateProvider method name, ephemeral.ConfigValidator
27+
// interface ValidateEphemeralResource method name, and resource.ConfigValidator
28+
// interface ValidateResource method name to allow generic validators.
29+
ValidateAction(context.Context, ValidateConfigRequest, *ValidateConfigResponse)
30+
}

action/validate_config.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package action
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
8+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
9+
)
10+
11+
// ValidateConfigRequest represents a request to validate the
12+
// configuration of an action. An instance of this request struct is
13+
// supplied as an argument to the Action ValidateConfig receiver method
14+
// or automatically passed through to each ConfigValidator.
15+
type ValidateConfigRequest struct {
16+
// Config is the configuration the user supplied for the action.
17+
//
18+
// This configuration may contain unknown values if a user uses
19+
// interpolation or other functionality that would prevent Terraform
20+
// from knowing the value at request time.
21+
Config tfsdk.Config
22+
}
23+
24+
// ValidateConfigResponse represents a response to a
25+
// ValidateConfigRequest. An instance of this response struct is
26+
// supplied as an argument to the Action ValidateConfig receiver method
27+
// or automatically passed through to each ConfigValidator.
28+
type ValidateConfigResponse struct {
29+
// Diagnostics report errors or warnings related to validating the action
30+
// configuration. An empty slice indicates success, with no warnings or
31+
// errors generated.
32+
Diagnostics diag.Diagnostics
33+
}

0 commit comments

Comments
 (0)