@@ -16,7 +16,6 @@ import (
1616 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1717 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1818
19- "github.com/hashicorp/terraform-plugin-framework/path"
2019 "github.com/hashicorp/terraform-plugin-framework/resource"
2120 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
2221 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -182,7 +181,15 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
182181 return
183182 }
184183 credentialId := * credentialsResp .Id
185- ctx = tflog .SetField (ctx , "credential_id" , credentialId )
184+ // Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
185+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
186+ "project_id" : projectId ,
187+ "instance_id" : instanceId ,
188+ "credential_id" : credentialId ,
189+ })
190+ if resp .Diagnostics .HasError () {
191+ return
192+ }
186193
187194 waitResp , err := wait .CreateCredentialsWaitHandler (ctx , r .client , projectId , instanceId , credentialId ).WaitWithContext (ctx )
188195 if err != nil {
@@ -303,9 +310,11 @@ func (r *credentialResource) ImportState(ctx context.Context, req resource.Impor
303310 return
304311 }
305312
306- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("project_id" ), idParts [0 ])... )
307- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("instance_id" ), idParts [1 ])... )
308- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("credential_id" ), idParts [2 ])... )
313+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]interface {}{
314+ "project_id" : idParts [0 ],
315+ "instance_id" : idParts [1 ],
316+ "credential_id" : idParts [2 ],
317+ })
309318 tflog .Info (ctx , "LogMe credential state imported" )
310319}
311320
0 commit comments