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

Consistent Error Handling for Setting Terraform State #17117

Closed
bflad opened this issue Jan 14, 2021 · 2 comments
Closed

Consistent Error Handling for Setting Terraform State #17117

bflad opened this issue Jan 14, 2021 · 2 comments
Labels
proposal Proposes new design or functionality. provider Pertains to the provider itself, rather than any interaction with AWS. stale Old or inactive issues managed by automation, if no further action taken these will get closed. technical-debt Addresses areas of the codebase that need refactoring or redesign.

Comments

@bflad
Copy link
Contributor

bflad commented Jan 14, 2021

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

Resources that need to write to the Terraform State use the Terraform Plugin SDK (helper/schema.ResourceData).Set() receiver method. e.g.

// Terraform Plugin SDK v1
resource ExampleThingRead(d *schema.ResourceData, meta interface{}) error {
  // ...
  if err := d.Set("attribute_name", /* ... */); err != nil {
    return fmt.Errorf("error setting attribute_name: %w", err)
  }
  // ...
}

// Terraform Plugin SDK v2
resource ExampleThingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostic {
  // ...
  if err := d.Set("attribute_name", /* ... */); err != nil {
    return diag.FromErr(fmt.Errorf("error setting attribute_name: %w", err))
  }
  // ...
}

Technically speaking, implementing the error checking for every attribute should be done to notify operators about the potential loss of drift detection and cover missing acceptance testing data scenarios. However for pragmatic purposes such as code readability and that most acceptance testing generally covers these types well, the error checking has been considered as optional for "simple" data types such as TypeString (string), instead suggesting it for "complex" types such as TypeList/TypeMap/TypeSet. The choice of whether or not to apply the error checking can be confusing for developers and the implementations can vary.

This issue to discuss if the handling should be standardized across all resources and attributes. If so, coming up with the desired code implementation, then the tactical details of documenting it, applying it to all existing code, and enforcing it via static analysis.

Given that our desire is to move to Terraform Plugin SDK v2 compatible resources over the coming months, it is likely best to focus on that implementation. We may want to consider our own simple diagnostic wrapper to always be used for simplicity and consistency. e.g.

resource ExampleThingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
  var diags diag.Diagnostics
  // ...
  diags = append(diags, attributeSetDiagnostic(d.Set("attribute_name", /* ... */)))
  // ...
  return diags
}

However, it would be better if the Terraform Plugin SDK hosted the implementation details here, since it should be generic across Terraform Providers. I will follow up with those maintainers. It may be better to consider a different receiver method (e.g. d.SetWithDiag()), but that team is better suited to make that determination.

References

@bflad bflad added thinking proposal Proposes new design or functionality. technical-debt Addresses areas of the codebase that need refactoring or redesign. provider Pertains to the provider itself, rather than any interaction with AWS. labels Jan 14, 2021
Copy link

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

@github-actions github-actions bot added the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Dec 27, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 30, 2024
Copy link

github-actions bot commented Mar 1, 2024

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
proposal Proposes new design or functionality. provider Pertains to the provider itself, rather than any interaction with AWS. stale Old or inactive issues managed by automation, if no further action taken these will get closed. technical-debt Addresses areas of the codebase that need refactoring or redesign.
Projects
None yet
Development

No branches or pull requests

2 participants