github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/mailgun/resource_mailgun_domain.go (about)

     1  package mailgun
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/pearkes/mailgun"
    11  )
    12  
    13  func resourceMailgunDomain() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceMailgunDomainCreate,
    16  		Read:   resourceMailgunDomainRead,
    17  		Delete: resourceMailgunDomainDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"spam_action": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Computed: true,
    29  				ForceNew: true,
    30  				Optional: true,
    31  			},
    32  
    33  			"smtp_password": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				ForceNew: true,
    36  				Required: true,
    37  			},
    38  
    39  			"smtp_login": &schema.Schema{
    40  				Type:     schema.TypeString,
    41  				Computed: true,
    42  				Optional: true,
    43  			},
    44  
    45  			"wildcard": &schema.Schema{
    46  				Type:     schema.TypeBool,
    47  				Computed: true,
    48  				ForceNew: true,
    49  				Optional: true,
    50  			},
    51  
    52  			"receiving_records": &schema.Schema{
    53  				Type:     schema.TypeList,
    54  				Computed: true,
    55  				Elem: &schema.Resource{
    56  					Schema: map[string]*schema.Schema{
    57  						"priority": &schema.Schema{
    58  							Type:     schema.TypeString,
    59  							Computed: true,
    60  						},
    61  						"record_type": &schema.Schema{
    62  							Type:     schema.TypeString,
    63  							Computed: true,
    64  						},
    65  						"valid": &schema.Schema{
    66  							Type:     schema.TypeString,
    67  							Computed: true,
    68  						},
    69  						"value": &schema.Schema{
    70  							Type:     schema.TypeString,
    71  							Computed: true,
    72  						},
    73  					},
    74  				},
    75  			},
    76  
    77  			"sending_records": &schema.Schema{
    78  				Type:     schema.TypeList,
    79  				Computed: true,
    80  				Elem: &schema.Resource{
    81  					Schema: map[string]*schema.Schema{
    82  						"name": &schema.Schema{
    83  							Type:     schema.TypeString,
    84  							Computed: true,
    85  						},
    86  						"record_type": &schema.Schema{
    87  							Type:     schema.TypeString,
    88  							Computed: true,
    89  						},
    90  						"valid": &schema.Schema{
    91  							Type:     schema.TypeString,
    92  							Computed: true,
    93  						},
    94  						"value": &schema.Schema{
    95  							Type:     schema.TypeString,
    96  							Computed: true,
    97  						},
    98  					},
    99  				},
   100  			},
   101  		},
   102  	}
   103  }
   104  
   105  func resourceMailgunDomainCreate(d *schema.ResourceData, meta interface{}) error {
   106  	client := meta.(*mailgun.Client)
   107  
   108  	opts := mailgun.CreateDomain{}
   109  
   110  	opts.Name = d.Get("name").(string)
   111  	opts.SmtpPassword = d.Get("smtp_password").(string)
   112  	opts.SpamAction = d.Get("spam_action").(string)
   113  	opts.Wildcard = d.Get("wildcard").(bool)
   114  
   115  	log.Printf("[DEBUG] Domain create configuration: %#v", opts)
   116  
   117  	domain, err := client.CreateDomain(&opts)
   118  
   119  	if err != nil {
   120  		return err
   121  	}
   122  
   123  	d.SetId(domain)
   124  
   125  	log.Printf("[INFO] Domain ID: %s", d.Id())
   126  
   127  	// Retrieve and update state of domain
   128  	_, err = resourceMailginDomainRetrieve(d.Id(), client, d)
   129  
   130  	if err != nil {
   131  		return err
   132  	}
   133  
   134  	return nil
   135  }
   136  
   137  func resourceMailgunDomainDelete(d *schema.ResourceData, meta interface{}) error {
   138  	client := meta.(*mailgun.Client)
   139  
   140  	log.Printf("[INFO] Deleting Domain: %s", d.Id())
   141  
   142  	// Destroy the domain
   143  	err := client.DestroyDomain(d.Id())
   144  	if err != nil {
   145  		return fmt.Errorf("Error deleting domain: %s", err)
   146  	}
   147  
   148  	// Give the destroy a chance to take effect
   149  	return resource.Retry(1*time.Minute, func() *resource.RetryError {
   150  		_, err = client.RetrieveDomain(d.Id())
   151  		if err == nil {
   152  			log.Printf("[INFO] Retrying until domain disappears...")
   153  			return resource.RetryableError(
   154  				fmt.Errorf("Domain seems to still exist; will check again."))
   155  		}
   156  		log.Printf("[INFO] Got error looking for domain, seems gone: %s", err)
   157  		return nil
   158  	})
   159  }
   160  
   161  func resourceMailgunDomainRead(d *schema.ResourceData, meta interface{}) error {
   162  	client := meta.(*mailgun.Client)
   163  
   164  	_, err := resourceMailginDomainRetrieve(d.Id(), client, d)
   165  
   166  	if err != nil {
   167  		return err
   168  	}
   169  
   170  	return nil
   171  }
   172  
   173  func resourceMailginDomainRetrieve(id string, client *mailgun.Client, d *schema.ResourceData) (*mailgun.DomainResponse, error) {
   174  	resp, err := client.RetrieveDomain(id)
   175  
   176  	if err != nil {
   177  		return nil, fmt.Errorf("Error retrieving domain: %s", err)
   178  	}
   179  
   180  	d.Set("name", resp.Domain.Name)
   181  	d.Set("smtp_password", resp.Domain.SmtpPassword)
   182  	d.Set("smtp_login", resp.Domain.SmtpLogin)
   183  	d.Set("wildcard", resp.Domain.Wildcard)
   184  	d.Set("spam_action", resp.Domain.SpamAction)
   185  
   186  	receivingRecords := make([]map[string]interface{}, len(resp.ReceivingRecords))
   187  	for i, r := range resp.ReceivingRecords {
   188  		receivingRecords[i] = make(map[string]interface{})
   189  		receivingRecords[i]["priority"] = r.Priority
   190  		receivingRecords[i]["valid"] = r.Valid
   191  		receivingRecords[i]["value"] = r.Value
   192  		receivingRecords[i]["record_type"] = r.RecordType
   193  	}
   194  	d.Set("receiving_records", receivingRecords)
   195  
   196  	sendingRecords := make([]map[string]interface{}, len(resp.SendingRecords))
   197  	for i, r := range resp.SendingRecords {
   198  		sendingRecords[i] = make(map[string]interface{})
   199  		sendingRecords[i]["name"] = r.Name
   200  		sendingRecords[i]["valid"] = r.Valid
   201  		sendingRecords[i]["value"] = r.Value
   202  		sendingRecords[i]["record_type"] = r.RecordType
   203  	}
   204  	d.Set("sending_records", sendingRecords)
   205  
   206  	return &resp, nil
   207  }