forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_powerdns_record.go
147 lines (115 loc) · 3.32 KB
/
resource_powerdns_record.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package powerdns
import (
"log"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)
func resourcePDNSRecord() *schema.Resource {
return &schema.Resource{
Create: resourcePDNSRecordCreate,
Read: resourcePDNSRecordRead,
Delete: resourcePDNSRecordDelete,
Exists: resourcePDNSRecordExists,
Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ttl": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"records": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
ForceNew: true,
Set: schema.HashString,
},
},
}
}
func resourcePDNSRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
rrSet := ResourceRecordSet{
Name: d.Get("name").(string),
Type: d.Get("type").(string),
TTL: d.Get("ttl").(int),
}
zone := d.Get("zone").(string)
ttl := d.Get("ttl").(int)
recs := d.Get("records").(*schema.Set).List()
if len(recs) > 0 {
records := make([]Record, 0, len(recs))
for _, recContent := range recs {
records = append(records, Record{Name: rrSet.Name, Type: rrSet.Type, TTL: ttl, Content: recContent.(string)})
}
rrSet.Records = records
log.Printf("[DEBUG] Creating PowerDNS Record: %#v", rrSet)
recId, err := client.ReplaceRecordSet(zone, rrSet)
if err != nil {
return fmt.Errorf("Failed to create PowerDNS Record: %s", err)
}
d.SetId(recId)
log.Printf("[INFO] Created PowerDNS Record with ID: %s", d.Id())
} else {
log.Printf("[DEBUG] Deleting empty PowerDNS Record: %#v", rrSet)
err := client.DeleteRecordSet(zone, rrSet.Name, rrSet.Type)
if err != nil {
return fmt.Errorf("Failed to delete PowerDNS Record: %s", err)
}
d.SetId(rrSet.Id())
}
return resourcePDNSRecordRead(d, meta)
}
func resourcePDNSRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
log.Printf("[DEBUG] Reading PowerDNS Record: %s", d.Id())
records, err := client.ListRecordsByID(d.Get("zone").(string), d.Id())
if err != nil {
return fmt.Errorf("Couldn't fetch PowerDNS Record: %s", err)
}
recs := make([]string, 0, len(records))
for _, r := range records {
recs = append(recs, r.Content)
}
d.Set("records", recs)
if len(records) > 0 {
d.Set("ttl", records[0].TTL)
}
return nil
}
func resourcePDNSRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
log.Printf("[INFO] Deleting PowerDNS Record: %s", d.Id())
err := client.DeleteRecordSetByID(d.Get("zone").(string), d.Id())
if err != nil {
return fmt.Errorf("Error deleting PowerDNS Record: %s", err)
}
return nil
}
func resourcePDNSRecordExists(d *schema.ResourceData, meta interface{}) (bool, error) {
zone := d.Get("zone").(string)
name := d.Get("name").(string)
tpe := d.Get("type").(string)
log.Printf("[INFO] Checking existence of PowerDNS Record: %s, %s", name, tpe)
client := meta.(*Client)
exists, err := client.RecordExists(zone, name, tpe)
if err != nil {
return false, fmt.Errorf("Error checking PowerDNS Record: %s", err)
} else {
return exists, nil
}
}