Skip to content

Commit

Permalink
Add Retention Duration to google_pubsub_topic (#5347) (#10501)
Browse files Browse the repository at this point in the history
Co-authored-by: upodroid <[email protected]>
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: upodroid <[email protected]>
  • Loading branch information
modular-magician and upodroid authored Nov 4, 2021
1 parent 0fda46f commit 13cc2fd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/5347.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
pubsub: added `message_retention_duration` field to `google_pubsub_topic`
```
10 changes: 10 additions & 0 deletions google/iam_pubsub_topic_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ resource "google_pubsub_topic" "example" {
labels = {
foo = "bar"
}
message_retention_duration = "86600s"
}
resource "google_pubsub_topic_iam_member" "foo" {
Expand All @@ -143,6 +145,8 @@ resource "google_pubsub_topic" "example" {
labels = {
foo = "bar"
}
message_retention_duration = "86600s"
}
data "google_iam_policy" "foo" {
Expand All @@ -168,6 +172,8 @@ resource "google_pubsub_topic" "example" {
labels = {
foo = "bar"
}
message_retention_duration = "86600s"
}
data "google_iam_policy" "foo" {
Expand All @@ -189,6 +195,8 @@ resource "google_pubsub_topic" "example" {
labels = {
foo = "bar"
}
message_retention_duration = "86600s"
}
resource "google_pubsub_topic_iam_binding" "foo" {
Expand All @@ -208,6 +216,8 @@ resource "google_pubsub_topic" "example" {
labels = {
foo = "bar"
}
message_retention_duration = "86600s"
}
resource "google_pubsub_topic_iam_binding" "foo" {
Expand Down
38 changes: 38 additions & 0 deletions google/resource_pubsub_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, ",")})
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions google/resource_pubsub_topic_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ resource "google_pubsub_topic" "example" {
labels = {
foo = "bar"
}
message_retention_duration = "86600s"
}
`, context)
}
Expand Down
12 changes: 12 additions & 0 deletions website/docs/r/pubsub_topic.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ resource "google_pubsub_topic" "example" {
labels = {
foo = "bar"
}
message_retention_duration = "86600s"
}
```
## Example Usage - Pubsub Topic Cmek
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit 13cc2fd

Please sign in to comment.