-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_domain_check.go
99 lines (85 loc) · 2.45 KB
/
data_source_domain_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package improvmx
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceDomainCheck() *schema.Resource {
return &schema.Resource{
Read: dataSourceDomainCheckRead,
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
Computed: false,
},
"records_are_valid": {
Type: schema.TypeBool,
Computed: true,
},
"record_mx_is_valid": {
Type: schema.TypeBool,
Computed: true,
},
"record_mx_expected_values": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"record_mx_actual_values": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"record_spf_is_valid": {
Type: schema.TypeBool,
Computed: true,
},
"record_spf_expected_value": {
Type: schema.TypeString,
Computed: true,
},
"record_spf_actual_value": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceDomainCheckRead(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
domainString := d.Get("domain").(string)
response := m.Client.GetDomainCheck(domainString)
log.Printf(
"[DEBUG] Got status code %v from ImprovMX API on Read for domain check %s, success: %v, errors: %v.",
response.Code,
domainString,
response.Success,
response.Errors,
)
if response.Code == 429 {
log.Printf("[DEBUG] Rate limit hit. Too many requests. Aborting.")
return fmt.Errorf("Rate limit hit. Too many requests. Aborting.")
}
if response.Code == 404 {
log.Printf("[DEBUG] Couldn't find the resource in ImprovMX. Aborting.")
return fmt.Errorf("HTTP response code %d, error text: %s", response.Code, response.Error)
}
if response.Success == false {
log.Printf("[DEBUG] Request was not successful. Aborting.")
return fmt.Errorf("HTTP response code %d, error text: %s", response.Code, response.Error)
}
d.Set("records_are_valid", response.Records.Valid)
d.Set("record_mx_is_valid", response.Records.Mx.Valid)
d.Set("record_mx_expected_values", response.Records.Mx.Expected)
d.Set("record_mx_actual_values", response.Records.Mx.Values)
d.Set("record_spf_is_valid", response.Records.Spf.Valid)
d.Set("record_spf_expected_value", response.Records.Spf.Expected)
d.Set("record_spf_actual_value", response.Records.Spf.Values)
d.SetId(domainString)
return nil
}