aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--mailgun/resource_mailgun_domain.go15
-rw-r--r--mailgun/resource_mailgun_domain_test.go218
2 files changed, 227 insertions, 6 deletions
diff --git a/mailgun/resource_mailgun_domain.go b/mailgun/resource_mailgun_domain.go
index 988b80c..45397cf 100644
--- a/mailgun/resource_mailgun_domain.go
+++ b/mailgun/resource_mailgun_domain.go
@@ -107,29 +107,29 @@ func resourceMailgunDomain() *schema.Resource {
107 "open_tracking_settings_active": &schema.Schema{ 107 "open_tracking_settings_active": &schema.Schema{
108 Type: schema.TypeBool, 108 Type: schema.TypeBool,
109 Optional: true, 109 Optional: true,
110 Computed: true, 110 Default: false,
111 }, 111 },
112 112
113 "click_tracking_settings_active": &schema.Schema{ 113 "click_tracking_settings_active": &schema.Schema{
114 Type: schema.TypeBool, 114 Type: schema.TypeBool,
115 Optional: true, 115 Optional: true,
116 Computed: true, 116 Default: false,
117 }, 117 },
118 118
119 "unsubscribe_tracking_settings_active": &schema.Schema{ 119 "unsubscribe_tracking_settings_active": &schema.Schema{
120 Type: schema.TypeBool, 120 Type: schema.TypeBool,
121 Optional: true, 121 Optional: true,
122 Computed: true, 122 Default: false,
123 }, 123 },
124 "unsubscribe_tracking_settings_html_footer": &schema.Schema{ 124 "unsubscribe_tracking_settings_html_footer": &schema.Schema{
125 Type: schema.TypeString, 125 Type: schema.TypeString,
126 Optional: true, 126 Optional: true,
127 Computed: true, 127 Default: "\n<br>\n<p><a href=\"%unsubscribe_url%\">unsubscribe</a></p>\n",
128 }, 128 },
129 "unsubscribe_tracking_settings_text_footer": &schema.Schema{ 129 "unsubscribe_tracking_settings_text_footer": &schema.Schema{
130 Type: schema.TypeString, 130 Type: schema.TypeString,
131 Optional: true, 131 Optional: true,
132 Computed: true, 132 Default: "\n\nTo unsubscribe click: <%unsubscribe_url%>\n\n",
133 }, 133 },
134 134
135 "require_tls": &schema.Schema{ 135 "require_tls": &schema.Schema{
@@ -425,9 +425,12 @@ func ReadDomain(d *schema.ResourceData, meta interface{}) error {
425 d.Set("unsubscribe_tracking_settings_html_footer", domainTracking.Unsubscribe.HTMLFooter) 425 d.Set("unsubscribe_tracking_settings_html_footer", domainTracking.Unsubscribe.HTMLFooter)
426 d.Set("unsubscribe_tracking_settings_text_footer", domainTracking.Unsubscribe.TextFooter) 426 d.Set("unsubscribe_tracking_settings_text_footer", domainTracking.Unsubscribe.TextFooter)
427 427
428 time.Sleep(20 * time.Second)
429
428 ipAddress, err := mg.ListDomainIPS(ctx) 430 ipAddress, err := mg.ListDomainIPS(ctx)
431
429 if err != nil { 432 if err != nil {
430 return fmt.Errorf("Error Getting mailgun domain ips for %s: Error: %s", d.Id(), err) 433 return fmt.Errorf("Error Getting mailgun domain ips1 for %s: Error: %s", d.Id(), err)
431 } 434 }
432 ips := make([]string, len(ipAddress)) 435 ips := make([]string, len(ipAddress))
433 for i, r := range ipAddress { 436 for i, r := range ipAddress {
diff --git a/mailgun/resource_mailgun_domain_test.go b/mailgun/resource_mailgun_domain_test.go
new file mode 100644
index 0000000..12e1725
--- /dev/null
+++ b/mailgun/resource_mailgun_domain_test.go
@@ -0,0 +1,218 @@
1package mailgun
2
3import (
4 "context"
5 "fmt"
6 "github.com/hashicorp/terraform/helper/resource"
7 "github.com/hashicorp/terraform/terraform"
8 "github.com/mailgun/mailgun-go"
9 "os"
10 "strconv"
11 "testing"
12 "time"
13)
14
15type fullDomain struct {
16 domainResponse mailgun.DomainResponse
17 domainConnection mailgun.DomainConnection
18 domainTracking mailgun.DomainTracking
19 ipAddress []string
20 credentials []mailgun.Credential
21}
22
23func getFullDomain(mg *mailgun.MailgunImpl, domainName string) (*fullDomain, error) {
24 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
25 defer cancel()
26 mg = mailgun.NewMailgun(domainName, mg.APIKey())
27
28 var domain fullDomain
29 var err error
30 domain.domainResponse, err = mg.GetDomain(ctx, domainName)
31 if err != nil {
32 return nil, fmt.Errorf("Error Getting mailgun domain Details for %s: Error: %s", domainName, err)
33 }
34
35 domain.domainConnection, err = mg.GetDomainConnection(ctx, domainName)
36 if err != nil {
37 return nil, fmt.Errorf("Error Getting mailgun domain connection Details for %s: Error: %s", domainName, err)
38 }
39
40 domain.domainTracking, err = mg.GetDomainTracking(ctx, domainName)
41 if err != nil {
42 return nil, fmt.Errorf("Error Getting mailgun domain tracking Details for %s: Error: %s", domainName, err)
43 }
44
45 ipAddress, err := mg.ListDomainIPS(ctx)
46 if err != nil {
47 return nil, fmt.Errorf("Error Getting mailgun domain ips2 for %s: Error: %s", domainName, err)
48 }
49 ips := make([]string, len(ipAddress))
50 for i, r := range ipAddress {
51 ips[i] = r.IP
52
53 }
54 domain.ipAddress = ips
55 domain.credentials, err = ListCredentials(domainName, mg.APIKey())
56 if err != nil {
57 return nil, fmt.Errorf("Error Getting mailgun credentials for %s: Error: %s", domainName, err)
58 }
59 return &domain, nil
60}
61
62func TestAccMailgunDomain_basic(t *testing.T) {
63 var domain fullDomain
64
65 resource.Test(t, resource.TestCase{
66 PreCheck: func() { testAccPreCheck(t) },
67 Providers: testAccProviders,
68 CheckDestroy: testAccDomainCheckDestroy(&domain),
69 Steps: []resource.TestStep{
70 {
71 Config: interpolateTerraformTemplateDomain(testAccDomainConfig_basic),
72 Check: resource.ComposeTestCheckFunc(
73 testAccDomainCheckExists("mailgun_domain.exemple", &domain),
74 testAccDomainCheckAttributes("mailgun_domain.exemple", &domain),
75 ),
76 },
77 },
78 })
79}
80
81func TestAccMailgunDomain_withUpdate(t *testing.T) {
82 var domain fullDomain
83
84 resource.Test(t, resource.TestCase{
85 PreCheck: func() { testAccPreCheck(t) },
86 Providers: testAccProviders,
87 CheckDestroy: testAccDomainCheckDestroy(&domain),
88 Steps: []resource.TestStep{
89 {
90 Config: interpolateTerraformTemplateDomain(testAccDomainConfig_basic),
91 Check: resource.ComposeTestCheckFunc(
92 testAccDomainCheckExists("mailgun_domain.exemple", &domain),
93 testAccDomainCheckAttributes("mailgun_domain.exemple", &domain),
94 ),
95 },
96
97 {
98 Config: interpolateTerraformTemplateDomain(testAccDomainConfig_update),
99 Check: resource.ComposeTestCheckFunc(
100 testAccDomainCheckExists("mailgun_domain.exemple", &domain),
101 testAccDomainCheckAttributes("mailgun_domain.exemple", &domain),
102 ),
103 },
104 },
105 })
106}
107
108func testAccDomainCheckExists(rn string, domain *fullDomain) resource.TestCheckFunc {
109 return func(s *terraform.State) error {
110 rs, ok := s.RootModule().Resources[rn]
111 if !ok {
112 return fmt.Errorf("resource not found: %s", rn)
113 }
114
115 if rs.Primary.ID == "" {
116 return fmt.Errorf("domainID not set")
117 }
118
119 mg := testAccProvider.Meta().(*mailgun.MailgunImpl)
120
121 domainId := rs.Primary.ID
122
123 gotDomain, err := getFullDomain(mg, domainId)
124 if err != nil {
125 return fmt.Errorf("error getting domain: %s", err)
126 }
127
128 *domain = *gotDomain
129
130 return nil
131 }
132}
133
134func testAccDomainCheckAttributes(rn string, domain *fullDomain) resource.TestCheckFunc {
135 return func(s *terraform.State) error {
136 attrs := s.RootModule().Resources[rn].Primary.Attributes
137
138 check := func(key, stateValue, domainValue string) error {
139 if domainValue != stateValue {
140 return fmt.Errorf("different values for %s in state (%s) and in mailgun (%s)",
141 key, stateValue, domainValue)
142 }
143 return nil
144 }
145
146 for key, value := range attrs {
147 var err error
148
149 switch key {
150 case "name":
151 err = check(key, value, domain.domainResponse.Domain.Name)
152 case "smtp_password":
153 err = check(key, value, domain.domainResponse.Domain.SMTPPassword)
154 case "smtp_login":
155 err = check(key, value, domain.domainResponse.Domain.SMTPLogin)
156 case "wildcard":
157 err = check(key, value, strconv.FormatBool(domain.domainResponse.Domain.Wildcard))
158 case "state":
159 err = check(key, value, domain.domainResponse.Domain.State)
160 case "open_tracking_settings_active":
161 err = check(key, value, strconv.FormatBool(domain.domainTracking.Open.Active))
162 case "click_tracking_settings_active":
163 err = check(key, value, strconv.FormatBool(domain.domainTracking.Click.Active))
164 case "unsubscribe_tracking_settings_active":
165 err = check(key, value, strconv.FormatBool(domain.domainTracking.Unsubscribe.Active))
166 case "unsubscribe_tracking_settings_html_footer":
167 err = check(key, value, domain.domainTracking.Unsubscribe.HTMLFooter)
168 case "unsubscribe_tracking_settings_text_footer":
169 err = check(key, value, domain.domainTracking.Unsubscribe.TextFooter)
170 case "skip_verification":
171 err = check(key, value, strconv.FormatBool(domain.domainConnection.SkipVerification))
172 case "require_tls":
173 err = check(key, value, strconv.FormatBool(domain.domainConnection.RequireTLS))
174 }
175 if err != nil {
176 return err
177 }
178 }
179 return nil
180 }
181}
182
183func testAccDomainCheckDestroy(domain *fullDomain) resource.TestCheckFunc {
184 return func(s *terraform.State) error {
185 mg := testAccProvider.Meta().(*mailgun.MailgunImpl)
186 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
187 defer cancel()
188
189 _, err := mg.GetDomain(ctx, domain.domainResponse.Domain.Name)
190 if err == nil {
191 return fmt.Errorf("domain still exists")
192 }
193
194 return nil
195 }
196}
197
198func interpolateTerraformTemplateDomain(template string) string {
199 domainName := ""
200
201 if v := os.Getenv("MAILGUN_DOMAIN"); v != "" {
202 domainName = v
203 }
204
205 return fmt.Sprintf(template, domainName)
206}
207
208const testAccDomainConfig_basic = `
209resource "mailgun_domain" "exemple" {
210 name="%s"
211}
212`
213
214const testAccDomainConfig_update = `
215resource "mailgun_domain" "exemple" {
216 name="%s"
217}
218`