-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_smtp_credential.go
120 lines (95 loc) · 3.38 KB
/
resource_smtp_credential.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
package improvmx
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceSMTPCredential() *schema.Resource {
return &schema.Resource{
Create: resourceSMTPCredentialCreate,
Read: resourceSMTPCredentialRead,
Update: resourceSMTPCredentialUpdate,
Delete: resourceSMTPCredentialDelete,
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
},
"username": {
Type: schema.TypeString,
Required: true,
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
},
}
}
func resourceSMTPCredentialCreate(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
for {
resp := m.Client.CreateSMTPCredential(d.Get("domain").(string), d.Get("username").(string), d.Get("password").(string))
log.Printf("[DEBUG] Got status code %v from ImprovMX API on Create for SMTP %s@%s, success: %v, errors: %v.", resp.Code, d.Get("username").(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 {
return resourceSMTPCredentialRead(d, meta)
}
}
}
func resourceSMTPCredentialRead(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
m.Mutex.Lock()
defer m.Mutex.Unlock()
for {
resp := m.Client.GetSMTPCredential(d.Get("domain").(string))
log.Printf("[DEBUG] Got status code %v from ImprovMX API on Read for SMTP domain %s, success: %v, errors: %v.", resp.Code, 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(d.Get("domain").(string))
d.Set("domain", d.Get("domain").(string))
return nil
}
}
}
func resourceSMTPCredentialUpdate(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
for {
resp := m.Client.UpdateSMTPCredential(d.Get("domain").(string), d.Get("username").(string), d.Get("password").(string))
log.Printf("[DEBUG] Got status code %v from ImprovMX API on Update for SMTP domain %s@%s, success: %v, errors: %v.", resp.Code, d.Get("username").(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 {
return resourceSMTPCredentialRead(d, meta)
}
}
}
func resourceSMTPCredentialDelete(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
m.Mutex.Lock()
defer m.Mutex.Unlock()
m.Client.DeleteSMTPCredential(d.Get("domain").(string), d.Get("username").(string))
return nil
}