/
schema_validation.go
73 lines (61 loc) · 2.33 KB
/
schema_validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package fwserver
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)
// ValidateSchemaRequest repesents a request for validating a Schema.
type ValidateSchemaRequest struct {
// Config contains the entire configuration of the data source, provider, or 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 tfsdk.Config
}
// ValidateSchemaResponse represents a response to a
// ValidateSchemaRequest.
type ValidateSchemaResponse struct {
// Diagnostics report errors or warnings related to validating the schema.
// An empty slice indicates success, with no warnings or errors generated.
Diagnostics diag.Diagnostics
}
// SchemaValidate performs all Attribute and Block validation.
//
// TODO: Clean up this abstraction back into an internal Schema type method.
// The extra Schema parameter is a carry-over of creating the proto6server
// package from the tfsdk package and not wanting to export the method.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/365
func SchemaValidate(ctx context.Context, s tfsdk.Schema, req ValidateSchemaRequest, resp *ValidateSchemaResponse) {
for name, attribute := range s.Attributes {
attributeReq := tfsdk.ValidateAttributeRequest{
AttributePath: path.Root(name),
AttributePathExpression: path.MatchRoot(name),
Config: req.Config,
}
attributeResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
AttributeValidate(ctx, attribute, attributeReq, attributeResp)
resp.Diagnostics = attributeResp.Diagnostics
}
for name, block := range s.Blocks {
attributeReq := tfsdk.ValidateAttributeRequest{
AttributePath: path.Root(name),
AttributePathExpression: path.MatchRoot(name),
Config: req.Config,
}
attributeResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
BlockValidate(ctx, block, attributeReq, attributeResp)
resp.Diagnostics = attributeResp.Diagnostics
}
if s.DeprecationMessage != "" {
resp.Diagnostics.AddWarning(
"Deprecated",
s.DeprecationMessage,
)
}
}