2014-08-25 00:40:54 +02:00
|
|
|
package mailgun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-01-21 16:51:08 +01:00
|
|
|
"time"
|
2014-08-25 00:40:54 +02:00
|
|
|
|
2016-01-21 16:51:08 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-08-25 00:40:54 +02:00
|
|
|
"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,
|
2014-08-25 01:00:17 +02:00
|
|
|
Required: true,
|
2014-08-25 00:40:54 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"smtp_login": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"wildcard": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2014-08-29 20:39:06 +02:00
|
|
|
|
|
|
|
"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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-08-25 00:40:54 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-12-15 15:26:17 +01:00
|
|
|
_, err = resourceMailginDomainRetrieve(d.Id(), client, d)
|
2014-08-25 00:40:54 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:51:08 +01:00
|
|
|
// Give the destroy a chance to take effect
|
|
|
|
return resource.Retry(1*time.Minute, func() error {
|
|
|
|
_, err = client.RetrieveDomain(d.Id())
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("[INFO] Retrying until domain disappears...")
|
|
|
|
return fmt.Errorf("Domain seems to still exist; will check again.")
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] Got error looking for domain, seems gone: %s", err)
|
|
|
|
return nil
|
|
|
|
})
|
2014-08-25 00:40:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMailgunDomainRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*mailgun.Client)
|
|
|
|
|
2014-12-15 15:26:17 +01:00
|
|
|
_, err := resourceMailginDomainRetrieve(d.Id(), client, d)
|
2014-08-25 00:40:54 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-15 15:26:17 +01:00
|
|
|
func resourceMailginDomainRetrieve(id string, client *mailgun.Client, d *schema.ResourceData) (*mailgun.DomainResponse, error) {
|
2014-08-29 20:15:14 +02:00
|
|
|
resp, err := client.RetrieveDomain(id)
|
2014-08-25 00:40:54 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error retrieving domain: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
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)
|
2014-08-25 00:40:54 +02:00
|
|
|
|
2015-02-19 00:18:19 +01:00
|
|
|
receivingRecords := make([]map[string]interface{}, len(resp.ReceivingRecords))
|
2014-08-29 20:39:06 +02:00
|
|
|
for i, r := range resp.ReceivingRecords {
|
2015-02-19 00:18:19 +01:00
|
|
|
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
|
2014-08-29 20:39:06 +02:00
|
|
|
}
|
2015-02-19 00:18:19 +01:00
|
|
|
d.Set("receiving_records", receivingRecords)
|
2014-08-29 20:39:06 +02:00
|
|
|
|
2015-02-19 00:18:19 +01:00
|
|
|
sendingRecords := make([]map[string]interface{}, len(resp.SendingRecords))
|
2014-08-29 20:39:06 +02:00
|
|
|
for i, r := range resp.SendingRecords {
|
2015-02-19 00:18:19 +01:00
|
|
|
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
|
2014-08-29 20:39:06 +02:00
|
|
|
}
|
2015-02-19 00:18:19 +01:00
|
|
|
d.Set("sending_records", sendingRecords)
|
2014-08-29 20:39:06 +02:00
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
return &resp, nil
|
2014-08-25 00:40:54 +02:00
|
|
|
}
|