Skip to content

Commit

Permalink
Update alloydb sweeper to sweep secondary clusters (GoogleCloudPlatfo…
Browse files Browse the repository at this point in the history
  • Loading branch information
rileykarson authored Apr 19, 2024
1 parent 28c680a commit fcde618
Showing 1 changed file with 14 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package alloydb

import (
"context"
"fmt"
"log"
"strings"
"testing"
Expand Down Expand Up @@ -48,10 +49,11 @@ func testSweepAlloydbCluster(region string) error {
},
}

// manual patch: use aggregated list instead of sweeper-specific location. This will clear secondary clusters.
<% unless version == 'ga' -%>
listTemplate := strings.Split("https://alloydb.googleapis.com/v1beta/projects/{{project}}/locations/{{location}}/clusters", "?")[0]
listTemplate := strings.Split("https://alloydb.googleapis.com/v1beta/projects/{{project}}/locations/-/clusters", "?")[0]
<% else -%>
listTemplate := strings.Split("https://alloydb.googleapis.com/v1/projects/{{project}}/locations/{{location}}/clusters", "?")[0]
listTemplate := strings.Split("https://alloydb.googleapis.com/v1/projects/{{project}}/locations/-/clusters", "?")[0]
<% end -%>
listUrl, err := tpgresource.ReplaceVars(d, config, listTemplate)
if err != nil {
Expand Down Expand Up @@ -84,33 +86,23 @@ func testSweepAlloydbCluster(region string) error {
nonPrefixCount := 0
for _, ri := range rl {
obj := ri.(map[string]interface{})
var name string
// Id detected in the delete URL, attempt to use id.
if obj["id"] != nil {
name = tpgresource.GetResourceNameFromSelfLink(obj["id"].(string))
} else if obj["name"] != nil {
name = tpgresource.GetResourceNameFromSelfLink(obj["name"].(string))
} else {
log.Printf("[INFO][SWEEPER_LOG] %s resource name and id were nil", resourceName)
return nil
}

// manual patch: use raw name for url instead of constructing it, so that resource locations are supplied through aggregated list
// manual patch: Using the force=true ensures that we delete instances as well.
name := obj["name"].(string)
shortname := tpgresource.GetResourceNameFromSelfLink(name)
// Skip resources that shouldn't be sweeped
if !sweeper.IsSweepableTestResource(name) {
if !sweeper.IsSweepableTestResource(shortname) {
nonPrefixCount++
continue
}

<% unless version == 'ga' -%>
deleteTemplate := "https://alloydb.googleapis.com/v1beta/projects/{{project}}/locations/{{location}}/clusters/{{cluster_id}}"
deleteTemplate := "https://alloydb.googleapis.com/v1beta/%s?force=true"
<% else -%>
deleteTemplate := "https://alloydb.googleapis.com/v1/projects/{{project}}/locations/{{location}}/clusters/{{cluster_id}}"
deleteTemplate := "https://alloydb.googleapis.com/v1/%s?force=true"
<% end -%>
deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)
return nil
}
deleteUrl = deleteUrl + name + "?force=true"
deleteUrl := fmt.Sprintf(deleteTemplate, name)

// Don't wait on operations as we may have a lot to delete
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Expand All @@ -123,7 +115,7 @@ func testSweepAlloydbCluster(region string) error {
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err)
} else {
log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name)
log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", name, shortname)
}
}

Expand Down

0 comments on commit fcde618

Please sign in to comment.