diff --git a/.changelog/5347.txt b/.changelog/5347.txt new file mode 100644 index 00000000000..f16c94d9f35 --- /dev/null +++ b/.changelog/5347.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +pubsub: added `message_retention_duration` field to `google_pubsub_topic` +``` diff --git a/google/iam_pubsub_topic_generated_test.go b/google/iam_pubsub_topic_generated_test.go index b124415a9f2..bab2e8084d9 100644 --- a/google/iam_pubsub_topic_generated_test.go +++ b/google/iam_pubsub_topic_generated_test.go @@ -124,6 +124,8 @@ resource "google_pubsub_topic" "example" { labels = { foo = "bar" } + + message_retention_duration = "86600s" } resource "google_pubsub_topic_iam_member" "foo" { @@ -143,6 +145,8 @@ resource "google_pubsub_topic" "example" { labels = { foo = "bar" } + + message_retention_duration = "86600s" } data "google_iam_policy" "foo" { @@ -168,6 +172,8 @@ resource "google_pubsub_topic" "example" { labels = { foo = "bar" } + + message_retention_duration = "86600s" } data "google_iam_policy" "foo" { @@ -189,6 +195,8 @@ resource "google_pubsub_topic" "example" { labels = { foo = "bar" } + + message_retention_duration = "86600s" } resource "google_pubsub_topic_iam_binding" "foo" { @@ -208,6 +216,8 @@ resource "google_pubsub_topic" "example" { labels = { foo = "bar" } + + message_retention_duration = "86600s" } resource "google_pubsub_topic_iam_binding" "foo" { diff --git a/google/resource_pubsub_topic.go b/google/resource_pubsub_topic.go index 5311f691b83..d845400371d 100644 --- a/google/resource_pubsub_topic.go +++ b/google/resource_pubsub_topic.go @@ -65,6 +65,17 @@ The expected format is 'projects/*/locations/*/keyRings/*/cryptoKeys/*'`, Description: `A set of key/value label pairs to assign to this Topic.`, Elem: &schema.Schema{Type: schema.TypeString}, }, + "message_retention_duration": { + Type: schema.TypeString, + Optional: true, + Description: `Indicates the minimum duration to retain a message after it is published +to the topic. If this field is set, messages published to the topic in +the last messageRetentionDuration are always available to subscribers. +For instance, it allows any attached subscription to seek to a timestamp +that is up to messageRetentionDuration in the past. If this field is not +set, message retention is controlled by settings on individual subscriptions. +Cannot be more than 7 days or less than 10 minutes.`, + }, "message_storage_policy": { Type: schema.TypeList, Computed: true, @@ -166,6 +177,12 @@ func resourcePubsubTopicCreate(d *schema.ResourceData, meta interface{}) error { } else if v, ok := d.GetOkExists("schema_settings"); !isEmptyValue(reflect.ValueOf(schemaSettingsProp)) && (ok || !reflect.DeepEqual(v, schemaSettingsProp)) { obj["schemaSettings"] = schemaSettingsProp } + messageRetentionDurationProp, err := expandPubsubTopicMessageRetentionDuration(d.Get("message_retention_duration"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("message_retention_duration"); !isEmptyValue(reflect.ValueOf(messageRetentionDurationProp)) && (ok || !reflect.DeepEqual(v, messageRetentionDurationProp)) { + obj["messageRetentionDuration"] = messageRetentionDurationProp + } obj, err = resourcePubsubTopicEncoder(d, meta, obj) if err != nil { @@ -297,6 +314,9 @@ func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error { if err := d.Set("schema_settings", flattenPubsubTopicSchemaSettings(res["schemaSettings"], d, config)); err != nil { return fmt.Errorf("Error reading Topic: %s", err) } + if err := d.Set("message_retention_duration", flattenPubsubTopicMessageRetentionDuration(res["messageRetentionDuration"], d, config)); err != nil { + return fmt.Errorf("Error reading Topic: %s", err) + } return nil } @@ -341,6 +361,12 @@ func resourcePubsubTopicUpdate(d *schema.ResourceData, meta interface{}) error { } else if v, ok := d.GetOkExists("schema_settings"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, schemaSettingsProp)) { obj["schemaSettings"] = schemaSettingsProp } + messageRetentionDurationProp, err := expandPubsubTopicMessageRetentionDuration(d.Get("message_retention_duration"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("message_retention_duration"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, messageRetentionDurationProp)) { + obj["messageRetentionDuration"] = messageRetentionDurationProp + } obj, err = resourcePubsubTopicUpdateEncoder(d, meta, obj) if err != nil { @@ -370,6 +396,10 @@ func resourcePubsubTopicUpdate(d *schema.ResourceData, meta interface{}) error { if d.HasChange("schema_settings") { updateMask = append(updateMask, "schemaSettings") } + + if d.HasChange("message_retention_duration") { + updateMask = append(updateMask, "messageRetentionDuration") + } // updateMask is a URL parameter but not present in the schema, so replaceVars // won't set it url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) @@ -505,6 +535,10 @@ func flattenPubsubTopicSchemaSettingsEncoding(v interface{}, d *schema.ResourceD return v } +func flattenPubsubTopicMessageRetentionDuration(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + func expandPubsubTopicName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return GetResourceNameFromSelfLink(v.(string)), nil } @@ -581,6 +615,10 @@ func expandPubsubTopicSchemaSettingsEncoding(v interface{}, d TerraformResourceD return v, nil } +func expandPubsubTopicMessageRetentionDuration(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + func resourcePubsubTopicEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { delete(obj, "name") return obj, nil diff --git a/google/resource_pubsub_topic_generated_test.go b/google/resource_pubsub_topic_generated_test.go index a9e6e14d3e5..640764c6a44 100644 --- a/google/resource_pubsub_topic_generated_test.go +++ b/google/resource_pubsub_topic_generated_test.go @@ -55,6 +55,8 @@ resource "google_pubsub_topic" "example" { labels = { foo = "bar" } + + message_retention_duration = "86600s" } `, context) } diff --git a/website/docs/r/pubsub_topic.html.markdown b/website/docs/r/pubsub_topic.html.markdown index 03cc19e2d1f..2da84d2629d 100644 --- a/website/docs/r/pubsub_topic.html.markdown +++ b/website/docs/r/pubsub_topic.html.markdown @@ -49,6 +49,8 @@ resource "google_pubsub_topic" "example" { labels = { foo = "bar" } + + message_retention_duration = "86600s" } ``` ## Example Usage - Pubsub Topic Cmek @@ -147,6 +149,16 @@ The following arguments are supported: Settings for validating messages published against a schema. Structure is [documented below](#nested_schema_settings). +* `message_retention_duration` - + (Optional) + Indicates the minimum duration to retain a message after it is published + to the topic. If this field is set, messages published to the topic in + the last messageRetentionDuration are always available to subscribers. + For instance, it allows any attached subscription to seek to a timestamp + that is up to messageRetentionDuration in the past. If this field is not + set, message retention is controlled by settings on individual subscriptions. + Cannot be more than 7 days or less than 10 minutes. + * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.