@@ -11,7 +11,6 @@ import (
1111 "github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
1212 "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
1313 "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
14- "github.com/hashicorp/terraform-plugin-framework/path"
1514 "github.com/hashicorp/terraform-plugin-framework/resource"
1615 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
1716 "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
@@ -280,8 +279,7 @@ func (r *zoneResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
280279func (r * zoneResource ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) { // nolint:gocritic // function signature required by Terraform
281280 // Retrieve values from plan
282281 var model Model
283- diags := req .Plan .Get (ctx , & model )
284- resp .Diagnostics .Append (diags ... )
282+ resp .Diagnostics .Append (req .Plan .Get (ctx , & model )... )
285283 if resp .Diagnostics .HasError () {
286284 return
287285 }
@@ -301,9 +299,17 @@ func (r *zoneResource) Create(ctx context.Context, req resource.CreateRequest, r
301299 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating zone" , fmt .Sprintf ("Calling API: %v" , err ))
302300 return
303301 }
302+
303+ // Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
304304 zoneId := * createResp .Zone .Id
305+ utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]interface {}{
306+ "project_id" : projectId ,
307+ "zone_id" : zoneId ,
308+ })
309+ if resp .Diagnostics .HasError () {
310+ return
311+ }
305312
306- ctx = tflog .SetField (ctx , "zone_id" , zoneId )
307313 waitResp , err := wait .CreateZoneWaitHandler (ctx , r .client , projectId , zoneId ).WaitWithContext (ctx )
308314 if err != nil {
309315 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating zone" , fmt .Sprintf ("Zone creation waiting: %v" , err ))
@@ -317,8 +323,7 @@ func (r *zoneResource) Create(ctx context.Context, req resource.CreateRequest, r
317323 return
318324 }
319325 // Set state to fully populated data
320- diags = resp .State .Set (ctx , model )
321- resp .Diagnostics .Append (diags ... )
326+ resp .Diagnostics .Append (resp .State .Set (ctx , model )... )
322327 if resp .Diagnostics .HasError () {
323328 return
324329 }
@@ -451,13 +456,11 @@ func (r *zoneResource) ImportState(ctx context.Context, req resource.ImportState
451456 return
452457 }
453458
454- projectId := idParts [ 0 ]
455- zoneId := idParts [1 ]
456- ctx = tflog . SetField ( ctx , "project_id" , projectId )
457- ctx = tflog . SetField ( ctx , "zone_id" , zoneId )
459+ utils . SetAndLogStateFields ( ctx , & resp . Diagnostics , & resp . State , map [ string ] interface {}{
460+ "project_id" : idParts [0 ],
461+ "zone_id" : idParts [ 1 ],
462+ } )
458463
459- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("project_id" ), projectId )... )
460- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("zone_id" ), zoneId )... )
461464 tflog .Info (ctx , "DNS zone state imported" )
462465}
463466
0 commit comments