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 ++++++++++++++---- mailgun/resource_mailgun_domain_test.go | 7 ++++--- 2 files changed, 18 insertions(+), 7 deletions(-) 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 +} diff --git a/mailgun/resource_mailgun_domain_test.go b/mailgun/resource_mailgun_domain_test.go index 952e013..a129b1f 100644 --- a/mailgun/resource_mailgun_domain_test.go +++ b/mailgun/resource_mailgun_domain_test.go @@ -21,7 +21,7 @@ type fullDomain struct { } func getFullDomain(mg *mailgun.MailgunImpl, domainName string) (*fullDomain, error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*120) defer cancel() mg = mailgun.NewMailgun(domainName, mg.APIKey()) @@ -42,7 +42,8 @@ func getFullDomain(mg *mailgun.MailgunImpl, domainName string) (*fullDomain, err return nil, fmt.Errorf("Error Getting mailgun domain tracking Details for %s: Error: %s", domainName, err) } - ipAddress, err := mg.ListDomainIPS(ctx) + ipAddress, err := getIps(ctx, mg) + if err != nil { return nil, fmt.Errorf("Error Getting mailgun domain ips2 for %s: Error: %s", domainName, err) } @@ -209,7 +210,7 @@ func testAccDomainCheckDestroy(domain *fullDomain) resource.TestCheckFunc { mg := testAccProvider.Meta().(*mailgun.MailgunImpl) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() - + time.Sleep(5 * time.Second) _, err := mg.GetDomain(ctx, domain.domainResponse.Domain.Name) if err == nil { return fmt.Errorf("domain still exists") -- 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 +++++++++++++------- mailgun/resource_mailgun_domain_test.go | 20 ++++++++++++++------ 2 files changed, 27 insertions(+), 13 deletions(-) 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 } diff --git a/mailgun/resource_mailgun_domain_test.go b/mailgun/resource_mailgun_domain_test.go index a129b1f..57aa630 100644 --- a/mailgun/resource_mailgun_domain_test.go +++ b/mailgun/resource_mailgun_domain_test.go @@ -10,6 +10,7 @@ import ( "strconv" "testing" "time" + "log" ) type fullDomain struct { @@ -210,13 +211,20 @@ func testAccDomainCheckDestroy(domain *fullDomain) resource.TestCheckFunc { mg := testAccProvider.Meta().(*mailgun.MailgunImpl) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() - time.Sleep(5 * time.Second) - _, err := mg.GetDomain(ctx, domain.domainResponse.Domain.Name) - if err == nil { - return fmt.Errorf("domain still exists") - } - return nil + log.Printf("[DEBUG] try to fetch destroyed domain %s",mg.Domain()) + + return resource.Retry(1*time.Minute, func() *resource.RetryError { + _, err := mg.GetDomain(ctx, domain.domainResponse.Domain.Name) + if err == nil { + log.Printf("[DEBUG] managed to fetch destroyed domain %s",mg.Domain()) + return resource.RetryableError(err) + } + + log.Printf("[DEBUG] failed to fetch destroyed domain %s",mg.Domain()) + + return nil + }) } } -- cgit v1.2.3