forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_dns_record_set.go
181 lines (150 loc) · 4.11 KB
/
resource_dns_record_set.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package google
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/dns/v1"
)
func resourceDnsRecordSet() *schema.Resource {
return &schema.Resource{
Create: resourceDnsRecordSetCreate,
Read: resourceDnsRecordSetRead,
Delete: resourceDnsRecordSetDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"managed_zone": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ttl": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"rrdatas": &schema.Schema{
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
zone := d.Get("managed_zone").(string)
rrdatasCount := d.Get("rrdatas.#").(int)
// Build the change
chg := &dns.Change{
Additions: []*dns.ResourceRecordSet{
&dns.ResourceRecordSet{
Name: d.Get("name").(string),
Type: d.Get("type").(string),
Ttl: int64(d.Get("ttl").(int)),
Rrdatas: make([]string, rrdatasCount),
},
},
}
for i := 0; i < rrdatasCount; i++ {
rrdata := fmt.Sprintf("rrdatas.%d", i)
chg.Additions[0].Rrdatas[i] = d.Get(rrdata).(string)
}
log.Printf("[DEBUG] DNS Record create request: %#v", chg)
chg, err := config.clientDns.Changes.Create(config.Project, zone, chg).Do()
if err != nil {
return fmt.Errorf("Error creating DNS RecordSet: %s", err)
}
d.SetId(chg.Id)
w := &DnsChangeWaiter{
Service: config.clientDns,
Change: chg,
Project: config.Project,
ManagedZone: zone,
}
state := w.Conf()
state.Delay = 10 * time.Second
state.Timeout = 10 * time.Minute
state.MinTimeout = 2 * time.Second
_, err = state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Google DNS change: %s", err)
}
return resourceDnsRecordSetRead(d, meta)
}
func resourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
zone := d.Get("managed_zone").(string)
// name and type are effectively the 'key'
name := d.Get("name").(string)
dnsType := d.Get("type").(string)
resp, err := config.clientDns.ResourceRecordSets.List(
config.Project, zone).Name(name).Type(dnsType).Do()
if err != nil {
return fmt.Errorf("Error reading DNS RecordSet: %#v", err)
}
if len(resp.Rrsets) == 0 {
// The resource doesn't exist anymore
d.SetId("")
return nil
}
if len(resp.Rrsets) > 1 {
return fmt.Errorf("Only expected 1 record set, got %d", len(resp.Rrsets))
}
d.Set("ttl", resp.Rrsets[0].Ttl)
d.Set("rrdatas", resp.Rrsets[0].Rrdatas)
return nil
}
func resourceDnsRecordSetDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
zone := d.Get("managed_zone").(string)
rrdatasCount := d.Get("rrdatas.#").(int)
// Build the change
chg := &dns.Change{
Deletions: []*dns.ResourceRecordSet{
&dns.ResourceRecordSet{
Name: d.Get("name").(string),
Type: d.Get("type").(string),
Ttl: int64(d.Get("ttl").(int)),
Rrdatas: make([]string, rrdatasCount),
},
},
}
for i := 0; i < rrdatasCount; i++ {
rrdata := fmt.Sprintf("rrdatas.%d", i)
chg.Deletions[0].Rrdatas[i] = d.Get(rrdata).(string)
}
log.Printf("[DEBUG] DNS Record delete request: %#v", chg)
chg, err := config.clientDns.Changes.Create(config.Project, zone, chg).Do()
if err != nil {
return fmt.Errorf("Error deleting DNS RecordSet: %s", err)
}
w := &DnsChangeWaiter{
Service: config.clientDns,
Change: chg,
Project: config.Project,
ManagedZone: zone,
}
state := w.Conf()
state.Delay = 10 * time.Second
state.Timeout = 10 * time.Minute
state.MinTimeout = 2 * time.Second
_, err = state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Google DNS change: %s", err)
}
d.SetId("")
return nil
}