Skip to content

Commit 29f49f1

Browse files
committed
chore(gcp): migrate pubsub helpers to cloud.google.com/go/pubsub/v2
The v1 pubsub package is deprecated in the bumped SDK, tripping staticcheck SA1019. Switch pubsub.go and its unit tests to v2, which uses TopicAdminClient / SubscriptionAdminClient with full resource names instead of the v1 client.Topic / client.Subscription handle APIs.
1 parent 86a518b commit 29f49f1

4 files changed

Lines changed: 55 additions & 30 deletions

File tree

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require (
4040

4141
require (
4242
cloud.google.com/go/cloudbuild v1.28.0
43-
cloud.google.com/go/pubsub v1.50.2
43+
cloud.google.com/go/pubsub/v2 v2.4.0
4444
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.0
4545
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1
4646
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appcontainers/armappcontainers/v3 v3.0.0
@@ -112,7 +112,6 @@ require (
112112
cloud.google.com/go/iam v1.7.0 // indirect
113113
cloud.google.com/go/longrunning v0.9.0 // indirect
114114
cloud.google.com/go/monitoring v1.24.3 // indirect
115-
cloud.google.com/go/pubsub/v2 v2.4.0 // indirect
116115
filippo.io/edwards25519 v1.1.1 // indirect
117116
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 // indirect
118117
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.2.0 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ cloud.google.com/go/compute/metadata v0.9.0 h1:pDUj4QMoPejqq20dK0Pg2N4yG9zIkYGdB
1313
cloud.google.com/go/compute/metadata v0.9.0/go.mod h1:E0bWwX5wTnLPedCKqk3pJmVgCBSM6qQI1yTBdEb3C10=
1414
cloud.google.com/go/iam v1.7.0 h1:JD3zh0C6LHl16aCn5Akff0+GELdp1+4hmh6ndoFLl8U=
1515
cloud.google.com/go/iam v1.7.0/go.mod h1:tetWZW1PD/m6vcuY2Zj/aU0eCHNPuxedbnbRTyKXvdY=
16-
cloud.google.com/go/kms v1.26.0 h1:cK9mN2cf+9V63D3H1f6koxTatWy39aTI/hCjz1I+adU=
17-
cloud.google.com/go/kms v1.26.0/go.mod h1:pHKOdFJm63hxBsiPkYtowZPltu9dW0MWvBa6IA4HM58=
1816
cloud.google.com/go/logging v1.13.2 h1:qqlHCBvieJT9Cdq4QqYx1KPadCQ2noD4FK02eNqHAjA=
1917
cloud.google.com/go/logging v1.13.2/go.mod h1:zaybliM3yun1J8mU2dVQ1/qDzjbOqEijZCn6hSBtKak=
2018
cloud.google.com/go/longrunning v0.9.0 h1:0EzbDEGsAvOZNbqXopgniY0w0a1phvu5IdUFq8grmqY=
2119
cloud.google.com/go/longrunning v0.9.0/go.mod h1:pkTz846W7bF4o2SzdWJ40Hu0Re+UoNT6Q5t+igIcb8E=
2220
cloud.google.com/go/monitoring v1.24.3 h1:dde+gMNc0UhPZD1Azu6at2e79bfdztVDS5lvhOdsgaE=
2321
cloud.google.com/go/monitoring v1.24.3/go.mod h1:nYP6W0tm3N9H/bOw8am7t62YTzZY+zUeQ+Bi6+2eonI=
24-
cloud.google.com/go/pubsub v1.50.2 h1:54Up97HnThdP4H8jjWJSSQ/mnYG2EKon7ZSNETRq0tM=
25-
cloud.google.com/go/pubsub v1.50.2/go.mod h1:jyCWeZdGFqd4mitSsBERnJcpqaHBsxQoPkNvjj4sp0w=
2622
cloud.google.com/go/pubsub/v2 v2.4.0 h1:oMKNiBQpXImRWnHYla9uSU66ZzByZwBSCJOEs/pTKVg=
2723
cloud.google.com/go/pubsub/v2 v2.4.0/go.mod h1:2lS/XQKq5qtOMs6kHBK+WX1ytUC36kLl2ig3zqsGUx8=
2824
cloud.google.com/go/storage v1.62.1 h1:Os0G3XbUbjZumkpDUf2Y0rLoXJTCF1kU2kWUujKYXD8=

modules/gcp/pubsub.go

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import (
44
"context"
55
"fmt"
66

7-
"cloud.google.com/go/pubsub"
7+
"cloud.google.com/go/pubsub/v2"
8+
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
89
"github.com/gruntwork-io/terratest/modules/logger"
910
"github.com/gruntwork-io/terratest/modules/testing"
1011
"github.com/stretchr/testify/require"
12+
"google.golang.org/grpc/codes"
13+
"google.golang.org/grpc/status"
1114
)
1215

1316
// AssertTopicExists checks if the given Pub/Sub topic exists and fails the test if it does not.
@@ -51,13 +54,15 @@ func AssertTopicExistsContextE(t testing.TestingT, ctx context.Context, projectI
5154
// (see pubsub_unit_test.go for the pattern).
5255
// The ctx parameter supports cancellation and timeouts.
5356
func AssertTopicExistsWithClient(ctx context.Context, client *pubsub.Client, topicName string) error {
54-
exists, err := client.Topic(topicName).Exists(ctx)
57+
projectID := client.Project()
58+
_, err := client.TopicAdminClient.GetTopic(ctx, &pubsubpb.GetTopicRequest{
59+
Topic: topicResource(projectID, topicName),
60+
})
5561
if err != nil {
56-
return fmt.Errorf("failed to check if Pub/Sub topic %s exists in project %s: %w", topicName, client.Project(), err)
57-
}
58-
59-
if !exists {
60-
return fmt.Errorf("Pub/Sub topic %s does not exist in project %s", topicName, client.Project())
62+
if status.Code(err) == codes.NotFound {
63+
return fmt.Errorf("Pub/Sub topic %s does not exist in project %s", topicName, projectID)
64+
}
65+
return fmt.Errorf("failed to check if Pub/Sub topic %s exists in project %s: %w", topicName, projectID, err)
6166
}
6267

6368
return nil
@@ -104,13 +109,15 @@ func AssertSubscriptionExistsContextE(t testing.TestingT, ctx context.Context, p
104109
// (see pubsub_unit_test.go for the pattern).
105110
// The ctx parameter supports cancellation and timeouts.
106111
func AssertSubscriptionExistsWithClient(ctx context.Context, client *pubsub.Client, subscriptionName string) error {
107-
exists, err := client.Subscription(subscriptionName).Exists(ctx)
112+
projectID := client.Project()
113+
_, err := client.SubscriptionAdminClient.GetSubscription(ctx, &pubsubpb.GetSubscriptionRequest{
114+
Subscription: subscriptionResource(projectID, subscriptionName),
115+
})
108116
if err != nil {
109-
return fmt.Errorf("failed to check if Pub/Sub subscription %s exists in project %s: %w", subscriptionName, client.Project(), err)
110-
}
111-
112-
if !exists {
113-
return fmt.Errorf("Pub/Sub subscription %s does not exist in project %s", subscriptionName, client.Project())
117+
if status.Code(err) == codes.NotFound {
118+
return fmt.Errorf("Pub/Sub subscription %s does not exist in project %s", subscriptionName, projectID)
119+
}
120+
return fmt.Errorf("failed to check if Pub/Sub subscription %s exists in project %s: %w", subscriptionName, projectID, err)
114121
}
115122

116123
return nil
@@ -157,8 +164,11 @@ func CreateTopicContextE(t testing.TestingT, ctx context.Context, projectID stri
157164
// (see pubsub_unit_test.go for the pattern).
158165
// The ctx parameter supports cancellation and timeouts.
159166
func CreateTopicWithClient(ctx context.Context, client *pubsub.Client, topicName string) error {
160-
if _, err := client.CreateTopic(ctx, topicName); err != nil {
161-
return fmt.Errorf("failed to create Pub/Sub topic %s in project %s: %w", topicName, client.Project(), err)
167+
projectID := client.Project()
168+
if _, err := client.TopicAdminClient.CreateTopic(ctx, &pubsubpb.Topic{
169+
Name: topicResource(projectID, topicName),
170+
}); err != nil {
171+
return fmt.Errorf("failed to create Pub/Sub topic %s in project %s: %w", topicName, projectID, err)
162172
}
163173

164174
return nil
@@ -205,8 +215,11 @@ func DeleteTopicContextE(t testing.TestingT, ctx context.Context, projectID stri
205215
// (see pubsub_unit_test.go for the pattern).
206216
// The ctx parameter supports cancellation and timeouts.
207217
func DeleteTopicWithClient(ctx context.Context, client *pubsub.Client, topicName string) error {
208-
if err := client.Topic(topicName).Delete(ctx); err != nil {
209-
return fmt.Errorf("failed to delete Pub/Sub topic %s in project %s: %w", topicName, client.Project(), err)
218+
projectID := client.Project()
219+
if err := client.TopicAdminClient.DeleteTopic(ctx, &pubsubpb.DeleteTopicRequest{
220+
Topic: topicResource(projectID, topicName),
221+
}); err != nil {
222+
return fmt.Errorf("failed to delete Pub/Sub topic %s in project %s: %w", topicName, projectID, err)
210223
}
211224

212225
return nil
@@ -253,10 +266,12 @@ func CreateSubscriptionContextE(t testing.TestingT, ctx context.Context, project
253266
// (see pubsub_unit_test.go for the pattern).
254267
// The ctx parameter supports cancellation and timeouts.
255268
func CreateSubscriptionWithClient(ctx context.Context, client *pubsub.Client, subscriptionName string, topicName string) error {
256-
if _, err := client.CreateSubscription(ctx, subscriptionName, pubsub.SubscriptionConfig{
257-
Topic: client.Topic(topicName),
269+
projectID := client.Project()
270+
if _, err := client.SubscriptionAdminClient.CreateSubscription(ctx, &pubsubpb.Subscription{
271+
Name: subscriptionResource(projectID, subscriptionName),
272+
Topic: topicResource(projectID, topicName),
258273
}); err != nil {
259-
return fmt.Errorf("failed to create Pub/Sub subscription %s on topic %s in project %s: %w", subscriptionName, topicName, client.Project(), err)
274+
return fmt.Errorf("failed to create Pub/Sub subscription %s on topic %s in project %s: %w", subscriptionName, topicName, projectID, err)
260275
}
261276

262277
return nil
@@ -303,8 +318,11 @@ func DeleteSubscriptionContextE(t testing.TestingT, ctx context.Context, project
303318
// (see pubsub_unit_test.go for the pattern).
304319
// The ctx parameter supports cancellation and timeouts.
305320
func DeleteSubscriptionWithClient(ctx context.Context, client *pubsub.Client, subscriptionName string) error {
306-
if err := client.Subscription(subscriptionName).Delete(ctx); err != nil {
307-
return fmt.Errorf("failed to delete Pub/Sub subscription %s in project %s: %w", subscriptionName, client.Project(), err)
321+
projectID := client.Project()
322+
if err := client.SubscriptionAdminClient.DeleteSubscription(ctx, &pubsubpb.DeleteSubscriptionRequest{
323+
Subscription: subscriptionResource(projectID, subscriptionName),
324+
}); err != nil {
325+
return fmt.Errorf("failed to delete Pub/Sub subscription %s in project %s: %w", subscriptionName, projectID, err)
308326
}
309327

310328
return nil
@@ -319,3 +337,15 @@ func newPubSubClient(ctx context.Context, projectID string) (*pubsub.Client, err
319337

320338
return client, nil
321339
}
340+
341+
// topicResource returns the fully-qualified Pub/Sub topic resource name ("projects/<p>/topics/<t>"),
342+
// required by the v2 admin client APIs.
343+
func topicResource(projectID, topicName string) string {
344+
return fmt.Sprintf("projects/%s/topics/%s", projectID, topicName)
345+
}
346+
347+
// subscriptionResource returns the fully-qualified Pub/Sub subscription resource name
348+
// ("projects/<p>/subscriptions/<s>"), required by the v2 admin client APIs.
349+
func subscriptionResource(projectID, subscriptionName string) string {
350+
return fmt.Sprintf("projects/%s/subscriptions/%s", projectID, subscriptionName)
351+
}

modules/gcp/pubsub_unit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"testing"
66

7-
"cloud.google.com/go/pubsub"
8-
"cloud.google.com/go/pubsub/pstest"
7+
"cloud.google.com/go/pubsub/v2"
8+
"cloud.google.com/go/pubsub/v2/pstest"
99
"github.com/gruntwork-io/terratest/modules/gcp"
1010
"github.com/stretchr/testify/require"
1111
"google.golang.org/api/option"

0 commit comments

Comments
 (0)