Skip to content

add ability to retry cloud functions on failure #1452

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

Merged
merged 1 commit into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions google/resource_cloudfunctions_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ func resourceCloudFunctionsFunction() *schema.Resource {
Computed: true,
},

"retry_on_failure": {
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"trigger_http"},
},

"project": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -282,6 +288,11 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro
// Topic must be in same project as function
Resource: fmt.Sprintf("projects/%s/topics/%s", project, v.(string)),
}
if d.Get("retry_on_failure").(bool) {
function.EventTrigger.FailurePolicy = &cloudfunctions.FailurePolicy{
Retry: &cloudfunctions.Retry{},
}
}
}

v, triggBucketOk := d.GetOk("trigger_bucket")
Expand All @@ -293,6 +304,11 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro
// Bucket must be in same project as function
Resource: fmt.Sprintf("projects/%s/buckets/%s", project, v.(string)),
}
if d.Get("retry_on_failure").(bool) {
function.EventTrigger.FailurePolicy = &cloudfunctions.FailurePolicy{
Retry: &cloudfunctions.Retry{},
}
}
}

if !triggHttpOk && !triggTopicOk && !triggBucketOk {
Expand Down Expand Up @@ -365,6 +381,8 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error
case "providers/cloud.storage/eventTypes/object.change":
d.Set("trigger_bucket", GetResourceNameFromSelfLink(function.EventTrigger.Resource))
}
retry := function.EventTrigger.FailurePolicy != nil && function.EventTrigger.FailurePolicy.Retry != nil
d.Set("retry_on_failure", retry)
}
d.Set("region", cloudFuncId.Region)
d.Set("project", cloudFuncId.Project)
Expand Down Expand Up @@ -409,6 +427,18 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro
updateMaskArr = append(updateMaskArr, "labels")
}

if d.HasChange("retry_on_failure") {
if d.Get("retry_on_failure").(bool) {
if function.EventTrigger == nil {
function.EventTrigger = &cloudfunctions.EventTrigger{}
}
function.EventTrigger.FailurePolicy = &cloudfunctions.FailurePolicy{
Retry: &cloudfunctions.Retry{},
}
}
updateMaskArr = append(updateMaskArr, "eventTrigger.failurePolicy.retry")
}

if len(updateMaskArr) > 0 {
log.Printf("[DEBUG] Send Patch CloudFunction Configuration request: %#v", function)
updateMask := strings.Join(updateMaskArr, ",")
Expand Down
51 changes: 50 additions & 1 deletion google/resource_cloudfunctions_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"testing"

"archive/zip"
"io/ioutil"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/cloudfunctions/v1"
"io/ioutil"
)

const (
Expand Down Expand Up @@ -209,6 +210,28 @@ func TestAccCloudFunctionsFunction_bucket(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccCloudFunctionsFunction_bucketNoRetry(functionName, bucketName, zipFilePath),
Check: resource.ComposeTestCheckFunc(
testAccCloudFunctionsFunctionExists(
funcResourceName, &function),
resource.TestCheckResourceAttr(funcResourceName,
"available_memory_mb", "128"),
testAccCloudFunctionsFunctionSource(fmt.Sprintf("gs://%s/index.zip", bucketName), &function),
testAccCloudFunctionsFunctionTrigger(FUNCTION_TRIGGER_BUCKET, &function),
resource.TestCheckResourceAttr(funcResourceName,
"timeout", "61"),
resource.TestCheckResourceAttr(funcResourceName,
"entry_point", "helloGCS"),
resource.TestCheckResourceAttr(funcResourceName,
"trigger_bucket", bucketName),
),
},
{
ResourceName: funcResourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -443,6 +466,7 @@ resource "google_cloudfunctions_function" "function" {
trigger_topic = "${google_pubsub_topic.sub.name}"
timeout = 61
entry_point = "helloPubSub"
retry_on_failure = true
}`, bucketName, zipFilePath, topic, functionName)
}

Expand All @@ -459,6 +483,31 @@ resource "google_storage_bucket_object" "archive" {
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}"
trigger_bucket = "${google_storage_bucket.bucket.name}"
timeout = 61
entry_point = "helloGCS"
retry_on_failure = 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
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/cloudfunctions_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ The following arguments are supported:

* `labels` - (Optional) A set of key/value label pairs to assign to the function.

* `retry_on_failure` - (Optional) Whether the function should be retried on failure. This only applies to bucket and topic triggers, not HTTPS triggers.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down