Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MessageTransform field to 'google_pubsub_subscription' #13203

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions mmv1/products/pubsub/Subscription.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,63 @@ properties:

Note that subscribers may still receive multiple copies of a message when `enable_exactly_once_delivery`
is true if the message was published multiple times by a publisher client. These copies are considered distinct by Pub/Sub and have distinct messageId values
- name: messageTransforms
type: Array
required: false
description: |
Transforms to be applied to messages published to the topic. Transforms are applied in the
order specified.
item_type:
type: NestedObject
properties:
- name: javascriptUdf
type: NestedObject
required: false
description: |
Javascript User Defined Function. If multiple Javascript UDFs are specified on a resource,
each one must have a unique `function_name`.
properties:
- name: functionName
type: String
required: true
description: |
Name of the JavaScript function that should be applied to Pub/Sub messages.
- name: code
type: String
required: true
description: |
JavaScript code that contains a function `function_name` with the
following signature:
```
/**
* Transforms a Pub/Sub message.
*
* @return {(Object<string, (string | Object<string, string>)>|null)} - To
* filter a message, return `null`. To transform a message return a map
* with the following keys:
* - (required) 'data' : {string}
* - (optional) 'attributes' : {Object<string, string>}
* Returning empty `attributes` will remove all attributes from the
* message.
*
* @param {(Object<string, (string | Object<string, string>)>} Pub/Sub
* message. Keys:
* - (required) 'data' : {string}
* - (required) 'attributes' : {Object<string, string>}
*
* @param {Object<string, any>} metadata - Pub/Sub message metadata.
* Keys:
* - (required) 'message_id' : {string}
* - (optional) 'publish_time': {string} YYYY-MM-DDTHH:MM:SSZ format
* - (optional) 'ordering_key': {string}
*/
function <function_name>(message, metadata) {
}
```
- name: enabled
type: Boolean
required: false
default_value: true
description: |
If set to `true`, the transform is enabled. If `false` the transform is disabled and will
not be applied to messages. Default: `true`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resource "google_pubsub_topic" "{{$.PrimaryResourceId}}" {
name = "{{index $.Vars "topic_name"}}"
}

resource "google_pubsub_subscription" "{{$.PrimaryResourceId}}" {
name = "{{index $.Vars "subscription_name"}}"
topic = google_pubsub_topic.{{$.PrimaryResourceId}}.id

message_transforms = [
{
javascript_udf {
function_name = "filter_falsy",
code = "function filter_falsy(message, metadata) {\n return message ? message : null;\n}\n"
}
enabled = true
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,54 @@ func TestAccPubsubSubscription_filter(t *testing.T) {
})
}

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

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: testAccPubsubSubscription_updateWithUpdatedJavascriptUdfSettings(topic, subscriptionShort),
},
{
ResourceName: "google_pubsub_subscription.foo",
ImportStateId: subscriptionShort,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccPubsubSubscription_updateWithUpdatedJavascriptUdfSettings(topic, subscription string) string {
return fmt.Sprintf(`
resource "google_pubsub_topic" "foo" {
name = "%s"
}

resource "google_pubsub_subscription" "foo" {
name = "%s"
topic = google_pubsub_topic.foo.id
name = "%s"
message_transforms = [
{
javascript_udf {
function_name = "filter_falsy",
code = "function filter_falsy(message, metadata) {\n return message ? message : null;\n}\n"
}
enabled = true
}
]
}
`, topic, subscription)
}


func testAccPubsubSubscription_emptyTTL(topic, subscription string) string {
return fmt.Sprintf(`
resource "google_pubsub_topic" "foo" {
Expand Down
Loading