88 "strings"
99
1010 "github.com/hashicorp/terraform-plugin-framework/diag"
11- "github.com/hashicorp/terraform-plugin-framework/path"
1211 "github.com/hashicorp/terraform-plugin-framework/resource"
1312 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
1413 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -18,7 +17,7 @@ import (
1817 "github.com/hashicorp/terraform-plugin-log/tflog"
1918 "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2019 logs "github.com/stackitcloud/stackit-sdk-go/services/logs/v1api"
21- wait "github.com/stackitcloud/stackit-sdk-go/services/logs/v1api/wait"
20+ "github.com/stackitcloud/stackit-sdk-go/services/logs/v1api/wait"
2221 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2322 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2423 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs/utils"
@@ -249,6 +248,21 @@ func (r *logsInstanceResource) Create(ctx context.Context, req resource.CreateRe
249248
250249 ctx = core .LogResponse (ctx )
251250
251+ if createResp == nil || createResp .Id == "" {
252+ core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating Logs Instance" , "Create API response: Incomplete response (id missing)" )
253+ return
254+ }
255+ instanceId := createResp .Id
256+ // Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
257+ ctx = tfutils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
258+ "project_id" : projectId ,
259+ "region" : region ,
260+ "instance_id" : instanceId ,
261+ })
262+ if resp .Diagnostics .HasError () {
263+ return
264+ }
265+
252266 waitResp , err := wait .CreateLogsInstanceWaitHandler (ctx , r .client .DefaultAPI , projectId , regionId , createResp .Id ).WaitWithContext (ctx )
253267 if err != nil {
254268 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating Logs Instance" , fmt .Sprintf ("Waiting for Logs Instance to become active: %v" , err ))
@@ -403,9 +417,12 @@ func (r *logsInstanceResource) ImportState(ctx context.Context, req resource.Imp
403417 core .LogAndAddError (ctx , & resp .Diagnostics , "Error importing Logs Instance" , fmt .Sprintf ("Invalid import ID %q: expected format is `project_id`,`region`,`instance_id`" , req .ID ))
404418 return
405419 }
406- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("project_id" ), idParts [0 ])... )
407- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("region" ), idParts [1 ])... )
408- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("instance_id" ), idParts [2 ])... )
420+
421+ ctx = tfutils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
422+ "project_id" : idParts [0 ],
423+ "region" : idParts [1 ],
424+ "instance_id" : idParts [2 ],
425+ })
409426 tflog .Info (ctx , "Logs Instance state imported" )
410427}
411428
0 commit comments