Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/schema: Add validation to prevent write-only attributes in sets and set based data #1095

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions internal/fwschema/write_only_nested_attribute_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ func ContainsAnyWriteOnlyChildAttributes(nestedAttr NestedAttribute) bool {
return false
}

// BlockContainsAnyWriteOnlyChildAttributes will return true if any child attribute for the
// given nested block has WriteOnly set to true.
func BlockContainsAnyWriteOnlyChildAttributes(block Block) bool {
nestedObjAttrs := block.GetNestedObject().GetAttributes()
nestedObjBlocks := block.GetNestedObject().GetBlocks()

for _, childAttr := range nestedObjAttrs {
if childAttr.IsWriteOnly() {
return true
}

nestedAttribute, ok := childAttr.(NestedAttribute)
if ok {
if ContainsAnyWriteOnlyChildAttributes(nestedAttribute) {
return true
}
}
}

for _, childBlock := range nestedObjBlocks {
if BlockContainsAnyWriteOnlyChildAttributes(childBlock) {
return true
}
}

return false
}

func InvalidWriteOnlyNestedAttributeDiag(attributePath path.Path) diag.Diagnostic {
return diag.NewErrorDiagnostic(
"Invalid Schema Implementation",
Expand All @@ -62,6 +90,26 @@ func InvalidWriteOnlyNestedAttributeDiag(attributePath path.Path) diag.Diagnosti
)
}

func InvalidSetNestedAttributeWithWriteOnlyDiag(attributePath path.Path) diag.Diagnostic {
return diag.NewErrorDiagnostic(
"Invalid Schema Implementation",
"When validating the schema, an implementation issue was found. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("%q is a set nested attribute that contains a WriteOnly child attribute.\n\n", attributePath)+
"Every child attribute of a set nested attribute must have WriteOnly set to false.",
)
}

func SetBlockCollectionWithWriteOnlyDiag(attributePath path.Path) diag.Diagnostic {
return diag.NewErrorDiagnostic(
"Invalid Schema Implementation",
"When validating the schema, an implementation issue was found. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("%q is a set nested block that contains a WriteOnly child attribute.\n\n", attributePath)+
"Every child attribute within a set nested block must have WriteOnly set to false.",
)
}

func InvalidComputedNestedAttributeWithWriteOnlyDiag(attributePath path.Path) diag.Diagnostic {
return diag.NewErrorDiagnostic(
"Invalid Schema Implementation",
Expand Down
Loading
Loading