@@ -726,71 +726,63 @@ func getAnywhereCacheListResult(d *schema.ResourceData, config *transport_tpg.Co
726
726
}
727
727
728
728
func deleteAnywhereCacheIfAny (d * schema.ResourceData , config * transport_tpg.Config ) error {
729
- // Get the initial list of Anywhere Caches
730
- cacheList , err := getAnywhereCacheListResult (d , config )
731
- if err != nil {
732
- return err
733
- }
734
-
735
- // If no cache exists initially, return early
736
- if len (cacheList ) == 0 {
737
- return nil
738
- }
739
-
740
- // Iterate over each object in the resource list
741
- for _ , item := range cacheList {
742
- // Ensure the item is a map
743
- obj , ok := item .(map [string ]interface {})
744
- if ! ok {
745
- return fmt .Errorf ("unexpected type for resource list item: %T" , item )
746
- }
747
-
748
- // Check the state of the object
749
- state , ok := obj ["state" ].(string )
750
- if ! ok {
751
- continue // If state is not a string, skip this item
752
- }
753
- if ! strings .EqualFold (state , "running" ) && ! strings .EqualFold (state , "paused" ) {
754
- continue
755
- }
756
-
757
- // Disable the cache if state is running or paused
758
- anywhereCacheId , ok := obj ["anywhereCacheId" ].(string )
759
- if ! ok {
760
- return fmt .Errorf ("missing or invalid anywhereCacheId: %v" , obj )
761
- }
762
- anywhereCacheUrl , err := tpgresource .ReplaceVars (d , config , "{{StorageBasePath}}b/{{name}}/anywhereCaches/" )
729
+ for {
730
+ // Get the list of Anywhere Caches
731
+ cacheList , err := getAnywhereCacheListResult (d , config )
763
732
if err != nil {
764
733
return err
765
734
}
766
- disableUrl := anywhereCacheUrl + fmt .Sprintf ("%s/disable" , anywhereCacheId )
767
735
768
- _ , err = transport_tpg .SendRequest (transport_tpg.SendRequestOptions {
769
- Config : config ,
770
- Method : "POST" ,
771
- Project : config .Project ,
772
- RawURL : disableUrl ,
773
- UserAgent : config .UserAgent ,
774
- })
775
- if err != nil {
776
- return err
736
+ // Check if the cache list is empty
737
+ if len (cacheList ) == 0 {
738
+ break
777
739
}
778
- }
779
- time .Sleep (80 * time .Minute ) // It takes around 70 minutes of time for cache to finally delete post it disable time.
780
740
781
- // Post this time, we check again!
782
- // Get the list of Anywhere Caches after the sleep
783
- cacheList , err = getAnywhereCacheListResult (d , config )
784
- if err != nil {
785
- return err
786
- }
741
+ // Iterate over each object in the resource list
742
+ for _ , item := range cacheList {
743
+ // Ensure the item is a map
744
+ obj , ok := item .(map [string ]interface {})
745
+ if ! ok {
746
+ return fmt .Errorf ("unexpected type for resource list item: %T" , item )
747
+ }
787
748
788
- // Check if the cache list is now empty
789
- if len (cacheList ) == 0 {
790
- return nil
749
+ // Check the state of the object
750
+ state , ok := obj ["state" ].(string )
751
+ if ! ok {
752
+ continue // If state is not a string, skip this item
753
+ }
754
+ if ! strings .EqualFold (state , "running" ) && ! strings .EqualFold (state , "paused" ) {
755
+ continue
756
+ }
757
+
758
+ // Disable the cache if state is running or paused
759
+ anywhereCacheId , ok := obj ["anywhereCacheId" ].(string )
760
+ if ! ok {
761
+ return fmt .Errorf ("missing or invalid anywhereCacheId: %v" , obj )
762
+ }
763
+ anywhereCacheUrl , err := tpgresource .ReplaceVars (d , config , "{{StorageBasePath}}b/{{name}}/anywhereCaches/" )
764
+ if err != nil {
765
+ return err
766
+ }
767
+ disableUrl := anywhereCacheUrl + fmt .Sprintf ("%s/disable" , anywhereCacheId )
768
+
769
+ _ , err = transport_tpg .SendRequest (transport_tpg.SendRequestOptions {
770
+ Config : config ,
771
+ Method : "POST" ,
772
+ Project : config .Project ,
773
+ RawURL : disableUrl ,
774
+ UserAgent : config .UserAgent ,
775
+ })
776
+ if err != nil {
777
+ return err
778
+ }
779
+ }
780
+
781
+ // Sleep for 1 minute
782
+ time .Sleep (1 * time .Minute )
791
783
}
792
784
793
- return fmt . Errorf ( "Error while deleting the cache: caches still exists post 80mins of their disable time" )
785
+ return nil
794
786
}
795
787
796
788
func resourceDataplexLabelDiffSuppress (k , old , new string , d * schema.ResourceData ) bool {
@@ -1195,7 +1187,7 @@ func resourceStorageBucketDelete(d *schema.ResourceData, meta interface{}) error
1195
1187
// Get the bucket
1196
1188
bucket := d .Get ("name" ).(string )
1197
1189
1198
- var listError , deleteObjectError error
1190
+ var listError , deleteObjectError , deleteCacheError error
1199
1191
for deleteObjectError == nil {
1200
1192
res , err := config .NewStorageClient (userAgent ).Objects .List (bucket ).Versions (true ).Do ()
1201
1193
if err != nil {
@@ -1255,7 +1247,7 @@ func resourceStorageBucketDelete(d *schema.ResourceData, meta interface{}) error
1255
1247
wp .Submit (func () {
1256
1248
err = deleteAnywhereCacheIfAny (d , config )
1257
1249
if err != nil {
1258
- deleteObjectError = fmt .Errorf ("error deleting the caches on the bucket %s : %w" , bucket , err )
1250
+ deleteCacheError = fmt .Errorf ("error deleting the caches on the bucket %s : %w" , bucket , err )
1259
1251
}
1260
1252
})
1261
1253
@@ -1295,6 +1287,9 @@ func resourceStorageBucketDelete(d *schema.ResourceData, meta interface{}) error
1295
1287
if gerr , ok := err .(* googleapi.Error ); ok && gerr .Code == 409 && strings .Contains (gerr .Message , "not empty" ) && deleteObjectError != nil {
1296
1288
return fmt .Errorf ("could not delete non-empty bucket due to error when deleting contents: %v" , deleteObjectError )
1297
1289
}
1290
+ if gerr , ok := err .(* googleapi.Error ); ok && gerr .Code == 409 && strings .Contains (gerr .Message , "Anywhere Caches" ) && deleteCacheError != nil {
1291
+ return fmt .Errorf ("could not delete bucket due to error when deleting anywhere caches on it: %v" , deleteCacheError )
1292
+ }
1298
1293
if err != nil {
1299
1294
log .Printf ("Error deleting bucket %s: %v" , bucket , err )
1300
1295
return err
0 commit comments