-
Notifications
You must be signed in to change notification settings - Fork 91
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
Computed properties cannot be updated in Read #809
Comments
Hi @timlinquist 👋 I've just tried to reproduce the issue you described with the following provider resource code: func (e *exampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"configurable_attribute": schema.StringAttribute{
Optional: true,
Computed: true,
MarkdownDescription: "Example configurable attribute",
},
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Example identifier",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
}
}
type exampleResourceData struct {
ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
Id types.String `tfsdk:"id"`
}
func (e *exampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data exampleResourceData
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
data.Id = types.StringValue("example-id")
data.ConfigurableAttribute = types.StringValue("configurable attribute")
tflog.Trace(ctx, "created a resource")
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (e *exampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data exampleResourceData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
data.ConfigurableAttribute = types.StringValue("set in read")
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
} When I run Terraform will perform the following actions:
# example_resource.example will be created
+ resource "example_resource" "example" {
+ configurable_attribute = (known after apply)
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
example_resource.example: Creating...
example_resource.example: Creation complete after 0s [id=example-id]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. {
"version": 4,
"terraform_version": "1.4.6",
"serial": 1,
"lineage": "2650d772-c5be-87d9-6eec-e1264ea29fa6",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "example_resource",
"name": "example",
"provider": "provider[\"registry.terraform.io/bendbennett/playground\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"configurable_attribute": "configurable attribute",
"id": "example-id"
},
"sensitive_attributes": []
}
]
}
],
"check_results": null
} When I execute example_resource.example: Refreshing state... [id=example-id]
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed. {
"version": 4,
"terraform_version": "1.4.6",
"serial": 2,
"lineage": "2650d772-c5be-87d9-6eec-e1264ea29fa6",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "example_resource",
"name": "example",
"provider": "provider[\"registry.terraform.io/bendbennett/playground\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"configurable_attribute": "set in read",
"id": "example-id"
},
"sensitive_attributes": []
}
]
}
],
"check_results": null
} So I'm unable to reproduce the issue you described. Is there any other provider code or configuration which may not have been included in the example you provided? |
@bendbennett Hi! Sorry I was out of town. I tested again and I indeed see it updated in the state also. Is this expected? Shouldn't a plan be generated that shows the computed property is going to change then the apply be invoked not just the update occur and terraform state nothing changed? Rather than: # s3-static-asset-copy_release.test-version:
resource "s3-static-asset-copy_release" "test-version" {
id = "12f55816-6fad-4160-9050-197a1149ba9e"
version = "25"
}
terraform apply
╷
│ Warning: Provider development overrides are in effect
│
│ The following provider development overrides are set in the CLI configuration:
│ - registry.iac.commercial1.sfdc.cl/gec-mulesoft/s3-static-asset-copy in /users/tlinquist/go/bin
│
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with
│ published releases.
╵
s3-static-asset-copy_release.test-version: Refreshing state... [id=12f55816-6fad-4160-9050-197a1149ba9e]
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
terraform state show s3-static-asset-copy_release.test-version
# s3-static-asset-copy_release.test-version:
resource "s3-static-asset-copy_release" "test-version" {
id = "12f55816-6fad-4160-9050-197a1149ba9e"
version = "44"
}
|
@timlinquist I believe that this is expected behaviour. If the However, if a plan modifier or a default, which is a particular form of plan modifier, is set on the attribute, and the attribute is initially configured in the supplied Terraform configuration during creation and then the configuration is subsequently removed, then there will be a difference between the plan and the state and the Terraform CLI will render this difference. For example: Initial configuration resource "example_resource" "example" {
configurable_attribute = "set in config"
} Schema "configurable_attribute": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString("default configurable attribute"),
}, Initial state "configurable_attribute": "set in config", Updated configuration resource "example_resource" "example" {
# configurable_attribute = "set in config"
} CLI Output Terraform will perform the following actions:
# example_resource.example will be updated in-place
~ resource "example_resource" "example" {
~ configurable_attribute = "set in read" -> "default configurable attribute"
id = "example-id"
}
Plan: 0 to add, 1 to change, 0 to destroy. Updated state "configurable_attribute": "default configurable attribute", |
@bendbennett Got it. Can I hook into this https://developer.hashicorp.com/terraform/plugin/framework/internals/rpcs#planresourcechange-rpc then I believe to do what I want with the computed property? If I am understanding this right I can override that method to generate the plan. Thanks for engaging with me! I can close this issue once you reply again in the even somebody else finds it on Google. |
@timlinquist you may be able to achieve what you want using Plan Modification as plan modifiers are called during the PlanResourceChange RPC. If you would like specific guidance for your use-case could I ask you to open a post on the Terraform Providers Discuss forum describing the details of what you are trying to achieve? As we've covered the original issue of whether computed properties can be updated in the |
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. |
Based on the documentation it suggests I should update the computed property in the Read method of the resource. However the computer properties do not seem to work this way. I believe a plan modifier is required in order for this to work? It's confusing why this is the case or what the intention is behind computed properties and why they can't be updated in the Read method as that is required to get the plan to detect changes are required.
Module version
Relevant provider source code
Terraform Configuration Files
Debug Output
Expected Behavior
Expected the plan to include an update to the version per this guide https://developer.hashicorp.com/terraform/plugin/framework/resources/read
Actual Behavior
A warning was logged that the value was not expected to be updated and the update on the resource state was ignored
Steps to Reproduce
Create a provider/resource using the terraform plugin framework interfaces
Configure a computed property
Change that computed property in the read method
References
The text was updated successfully, but these errors were encountered: