-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_email_forward.go
127 lines (98 loc) · 3.49 KB
/
resource_email_forward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package improvmx
import (
"fmt"
"log"
"strconv"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceEmailForward() *schema.Resource {
return &schema.Resource{
Create: resourceEmailForwardCreate,
Read: resourceEmailForwardRead,
Update: resourceEmailForwardUpdate,
Delete: resourceEmailForwardDelete,
Importer: &schema.ResourceImporter{
State: resourceEmailForwardImport,
},
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
},
"alias_name": {
Type: schema.TypeString,
Required: true,
},
"destination_email": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceEmailForwardCreate(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
m.Client.CreateEmailForward(d.Get("domain").(string), d.Get("alias_name").(string), d.Get("destination_email").(string))
return resourceEmailForwardRead(d, meta)
}
func resourceEmailForwardRead(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
m.Mutex.Lock()
defer m.Mutex.Unlock()
for {
resp := m.Client.GetEmailForward(d.Get("domain").(string), d.Get("alias_name").(string))
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)
if resp.Code == 429 {
log.Printf("[DEBUG] Sleeping for 10 seconds to allow rate limit to recover.")
time.Sleep(10 * time.Second)
}
if resp.Code == 404 {
log.Printf("[DEBUG] Couldn't find the resource in ImprovMX. Aborting")
return fmt.Errorf("HTTP response code %d, error text: %s", resp.Code, resp.Errors.Domain)
}
if resp.Success {
d.SetId(strconv.FormatInt(resp.Alias.Id, 10))
d.Set("alias_name", resp.Alias.Alias)
d.Set("destination_email", resp.Alias.Forward)
return nil
}
}
}
func resourceEmailForwardUpdate(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
for {
resp := m.Client.UpdateEmailForward(d.Get("domain").(string), d.Get("alias_name").(string), d.Get("destination_email").(string))
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)
if resp.Code == 429 {
log.Printf("[DEBUG] Sleeping for 10 seconds to allow rate limit to recover.")
time.Sleep(10 * time.Second)
}
if resp.Code == 404 {
log.Printf("[DEBUG] Couldn't find the resource in ImprovMX. Aborting")
return fmt.Errorf("HTTP response code %d, error text: %s", resp.Code, resp.Errors.Domain)
}
if resp.Success {
return resourceEmailForwardRead(d, meta)
}
}
}
func resourceEmailForwardDelete(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
m.Mutex.Lock()
defer m.Mutex.Unlock()
m.Client.DeleteEmailForward(d.Get("domain").(string), d.Get("alias_name").(string))
return nil
}
func resourceEmailForwardImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "_")
if len(parts) != 2 {
return nil, fmt.Errorf("Error Importing email forward. Please make sure the email forward ID is in the form DOMAIN_EMAILFORWARDNAME (i.e. example.com_hi)")
}
d.SetId(parts[1])
d.Set("domain", parts[0])
d.Set("alias_name", parts[1])
resourceEmailForwardRead(d, meta)
return []*schema.ResourceData{d}, nil
}