@@ -21,7 +21,6 @@ import (
2121 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
2222 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
2323
24- "github.com/hashicorp/terraform-plugin-framework/path"
2524 "github.com/hashicorp/terraform-plugin-framework/resource"
2625 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
2726 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -329,9 +328,22 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
329328 }
330329
331330 ctx = core .LogResponse (ctx )
332-
331+ if createResp .Id == nil {
332+ core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , "Got empty instance id" )
333+ return
334+ }
333335 instanceId := * createResp .Id
334- ctx = tflog .SetField (ctx , "instance_id" , instanceId )
336+
337+ // Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
338+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
339+ "project_id" : projectId ,
340+ "region" : region ,
341+ "instance_id" : instanceId ,
342+ })
343+ if resp .Diagnostics .HasError () {
344+ return
345+ }
346+
335347 waitResp , err := wait .CreateInstanceWaitHandler (ctx , r .client , projectId , region , instanceId ).WaitWithContext (ctx )
336348 if err != nil {
337349 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , fmt .Sprintf ("Instance creation waiting: %v" , err ))
@@ -554,9 +566,11 @@ func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportS
554566 return
555567 }
556568
557- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("project_id" ), idParts [0 ])... )
558- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("region" ), idParts [1 ])... )
559- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("instance_id" ), idParts [2 ])... )
569+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
570+ "project_id" : idParts [0 ],
571+ "region" : idParts [1 ],
572+ "instance_id" : idParts [2 ],
573+ })
560574 tflog .Info (ctx , "Postgres Flex instance state imported" )
561575}
562576
0 commit comments