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

support firestore cloud function triggers #2480

Merged
merged 2 commits into from
Nov 21, 2018
Merged
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
29 changes: 28 additions & 1 deletion google/resource_cloudfunctions_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,17 @@ func expandEventTrigger(configured []interface{}, project string) *cloudfunction
shape = "projects/%s/buckets/%s"
case strings.HasPrefix(eventType, "google.pubsub.topic."):
shape = "projects/%s/topics/%s"
case strings.HasPrefix(eventType, "google.firestore.document."):
matthewrj marked this conversation as resolved.
Show resolved Hide resolved
shape = "projects/%s/databases/(default)/documents/%s"
// Legacy style triggers
case strings.HasPrefix(eventType, "providers/cloud.storage/eventTypes/"):
shape = "projects/%s/buckets/%s"
case strings.HasPrefix(eventType, "providers/cloud.pubsub/eventTypes/"):
shape = "projects/%s/topics/%s"
case strings.HasPrefix(eventType, "providers/cloud.firestore/eventTypes/"):
shape = "projects/%s/databases/(default)/documents/%s"
matthewrj marked this conversation as resolved.
Show resolved Hide resolved
default:
return nil
}

return &cloudfunctions.EventTrigger{
Expand All @@ -550,9 +556,30 @@ func flattenEventTrigger(eventTrigger *cloudfunctions.EventTrigger) []map[string
return result
}

resource := ""
switch {
case strings.HasPrefix(eventTrigger.EventType, "google.storage.object."):
resource = GetResourceNameFromSelfLink(eventTrigger.Resource)
case strings.HasPrefix(eventTrigger.EventType, "google.pubsub.topic."):
resource = GetResourceNameFromSelfLink(eventTrigger.Resource)
case strings.HasPrefix(eventTrigger.EventType, "google.firestore.document."):
parts := strings.SplitN(eventTrigger.Resource, "/", 6)
resource = parts[len(parts)-1]
// Legacy style triggers
case strings.HasPrefix(eventTrigger.EventType, "providers/cloud.storage/eventTypes/"):
resource = GetResourceNameFromSelfLink(eventTrigger.Resource)
case strings.HasPrefix(eventTrigger.EventType, "providers/cloud.pubsub/eventTypes/"):
resource = GetResourceNameFromSelfLink(eventTrigger.Resource)
case strings.HasPrefix(eventTrigger.EventType, "providers/cloud.firestore/eventTypes/"):
parts := strings.SplitN(eventTrigger.Resource, "/", 6)
matthewrj marked this conversation as resolved.
Show resolved Hide resolved
resource = parts[len(parts)-1]
default:
matthewrj marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

result = append(result, map[string]interface{}{
"event_type": eventTrigger.EventType,
"resource": GetResourceNameFromSelfLink(eventTrigger.Resource),
"resource": resource,
"failure_policy": flattenFailurePolicy(eventTrigger.FailurePolicy),
})

Expand Down
56 changes: 56 additions & 0 deletions google/resource_cloudfunctions_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const testHTTPTriggerPath = "./test-fixtures/cloudfunctions/http_trigger.js"
const testHTTPTriggerUpdatePath = "./test-fixtures/cloudfunctions/http_trigger_update.js"
const testPubSubTriggerPath = "./test-fixtures/cloudfunctions/pubsub_trigger.js"
const testBucketTriggerPath = "./test-fixtures/cloudfunctions/bucket_trigger.js"
const testFirestoreTriggerPath = "./test-fixtures/cloudfunctions/firestore_trigger.js"

func TestAccCloudFunctionsFunction_basic(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -205,6 +206,34 @@ func TestAccCloudFunctionsFunction_bucket(t *testing.T) {
})
}

func TestAccCloudFunctionsFunction_firestore(t *testing.T) {
t.Parallel()
funcResourceName := "google_cloudfunctions_function.function"
functionName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
bucketName := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())
zipFilePath, err := createZIPArchiveForIndexJs(testFirestoreTriggerPath)
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove(zipFilePath) // clean up

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudFunctionsFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudFunctionsFunction_firestore(functionName, bucketName, zipFilePath),
},
{
ResourceName: funcResourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckCloudFunctionsFunctionDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down Expand Up @@ -508,3 +537,30 @@ resource "google_cloudfunctions_function" "function" {
}
}`, bucketName, zipFilePath, functionName)
}

func testAccCloudFunctionsFunction_firestore(functionName string, bucketName string,
zipFilePath string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
}

resource "google_storage_bucket_object" "archive" {
name = "index.zip"
bucket = "${google_storage_bucket.bucket.name}"
source = "%s"
}

resource "google_cloudfunctions_function" "function" {
name = "%s"
available_memory_mb = 128
source_archive_bucket = "${google_storage_bucket.bucket.name}"
source_archive_object = "${google_storage_bucket_object.archive.name}"
timeout = 61
entry_point = "helloFirestore"
event_trigger {
event_type = "providers/cloud.firestore/eventTypes/document.write"
resource = "messages/{messageId}"
}
}`, bucketName, zipFilePath, functionName)
}
13 changes: 13 additions & 0 deletions google/test-fixtures/cloudfunctions/firestore_trigger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Background Cloud Function to be triggered by Firestore.
*
* @param {object} event The Cloud Functions event.
* @param {function} callback The callback function.
*/
exports.helloFirestore = function (event, callback) {
const messageId = event.params.messageId;

console.log(`Received message ${messageId}`);

callback();
};