@@ -428,8 +428,21 @@ func (r *networkResource) Create(ctx context.Context, req resource.CreateRequest
428428 return
429429 }
430430
431+ if network .Id == nil {
432+ core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating network" , "Got empty network id" )
433+ return
434+ }
435+
431436 networkId := * network .Id
432- ctx = tflog .SetField (ctx , "network_id" , networkId )
437+ // Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
438+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
439+ "project_id" : projectId ,
440+ "region" : region ,
441+ "network_id" : networkId ,
442+ })
443+ if resp .Diagnostics .HasError () {
444+ return
445+ }
433446
434447 network , err = wait .CreateNetworkWaitHandler (ctx , r .client , projectId , region , networkId ).WaitWithContext (ctx )
435448 if err != nil {
@@ -592,17 +605,11 @@ func (r *networkResource) ImportState(ctx context.Context, req resource.ImportSt
592605 )
593606 return
594607 }
595-
596- projectId := idParts [0 ]
597- region := idParts [1 ]
598- networkId := idParts [2 ]
599- ctx = tflog .SetField (ctx , "project_id" , projectId )
600- ctx = tflog .SetField (ctx , "region" , region )
601- ctx = tflog .SetField (ctx , "network_id" , networkId )
602-
603- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("project_id" ), projectId )... )
604- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("region" ), region )... )
605- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("network_id" ), networkId )... )
608+ ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
609+ "project_id" : idParts [0 ],
610+ "region" : idParts [1 ],
611+ "network_id" : idParts [2 ],
612+ })
606613 tflog .Info (ctx , "Network state imported" )
607614}
608615
0 commit comments