@@ -519,7 +519,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
519519 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating image" , "Error in config" )
520520 return
521521 }
522- file , err := downloadImage (ctx , & resp . Diagnostics , downloadModel .URL .ValueString ())
522+ file , err := downloadImage (ctx , downloadModel .URL .ValueString ())
523523 if err != nil {
524524 core .LogAndAddError (ctx , & resp .Diagnostics , "Error downloading image" , fmt .Sprintf ("Downloading Image: %v" , err ))
525525 return
@@ -1019,62 +1019,73 @@ func uploadImage(ctx context.Context, diags *diag.Diagnostics, filePath, uploadU
10191019 return nil
10201020}
10211021
1022- // file zurückgeben - unit test mock server (dummy file), file pointer checken | diags raus
1023- func downloadImage (ctx context.Context , diags * diag.Diagnostics , downloadURL string ) (* os.File , error ) {
1022+ func downloadImage (ctx context.Context , downloadURL string ) (* os.File , error ) {
10241023 if downloadURL == "" {
1025- return nil , fmt .Errorf ("upload URL is empty" )
1024+ return nil , fmt .Errorf ("download URL is empty" )
10261025 }
1026+
10271027 md5sum := fmt .Sprintf ("%x" , md5 .Sum ([]byte (downloadURL )))
1028- // uuid?
1029- // go tmp verzeichnis pro ressource -> kein konflikt
1030- tmpDir , err := os .MkdirTemp ("" , "tf-prodiver-download-*" )
1028+
1029+ tmpDir , err := os .MkdirTemp ("" , "tf-provider-download-*" )
10311030 if err != nil {
10321031 return nil , fmt .Errorf ("failed to create temp dir: %w" , err )
10331032 }
1033+
10341034 filename := filepath .Join (tmpDir , md5sum + ".img" )
1035- delFile := func () {
1036- if err := os .Remove (filename ); err != nil {
1037- tflog .Debug (ctx , "failed to cleanup file" )
1035+
1036+ cleanupOnErr := func () {
1037+ if err := os .RemoveAll (tmpDir ); err != nil {
1038+ tflog .Warn (ctx , "failed to cleanup temp directory" , map [string ]interface {}{
1039+ "dir" : tmpDir ,
1040+ "error" : err .Error (),
1041+ })
10381042 }
10391043 }
1040- // TODO: retry
1044+
10411045 req , err := http .NewRequestWithContext (ctx , http .MethodGet , downloadURL , nil )
10421046 if err != nil {
1047+ cleanupOnErr ()
10431048 return nil , fmt .Errorf ("create download request: %w" , err )
10441049 }
10451050
10461051 client := & http.Client {}
10471052 resp , err := client .Do (req )
10481053 if err != nil {
1054+ cleanupOnErr ()
10491055 return nil , fmt .Errorf ("download image: %w" , err )
10501056 }
10511057
10521058 defer func () {
1053- err = resp .Body .Close ()
1054- if err != nil {
1055- // can test if handled, return caller should care
1056- core . LogAndAddError ( ctx , diags , "Error downloading image" , fmt . Sprintf ( "Closing response body: %v" , err ) )
1059+ if err : = resp .Body .Close (); err != nil {
1060+ tflog . Debug ( ctx , "failed to close HTTP response body" , map [ string ] interface {} {
1061+ "error" : err . Error (),
1062+ } )
10571063 }
10581064 }()
10591065
10601066 if resp .StatusCode != http .StatusOK {
1061- return nil , fmt .Errorf ("upload image: %s" , resp .Status )
1067+ cleanupOnErr ()
1068+ return nil , fmt .Errorf ("download image unexpected status: %s" , resp .Status )
10621069 }
10631070
1064- file , err := os .CreateTemp ( "" , filename )
1071+ file , err := os .Create ( filename )
10651072 if err != nil {
1066- delFile ()
1073+ cleanupOnErr ()
10671074 return nil , fmt .Errorf ("creating file: %w" , err )
10681075 }
1069- defer func () {
1070- err = resp .Body .Close ()
1071- if err != nil {
1072- core .LogAndAddError (ctx , diags , "Error uploading image" , fmt .Sprintf ("Closing response body: %v" , err ))
1073- }
1074- }()
1076+
10751077 _ , err = io .Copy (file , resp .Body )
10761078 if err != nil {
1079+ file .Close ()
1080+ cleanupOnErr ()
10771081 return nil , fmt .Errorf ("writing to file: %w" , err )
10781082 }
1083+
1084+ if _ , err := file .Seek (0 , 0 ); err != nil {
1085+ file .Close ()
1086+ cleanupOnErr ()
1087+ return nil , fmt .Errorf ("seeking file: %w" , err )
1088+ }
1089+
10791090 return file , nil
10801091}
0 commit comments