Skip to content

Commit e3d54cb

Browse files
committed
providers/mailgun: initial commit
1 parent b7ec5cc commit e3d54cb

5 files changed

Lines changed: 386 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package mailgun
2+
3+
import (
4+
"log"
5+
"os"
6+
7+
"github.com/pearkes/mailgun"
8+
)
9+
10+
type Config struct {
11+
APIKey string `mapstructure:"api_key"`
12+
}
13+
14+
// Client() returns a new client for accessing mailgun.
15+
//
16+
func (c *Config) Client() (*mailgun.Client, error) {
17+
18+
// If we have env vars set (like in the acc) tests,
19+
// we need to override the values passed in here.
20+
if v := os.Getenv("MAILGUN_API_KEY"); v != "" {
21+
c.APIKey = v
22+
}
23+
24+
// We don't set a domain right away
25+
client, err := mailgun.NewClient(c.APIKey)
26+
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
log.Printf("[INFO] Mailgun Client configured ")
32+
33+
return client, nil
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package mailgun
2+
3+
import (
4+
"log"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
"github.com/mitchellh/mapstructure"
8+
)
9+
10+
// Provider returns a terraform.ResourceProvider.
11+
func Provider() *schema.Provider {
12+
return &schema.Provider{
13+
Schema: map[string]*schema.Schema{
14+
"api_key": &schema.Schema{
15+
Type: schema.TypeString,
16+
Optional: false,
17+
},
18+
19+
"public_api_key": &schema.Schema{
20+
Type: schema.TypeString,
21+
Optional: false,
22+
},
23+
},
24+
25+
ResourcesMap: map[string]*schema.Resource{
26+
"mailgun_domain": resourceMailgunDomain(),
27+
},
28+
29+
ConfigureFunc: providerConfigure,
30+
}
31+
}
32+
33+
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
34+
var config Config
35+
configRaw := d.Get("").(map[string]interface{})
36+
if err := mapstructure.Decode(configRaw, &config); err != nil {
37+
return nil, err
38+
}
39+
40+
log.Println("[INFO] Initializing Mailgun client")
41+
42+
return config.Client()
43+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package mailgun
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/config"
8+
"github.com/hashicorp/terraform/helper/schema"
9+
"github.com/hashicorp/terraform/terraform"
10+
"github.com/pearkes/mailgun"
11+
)
12+
13+
var testAccProviders map[string]terraform.ResourceProvider
14+
var testAccProvider *schema.Provider
15+
16+
func init() {
17+
testAccProvider = Provider()
18+
testAccProviders = map[string]terraform.ResourceProvider{
19+
"mailgun": testAccProvider,
20+
}
21+
}
22+
23+
func TestProvider(t *testing.T) {
24+
if err := Provider().InternalValidate(); err != nil {
25+
t.Fatalf("err: %s", err)
26+
}
27+
}
28+
29+
func TestProvider_impl(t *testing.T) {
30+
var _ terraform.ResourceProvider = Provider()
31+
}
32+
33+
func TestProviderConfigure(t *testing.T) {
34+
var expectedKey string
35+
36+
if v := os.Getenv("MAILGUN_API_KEY"); v != "" {
37+
expectedKey = v
38+
} else {
39+
expectedKey = "foo"
40+
}
41+
42+
raw := map[string]interface{}{
43+
"api_key": expectedKey,
44+
}
45+
46+
rawConfig, err := config.NewRawConfig(raw)
47+
if err != nil {
48+
t.Fatalf("err: %s", err)
49+
}
50+
51+
rp := Provider()
52+
err = rp.Configure(terraform.NewResourceConfig(rawConfig))
53+
if err != nil {
54+
t.Fatalf("err: %s", err)
55+
}
56+
57+
config := rp.Meta().(*mailgun.Client)
58+
if config.ApiKey != expectedKey {
59+
t.Fatalf("bad: %#v", config)
60+
}
61+
}
62+
63+
func testAccPreCheck(t *testing.T) {
64+
if v := os.Getenv("MAILGUN_API_KEY"); v == "" {
65+
t.Fatal("MAILGUN_API_KEY must be set for acceptance tests")
66+
}
67+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package mailgun
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform/helper/schema"
8+
"github.com/pearkes/mailgun"
9+
)
10+
11+
func resourceMailgunDomain() *schema.Resource {
12+
return &schema.Resource{
13+
Create: resourceMailgunDomainCreate,
14+
Read: resourceMailgunDomainRead,
15+
Delete: resourceMailgunDomainDelete,
16+
17+
Schema: map[string]*schema.Schema{
18+
"name": &schema.Schema{
19+
Type: schema.TypeString,
20+
Required: true,
21+
ForceNew: true,
22+
},
23+
24+
"spam_action": &schema.Schema{
25+
Type: schema.TypeString,
26+
Computed: true,
27+
ForceNew: true,
28+
Optional: true,
29+
},
30+
31+
"smtp_password": &schema.Schema{
32+
Type: schema.TypeString,
33+
Computed: true,
34+
ForceNew: true,
35+
Optional: true,
36+
},
37+
38+
"smtp_login": &schema.Schema{
39+
Type: schema.TypeString,
40+
Computed: true,
41+
Optional: true,
42+
},
43+
44+
"wildcard": &schema.Schema{
45+
Type: schema.TypeBool,
46+
Computed: true,
47+
ForceNew: true,
48+
Optional: true,
49+
},
50+
},
51+
}
52+
}
53+
54+
func resourceMailgunDomainCreate(d *schema.ResourceData, meta interface{}) error {
55+
client := meta.(*mailgun.Client)
56+
57+
opts := mailgun.CreateDomain{}
58+
59+
opts.Name = d.Get("name").(string)
60+
opts.SmtpPassword = d.Get("smtp_password").(string)
61+
opts.SpamAction = d.Get("spam_action").(string)
62+
opts.Wildcard = d.Get("wildcard").(bool)
63+
64+
log.Printf("[DEBUG] Domain create configuration: %#v", opts)
65+
66+
domain, err := client.CreateDomain(&opts)
67+
68+
if err != nil {
69+
return err
70+
}
71+
72+
d.SetId(domain)
73+
74+
log.Printf("[INFO] Domain ID: %s", d.Id())
75+
76+
// Retrieve and update state of domain
77+
_, err = resource_mailgin_domain_retrieve(d.Id(), client, d)
78+
79+
if err != nil {
80+
return err
81+
}
82+
83+
return nil
84+
}
85+
86+
func resourceMailgunDomainDelete(d *schema.ResourceData, meta interface{}) error {
87+
client := meta.(*mailgun.Client)
88+
89+
log.Printf("[INFO] Deleting Domain: %s", d.Id())
90+
91+
// Destroy the domain
92+
err := client.DestroyDomain(d.Id())
93+
if err != nil {
94+
return fmt.Errorf("Error deleting domain: %s", err)
95+
}
96+
97+
return nil
98+
}
99+
100+
func resourceMailgunDomainRead(d *schema.ResourceData, meta interface{}) error {
101+
client := meta.(*mailgun.Client)
102+
103+
_, err := resource_mailgin_domain_retrieve(d.Id(), client, d)
104+
105+
if err != nil {
106+
return err
107+
}
108+
109+
return nil
110+
}
111+
112+
func resource_mailgin_domain_retrieve(id string, client *mailgun.Client, d *schema.ResourceData) (*mailgun.Domain, error) {
113+
domain, err := client.RetrieveDomain(id)
114+
115+
if err != nil {
116+
return nil, fmt.Errorf("Error retrieving domain: %s", err)
117+
}
118+
119+
d.Set("name", domain.Name)
120+
d.Set("smtp_password", domain.SmtpPassword)
121+
d.Set("smtp_login", domain.SmtpLogin)
122+
d.Set("wildcard", domain.Wildcard)
123+
d.Set("spam_action", domain.SpamAction)
124+
125+
return &domain, nil
126+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package mailgun
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
"github.com/pearkes/mailgun"
10+
)
11+
12+
func TestAccMailgunDomain_Basic(t *testing.T) {
13+
var domain mailgun.Domain
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
CheckDestroy: testAccCheckMailgunDomainDestroy,
19+
Steps: []resource.TestStep{
20+
resource.TestStep{
21+
Config: testAccCheckMailgunDomainConfig_basic,
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccCheckMailgunDomainExists("mailgun_domain.foobar", &domain),
24+
testAccCheckMailgunDomainAttributes(&domain),
25+
resource.TestCheckResourceAttr(
26+
"mailgun_domain.foobar", "name", "terraform.example.com"),
27+
resource.TestCheckResourceAttr(
28+
"mailgun_domain.foobar", "spam_action", "disabled"),
29+
resource.TestCheckResourceAttr(
30+
"mailgun_domain.foobar", "smtp_password", "foobar"),
31+
resource.TestCheckResourceAttr(
32+
"mailgun_domain.foobar", "wildcard", "true"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccCheckMailgunDomainDestroy(s *terraform.State) error {
40+
client := testAccProvider.Meta().(*mailgun.Client)
41+
42+
for _, rs := range s.Resources {
43+
if rs.Type != "mailgun_domain" {
44+
continue
45+
}
46+
47+
_, err := client.RetrieveDomain(rs.ID)
48+
49+
if err == nil {
50+
return fmt.Errorf("Domain still exists")
51+
}
52+
}
53+
54+
return nil
55+
}
56+
57+
func testAccCheckMailgunDomainAttributes(Domain *mailgun.Domain) resource.TestCheckFunc {
58+
return func(s *terraform.State) error {
59+
60+
if Domain.Name != "terraform.example.com" {
61+
return fmt.Errorf("Bad name: %s", Domain.Name)
62+
}
63+
64+
if Domain.SpamAction != "disabled" {
65+
return fmt.Errorf("Bad spam_action: %s", Domain.SpamAction)
66+
}
67+
68+
if Domain.Wildcard != true {
69+
return fmt.Errorf("Bad wildcard: %s", Domain.SpamAction)
70+
}
71+
72+
if Domain.SmtpPassword != "foobar" {
73+
return fmt.Errorf("Bad smtp_password: %s", Domain.SmtpPassword)
74+
}
75+
76+
return nil
77+
}
78+
}
79+
80+
func testAccCheckMailgunDomainExists(n string, Domain *mailgun.Domain) resource.TestCheckFunc {
81+
return func(s *terraform.State) error {
82+
rs, ok := s.Resources[n]
83+
84+
if !ok {
85+
return fmt.Errorf("Not found: %s", n)
86+
}
87+
88+
if rs.ID == "" {
89+
return fmt.Errorf("No Domain ID is set")
90+
}
91+
92+
client := testAccProvider.Meta().(*mailgun.Client)
93+
94+
foundDomain, err := client.RetrieveDomain(rs.ID)
95+
96+
if err != nil {
97+
return err
98+
}
99+
100+
if foundDomain.Name != rs.ID {
101+
return fmt.Errorf("Domain not found")
102+
}
103+
104+
*Domain = foundDomain
105+
106+
return nil
107+
}
108+
}
109+
110+
const testAccCheckMailgunDomainConfig_basic = `
111+
resource "mailgun_domain" "foobar" {
112+
name = "terraform.example.com"
113+
spam_action = "disabled"
114+
smtp_password = "foobar"
115+
wildcard = true
116+
}`

0 commit comments

Comments
 (0)