Skip to content

Commit

Permalink
Add support for setting Pub/Sub subscription filename_datetime_format (
Browse files Browse the repository at this point in the history
…GoogleCloudPlatform#10713)

Co-authored-by: Lauren Huang <[email protected]>
  • Loading branch information
2 people authored and Cheriit committed Jun 4, 2024
1 parent 171b856 commit 255f738
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
4 changes: 4 additions & 0 deletions mmv1/products/pubsub/Subscription.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ properties:
name: 'filenameSuffix'
description: |
User-provided suffix for Cloud Storage filename. Must not end in "/".
- !ruby/object:Api::Type::String
name: 'filenameDatetimeFormat'
description: |
User-provided format string specifying how to represent datetimes in Cloud Storage filenames.
- !ruby/object:Api::Type::String
name: 'maxDuration'
description: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ resource "google_pubsub_subscription" "<%= ctx[:primary_resource_id] %>" {

filename_prefix = "pre-"
filename_suffix = "-%{random_suffix}"

filename_datetime_format = "YYYY-MM-DD/hh_mm_ssZ"

max_bytes = 1000
max_duration = "300s"
}
depends_on = [
depends_on = [
google_storage_bucket.<%= ctx[:primary_resource_id] %>,
google_storage_bucket_iam_member.admin,
]
Expand All @@ -34,4 +35,4 @@ resource "google_storage_bucket_iam_member" "admin" {
bucket = google_storage_bucket.<%= ctx[:primary_resource_id] %>.name
role = "roles/storage.admin"
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ resource "google_pubsub_subscription" "<%= ctx[:primary_resource_id] %>" {

filename_prefix = "pre-"
filename_suffix = "-%{random_suffix}"

filename_datetime_format = "YYYY-MM-DD/hh_mm_ssZ"

max_bytes = 1000
max_duration = "300s"

avro_config {
write_metadata = true
}
}
depends_on = [
depends_on = [
google_storage_bucket.<%= ctx[:primary_resource_id] %>,
google_storage_bucket_iam_member.admin,
]
Expand All @@ -38,4 +39,4 @@ resource "google_storage_bucket_iam_member" "admin" {
bucket = google_storage_bucket.<%= ctx[:primary_resource_id] %>.name
role = "roles/storage.admin"
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,40 @@ func TestAccPubsubSubscriptionBigQuery_update(t *testing.T) {
})
}

func TestAccPubsubSubscriptionCloudStorage_update(t *testing.T) {
t.Parallel()

bucket := fmt.Sprintf("tf-test-bucket-%s", acctest.RandString(t, 10))
topic := fmt.Sprintf("tf-test-topic-%s", acctest.RandString(t, 10))
subscriptionShort := fmt.Sprintf("tf-test-sub-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccPubsubSubscriptionCloudStorage_basic(bucket, topic, subscriptionShort, "", "", "", 0, ""),
},
{
ResourceName: "google_pubsub_subscription.foo",
ImportStateId: subscriptionShort,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccPubsubSubscriptionCloudStorage_basic(bucket, topic, subscriptionShort, "pre-", "-suffix", "YYYY-MM-DD/hh_mm_ssZ", 1000, "300s"),
},
{
ResourceName: "google_pubsub_subscription.foo",
ImportStateId: subscriptionShort,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

// Context: hashicorp/terraform-provider-google#4993
// This test makes a call to GET an subscription before it is actually created.
// The PubSub API negative-caches responses so this tests we are
Expand Down Expand Up @@ -459,6 +493,66 @@ resource "google_pubsub_subscription" "foo" {
`, dataset, table, topic, subscription, useTableSchema)
}

func testAccPubsubSubscriptionCloudStorage_basic(bucket, topic, subscription, filenamePrefix, filenameSuffix, filenameDatetimeFormat string, maxBytes int, maxDuration string) string {
filenamePrefixString := ""
if filenamePrefix != "" {
filenamePrefixString = fmt.Sprintf(`filename_prefix = "%s"`, filenamePrefix)
}
filenameSuffixString := ""
if filenameSuffix != "" {
filenameSuffixString = fmt.Sprintf(`filename_suffix = "%s"`, filenameSuffix)
}
filenameDatetimeString := ""
if filenameDatetimeFormat != "" {
filenameDatetimeString = fmt.Sprintf(`filename_datetime_format = "%s"`, filenameDatetimeFormat)
}
maxBytesString := ""
if maxBytes != 0 {
maxBytesString = fmt.Sprintf(`max_bytes = %d`, maxBytes)
}
maxDurationString := ""
if maxDuration != "" {
maxDurationString = fmt.Sprintf(`max_duration = "%s"`, maxDuration)
}
return fmt.Sprintf(`
data "google_project" "project" { }
resource "google_storage_bucket_iam_member" "admin" {
bucket = google_storage_bucket.test.name
role = "roles/storage.admin"
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
}
resource "google_storage_bucket" "test" {
name = "%s"
location = "US"
}
resource "google_pubsub_topic" "foo" {
name = "%s"
}
resource "google_pubsub_subscription" "foo" {
name = "%s"
topic = google_pubsub_topic.foo.id
cloud_storage_config {
bucket = "${google_storage_bucket.test.name}"
%s
%s
%s
%s
%s
}
depends_on = [
google_storage_bucket.test,
google_storage_bucket_iam_member.admin,
]
}
`, bucket, topic, subscription, filenamePrefixString, filenameSuffixString, filenameDatetimeString, maxBytesString, maxDurationString)
}

func testAccPubsubSubscription_topicOnly(topic string) string {
return fmt.Sprintf(`
resource "google_pubsub_topic" "foo" {
Expand Down

0 comments on commit 255f738

Please sign in to comment.