-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathblock_validation.go
More file actions
155 lines (127 loc) · 4.75 KB
/
block_validation.go
File metadata and controls
155 lines (127 loc) · 4.75 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package fwserver
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// BlockValidate performs all Block validation.
//
// TODO: Clean up this abstraction back into an internal Block type method.
// The extra Block 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 BlockValidate(ctx context.Context, b tfsdk.Block, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {
attributeConfig, diags := ConfigGetAttributeValue(ctx, req.Config, req.AttributePath)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
req.AttributeConfig = attributeConfig
for _, validator := range b.Validators {
validator.Validate(ctx, req, resp)
}
nm := b.NestingMode
switch nm {
case tfsdk.BlockNestingModeList:
l, ok := req.AttributeConfig.(types.List)
if !ok {
err := fmt.Errorf("unknown block value type (%s) for nesting mode (%T) at path: %s", req.AttributeConfig.Type(ctx), nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Error",
"Block validation cannot walk schema. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
for idx := range l.Elems {
for name, attr := range b.Attributes {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtListIndex(idx).AtName(name),
AttributePathExpression: req.AttributePathExpression.AtListIndex(idx).AtName(name),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
AttributeValidate(ctx, attr, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
for name, block := range b.Blocks {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtListIndex(idx).AtName(name),
AttributePathExpression: req.AttributePathExpression.AtListIndex(idx).AtName(name),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
BlockValidate(ctx, block, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
}
case tfsdk.BlockNestingModeSet:
s, ok := req.AttributeConfig.(types.Set)
if !ok {
err := fmt.Errorf("unknown block value type (%s) for nesting mode (%T) at path: %s", req.AttributeConfig.Type(ctx), nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Error",
"Block validation cannot walk schema. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
for _, value := range s.Elems {
for name, attr := range b.Attributes {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtSetValue(value).AtName(name),
AttributePathExpression: req.AttributePathExpression.AtSetValue(value).AtName(name),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
AttributeValidate(ctx, attr, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
for name, block := range b.Blocks {
nestedAttrReq := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.AtSetValue(value).AtName(name),
AttributePathExpression: req.AttributePathExpression.AtSetValue(value).AtName(name),
Config: req.Config,
}
nestedAttrResp := &tfsdk.ValidateAttributeResponse{
Diagnostics: resp.Diagnostics,
}
BlockValidate(ctx, block, nestedAttrReq, nestedAttrResp)
resp.Diagnostics = nestedAttrResp.Diagnostics
}
}
default:
err := fmt.Errorf("unknown block validation nesting mode (%T: %v) at path: %s", nm, nm, req.AttributePath)
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Error",
"Block validation cannot walk schema. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
if b.DeprecationMessage != "" && attributeConfig != nil {
tfValue, err := attributeConfig.ToTerraformValue(ctx)
if err != nil {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Error",
"Block validation cannot convert value. Report this to the provider developer:\n\n"+err.Error(),
)
return
}
if !tfValue.IsNull() {
resp.Diagnostics.AddAttributeWarning(
req.AttributePath,
"Block Deprecated",
b.DeprecationMessage,
)
}
}
}