From 86a49ee383b6666fd144baba91eda41c20c2cc5a Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 28 Aug 2019 14:04:44 -0700 Subject: [PATCH] Add retry predicate for 400 pubsub error (#4352) Signed-off-by: Modular Magician --- google/error_retry_predicates.go | 17 +++++++++++++++-- google/resource_pubsub_topic.go | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/google/error_retry_predicates.go b/google/error_retry_predicates.go index 7518540a4b4..73add242299 100644 --- a/google/error_retry_predicates.go +++ b/google/error_retry_predicates.go @@ -1,9 +1,9 @@ package google import ( - "strings" - "google.golang.org/api/googleapi" + "log" + "strings" ) // If a permission necessary to provision a resource is created in the same config @@ -17,3 +17,16 @@ func iamMemberMissing(err error) (bool, string) { } return false, "" } + +// Cloud PubSub returns a 400 error if a topic's parent project was recently created and an +// organization policy has not propagated. +// See https://github.com/terraform-providers/terraform-provider-google/issues/4349 +func pubsubTopicProjectNotReady(err error) (bool, string) { + if gerr, ok := err.(*googleapi.Error); ok { + if gerr.Code == 400 && strings.Contains(gerr.Body, "retry this operation") { + log.Printf("[DEBUG] Dismissed error as a retryable operation: %s", err) + return true, "Waiting for Pubsub topic's project to properly initialize with organiation policy" + } + } + return false, "" +} diff --git a/google/resource_pubsub_topic.go b/google/resource_pubsub_topic.go index 3061a6affa4..65f24924cd5 100644 --- a/google/resource_pubsub_topic.go +++ b/google/resource_pubsub_topic.go @@ -128,7 +128,7 @@ func resourcePubsubTopicCreate(d *schema.ResourceData, meta interface{}) error { if err != nil { return err } - res, err := sendRequestWithTimeout(config, "PUT", project, url, obj, d.Timeout(schema.TimeoutCreate)) + res, err := sendRequestWithTimeout(config, "PUT", project, url, obj, d.Timeout(schema.TimeoutCreate), pubsubTopicProjectNotReady) if err != nil { return fmt.Errorf("Error creating Topic: %s", err) }