forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_mailgun_domain.go
207 lines (172 loc) · 4.98 KB
/
resource_mailgun_domain.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package mailgun
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/pearkes/mailgun"
)
func resourceMailgunDomain() *schema.Resource {
return &schema.Resource{
Create: resourceMailgunDomainCreate,
Read: resourceMailgunDomainRead,
Delete: resourceMailgunDomainDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"spam_action": &schema.Schema{
Type: schema.TypeString,
Computed: true,
ForceNew: true,
Optional: true,
},
"smtp_password": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"smtp_login": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"wildcard": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
ForceNew: true,
Optional: true,
},
"receiving_records": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"priority": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"record_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"valid": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
},
},
"sending_records": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"record_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"valid": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func resourceMailgunDomainCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*mailgun.Client)
opts := mailgun.CreateDomain{}
opts.Name = d.Get("name").(string)
opts.SmtpPassword = d.Get("smtp_password").(string)
opts.SpamAction = d.Get("spam_action").(string)
opts.Wildcard = d.Get("wildcard").(bool)
log.Printf("[DEBUG] Domain create configuration: %#v", opts)
domain, err := client.CreateDomain(&opts)
if err != nil {
return err
}
d.SetId(domain)
log.Printf("[INFO] Domain ID: %s", d.Id())
// Retrieve and update state of domain
_, err = resourceMailginDomainRetrieve(d.Id(), client, d)
if err != nil {
return err
}
return nil
}
func resourceMailgunDomainDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*mailgun.Client)
log.Printf("[INFO] Deleting Domain: %s", d.Id())
// Destroy the domain
err := client.DestroyDomain(d.Id())
if err != nil {
return fmt.Errorf("Error deleting domain: %s", err)
}
// Give the destroy a chance to take effect
return resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err = client.RetrieveDomain(d.Id())
if err == nil {
log.Printf("[INFO] Retrying until domain disappears...")
return resource.RetryableError(
fmt.Errorf("Domain seems to still exist; will check again."))
}
log.Printf("[INFO] Got error looking for domain, seems gone: %s", err)
return nil
})
}
func resourceMailgunDomainRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*mailgun.Client)
_, err := resourceMailginDomainRetrieve(d.Id(), client, d)
if err != nil {
return err
}
return nil
}
func resourceMailginDomainRetrieve(id string, client *mailgun.Client, d *schema.ResourceData) (*mailgun.DomainResponse, error) {
resp, err := client.RetrieveDomain(id)
if err != nil {
return nil, fmt.Errorf("Error retrieving domain: %s", err)
}
d.Set("name", resp.Domain.Name)
d.Set("smtp_password", resp.Domain.SmtpPassword)
d.Set("smtp_login", resp.Domain.SmtpLogin)
d.Set("wildcard", resp.Domain.Wildcard)
d.Set("spam_action", resp.Domain.SpamAction)
receivingRecords := make([]map[string]interface{}, len(resp.ReceivingRecords))
for i, r := range resp.ReceivingRecords {
receivingRecords[i] = make(map[string]interface{})
receivingRecords[i]["priority"] = r.Priority
receivingRecords[i]["valid"] = r.Valid
receivingRecords[i]["value"] = r.Value
receivingRecords[i]["record_type"] = r.RecordType
}
d.Set("receiving_records", receivingRecords)
sendingRecords := make([]map[string]interface{}, len(resp.SendingRecords))
for i, r := range resp.SendingRecords {
sendingRecords[i] = make(map[string]interface{})
sendingRecords[i]["name"] = r.Name
sendingRecords[i]["valid"] = r.Valid
sendingRecords[i]["value"] = r.Value
sendingRecords[i]["record_type"] = r.RecordType
}
d.Set("sending_records", sendingRecords)
return &resp, nil
}