hashicorp / terraform-plugin-go

A low-level Go binding for the Terraform protocol for integrations to be built on top of.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider Raising Error Diagnostic If Any Diagnostic Is Missing Severity

bflad opened this issue · comments

terraform-plugin-go version

v0.15.0

Use cases

When creating tfprotov5.Diagnostic or tfprotov6.Diagnostic, it is possible to omit the diagnostic Severity field. While the severity has a fallback "invalid" level and Terraform will generally stop execution, it can cause issues with terraform-exec not reporting the diagnostic in its error return for commands. Provider acceptance testing via terraform-plugin-testing using terraform-exec means tests can have undefined behavior to real usage, such as potentially passing tests unexpectedly and not alerting provider developers to a real issue.

Attempted solutions

If there was something to be done here, it would likely be creating static analysis tooling to catch the issue.

Proposal

Rather than a more drastic change in this Go module of making it not possible to create diagnostics without a severity, we can consider augmenting the toproto implementations to raise their own error diagnostic if they detect response diagnostics missing a severity.

Potentially something like:

func Diagnostics(in []*tfprotov5.Diagnostic) ([]*tfplugin5.Diagnostic, error) {
	diagnostics := make([]*tfplugin5.Diagnostic, 0, len(in))
	for _, diag := range in {
		if diag == nil {
			diagnostics = append(diagnostics, nil)
			continue
		}
		d, err := Diagnostic(diag)
		if err != nil {
			return diagnostics, err
		}
		diagnostics = append(diagnostics, d)
		if diag.Severity == tfprotov5.DiagnosticSeverityInvalid { // new logic
			diagnostics = append(
				diagnostics,
				tfplugin5.Diagnostic{
					Severity: tfplugin5.Diagnostic_ERROR,
					Summary: "Diagnostic Missing Severity",
					Detail: "A response diagnostic was missing a severity level. "+
						"This can cause consumers of Terraform command outputs to miss the diagnostic, which can cause unexpected behavior. "+
						"Please report this issue to the provider developers.\n\n"+
						"Summary for Diagnostic Missing Severity: "+diag.Summary, 
				},
			)
		}
	}
	return diagnostics, nil
}

References