@@ -16,7 +16,6 @@ import (
1616
1717 "github.com/google/uuid"
1818 "github.com/hashicorp/terraform-plugin-framework/attr"
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"
@@ -212,6 +211,15 @@ func (r *customDomainResource) Create(ctx context.Context, req resource.CreateRe
212211 }
213212
214213 ctx = core .LogResponse (ctx )
214+ // Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
215+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
216+ "project_id" : projectId ,
217+ "distribution_id" : distributionId ,
218+ "name" : name ,
219+ })
220+ if resp .Diagnostics .HasError () {
221+ return
222+ }
215223
216224 _ , err = wait .CreateCDNCustomDomainWaitHandler (ctx , r .client , projectId , distributionId , name ).SetTimeout (5 * time .Minute ).WaitWithContext (ctx )
217225 if err != nil {
@@ -382,9 +390,11 @@ func (r *customDomainResource) ImportState(ctx context.Context, req resource.Imp
382390 if len (idParts ) != 3 || idParts [0 ] == "" || idParts [1 ] == "" || idParts [2 ] == "" {
383391 core .LogAndAddError (ctx , & resp .Diagnostics , "Error importing CDN custom domain" , fmt .Sprintf ("Expected import identifier on the format: [project_id]%q[distribution_id]%q[custom_domain_name], got %q" , core .Separator , core .Separator , req .ID ))
384392 }
385- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("project_id" ), idParts [0 ])... )
386- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("distribution_id" ), idParts [1 ])... )
387- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("name" ), idParts [2 ])... )
393+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]interface {}{
394+ "project_id" : idParts [0 ],
395+ "distribution_id" : idParts [1 ],
396+ "name" : idParts [2 ],
397+ })
388398 tflog .Info (ctx , "CDN custom domain state imported" )
389399}
390400
0 commit comments