Skip to content

Commit 93a6b82

Browse files
Add support for setting Pub/Sub subscription filename_datetime_format (#10713) (#18180)
[upstream:ace54f65a242d5b10e1c1a0f6335433f7384318a] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 3c97301 commit 93a6b82

4 files changed

+134
-10
lines changed

google/services/pubsub/resource_pubsub_subscription.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ If all three are empty, then the subscriber will pull and ack messages using API
180180
},
181181
},
182182
},
183+
"filename_datetime_format": {
184+
Type: schema.TypeString,
185+
Optional: true,
186+
Description: `User-provided format string specifying how to represent datetimes in Cloud Storage filenames.`,
187+
},
183188
"filename_prefix": {
184189
Type: schema.TypeString,
185190
Optional: true,
@@ -1122,6 +1127,8 @@ func flattenPubsubSubscriptionCloudStorageConfig(v interface{}, d *schema.Resour
11221127
flattenPubsubSubscriptionCloudStorageConfigFilenamePrefix(original["filenamePrefix"], d, config)
11231128
transformed["filename_suffix"] =
11241129
flattenPubsubSubscriptionCloudStorageConfigFilenameSuffix(original["filenameSuffix"], d, config)
1130+
transformed["filename_datetime_format"] =
1131+
flattenPubsubSubscriptionCloudStorageConfigFilenameDatetimeFormat(original["filenameDatetimeFormat"], d, config)
11251132
transformed["max_duration"] =
11261133
flattenPubsubSubscriptionCloudStorageConfigMaxDuration(original["maxDuration"], d, config)
11271134
transformed["max_bytes"] =
@@ -1144,6 +1151,10 @@ func flattenPubsubSubscriptionCloudStorageConfigFilenameSuffix(v interface{}, d
11441151
return v
11451152
}
11461153

1154+
func flattenPubsubSubscriptionCloudStorageConfigFilenameDatetimeFormat(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1155+
return v
1156+
}
1157+
11471158
func flattenPubsubSubscriptionCloudStorageConfigMaxDuration(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
11481159
return v
11491160
}
@@ -1505,6 +1516,13 @@ func expandPubsubSubscriptionCloudStorageConfig(v interface{}, d tpgresource.Ter
15051516
transformed["filenameSuffix"] = transformedFilenameSuffix
15061517
}
15071518

1519+
transformedFilenameDatetimeFormat, err := expandPubsubSubscriptionCloudStorageConfigFilenameDatetimeFormat(original["filename_datetime_format"], d, config)
1520+
if err != nil {
1521+
return nil, err
1522+
} else if val := reflect.ValueOf(transformedFilenameDatetimeFormat); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1523+
transformed["filenameDatetimeFormat"] = transformedFilenameDatetimeFormat
1524+
}
1525+
15081526
transformedMaxDuration, err := expandPubsubSubscriptionCloudStorageConfigMaxDuration(original["max_duration"], d, config)
15091527
if err != nil {
15101528
return nil, err
@@ -1548,6 +1566,10 @@ func expandPubsubSubscriptionCloudStorageConfigFilenameSuffix(v interface{}, d t
15481566
return v, nil
15491567
}
15501568

1569+
func expandPubsubSubscriptionCloudStorageConfigFilenameDatetimeFormat(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1570+
return v, nil
1571+
}
1572+
15511573
func expandPubsubSubscriptionCloudStorageConfigMaxDuration(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
15521574
return v, nil
15531575
}

google/services/pubsub/resource_pubsub_subscription_generated_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,12 @@ resource "google_pubsub_subscription" "example" {
393393
394394
filename_prefix = "pre-"
395395
filename_suffix = "-%{random_suffix}"
396-
396+
filename_datetime_format = "YYYY-MM-DD/hh_mm_ssZ"
397+
397398
max_bytes = 1000
398399
max_duration = "300s"
399400
}
400-
depends_on = [
401+
depends_on = [
401402
google_storage_bucket.example,
402403
google_storage_bucket_iam_member.admin,
403404
]
@@ -460,15 +461,16 @@ resource "google_pubsub_subscription" "example" {
460461
461462
filename_prefix = "pre-"
462463
filename_suffix = "-%{random_suffix}"
463-
464+
filename_datetime_format = "YYYY-MM-DD/hh_mm_ssZ"
465+
464466
max_bytes = 1000
465467
max_duration = "300s"
466-
468+
467469
avro_config {
468470
write_metadata = true
469471
}
470472
}
471-
depends_on = [
473+
depends_on = [
472474
google_storage_bucket.example,
473475
google_storage_bucket_iam_member.admin,
474476
]

google/services/pubsub/resource_pubsub_subscription_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,40 @@ func TestAccPubsubSubscriptionBigQuery_update(t *testing.T) {
208208
})
209209
}
210210

211+
func TestAccPubsubSubscriptionCloudStorage_update(t *testing.T) {
212+
t.Parallel()
213+
214+
bucket := fmt.Sprintf("tf-test-bucket-%s", acctest.RandString(t, 10))
215+
topic := fmt.Sprintf("tf-test-topic-%s", acctest.RandString(t, 10))
216+
subscriptionShort := fmt.Sprintf("tf-test-sub-%s", acctest.RandString(t, 10))
217+
218+
acctest.VcrTest(t, resource.TestCase{
219+
PreCheck: func() { acctest.AccTestPreCheck(t) },
220+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
221+
CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t),
222+
Steps: []resource.TestStep{
223+
{
224+
Config: testAccPubsubSubscriptionCloudStorage_basic(bucket, topic, subscriptionShort, "", "", "", 0, ""),
225+
},
226+
{
227+
ResourceName: "google_pubsub_subscription.foo",
228+
ImportStateId: subscriptionShort,
229+
ImportState: true,
230+
ImportStateVerify: true,
231+
},
232+
{
233+
Config: testAccPubsubSubscriptionCloudStorage_basic(bucket, topic, subscriptionShort, "pre-", "-suffix", "YYYY-MM-DD/hh_mm_ssZ", 1000, "300s"),
234+
},
235+
{
236+
ResourceName: "google_pubsub_subscription.foo",
237+
ImportStateId: subscriptionShort,
238+
ImportState: true,
239+
ImportStateVerify: true,
240+
},
241+
},
242+
})
243+
}
244+
211245
// Context: hashicorp/terraform-provider-google#4993
212246
// This test makes a call to GET an subscription before it is actually created.
213247
// The PubSub API negative-caches responses so this tests we are
@@ -461,6 +495,66 @@ resource "google_pubsub_subscription" "foo" {
461495
`, dataset, table, topic, subscription, useTableSchema)
462496
}
463497

498+
func testAccPubsubSubscriptionCloudStorage_basic(bucket, topic, subscription, filenamePrefix, filenameSuffix, filenameDatetimeFormat string, maxBytes int, maxDuration string) string {
499+
filenamePrefixString := ""
500+
if filenamePrefix != "" {
501+
filenamePrefixString = fmt.Sprintf(`filename_prefix = "%s"`, filenamePrefix)
502+
}
503+
filenameSuffixString := ""
504+
if filenameSuffix != "" {
505+
filenameSuffixString = fmt.Sprintf(`filename_suffix = "%s"`, filenameSuffix)
506+
}
507+
filenameDatetimeString := ""
508+
if filenameDatetimeFormat != "" {
509+
filenameDatetimeString = fmt.Sprintf(`filename_datetime_format = "%s"`, filenameDatetimeFormat)
510+
}
511+
maxBytesString := ""
512+
if maxBytes != 0 {
513+
maxBytesString = fmt.Sprintf(`max_bytes = %d`, maxBytes)
514+
}
515+
maxDurationString := ""
516+
if maxDuration != "" {
517+
maxDurationString = fmt.Sprintf(`max_duration = "%s"`, maxDuration)
518+
}
519+
return fmt.Sprintf(`
520+
data "google_project" "project" { }
521+
522+
resource "google_storage_bucket_iam_member" "admin" {
523+
bucket = google_storage_bucket.test.name
524+
role = "roles/storage.admin"
525+
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
526+
}
527+
528+
resource "google_storage_bucket" "test" {
529+
name = "%s"
530+
location = "US"
531+
}
532+
533+
resource "google_pubsub_topic" "foo" {
534+
name = "%s"
535+
}
536+
537+
resource "google_pubsub_subscription" "foo" {
538+
name = "%s"
539+
topic = google_pubsub_topic.foo.id
540+
541+
cloud_storage_config {
542+
bucket = "${google_storage_bucket.test.name}"
543+
%s
544+
%s
545+
%s
546+
%s
547+
%s
548+
}
549+
550+
depends_on = [
551+
google_storage_bucket.test,
552+
google_storage_bucket_iam_member.admin,
553+
]
554+
}
555+
`, bucket, topic, subscription, filenamePrefixString, filenameSuffixString, filenameDatetimeString, maxBytesString, maxDurationString)
556+
}
557+
464558
func testAccPubsubSubscription_topicOnly(topic string) string {
465559
return fmt.Sprintf(`
466560
resource "google_pubsub_topic" "foo" {

website/docs/r/pubsub_subscription.html.markdown

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,12 @@ resource "google_pubsub_subscription" "example" {
278278
279279
filename_prefix = "pre-"
280280
filename_suffix = "-%{random_suffix}"
281-
281+
filename_datetime_format = "YYYY-MM-DD/hh_mm_ssZ"
282+
282283
max_bytes = 1000
283284
max_duration = "300s"
284285
}
285-
depends_on = [
286+
depends_on = [
286287
google_storage_bucket.example,
287288
google_storage_bucket_iam_member.admin,
288289
]
@@ -325,15 +326,16 @@ resource "google_pubsub_subscription" "example" {
325326
326327
filename_prefix = "pre-"
327328
filename_suffix = "-%{random_suffix}"
328-
329+
filename_datetime_format = "YYYY-MM-DD/hh_mm_ssZ"
330+
329331
max_bytes = 1000
330332
max_duration = "300s"
331-
333+
332334
avro_config {
333335
write_metadata = true
334336
}
335337
}
336-
depends_on = [
338+
depends_on = [
337339
google_storage_bucket.example,
338340
google_storage_bucket_iam_member.admin,
339341
]
@@ -528,6 +530,10 @@ The following arguments are supported:
528530
(Optional)
529531
User-provided suffix for Cloud Storage filename. Must not end in "/".
530532

533+
* `filename_datetime_format` -
534+
(Optional)
535+
User-provided format string specifying how to represent datetimes in Cloud Storage filenames.
536+
531537
* `max_duration` -
532538
(Optional)
533539
The maximum duration that can elapse before a new Cloud Storage file is created. Min 1 minute, max 10 minutes, default 5 minutes.

0 commit comments

Comments
 (0)