@@ -20,7 +20,6 @@ import (
2020 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2121 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
2222
23- "github.com/hashicorp/terraform-plugin-framework/path"
2423 "github.com/hashicorp/terraform-plugin-framework/resource"
2524 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
2625 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -433,8 +432,19 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
433432
434433 ctx = core .LogResponse (ctx )
435434
435+ if createResp .InstanceId == nil {
436+ core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , "Got empty instance id" )
437+ return
438+ }
436439 instanceId := * createResp .InstanceId
437- ctx = tflog .SetField (ctx , "instance_id" , instanceId )
440+ // Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
441+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
442+ "project_id" : projectId ,
443+ "instance_id" : instanceId ,
444+ })
445+ if resp .Diagnostics .HasError () {
446+ return
447+ }
438448 waitResp , err := wait .CreateInstanceWaitHandler (ctx , r .client , projectId , instanceId ).WaitWithContext (ctx )
439449 if err != nil {
440450 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , fmt .Sprintf ("Instance creation waiting: %v" , err ))
@@ -624,8 +634,10 @@ func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportS
624634 return
625635 }
626636
627- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("project_id" ), idParts [0 ])... )
628- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("instance_id" ), idParts [1 ])... )
637+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
638+ "project_id" : idParts [0 ],
639+ "instance_id" : idParts [1 ],
640+ })
629641 tflog .Info (ctx , "Redis instance state imported" )
630642}
631643
0 commit comments