From 5976bba0b5dd67d2169641a5c3dab935155fb885 Mon Sep 17 00:00:00 2001 From: Alexandre Garand Date: Tue, 9 Jul 2019 10:20:59 +0200 Subject: fix the tests the ips are now fetched correctly a sleep was put in the destroy check to ensure that the api have time to destroy before it is checked --- mailgun/resource_mailgun_domain.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'mailgun/resource_mailgun_domain.go') diff --git a/mailgun/resource_mailgun_domain.go b/mailgun/resource_mailgun_domain.go index 86531c6..e2bfe73 100644 --- a/mailgun/resource_mailgun_domain.go +++ b/mailgun/resource_mailgun_domain.go @@ -366,7 +366,7 @@ func DeleteDomain(d *schema.ResourceData, meta interface{}) error { func ReadDomain(d *schema.ResourceData, meta interface{}) error { mg := meta.(*mailgun.MailgunImpl) - ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*120) defer cancel() domainName := d.Id() mg = mailgun.NewMailgun(domainName, mg.APIKey()) @@ -425,9 +425,7 @@ func ReadDomain(d *schema.ResourceData, meta interface{}) error { d.Set("unsubscribe_tracking_settings_html_footer", domainTracking.Unsubscribe.HTMLFooter) d.Set("unsubscribe_tracking_settings_text_footer", domainTracking.Unsubscribe.TextFooter) - time.Sleep(25 * time.Second) - - ipAddress, err := mg.ListDomainIPS(ctx) + ipAddress, err := getIps(ctx, mg) if err != nil { return fmt.Errorf("Error Getting mailgun domain ips1 for %s: Error: %s", d.Id(), err) @@ -493,3 +491,15 @@ func ImportStatePassthroughDomain(d *schema.ResourceData, meta interface{}) ([]* } return []*schema.ResourceData{d}, nil } + +func getIps(ctx context.Context,mg *mailgun.MailgunImpl) ([]mailgun.IPAddress, error){ + start := time.Now() + t := time.Now() + ipAddress, err := mg.ListDomainIPS(ctx) + for (err != nil && t.Sub(start)< 120 * time.Second) { + ipAddress, err = mg.ListDomainIPS(ctx) + time.Sleep(5 * time.Second) + t = time.Now() + } + return ipAddress, err +} -- cgit v1.2.3 From 1de838cb1473ab472860ebe63a22d56aba5468b6 Mon Sep 17 00:00:00 2001 From: Alexandre Garand Date: Tue, 9 Jul 2019 13:48:52 +0200 Subject: replace sleep by resource.Retry --- mailgun/resource_mailgun_domain.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'mailgun/resource_mailgun_domain.go') diff --git a/mailgun/resource_mailgun_domain.go b/mailgun/resource_mailgun_domain.go index e2bfe73..c195cf0 100644 --- a/mailgun/resource_mailgun_domain.go +++ b/mailgun/resource_mailgun_domain.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/resource" "github.com/mailgun/mailgun-go/v3" "log" "time" @@ -493,13 +494,18 @@ func ImportStatePassthroughDomain(d *schema.ResourceData, meta interface{}) ([]* } func getIps(ctx context.Context,mg *mailgun.MailgunImpl) ([]mailgun.IPAddress, error){ - start := time.Now() - t := time.Now() - ipAddress, err := mg.ListDomainIPS(ctx) - for (err != nil && t.Sub(start)< 120 * time.Second) { + var ipAddress []mailgun.IPAddress + log.Printf("[DEBUG] begin to fetch ips for %s",mg.Domain()) + err := resource.Retry(2*time.Minute, func() *resource.RetryError { + var err error ipAddress, err = mg.ListDomainIPS(ctx) - time.Sleep(5 * time.Second) - t = time.Now() - } + if err != nil { + log.Printf("[DEBUG] failed to fetch ips for %s",mg.Domain()) + return resource.RetryableError(err) + } + log.Printf("[DEBUG] managed to fetch ips for %s",mg.Domain()) + + return nil + }) return ipAddress, err } -- cgit v1.2.3