Skip to content

Commit ecd80f6

Browse files
authored
resource/schema: Add validation to prevent write-only attributes in sets and set based data (#1095)
* Add validation to prevent write-only attributes in sets, set nested blocks, and set nested attributes * Update website documentation
1 parent 0724df1 commit ecd80f6

12 files changed

+786
-722
lines changed

internal/fwschema/write_only_nested_attribute_validation.go

+48
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ func ContainsAnyWriteOnlyChildAttributes(nestedAttr NestedAttribute) bool {
5252
return false
5353
}
5454

55+
// BlockContainsAnyWriteOnlyChildAttributes will return true if any child attribute for the
56+
// given nested block has WriteOnly set to true.
57+
func BlockContainsAnyWriteOnlyChildAttributes(block Block) bool {
58+
nestedObjAttrs := block.GetNestedObject().GetAttributes()
59+
nestedObjBlocks := block.GetNestedObject().GetBlocks()
60+
61+
for _, childAttr := range nestedObjAttrs {
62+
if childAttr.IsWriteOnly() {
63+
return true
64+
}
65+
66+
nestedAttribute, ok := childAttr.(NestedAttribute)
67+
if ok {
68+
if ContainsAnyWriteOnlyChildAttributes(nestedAttribute) {
69+
return true
70+
}
71+
}
72+
}
73+
74+
for _, childBlock := range nestedObjBlocks {
75+
if BlockContainsAnyWriteOnlyChildAttributes(childBlock) {
76+
return true
77+
}
78+
}
79+
80+
return false
81+
}
82+
5583
func InvalidWriteOnlyNestedAttributeDiag(attributePath path.Path) diag.Diagnostic {
5684
return diag.NewErrorDiagnostic(
5785
"Invalid Schema Implementation",
@@ -62,6 +90,26 @@ func InvalidWriteOnlyNestedAttributeDiag(attributePath path.Path) diag.Diagnosti
6290
)
6391
}
6492

93+
func InvalidSetNestedAttributeWithWriteOnlyDiag(attributePath path.Path) diag.Diagnostic {
94+
return diag.NewErrorDiagnostic(
95+
"Invalid Schema Implementation",
96+
"When validating the schema, an implementation issue was found. "+
97+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
98+
fmt.Sprintf("%q is a set nested attribute that contains a WriteOnly child attribute.\n\n", attributePath)+
99+
"Every child attribute of a set nested attribute must have WriteOnly set to false.",
100+
)
101+
}
102+
103+
func SetBlockCollectionWithWriteOnlyDiag(attributePath path.Path) diag.Diagnostic {
104+
return diag.NewErrorDiagnostic(
105+
"Invalid Schema Implementation",
106+
"When validating the schema, an implementation issue was found. "+
107+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
108+
fmt.Sprintf("%q is a set nested block that contains a WriteOnly child attribute.\n\n", attributePath)+
109+
"Every child attribute within a set nested block must have WriteOnly set to false.",
110+
)
111+
}
112+
65113
func InvalidComputedNestedAttributeWithWriteOnlyDiag(attributePath path.Path) diag.Diagnostic {
66114
return diag.NewErrorDiagnostic(
67115
"Invalid Schema Implementation",

0 commit comments

Comments
 (0)