|
1 | 1 | package schema
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "errors"
|
5 | 6 |
|
6 | 7 | "github.com/hashicorp/terraform-plugin-framework/attr"
|
| 8 | + "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
7 | 9 | "github.com/hashicorp/terraform-plugin-go/tftypes"
|
8 | 10 | )
|
9 | 11 |
|
@@ -64,6 +66,9 @@ type Attribute struct {
|
64 | 66 | // using this attribute, warning them that it is deprecated and
|
65 | 67 | // instructing them on what upgrade steps to take.
|
66 | 68 | DeprecationMessage string
|
| 69 | + |
| 70 | + // Validators defines validation functionality for the attribute. |
| 71 | + Validators []AttributeValidator |
67 | 72 | }
|
68 | 73 |
|
69 | 74 | // ApplyTerraform5AttributePathStep transparently calls
|
@@ -119,3 +124,62 @@ func (a Attribute) Equal(o Attribute) bool {
|
119 | 124 | }
|
120 | 125 | return true
|
121 | 126 | }
|
| 127 | + |
| 128 | +func (a Attribute) Validate(ctx context.Context, req ValidateAttributeRequest, resp *ValidateAttributeResponse) { |
| 129 | + if (a.Attributes == nil || len(a.Attributes.GetAttributes()) == 0) && a.Type == nil { |
| 130 | + resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{ |
| 131 | + Severity: tfprotov6.DiagnosticSeverityError, |
| 132 | + Summary: "Invalid Attribute Definition", |
| 133 | + Detail: "Attribute must define either Attributes or Type. This is always a problem with the provider and should be reported to the provider developer.", |
| 134 | + Attribute: req.AttributePath, |
| 135 | + }) |
| 136 | + |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + if a.Attributes != nil && len(a.Attributes.GetAttributes()) > 0 && a.Type != nil { |
| 141 | + resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{ |
| 142 | + Severity: tfprotov6.DiagnosticSeverityError, |
| 143 | + Summary: "Invalid Attribute Definition", |
| 144 | + Detail: "Attribute cannot define both Attributes and Type. This is always a problem with the provider and should be reported to the provider developer.", |
| 145 | + Attribute: req.AttributePath, |
| 146 | + }) |
| 147 | + |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + attributeConfig, err := req.Config.GetAttribute(ctx, req.AttributePath) |
| 152 | + |
| 153 | + if err != nil { |
| 154 | + resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{ |
| 155 | + Severity: tfprotov6.DiagnosticSeverityError, |
| 156 | + Summary: "Attribute Value Error", |
| 157 | + Detail: "Attribute validation cannot read configuration value. Report this to the provider developer:\n\n" + err.Error(), |
| 158 | + Attribute: req.AttributePath, |
| 159 | + }) |
| 160 | + |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + req.AttributeConfig = attributeConfig |
| 165 | + |
| 166 | + for _, validator := range a.Validators { |
| 167 | + validator.Validate(ctx, req, resp) |
| 168 | + } |
| 169 | + |
| 170 | + if a.Attributes != nil { |
| 171 | + for nestedName, nestedAttr := range a.Attributes.GetAttributes() { |
| 172 | + nestedAttrReq := ValidateAttributeRequest{ |
| 173 | + AttributePath: req.AttributePath.WithAttributeName(nestedName), |
| 174 | + Config: req.Config, |
| 175 | + } |
| 176 | + nestedAttrResp := &ValidateAttributeResponse{} |
| 177 | + |
| 178 | + nestedAttr.Validate(ctx, nestedAttrReq, nestedAttrResp) |
| 179 | + |
| 180 | + resp.Diagnostics = append(resp.Diagnostics, nestedAttrResp.Diagnostics...) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return |
| 185 | +} |
0 commit comments