Skip to content

Commit 968ffe1

Browse files
Update alloydb sweeper to sweep secondary clusters (#10498) (#7275)
[upstream:fcde618533667374eaed74a99bf482e628f1fa47] Signed-off-by: Modular Magician <[email protected]>
1 parent fbfe322 commit 968ffe1

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

google-beta/services/alloydb/resource_alloydb_cluster_sweeper.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package alloydb
44

55
import (
66
"context"
7+
"fmt"
78
"log"
89
"strings"
910
"testing"
@@ -49,7 +50,8 @@ func testSweepAlloydbCluster(region string) error {
4950
},
5051
}
5152

52-
listTemplate := strings.Split("https://alloydb.googleapis.com/v1beta/projects/{{project}}/locations/{{location}}/clusters", "?")[0]
53+
// manual patch: use aggregated list instead of sweeper-specific location. This will clear secondary clusters.
54+
listTemplate := strings.Split("https://alloydb.googleapis.com/v1beta/projects/{{project}}/locations/-/clusters", "?")[0]
5355
listUrl, err := tpgresource.ReplaceVars(d, config, listTemplate)
5456
if err != nil {
5557
log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err)
@@ -81,29 +83,19 @@ func testSweepAlloydbCluster(region string) error {
8183
nonPrefixCount := 0
8284
for _, ri := range rl {
8385
obj := ri.(map[string]interface{})
84-
var name string
85-
// Id detected in the delete URL, attempt to use id.
86-
if obj["id"] != nil {
87-
name = tpgresource.GetResourceNameFromSelfLink(obj["id"].(string))
88-
} else if obj["name"] != nil {
89-
name = tpgresource.GetResourceNameFromSelfLink(obj["name"].(string))
90-
} else {
91-
log.Printf("[INFO][SWEEPER_LOG] %s resource name and id were nil", resourceName)
92-
return nil
93-
}
86+
87+
// manual patch: use raw name for url instead of constructing it, so that resource locations are supplied through aggregated list
88+
// manual patch: Using the force=true ensures that we delete instances as well.
89+
name := obj["name"].(string)
90+
shortname := tpgresource.GetResourceNameFromSelfLink(name)
9491
// Skip resources that shouldn't be sweeped
95-
if !sweeper.IsSweepableTestResource(name) {
92+
if !sweeper.IsSweepableTestResource(shortname) {
9693
nonPrefixCount++
9794
continue
9895
}
9996

100-
deleteTemplate := "https://alloydb.googleapis.com/v1beta/projects/{{project}}/locations/{{location}}/clusters/{{cluster_id}}"
101-
deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate)
102-
if err != nil {
103-
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)
104-
return nil
105-
}
106-
deleteUrl = deleteUrl + name + "?force=true"
97+
deleteTemplate := "https://alloydb.googleapis.com/v1beta/%s?force=true"
98+
deleteUrl := fmt.Sprintf(deleteTemplate, name)
10799

108100
// Don't wait on operations as we may have a lot to delete
109101
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
@@ -116,7 +108,7 @@ func testSweepAlloydbCluster(region string) error {
116108
if err != nil {
117109
log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err)
118110
} else {
119-
log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name)
111+
log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", name, shortname)
120112
}
121113
}
122114

0 commit comments

Comments
 (0)