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

fix incorrect field reference #595

Merged
merged 1 commit into from Dec 19, 2022
Merged

fix incorrect field reference #595

merged 1 commit into from Dec 19, 2022

Conversation

Integralist
Copy link
Contributor

While testing out the documentation for creating a default value my LSP informed me that there was no field AttributePlan and upon checking the relevant type I found there was a PlanValue which seems to be what should be referenced in the example code.

@Integralist Integralist requested a review from a team as a code owner December 18, 2022 20:21
@hashicorp-cla
Copy link

hashicorp-cla commented Dec 18, 2022

CLA assistant check
All committers have signed the CLA.

@@ -89,7 +89,7 @@ func (m stringDefaultModifier) PlanModifyString(ctx context.Context, req planmod
return
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should add I was confused by the above logic for returning early if 'unknown' as the attribute has to be marked as both computed and optional so that the user can configure a value while the provider can set a value if none is provided by the user, and so the PlanValue will naturally be 'unknown' or 'known'.

If it's known (e.g. the user provided a value), then of course return early and don't set a value. But if the value is 'unknown' (e.g. the user didn't provide a value), then returning early here will mean you have to programmatically check in the resource's Create method and set the default there (making this whole implementation sort of pointless from a 'I need to set a default' perspective).

So for myself I modified the value like so:
Integralist/terraform-provider-fastly-framework@35d10ec#diff-5dba0fe3376c40571fbc1323ead12e5626199bed5a36e4c9f217e3d9aced8cabR36-R37

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unknown values can come from two main sources:

  • The framework automatically marking Computed attributes with a null configuration value during planning to prevent a class of inconsistent result after apply Terraform errors
  • Other data source and resource attribute values which are only known after apply, e.g.
resource "examplecloud_thing" "example" {}

resource "examplecloud_widget" "example" {
  thing_id = examplecloud_thing.example.id # incoming unknown value during planning
}

It can be incorrect in the latter scenario to unilaterally change the planned value to a default value when the planned value is unknown, since it may be an artifact of the way the configuration is written. Terraform would raise an error if the plan was set to the default and the incoming configuration value was different.

This code example might be best served with an update to check !req.ConfigValue.IsNull() for clarity (and to allow prior plan modification to set a null plan value successfully), but it might be best to create a separate issue/PR to discuss this type of example change further.

Copy link
Member

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank for you this code example fix, @Integralist 🚀

@@ -89,7 +89,7 @@ func (m stringDefaultModifier) PlanModifyString(ctx context.Context, req planmod
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unknown values can come from two main sources:

  • The framework automatically marking Computed attributes with a null configuration value during planning to prevent a class of inconsistent result after apply Terraform errors
  • Other data source and resource attribute values which are only known after apply, e.g.
resource "examplecloud_thing" "example" {}

resource "examplecloud_widget" "example" {
  thing_id = examplecloud_thing.example.id # incoming unknown value during planning
}

It can be incorrect in the latter scenario to unilaterally change the planned value to a default value when the planned value is unknown, since it may be an artifact of the way the configuration is written. Terraform would raise an error if the plan was set to the default and the incoming configuration value was different.

This code example might be best served with an update to check !req.ConfigValue.IsNull() for clarity (and to allow prior plan modification to set a null plan value successfully), but it might be best to create a separate issue/PR to discuss this type of example change further.

@bflad bflad merged commit 5a6c8f4 into hashicorp:main Dec 19, 2022
@bflad bflad added this to the v1.0.1 milestone Dec 19, 2022
@bflad bflad added the documentation Improvements or additions to documentation label Dec 19, 2022
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, 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 Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants