@@ -519,11 +519,12 @@ 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- filename , err = downloadImage (ctx , & resp .Diagnostics , downloadModel . CachePath . ValueString () , downloadModel .URL .ValueString ())
522+ file , err : = downloadImage (ctx , & resp .Diagnostics , downloadModel .URL .ValueString ())
523523 if err != nil {
524524 core .LogAndAddError (ctx , & resp .Diagnostics , "Error downloading image" , fmt .Sprintf ("Downloading Image: %v" , err ))
525525 return
526526 }
527+ filename = file .Name ()
527528 } else {
528529 diags = imageFile .Download .As (ctx , & localModel , basetypes.ObjectAsOptions {})
529530 resp .Diagnostics .Append (diags ... )
@@ -1019,31 +1020,33 @@ func uploadImage(ctx context.Context, diags *diag.Diagnostics, filePath, uploadU
10191020}
10201021
10211022// file zurückgeben - unit test mock server (dummy file), file pointer checken | diags raus
1022- func downloadImage (ctx context.Context , diags * diag.Diagnostics , cachePath , downloadURL string ) (string , error ) {
1023+ func downloadImage (ctx context.Context , diags * diag.Diagnostics , downloadURL string ) (* os. File , error ) {
10231024 if downloadURL == "" {
1024- return "" , fmt .Errorf ("upload URL is empty" )
1025+ return nil , fmt .Errorf ("upload URL is empty" )
10251026 }
10261027 md5sum := fmt .Sprintf ("%x" , md5 .Sum ([]byte (downloadURL )))
10271028 // uuid?
10281029 // go tmp verzeichnis pro ressource -> kein konflikt
1029- filename := filepath .Join (cachePath , md5sum + ".img" )
1030+ tmpDir , err := os .MkdirTemp ("" , "tf-prodiver-download-*" )
1031+ if err != nil {
1032+ return nil , fmt .Errorf ("failed to create temp dir: %w" , err )
1033+ }
1034+ filename := filepath .Join (tmpDir , md5sum + ".img" )
10301035 delFile := func () {
10311036 if err := os .Remove (filename ); err != nil {
10321037 tflog .Debug (ctx , "failed to cleanup file" )
10331038 }
10341039 }
1035- unlock := iaasUtils .LockimageDownload (filename )
1036- defer unlock ()
10371040 // TODO: retry
10381041 req , err := http .NewRequestWithContext (ctx , http .MethodGet , downloadURL , nil )
10391042 if err != nil {
1040- return "" , fmt .Errorf ("create download request: %w" , err )
1043+ return nil , fmt .Errorf ("create download request: %w" , err )
10411044 }
10421045
10431046 client := & http.Client {}
10441047 resp , err := client .Do (req )
10451048 if err != nil {
1046- return "" , fmt .Errorf ("download image: %w" , err )
1049+ return nil , fmt .Errorf ("download image: %w" , err )
10471050 }
10481051
10491052 defer func () {
@@ -1055,26 +1058,13 @@ func downloadImage(ctx context.Context, diags *diag.Diagnostics, cachePath, down
10551058 }()
10561059
10571060 if resp .StatusCode != http .StatusOK {
1058- return "" , fmt .Errorf ("upload image: %s" , resp .Status )
1059- }
1060-
1061- info , err := os .Stat (filename )
1062- if err != nil && ! os .IsNotExist (err ) {
1063- return "" , fmt .Errorf ("accessing file %q: %w" , filename , err )
1064- }
1065-
1066- if info != nil {
1067- if info .Size () != 0 {
1068- // cache hit
1069- return filename , nil
1070- }
1071- delFile ()
1061+ return nil , fmt .Errorf ("upload image: %s" , resp .Status )
10721062 }
10731063
1074- file , err := os .Create ( filename )
1064+ file , err := os .CreateTemp ( "" , filename )
10751065 if err != nil {
10761066 delFile ()
1077- return "" , fmt .Errorf ("creating file: %w" , err )
1067+ return nil , fmt .Errorf ("creating file: %w" , err )
10781068 }
10791069 defer func () {
10801070 err = resp .Body .Close ()
@@ -1084,7 +1074,7 @@ func downloadImage(ctx context.Context, diags *diag.Diagnostics, cachePath, down
10841074 }()
10851075 _ , err = io .Copy (file , resp .Body )
10861076 if err != nil {
1087- return "" , fmt .Errorf ("writing to file: %w" , err )
1077+ return nil , fmt .Errorf ("writing to file: %w" , err )
10881078 }
1089- return filename , nil
1079+ return file , nil
10901080}
0 commit comments