diff --git a/gcp/artifacts/client.go b/gcp/artifacts/client.go index 76ba899a..f179110a 100644 --- a/gcp/artifacts/client.go +++ b/gcp/artifacts/client.go @@ -2,6 +2,7 @@ package artifacts import ( "fmt" + "time" gcpartifact "google.golang.org/api/artifactregistry/v1" "google.golang.org/api/googleapi" @@ -25,9 +26,28 @@ func NewClient(project string, service *gcpartifact.Service, logger logger) clie } } -func (c client) ListRepositories(region string) (*gcpartifact.ListRepositoriesResponse, error) { +func (c client) ListRepositories(region string) ([]*gcpartifact.Repository, error) { parent := fmt.Sprintf("projects/%v/locations/%v", c.project, region) - return c.repositories.List(parent).Do() + var token string + var list []*gcpartifact.Repository + + for { + resp, err := c.repositories.List(parent).PageToken(token).Do() + if err != nil { + return nil, err + } + + list = append(list, resp.Repositories...) + + token = resp.NextPageToken + if token == "" { + break + } + + time.Sleep(time.Second) + } + + return list, nil } func (c client) DeleteRepository(name string) error { diff --git a/gcp/artifacts/fakes/repositories_client.go b/gcp/artifacts/fakes/repositories_client.go index 3c042fbf..9731d9aa 100644 --- a/gcp/artifacts/fakes/repositories_client.go +++ b/gcp/artifacts/fakes/repositories_client.go @@ -25,10 +25,10 @@ type RepositoriesClient struct { Region string } Returns struct { - ListRepositoriesResponse *artifactregistry.ListRepositoriesResponse + ListRepositoriesResponse []*artifactregistry.Repository Error error } - Stub func(string) (*artifactregistry.ListRepositoriesResponse, error) + Stub func(string) ([]*artifactregistry.Repository, error) } } @@ -42,7 +42,7 @@ func (f *RepositoriesClient) DeleteRepository(param1 string) error { } return f.DeleteRepositoryCall.Returns.Error } -func (f *RepositoriesClient) ListRepositories(param1 string) (*artifactregistry.ListRepositoriesResponse, error) { +func (f *RepositoriesClient) ListRepositories(param1 string) ([]*artifactregistry.Repository, error) { f.ListRepositoriesCall.mutex.Lock() defer f.ListRepositoriesCall.mutex.Unlock() f.ListRepositoriesCall.CallCount++ diff --git a/gcp/artifacts/repositories.go b/gcp/artifacts/repositories.go index 750ab843..2d5c8ab5 100644 --- a/gcp/artifacts/repositories.go +++ b/gcp/artifacts/repositories.go @@ -2,10 +2,9 @@ package artifacts import ( "fmt" - "strings" - "github.com/genevieve/leftovers/common" gcpartifact "google.golang.org/api/artifactregistry/v1" + "strings" ) type Repositories struct { @@ -16,7 +15,7 @@ type Repositories struct { //go:generate faux --interface repositoriesClient --output fakes/repositories_client.go type repositoriesClient interface { - ListRepositories(region string) (*gcpartifact.ListRepositoriesResponse, error) + ListRepositories(region string) ([]*gcpartifact.Repository, error) DeleteRepository(cluster string) error } @@ -31,13 +30,12 @@ func NewRepositories(client repositoriesClient, logger logger, regions map[strin func (c Repositories) List(filter string) ([]common.Deletable, error) { repositories := []*gcpartifact.Repository{} for _, region := range c.regions { - c.logger.Debugf("Listing Repositories for region %s...\n", region) - l, err := c.client.ListRepositories(region) + repositoryList, err := c.client.ListRepositories(region) if err != nil { return nil, fmt.Errorf("list repositories for region %v: %w", region, err) } - repositories = append(repositories, l.Repositories...) + repositories = append(repositories, repositoryList...) } deletables := []common.Deletable{} diff --git a/gcp/artifacts/repositories_test.go b/gcp/artifacts/repositories_test.go index 5400a6af..10a1882c 100644 --- a/gcp/artifacts/repositories_test.go +++ b/gcp/artifacts/repositories_test.go @@ -32,11 +32,9 @@ var _ = Describe("Clusters", func() { Describe("List", func() { BeforeEach(func() { - client.ListRepositoriesCall.Returns.ListRepositoriesResponse = &gcpartifact.ListRepositoriesResponse{ - Repositories: []*gcpartifact.Repository{{ - Name: "banana-repository", - }}, - } + client.ListRepositoriesCall.Returns.ListRepositoriesResponse = []*gcpartifact.Repository{{ + Name: "banana-repository", + }} }) It("returns a list of clusters to delete", func() { @@ -64,11 +62,9 @@ var _ = Describe("Clusters", func() { Context("when the resource name does not contain the filter", func() { BeforeEach(func() { - client.ListRepositoriesCall.Returns.ListRepositoriesResponse = &gcpartifact.ListRepositoriesResponse{ - Repositories: []*gcpartifact.Repository{{ - Name: "kiwi-repository", - }}, - } + client.ListRepositoriesCall.Returns.ListRepositoriesResponse = []*gcpartifact.Repository{{ + Name: "kiwi-repository", + }} }) It("does not return the resource in the list", func() { diff --git a/gcp/container/client.go b/gcp/container/client.go index ef2ba676..51a589ad 100644 --- a/gcp/container/client.go +++ b/gcp/container/client.go @@ -30,8 +30,8 @@ func (c client) ListClusters() (*gcpcontainer.ListClustersResponse, error) { return c.containers.List(parent).Do() } -func (c client) DeleteCluster(zone string, cluster string) error { - name := fmt.Sprintf("projects/%v/locations/%v/clusters/%v", c.project, zone, cluster) +func (c client) DeleteCluster(location string, cluster string) error { + name := fmt.Sprintf("projects/%v/locations/%v/clusters/%v", c.project, location, cluster) return c.wait(c.containers.Delete(name)) } diff --git a/gcp/container/cluster.go b/gcp/container/cluster.go index 7b53e083..acfe7ef7 100644 --- a/gcp/container/cluster.go +++ b/gcp/container/cluster.go @@ -3,21 +3,21 @@ package container import "fmt" type Cluster struct { - name string - zone string - client clustersClient + name string + location string + client clustersClient } -func NewCluster(client clustersClient, zone string, name string) Cluster { +func NewCluster(client clustersClient, location string, name string) Cluster { return Cluster{ - name: name, - zone: zone, - client: client, + name: name, + location: location, + client: client, } } func (c Cluster) Delete() error { - err := c.client.DeleteCluster(c.zone, c.name) + err := c.client.DeleteCluster(c.location, c.name) if err != nil { return fmt.Errorf("Delete: %s", err) } diff --git a/gcp/container/clusters.go b/gcp/container/clusters.go index adc2f0f4..c1cbcccd 100644 --- a/gcp/container/clusters.go +++ b/gcp/container/clusters.go @@ -17,7 +17,7 @@ type Clusters struct { //go:generate faux --interface clustersClient --output fakes/clusters_client.go type clustersClient interface { ListClusters() (*gcpcontainer.ListClustersResponse, error) - DeleteCluster(zone, cluster string) error + DeleteCluster(location, cluster string) error } func NewClusters(client clustersClient, zones map[string]string, logger logger) Clusters { @@ -38,7 +38,7 @@ func (c Clusters) List(filter string) ([]common.Deletable, error) { deletables := []common.Deletable{} for _, cluster := range clusters { - resource := NewCluster(c.client, cluster.Zone, cluster.Name) + resource := NewCluster(c.client, cluster.Location, cluster.Name) if !strings.Contains(resource.Name(), filter) { continue diff --git a/gcp/container/operation_waiter.go b/gcp/container/operation_waiter.go index fc1713c4..be4033f3 100644 --- a/gcp/container/operation_waiter.go +++ b/gcp/container/operation_waiter.go @@ -2,7 +2,6 @@ package container import ( "fmt" - "github.com/genevieve/leftovers/gcp/common" gcpcontainer "google.golang.org/api/container/v1" @@ -10,7 +9,7 @@ import ( type operationWaiter struct { op *gcpcontainer.Operation - service *gcpcontainer.ProjectsZonesService + service *gcpcontainer.ProjectsLocationsService project string logger logger } @@ -18,7 +17,7 @@ type operationWaiter struct { func NewOperationWaiter(op *gcpcontainer.Operation, service *gcpcontainer.Service, project string, logger logger) operationWaiter { return operationWaiter{ op: op, - service: service.Projects.Zones, + service: service.Projects.Locations, project: project, logger: logger, } @@ -42,8 +41,14 @@ func (w *operationWaiter) Wait() error { func (c *operationWaiter) refreshFunc() common.StateRefreshFunc { return func() (interface{}, string, error) { - op, err := c.service.Operations.Get(c.project, c.op.Zone, c.op.Name).Do() + location := c.op.Location + if location == "" { + // For some reason `c.op.Location` can be empty here despite this being a regional cluster + location = c.op.Zone + } + name := fmt.Sprintf("projects/%v/locations/%v/operations/%v", c.project, location, c.op.Name) + op, err := c.service.Operations.Get(name).Do() if err != nil { return nil, "", fmt.Errorf("Refreshing operation request: %s", err) } diff --git a/go.mod b/go.mod index 06c5f6e3..764cfa93 100644 --- a/go.mod +++ b/go.mod @@ -12,16 +12,16 @@ require ( github.com/hashicorp/go-multierror v1.1.1 github.com/jessevdk/go-flags v1.5.0 github.com/mitchellh/go-homedir v1.1.0 - github.com/onsi/ginkgo/v2 v2.9.7 - github.com/onsi/gomega v1.27.7 + github.com/onsi/ginkgo/v2 v2.10.0 + github.com/onsi/gomega v1.27.8 github.com/vmware/go-vmware-nsxt v0.0.0-20180719200125-23af5e753efe github.com/vmware/govmomi v0.30.4 golang.org/x/oauth2 v0.8.0 - google.golang.org/api v0.124.0 + google.golang.org/api v0.126.0 ) require ( - cloud.google.com/go/compute v1.19.0 // indirect + cloud.google.com/go/compute v1.19.3 // indirect cloud.google.com/go/compute/metadata v0.2.3 // indirect github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect @@ -36,7 +36,7 @@ require ( github.com/google/s2a-go v0.1.4 // indirect github.com/google/uuid v1.3.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect - github.com/googleapis/gax-go/v2 v2.8.0 // indirect + github.com/googleapis/gax-go/v2 v2.10.0 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/kylelemons/godebug v1.1.0 // indirect @@ -49,9 +49,9 @@ require ( golang.org/x/net v0.10.0 // indirect golang.org/x/sys v0.8.0 // indirect golang.org/x/text v0.9.0 // indirect - golang.org/x/tools v0.9.1 // indirect + golang.org/x/tools v0.9.3 // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect google.golang.org/grpc v1.55.0 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 143cda23..e4c0402a 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,9 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.110.0 h1:Zc8gqp3+a9/Eyph2KDmcGaPtbKRIoqq4YTlL4NMD0Ys= -cloud.google.com/go/compute v1.19.0 h1:+9zda3WGgW1ZSTlVppLCYFIr48Pa35q1uG2N1itbCEQ= -cloud.google.com/go/compute v1.19.0/go.mod h1:rikpw2y+UMidAe9tISo04EHNOIf42RLYF/q8Bs93scU= +cloud.google.com/go/compute v1.19.3 h1:DcTwsFgGev/wV5+q8o2fzgcHOaac+DKGC91ZlvpsQds= +cloud.google.com/go/compute v1.19.3/go.mod h1:qxvISKp/gYnXkSAD1ppcSOveRAmzxicEv/JlizULFrI= cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY= cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= -cloud.google.com/go/longrunning v0.4.1 h1:v+yFJOfKC3yZdY6ZUI933pIYdhyhV8S3NpWrXWmg7jM= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0 h1:8kDqDngH+DmVBiCtIjCFTGa7MBnsIOkF9IccInFEbjk= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q= github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 h1:vcYCAze6p19qBW7MhZybIsqD8sMV8js0NyQM8JDnVtg= @@ -91,8 +89,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/enterprise-certificate-proxy v0.2.3 h1:yk9/cqRKtT9wXZSsRH9aurXEpJX+U6FLtpYTdC3R06k= github.com/googleapis/enterprise-certificate-proxy v0.2.3/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k= -github.com/googleapis/gax-go/v2 v2.8.0 h1:UBtEZqx1bjXtOQ5BVTkuYghXrr3N4V123VKJK67vJZc= -github.com/googleapis/gax-go/v2 v2.8.0/go.mod h1:4orTrqY6hXxxaUL4LHIPl6lGo8vAE38/qKbhSAKP6QI= +github.com/googleapis/gax-go/v2 v2.10.0 h1:ebSgKfMxynOdxw8QQuFOKMgomqeLGPqNLQox2bo42zg= +github.com/googleapis/gax-go/v2 v2.10.0/go.mod h1:4UOEnMCrxsSqQ940WnTiD6qJ63le2ev3xfyagutxiPw= github.com/gophercloud/gophercloud v1.4.0 h1:RqEu43vaX0lb0LanZr5BylK5ICVxjpFFoc0sxivyuHU= github.com/gophercloud/gophercloud v1.4.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= @@ -115,10 +113,10 @@ github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPn github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/onsi/ginkgo/v2 v2.9.7 h1:06xGQy5www2oN160RtEZoTvnP2sPhEfePYmCDc2szss= -github.com/onsi/ginkgo/v2 v2.9.7/go.mod h1:cxrmXWykAwTwhQsJOPfdIDiJ+l2RYq7U8hFU+M/1uw0= -github.com/onsi/gomega v1.27.7 h1:fVih9JD6ogIiHUN6ePK7HJidyEDpWGVB5mzM7cWNXoU= -github.com/onsi/gomega v1.27.7/go.mod h1:1p8OOlwo2iUUDsHnOrjE5UKYJ+e3W8eQ3qSlRahPmr4= +github.com/onsi/ginkgo/v2 v2.10.0 h1:sfUl4qgLdvkChZrWCYndY2EAu9BRIw1YphNAzy1VNWs= +github.com/onsi/ginkgo/v2 v2.10.0/go.mod h1:UDQOh5wbQUlMnkLfVaIUMtQ1Vus92oM+P2JX1aulgcE= +github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc= +github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJKFnNQ= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -219,13 +217,13 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo= -golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc= +golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM= +golang.org/x/tools v0.9.3/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/api v0.124.0 h1:dP6Ef1VgOGqQ8eiv4GiY8RhmeyqzovcXBYPDUYG8Syo= -google.golang.org/api v0.124.0/go.mod h1:xu2HQurE5gi/3t1aFCvhPD781p0a3p11sdunTJ2BlP4= +google.golang.org/api v0.126.0 h1:q4GJq+cAdMAC7XP7njvQ4tvohGLiSlytuL4BQxbIZ+o= +google.golang.org/api v0.126.0/go.mod h1:mBwVAtz+87bEN6CbA1GtZPDOqY2R5ONPqJeIlvyo4Aw= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= @@ -234,8 +232,10 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A= -google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU= +google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc h1:8DyZCyvI8mE1IdLy/60bS+52xfymkE72wv1asokgtao= +google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc h1:kVKPf/IiYSBWEWtkIn6wZXwWGCnLKcC8oWfZvXjsGnM= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc h1:XSJ8Vk1SWuNr8S18z1NZSziL0CPIXLCCMDOEFtHBOFc= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=