|
| 1 | +package gcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "cloud.google.com/go/pubsub" |
| 8 | + "github.com/gruntwork-io/terratest/modules/logger" |
| 9 | + "github.com/gruntwork-io/terratest/modules/testing" |
| 10 | +) |
| 11 | + |
| 12 | +// AssertTopicExistsContext checks if the given Pub/Sub topic exists and fails the test if it does not. |
| 13 | +// The ctx parameter supports cancellation and timeouts. |
| 14 | +func AssertTopicExistsContext(t testing.TestingT, ctx context.Context, projectID string, topicName string) { |
| 15 | + err := AssertTopicExistsContextE(t, ctx, projectID, topicName) |
| 16 | + if err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// AssertTopicExistsContextE checks if the given Pub/Sub topic exists and returns an error if it does not. |
| 22 | +// The ctx parameter supports cancellation and timeouts. |
| 23 | +func AssertTopicExistsContextE(t testing.TestingT, ctx context.Context, projectID string, topicName string) error { |
| 24 | + logger.Default.Logf(t, "Verifying Pub/Sub topic %s exists in project %s", topicName, projectID) |
| 25 | + |
| 26 | + client, err := newPubSubClient(ctx, projectID) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + defer func() { _ = client.Close() }() |
| 31 | + |
| 32 | + exists, err := client.Topic(topicName).Exists(ctx) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("failed to check if Pub/Sub topic %s exists in project %s: %w", topicName, projectID, err) |
| 35 | + } |
| 36 | + |
| 37 | + if !exists { |
| 38 | + return fmt.Errorf("Pub/Sub topic %s does not exist in project %s", topicName, projectID) |
| 39 | + } |
| 40 | + |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +// AssertSubscriptionExistsContext checks if the given Pub/Sub subscription exists and fails the test if it does not. |
| 45 | +// The ctx parameter supports cancellation and timeouts. |
| 46 | +func AssertSubscriptionExistsContext(t testing.TestingT, ctx context.Context, projectID string, subscriptionName string) { |
| 47 | + err := AssertSubscriptionExistsContextE(t, ctx, projectID, subscriptionName) |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// AssertSubscriptionExistsContextE checks if the given Pub/Sub subscription exists and returns an error if it does not. |
| 54 | +// The ctx parameter supports cancellation and timeouts. |
| 55 | +func AssertSubscriptionExistsContextE(t testing.TestingT, ctx context.Context, projectID string, subscriptionName string) error { |
| 56 | + logger.Default.Logf(t, "Verifying Pub/Sub subscription %s exists in project %s", subscriptionName, projectID) |
| 57 | + |
| 58 | + client, err := newPubSubClient(ctx, projectID) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + defer func() { _ = client.Close() }() |
| 63 | + |
| 64 | + exists, err := client.Subscription(subscriptionName).Exists(ctx) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("failed to check if Pub/Sub subscription %s exists in project %s: %w", subscriptionName, projectID, err) |
| 67 | + } |
| 68 | + |
| 69 | + if !exists { |
| 70 | + return fmt.Errorf("Pub/Sub subscription %s does not exist in project %s", subscriptionName, projectID) |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// CreateTopicContext creates a new Pub/Sub topic and fails the test if it cannot. |
| 77 | +// The ctx parameter supports cancellation and timeouts. |
| 78 | +func CreateTopicContext(t testing.TestingT, ctx context.Context, projectID string, topicName string) { |
| 79 | + err := CreateTopicContextE(t, ctx, projectID, topicName) |
| 80 | + if err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// CreateTopicContextE creates a new Pub/Sub topic and returns an error if it fails. |
| 86 | +// The ctx parameter supports cancellation and timeouts. |
| 87 | +func CreateTopicContextE(t testing.TestingT, ctx context.Context, projectID string, topicName string) error { |
| 88 | + logger.Default.Logf(t, "Creating Pub/Sub topic %s in project %s", topicName, projectID) |
| 89 | + |
| 90 | + client, err := newPubSubClient(ctx, projectID) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + defer func() { _ = client.Close() }() |
| 95 | + |
| 96 | + _, err = client.CreateTopic(ctx, topicName) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("failed to create Pub/Sub topic %s in project %s: %w", topicName, projectID, err) |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// DeleteTopicContext deletes the given Pub/Sub topic and fails the test if it cannot. |
| 105 | +// The ctx parameter supports cancellation and timeouts. |
| 106 | +func DeleteTopicContext(t testing.TestingT, ctx context.Context, projectID string, topicName string) { |
| 107 | + err := DeleteTopicContextE(t, ctx, projectID, topicName) |
| 108 | + if err != nil { |
| 109 | + t.Fatal(err) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// DeleteTopicContextE deletes the given Pub/Sub topic and returns an error if it fails. |
| 114 | +// The ctx parameter supports cancellation and timeouts. |
| 115 | +func DeleteTopicContextE(t testing.TestingT, ctx context.Context, projectID string, topicName string) error { |
| 116 | + logger.Default.Logf(t, "Deleting Pub/Sub topic %s in project %s", topicName, projectID) |
| 117 | + |
| 118 | + client, err := newPubSubClient(ctx, projectID) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + defer func() { _ = client.Close() }() |
| 123 | + |
| 124 | + if err := client.Topic(topicName).Delete(ctx); err != nil { |
| 125 | + return fmt.Errorf("failed to delete Pub/Sub topic %s in project %s: %w", topicName, projectID, err) |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +// CreateSubscriptionContext creates a new Pub/Sub subscription on the given topic and fails the test if it cannot. |
| 132 | +// The ctx parameter supports cancellation and timeouts. |
| 133 | +func CreateSubscriptionContext(t testing.TestingT, ctx context.Context, projectID string, subscriptionName string, topicName string) { |
| 134 | + err := CreateSubscriptionContextE(t, ctx, projectID, subscriptionName, topicName) |
| 135 | + if err != nil { |
| 136 | + t.Fatal(err) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// CreateSubscriptionContextE creates a new Pub/Sub subscription on the given topic and returns an error if it fails. |
| 141 | +// The ctx parameter supports cancellation and timeouts. |
| 142 | +func CreateSubscriptionContextE(t testing.TestingT, ctx context.Context, projectID string, subscriptionName string, topicName string) error { |
| 143 | + logger.Default.Logf(t, "Creating Pub/Sub subscription %s on topic %s in project %s", subscriptionName, topicName, projectID) |
| 144 | + |
| 145 | + client, err := newPubSubClient(ctx, projectID) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + defer func() { _ = client.Close() }() |
| 150 | + |
| 151 | + _, err = client.CreateSubscription(ctx, subscriptionName, pubsub.SubscriptionConfig{ |
| 152 | + Topic: client.Topic(topicName), |
| 153 | + }) |
| 154 | + if err != nil { |
| 155 | + return fmt.Errorf("failed to create Pub/Sub subscription %s in project %s: %w", subscriptionName, projectID, err) |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +// DeleteSubscriptionContext deletes the given Pub/Sub subscription and fails the test if it cannot. |
| 162 | +// The ctx parameter supports cancellation and timeouts. |
| 163 | +func DeleteSubscriptionContext(t testing.TestingT, ctx context.Context, projectID string, subscriptionName string) { |
| 164 | + err := DeleteSubscriptionContextE(t, ctx, projectID, subscriptionName) |
| 165 | + if err != nil { |
| 166 | + t.Fatal(err) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +// DeleteSubscriptionContextE deletes the given Pub/Sub subscription and returns an error if it fails. |
| 171 | +// The ctx parameter supports cancellation and timeouts. |
| 172 | +func DeleteSubscriptionContextE(t testing.TestingT, ctx context.Context, projectID string, subscriptionName string) error { |
| 173 | + logger.Default.Logf(t, "Deleting Pub/Sub subscription %s in project %s", subscriptionName, projectID) |
| 174 | + |
| 175 | + client, err := newPubSubClient(ctx, projectID) |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + defer func() { _ = client.Close() }() |
| 180 | + |
| 181 | + if err := client.Subscription(subscriptionName).Delete(ctx); err != nil { |
| 182 | + return fmt.Errorf("failed to delete Pub/Sub subscription %s in project %s: %w", subscriptionName, projectID, err) |
| 183 | + } |
| 184 | + |
| 185 | + return nil |
| 186 | +} |
| 187 | + |
| 188 | +// newPubSubClient creates a new Pub/Sub client using the provided project ID and global GCP auth options. |
| 189 | +func newPubSubClient(ctx context.Context, projectID string) (*pubsub.Client, error) { |
| 190 | + client, err := pubsub.NewClient(ctx, projectID, withOptions()...) |
| 191 | + if err != nil { |
| 192 | + return nil, err |
| 193 | + } |
| 194 | + return client, nil |
| 195 | +} |
0 commit comments