diff --git a/builtin/providers/mailgun/config.go b/builtin/providers/mailgun/config.go new file mode 100644 index 000000000..4c9d6d279 --- /dev/null +++ b/builtin/providers/mailgun/config.go @@ -0,0 +1,34 @@ +package mailgun + +import ( + "log" + "os" + + "github.com/pearkes/mailgun" +) + +type Config struct { + APIKey string `mapstructure:"api_key"` +} + +// Client() returns a new client for accessing mailgun. +// +func (c *Config) Client() (*mailgun.Client, error) { + + // If we have env vars set (like in the acc) tests, + // we need to override the values passed in here. + if v := os.Getenv("MAILGUN_API_KEY"); v != "" { + c.APIKey = v + } + + // We don't set a domain right away + client, err := mailgun.NewClient(c.APIKey) + + if err != nil { + return nil, err + } + + log.Printf("[INFO] Mailgun Client configured ") + + return client, nil +} diff --git a/builtin/providers/mailgun/provider.go b/builtin/providers/mailgun/provider.go new file mode 100644 index 000000000..25d29b343 --- /dev/null +++ b/builtin/providers/mailgun/provider.go @@ -0,0 +1,38 @@ +package mailgun + +import ( + "log" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/mitchellh/mapstructure" +) + +// Provider returns a terraform.ResourceProvider. +func Provider() *schema.Provider { + return &schema.Provider{ + Schema: map[string]*schema.Schema{ + "api_key": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + }, + + ResourcesMap: map[string]*schema.Resource{ + "mailgun_domain": resourceMailgunDomain(), + }, + + ConfigureFunc: providerConfigure, + } +} + +func providerConfigure(d *schema.ResourceData) (interface{}, error) { + var config Config + configRaw := d.Get("").(map[string]interface{}) + if err := mapstructure.Decode(configRaw, &config); err != nil { + return nil, err + } + + log.Println("[INFO] Initializing Mailgun client") + + return config.Client() +} diff --git a/builtin/providers/mailgun/provider_test.go b/builtin/providers/mailgun/provider_test.go new file mode 100644 index 000000000..85dd603ea --- /dev/null +++ b/builtin/providers/mailgun/provider_test.go @@ -0,0 +1,67 @@ +package mailgun + +import ( + "os" + "testing" + + "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" + "github.com/pearkes/mailgun" +) + +var testAccProviders map[string]terraform.ResourceProvider +var testAccProvider *schema.Provider + +func init() { + testAccProvider = Provider() + testAccProviders = map[string]terraform.ResourceProvider{ + "mailgun": testAccProvider, + } +} + +func TestProvider(t *testing.T) { + if err := Provider().InternalValidate(); err != nil { + t.Fatalf("err: %s", err) + } +} + +func TestProvider_impl(t *testing.T) { + var _ terraform.ResourceProvider = Provider() +} + +func TestProviderConfigure(t *testing.T) { + var expectedKey string + + if v := os.Getenv("MAILGUN_API_KEY"); v != "" { + expectedKey = v + } else { + expectedKey = "foo" + } + + raw := map[string]interface{}{ + "api_key": expectedKey, + } + + rawConfig, err := config.NewRawConfig(raw) + if err != nil { + t.Fatalf("err: %s", err) + } + + rp := Provider() + err = rp.Configure(terraform.NewResourceConfig(rawConfig)) + if err != nil { + t.Fatalf("err: %s", err) + } + + config := rp.Meta().(*mailgun.Client) + if config.ApiKey != expectedKey { + t.Fatalf("bad: %#v", config) + } +} + +func testAccPreCheck(t *testing.T) { + if v := os.Getenv("MAILGUN_API_KEY"); v == "" { + t.Fatal("MAILGUN_API_KEY must be set for acceptance tests") + } +} diff --git a/builtin/providers/mailgun/resource_mailgun_domain.go b/builtin/providers/mailgun/resource_mailgun_domain.go new file mode 100644 index 000000000..e4b9cc6cb --- /dev/null +++ b/builtin/providers/mailgun/resource_mailgun_domain.go @@ -0,0 +1,125 @@ +package mailgun + +import ( + "fmt" + "log" + + "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, + }, + }, + } +} + +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 = resource_mailgin_domain_retrieve(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) + } + + return nil +} + +func resourceMailgunDomainRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*mailgun.Client) + + _, err := resource_mailgin_domain_retrieve(d.Id(), client, d) + + if err != nil { + return err + } + + return nil +} + +func resource_mailgin_domain_retrieve(id string, client *mailgun.Client, d *schema.ResourceData) (*mailgun.Domain, error) { + domain, err := client.RetrieveDomain(id) + + if err != nil { + return nil, fmt.Errorf("Error retrieving domain: %s", err) + } + + d.Set("name", domain.Name) + d.Set("smtp_password", domain.SmtpPassword) + d.Set("smtp_login", domain.SmtpLogin) + d.Set("wildcard", domain.Wildcard) + d.Set("spam_action", domain.SpamAction) + + return &domain, nil +} diff --git a/builtin/providers/mailgun/resource_mailgun_domain_test.go b/builtin/providers/mailgun/resource_mailgun_domain_test.go new file mode 100644 index 000000000..c25fe230f --- /dev/null +++ b/builtin/providers/mailgun/resource_mailgun_domain_test.go @@ -0,0 +1,116 @@ +package mailgun + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/pearkes/mailgun" +) + +func TestAccMailgunDomain_Basic(t *testing.T) { + var domain mailgun.Domain + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckMailgunDomainDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckMailgunDomainConfig_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckMailgunDomainExists("mailgun_domain.foobar", &domain), + testAccCheckMailgunDomainAttributes(&domain), + resource.TestCheckResourceAttr( + "mailgun_domain.foobar", "name", "terraform.example.com"), + resource.TestCheckResourceAttr( + "mailgun_domain.foobar", "spam_action", "disabled"), + resource.TestCheckResourceAttr( + "mailgun_domain.foobar", "smtp_password", "foobar"), + resource.TestCheckResourceAttr( + "mailgun_domain.foobar", "wildcard", "true"), + ), + }, + }, + }) +} + +func testAccCheckMailgunDomainDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*mailgun.Client) + + for _, rs := range s.Resources { + if rs.Type != "mailgun_domain" { + continue + } + + _, err := client.RetrieveDomain(rs.ID) + + if err == nil { + return fmt.Errorf("Domain still exists") + } + } + + return nil +} + +func testAccCheckMailgunDomainAttributes(Domain *mailgun.Domain) resource.TestCheckFunc { + return func(s *terraform.State) error { + + if Domain.Name != "terraform.example.com" { + return fmt.Errorf("Bad name: %s", Domain.Name) + } + + if Domain.SpamAction != "disabled" { + return fmt.Errorf("Bad spam_action: %s", Domain.SpamAction) + } + + if Domain.Wildcard != true { + return fmt.Errorf("Bad wildcard: %s", Domain.Wildcard) + } + + if Domain.SmtpPassword != "foobar" { + return fmt.Errorf("Bad smtp_password: %s", Domain.SmtpPassword) + } + + return nil + } +} + +func testAccCheckMailgunDomainExists(n string, Domain *mailgun.Domain) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.Resources[n] + + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.ID == "" { + return fmt.Errorf("No Domain ID is set") + } + + client := testAccProvider.Meta().(*mailgun.Client) + + foundDomain, err := client.RetrieveDomain(rs.ID) + + if err != nil { + return err + } + + if foundDomain.Name != rs.ID { + return fmt.Errorf("Domain not found") + } + + *Domain = foundDomain + + return nil + } +} + +const testAccCheckMailgunDomainConfig_basic = ` +resource "mailgun_domain" "foobar" { + name = "terraform.example.com" + spam_action = "disabled" + smtp_password = "foobar" + wildcard = true +}` diff --git a/website/source/docs/providers/mailgun/index.html.markdown b/website/source/docs/providers/mailgun/index.html.markdown new file mode 100644 index 000000000..13c89923e --- /dev/null +++ b/website/source/docs/providers/mailgun/index.html.markdown @@ -0,0 +1,34 @@ +--- +layout: "mailgun" +page_title: "Provider: Mailgun" +sidebar_current: "docs-mailgun-index" +--- + +# Provider + +The Mailgun provider is used to interact with the +resources supported by Mailgun. The provider needs to be configured +with the proper credentials before it can be used. + +Use the navigation to the left to read about the available resources. + +## Example Usage + +``` +# Configure the Mailgun provider +provider "mailgun" { + api_key = "${var.mailgun_api_key}" +} + +# Create a new domain +resource "mailgun_domain" "default" { + ... +} +``` + +## Argument Reference + +The following arguments are supported: + +* `api_key` - (Required) Mailgun API key + diff --git a/website/source/docs/providers/mailgun/r/domain.html.markdown b/website/source/docs/providers/mailgun/r/domain.html.markdown new file mode 100644 index 000000000..d060b19b3 --- /dev/null +++ b/website/source/docs/providers/mailgun/r/domain.html.markdown @@ -0,0 +1,43 @@ +--- +layout: "mailgun" +page_title: "Mailgun: mailgun_domain" +sidebar_current: "docs-mailgun-resource-domain" +--- + +# mailgun\_domain + +Provides a Mailgun App resource. This can be used to +create and manage applications on Mailgun. + +## Example Usage + +``` +# Create a new mailgun domain +resource "mailgun_domain" "default" { + name = "test.example.com" + spam_action = "disabled" + smtp_password = "foobar" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The domain to add to Mailgun +* `smtp_password` - (Required) Password for SMTP authentication +* `spam_action` - (Optional) `disabled` or `tag` Disable, no spam + filtering will occur for inbound messages. Tag, messages + will be tagged wtih a spam header. +* `wildcard` - (Optional) Boolean determines whether + the domain will accept email for sub-domains. + +## Attributes Reference + +The following attributes are exported: + +* `name` - The name of the domain. +* `smtp_login` - The login email for the SMTP server. +* `smtp_password` - The password to the SMTP server. +* `wildcard` - Whether or not the domain will accept email for sub-domains. +* `spam_action` - The spam filtering setting. diff --git a/website/source/layouts/docs.erb b/website/source/layouts/docs.erb index 24ad5905f..efa809b5c 100644 --- a/website/source/layouts/docs.erb +++ b/website/source/layouts/docs.erb @@ -99,6 +99,10 @@