-
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
Implement AddResource()
: Explicitly Set Resource into State Regardless of Returned Errors
#927
Comments
Hi @zliang-akamai 👋 Thank you for raising this. The framework equivalent of terraform-plugin-sdk's func (r ExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data ExampleResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
resp.State.SetAttribute(ctx, path.Root("id"), types.StringValue("testing"))
} Depending on the schema, it may also be possible to use the data model setup and set only the func (r ExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data ExampleResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
data.ID = types.StringValue("testing")
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
} To replicate your use case, I added a package provider
import (
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
)
func TestExampleResource_basic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `resource "framework_example" "test" {}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("framework_example.test", "id", "testing"),
),
ExpectError: regexp.MustCompile(`create error summary`),
},
{
Config: `resource "framework_example" "test" {}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("framework_example.test", plancheck.ResourceActionReplace),
},
},
ExpectError: regexp.MustCompile(`create error summary`),
},
},
})
} If you are seeing different behavior, one thing you can double check is whether your logic is returning early when checking for error diagnostics before the Can you please let us know? Thanks. |
Thank you @bflad! It tuned out to be my mistake that I didn't set 2 of the IDs required for Read so my state was cleaned up by the refresh. Thank you so much for the detailed explanation of it! |
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. |
Use-cases
Currently, the behavior of the framework is:
While SDKv2 put the resource into state whenever
d.SetId("some_id")
is called, even an error is returned.Framework has the
RemoveResource()
function for removing the resource from the state, which seems to be equivalent to SDKv2'sd.SetId("")
. But there isn't something in the framework that's equivalent tod.SetId("some_id")
to explicitly set the resource into state.Sometimes, after a resource is created on the cloud, the provider would have to do some other operations for the resource. If those operations failed and an error included in the response of framework resource's
Create
function, this resource will become dangling resource because it won't be managed by Terraform.Attempted Solutions
As a workaround,
resp.AddWarning()
can be used instead ofresp.AddError()
. But this is also problematic because warnings are too weak to notify users about the failing operations. Also, in a helper function that does an operations for bothCreate
andUpdate
, it is a little bit annoying to determine either an error or a warning should be added.Proposal
It would be worth to consider that adding a function like
AddResource()
to allow setting the resource explicitly.The text was updated successfully, but these errors were encountered: