Skip to content

Commit 4fa15de

Browse files
committed
resource_email_forward: Fix email forward creation operations
- Since 5041c41, configuring email forwards have been broken with: ``` panic: interface conversion: interface {} is *improvmx.Meta, not *improvmx.Client goroutine 22 [running]: github.com/issyl0/terraform-provider-improvmx/improvmx.resourceEmailForwardCreate(0xc000179000, 0x1765080, 0xc00061e288, 0x0, 0xffffffffffffffff) github.com/issyl0/terraform-provider-improvmx/improvmx/resource_email_forward.go:42 +0x25f ``` - I decided to use the same style of handling as in `resource_domain`, by using a mutex lock on read, and doing rate limit debug logging too.
1 parent 9e392da commit 4fa15de

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

improvmx/resource_email_forward.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package improvmx
22

33
import (
44
"fmt"
5+
"log"
56
"strconv"
67
"strings"
8+
"time"
79

810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9-
improvmxApi "github.com/issyl0/go-improvmx"
1011
)
1112

1213
func resourceEmailForward() *schema.Resource {
@@ -39,33 +40,62 @@ func resourceEmailForward() *schema.Resource {
3940
}
4041

4142
func resourceEmailForwardCreate(d *schema.ResourceData, meta interface{}) error {
42-
client := meta.(*improvmxApi.Client)
43-
client.CreateEmailForward(d.Get("domain").(string), d.Get("alias_name").(string), d.Get("destination_email").(string))
43+
m := meta.(*Meta)
44+
m.Client.CreateEmailForward(d.Get("domain").(string), d.Get("alias_name").(string), d.Get("destination_email").(string))
4445

4546
return resourceEmailForwardRead(d, meta)
4647
}
4748

4849
func resourceEmailForwardRead(d *schema.ResourceData, meta interface{}) error {
49-
client := meta.(*improvmxApi.Client)
50-
resp := client.GetEmailForward(d.Get("domain").(string), d.Get("alias_name").(string))
50+
m := meta.(*Meta)
51+
m.Mutex.Lock()
52+
defer m.Mutex.Unlock()
5153

52-
d.SetId(strconv.FormatInt(resp.Alias.Id, 10))
53-
d.Set("alias_name", resp.Alias.Alias)
54-
d.Set("destination_email", resp.Alias.Forward)
54+
for {
55+
resp := m.Client.GetEmailForward(d.Get("domain").(string), d.Get("alias_name").(string))
5556

56-
return nil
57+
log.Printf("[DEBUG] Got status code %v from ImprovMX API on Read for email_forward %s@%s, success: %v, errors: %v.", resp.Code, d.Get("alias_name").(string), d.Get("domain").(string), resp.Success, resp.Errors)
58+
59+
if resp.Code == 429 {
60+
log.Printf("[DEBUG] Sleeping for 10 seconds to allow rate limit to recover.")
61+
time.Sleep(10 * time.Second)
62+
}
63+
64+
if resp.Success {
65+
d.SetId(strconv.FormatInt(resp.Alias.Id, 10))
66+
d.Set("alias_name", resp.Alias.Alias)
67+
d.Set("destination_email", resp.Alias.Forward)
68+
69+
return nil
70+
}
71+
}
5772
}
5873

5974
func resourceEmailForwardUpdate(d *schema.ResourceData, meta interface{}) error {
60-
client := meta.(*improvmxApi.Client)
61-
client.UpdateEmailForward(d.Get("domain").(string), d.Get("alias_name").(string), d.Get("destination_email").(string))
75+
m := meta.(*Meta)
6276

63-
return resourceEmailForwardRead(d, meta)
77+
for {
78+
resp := m.Client.UpdateEmailForward(d.Get("domain").(string), d.Get("alias_name").(string), d.Get("destination_email").(string))
79+
80+
log.Printf("[DEBUG] Got status code %v from ImprovMX API on Update for email_forward %s@%s, success: %v, errors: %v.", resp.Code, d.Get("domain").(string), d.Get("alias_name").(string), resp.Success, resp.Errors)
81+
82+
if resp.Code == 429 {
83+
log.Printf("[DEBUG] Sleeping for 10 seconds to allow rate limit to recover.")
84+
time.Sleep(10 * time.Second)
85+
}
86+
87+
if resp.Success {
88+
return resourceEmailForwardRead(d, meta)
89+
}
90+
}
6491
}
6592

6693
func resourceEmailForwardDelete(d *schema.ResourceData, meta interface{}) error {
67-
client := meta.(*improvmxApi.Client)
68-
client.DeleteEmailForward(d.Get("domain").(string), d.Get("alias_name").(string))
94+
m := meta.(*Meta)
95+
m.Mutex.Lock()
96+
defer m.Mutex.Unlock()
97+
98+
m.Client.DeleteEmailForward(d.Get("domain").(string), d.Get("alias_name").(string))
6999

70100
return nil
71101
}

0 commit comments

Comments
 (0)