2018-01-10 21:25:43 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"archive/zip"
|
2018-05-09 20:57:54 +00:00
|
|
|
"io/ioutil"
|
|
|
|
|
2018-01-10 21:25:43 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"google.golang.org/api/cloudfunctions/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FUNCTION_TRIGGER_HTTP = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
const testHTTPTriggerPath = "./test-fixtures/cloudfunctions/http_trigger.js"
|
2018-07-18 21:48:00 +00:00
|
|
|
const testHTTPTriggerUpdatePath = "./test-fixtures/cloudfunctions/http_trigger_update.js"
|
2018-01-10 21:25:43 +00:00
|
|
|
const testPubSubTriggerPath = "./test-fixtures/cloudfunctions/pubsub_trigger.js"
|
|
|
|
const testBucketTriggerPath = "./test-fixtures/cloudfunctions/bucket_trigger.js"
|
|
|
|
|
|
|
|
func TestAccCloudFunctionsFunction_basic(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var function cloudfunctions.CloudFunction
|
|
|
|
|
|
|
|
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(testHTTPTriggerPath)
|
|
|
|
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_basic(functionName, bucketName, zipFilePath),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCloudFunctionsFunctionExists(
|
|
|
|
funcResourceName, &function),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"name", functionName),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"description", "test function"),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"available_memory_mb", "128"),
|
|
|
|
testAccCloudFunctionsFunctionSource(fmt.Sprintf("gs://%s/index.zip", bucketName), &function),
|
|
|
|
testAccCloudFunctionsFunctionTrigger(FUNCTION_TRIGGER_HTTP, &function),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"timeout", "61"),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"entry_point", "helloGET"),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"trigger_http", "true"),
|
|
|
|
testAccCloudFunctionsFunctionHasLabel("my-label", "my-label-value", &function),
|
2018-09-11 00:37:43 +00:00
|
|
|
testAccCloudFunctionsFunctionHasEnvironmentVariable("TEST_ENV_VARIABLE",
|
|
|
|
"test-env-variable-value", &function),
|
2018-01-10 21:25:43 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResourceName: funcResourceName,
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccCloudFunctionsFunction_update(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var function cloudfunctions.CloudFunction
|
|
|
|
|
|
|
|
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(testHTTPTriggerPath)
|
2018-07-18 21:48:00 +00:00
|
|
|
zipFileUpdatePath, err := createZIPArchiveForIndexJs(testHTTPTriggerUpdatePath)
|
2018-01-10 21:25:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
defer os.Remove(zipFilePath) // clean up
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccCloudFunctionsFunction_basic(functionName, bucketName, zipFilePath),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCloudFunctionsFunctionExists(
|
|
|
|
funcResourceName, &function),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"available_memory_mb", "128"),
|
|
|
|
testAccCloudFunctionsFunctionHasLabel("my-label", "my-label-value", &function),
|
|
|
|
),
|
|
|
|
},
|
2018-10-26 00:34:41 +00:00
|
|
|
{
|
|
|
|
ResourceName: funcResourceName,
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2018-01-10 21:25:43 +00:00
|
|
|
{
|
2018-07-18 21:48:00 +00:00
|
|
|
Config: testAccCloudFunctionsFunction_updated(functionName, bucketName, zipFileUpdatePath),
|
2018-01-10 21:25:43 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCloudFunctionsFunctionExists(
|
|
|
|
funcResourceName, &function),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"available_memory_mb", "256"),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"description", "test function updated"),
|
|
|
|
resource.TestCheckResourceAttr(funcResourceName,
|
|
|
|
"timeout", "91"),
|
|
|
|
testAccCloudFunctionsFunctionHasLabel("my-label", "my-updated-label-value", &function),
|
|
|
|
testAccCloudFunctionsFunctionHasLabel("a-new-label", "a-new-label-value", &function),
|
2018-09-11 00:37:43 +00:00
|
|
|
testAccCloudFunctionsFunctionHasEnvironmentVariable("TEST_ENV_VARIABLE",
|
|
|
|
"test-env-variable-value", &function),
|
|
|
|
testAccCloudFunctionsFunctionHasEnvironmentVariable("NEW_ENV_VARIABLE",
|
|
|
|
"new-env-variable-value", &function),
|
2018-01-10 21:25:43 +00:00
|
|
|
),
|
|
|
|
},
|
2018-10-26 00:34:41 +00:00
|
|
|
{
|
|
|
|
ResourceName: funcResourceName,
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2018-01-10 21:25:43 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccCloudFunctionsFunction_pubsub(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-10-03 18:15:45 +00:00
|
|
|
funcResourceName := "google_cloudfunctions_function.function"
|
|
|
|
functionName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
|
|
|
bucketName := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())
|
|
|
|
topicName := fmt.Sprintf("tf-test-sub-%s", acctest.RandString(10))
|
|
|
|
zipFilePath, err := createZIPArchiveForIndexJs(testPubSubTriggerPath)
|
|
|
|
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_pubsub(functionName, bucketName,
|
|
|
|
topicName, zipFilePath),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResourceName: funcResourceName,
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccCloudFunctionsFunction_bucket(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(testBucketTriggerPath)
|
|
|
|
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_bucket(functionName, bucketName, zipFilePath),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResourceName: funcResourceName,
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccCloudFunctionsFunction_bucketNoRetry(functionName, bucketName, zipFilePath),
|
|
|
|
},
|
2018-01-10 21:25:43 +00:00
|
|
|
{
|
|
|
|
ResourceName: funcResourceName,
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2018-10-03 18:15:45 +00:00
|
|
|
|
2018-01-10 21:25:43 +00:00
|
|
|
func testAccCheckCloudFunctionsFunctionDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_cloudfunctions_function" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
project := rs.Primary.Attributes["project"]
|
|
|
|
region := rs.Primary.Attributes["region"]
|
|
|
|
cloudFuncId := &cloudFunctionId{
|
|
|
|
Project: project,
|
|
|
|
Region: region,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
_, err := config.clientCloudFunctions.Projects.Locations.Functions.Get(cloudFuncId.cloudFunctionId()).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Function still exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCloudFunctionsFunctionExists(n string, function *cloudfunctions.CloudFunction) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
project := rs.Primary.Attributes["project"]
|
|
|
|
region := rs.Primary.Attributes["region"]
|
|
|
|
cloudFuncId := &cloudFunctionId{
|
|
|
|
Project: project,
|
|
|
|
Region: region,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
found, err := config.clientCloudFunctions.Projects.Locations.Functions.Get(cloudFuncId.cloudFunctionId()).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CloudFunctions Function not present")
|
|
|
|
}
|
|
|
|
|
|
|
|
*function = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCloudFunctionsFunctionSource(n string, function *cloudfunctions.CloudFunction) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if n != function.SourceArchiveUrl {
|
|
|
|
return fmt.Errorf("Expected source to be %v, got %v", n, function.EntryPoint)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCloudFunctionsFunctionTrigger(n int, function *cloudfunctions.CloudFunction) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
switch n {
|
|
|
|
case FUNCTION_TRIGGER_HTTP:
|
|
|
|
if function.HttpsTrigger == nil {
|
|
|
|
return fmt.Errorf("Expected HttpsTrigger to be set")
|
|
|
|
}
|
|
|
|
default:
|
2018-10-24 22:04:58 +00:00
|
|
|
return fmt.Errorf("testAccCloudFunctionsFunctionTrigger expects only FUNCTION_TRIGGER_HTTP, ")
|
2018-01-10 21:25:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCloudFunctionsFunctionHasLabel(key, value string,
|
|
|
|
function *cloudfunctions.CloudFunction) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
val, ok := function.Labels[key]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Label with key %s not found", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val != value {
|
|
|
|
return fmt.Errorf("Label value did not match for key %s: expected %s but found %s", key, value, val)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 00:37:43 +00:00
|
|
|
func testAccCloudFunctionsFunctionHasEnvironmentVariable(key, value string,
|
|
|
|
function *cloudfunctions.CloudFunction) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if val, ok := function.EnvironmentVariables[key]; ok {
|
|
|
|
if val != value {
|
|
|
|
return fmt.Errorf("Environment Variable value did not match for key %s: expected %s but found %s",
|
|
|
|
key, value, val)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Environment Variable with key %s not found", key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 21:25:43 +00:00
|
|
|
func createZIPArchiveForIndexJs(sourcePath string) (string, error) {
|
|
|
|
source, err := ioutil.ReadFile(sourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Create a buffer to write our archive to.
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
// Create a new zip archive.
|
|
|
|
w := zip.NewWriter(buf)
|
|
|
|
|
|
|
|
f, err := w.Create("index.js")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, err = f.Write(source)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to check the error on Close.
|
|
|
|
err = w.Close()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Create temp file to write zip to
|
|
|
|
tmpfile, err := ioutil.TempFile("", "zip")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tmpfile.Write(buf.Bytes()); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := tmpfile.Close(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return tmpfile.Name(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCloudFunctionsFunction_basic(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"
|
|
|
|
description = "test function"
|
|
|
|
available_memory_mb = 128
|
|
|
|
source_archive_bucket = "${google_storage_bucket.bucket.name}"
|
|
|
|
source_archive_object = "${google_storage_bucket_object.archive.name}"
|
|
|
|
trigger_http = true
|
|
|
|
timeout = 61
|
|
|
|
entry_point = "helloGET"
|
|
|
|
labels {
|
|
|
|
my-label = "my-label-value"
|
|
|
|
}
|
2018-09-11 00:37:43 +00:00
|
|
|
environment_variables {
|
|
|
|
TEST_ENV_VARIABLE = "test-env-variable-value"
|
|
|
|
}
|
2018-01-10 21:25:43 +00:00
|
|
|
}
|
|
|
|
`, bucketName, zipFilePath, functionName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCloudFunctionsFunction_updated(functionName string, bucketName string, zipFilePath string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_storage_bucket" "bucket" {
|
|
|
|
name = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_storage_bucket_object" "archive" {
|
2018-07-18 21:48:00 +00:00
|
|
|
name = "index_update.zip"
|
2018-01-10 21:25:43 +00:00
|
|
|
bucket = "${google_storage_bucket.bucket.name}"
|
|
|
|
source = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_cloudfunctions_function" "function" {
|
|
|
|
name = "%s"
|
|
|
|
description = "test function updated"
|
|
|
|
available_memory_mb = 256
|
|
|
|
source_archive_bucket = "${google_storage_bucket.bucket.name}"
|
|
|
|
source_archive_object = "${google_storage_bucket_object.archive.name}"
|
|
|
|
trigger_http = true
|
2018-10-26 00:34:41 +00:00
|
|
|
runtime = "nodejs8"
|
2018-01-10 21:25:43 +00:00
|
|
|
timeout = 91
|
|
|
|
entry_point = "helloGET"
|
|
|
|
labels {
|
|
|
|
my-label = "my-updated-label-value"
|
|
|
|
a-new-label = "a-new-label-value"
|
|
|
|
}
|
2018-09-11 00:37:43 +00:00
|
|
|
environment_variables {
|
|
|
|
TEST_ENV_VARIABLE = "test-env-variable-value"
|
|
|
|
NEW_ENV_VARIABLE = "new-env-variable-value"
|
|
|
|
}
|
2018-01-10 21:25:43 +00:00
|
|
|
}`, bucketName, zipFilePath, functionName)
|
|
|
|
}
|
|
|
|
|
2018-10-03 18:15:45 +00:00
|
|
|
func testAccCloudFunctionsFunction_pubsub(functionName string, bucketName string,
|
|
|
|
topic 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_pubsub_topic" "sub" {
|
|
|
|
name = "%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 = "helloPubSub"
|
|
|
|
event_trigger {
|
|
|
|
event_type = "providers/cloud.pubsub/eventTypes/topic.publish"
|
|
|
|
resource = "${google_pubsub_topic.sub.name}"
|
|
|
|
failure_policy {
|
|
|
|
retry = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`, bucketName, zipFilePath, topic, functionName)
|
|
|
|
}
|
|
|
|
|
2018-01-10 21:25:43 +00:00
|
|
|
func testAccCloudFunctionsFunction_bucket(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"
|
|
|
|
}
|
|
|
|
|
2018-10-03 18:15:45 +00:00
|
|
|
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 = "helloGCS"
|
|
|
|
event_trigger {
|
2018-11-06 17:30:19 +00:00
|
|
|
event_type = "google.storage.object.finalize"
|
2018-10-03 18:15:45 +00:00
|
|
|
resource = "${google_storage_bucket.bucket.name}"
|
|
|
|
failure_policy {
|
|
|
|
retry = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`, bucketName, zipFilePath, functionName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCloudFunctionsFunction_bucketNoRetry(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 = "helloGCS"
|
|
|
|
event_trigger {
|
2018-11-06 17:30:19 +00:00
|
|
|
event_type = "google.storage.object.finalize"
|
2018-10-03 18:15:45 +00:00
|
|
|
resource = "${google_storage_bucket.bucket.name}"
|
|
|
|
}
|
|
|
|
}`, bucketName, zipFilePath, functionName)
|
|
|
|
}
|