forked from gophercloud/gophercloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
175 lines (139 loc) · 4.82 KB
/
dns.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
package v2
import (
"testing"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/dns/v2/recordsets"
"github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
th "github.com/gophercloud/gophercloud/testhelper"
)
// CreateRecordSet will create a RecordSet with a random name. An error will
// be returned if the zone was unable to be created.
func CreateRecordSet(t *testing.T, client *gophercloud.ServiceClient, zone *zones.Zone) (*recordsets.RecordSet, error) {
t.Logf("Attempting to create recordset: %s", zone.Name)
createOpts := recordsets.CreateOpts{
Name: zone.Name,
Type: "A",
TTL: 3600,
Description: "Test recordset",
Records: []string{"10.1.0.2"},
}
rs, err := recordsets.Create(client, zone.ID, createOpts).Extract()
if err != nil {
return rs, err
}
if err := WaitForRecordSetStatus(client, rs, "ACTIVE"); err != nil {
return rs, err
}
newRS, err := recordsets.Get(client, rs.ZoneID, rs.ID).Extract()
if err != nil {
return newRS, err
}
t.Logf("Created record set: %s", newRS.Name)
th.AssertEquals(t, newRS.Name, zone.Name)
return rs, nil
}
// CreateZone will create a Zone with a random name. An error will
// be returned if the zone was unable to be created.
func CreateZone(t *testing.T, client *gophercloud.ServiceClient) (*zones.Zone, error) {
zoneName := tools.RandomString("ACPTTEST", 8) + ".com."
t.Logf("Attempting to create zone: %s", zoneName)
createOpts := zones.CreateOpts{
Name: zoneName,
Email: "root@example.com",
Type: "PRIMARY",
TTL: 7200,
Description: "Test zone",
}
zone, err := zones.Create(client, createOpts).Extract()
if err != nil {
return zone, err
}
if err := WaitForZoneStatus(client, zone, "ACTIVE"); err != nil {
return zone, err
}
newZone, err := zones.Get(client, zone.ID).Extract()
if err != nil {
return zone, err
}
t.Logf("Created Zone: %s", zoneName)
th.AssertEquals(t, newZone.Name, zoneName)
th.AssertEquals(t, newZone.TTL, 7200)
return newZone, nil
}
// CreateSecondaryZone will create a Zone with a random name. An error will
// be returned if the zone was unable to be created.
//
// This is only for example purposes as it will try to do a zone transfer.
func CreateSecondaryZone(t *testing.T, client *gophercloud.ServiceClient) (*zones.Zone, error) {
zoneName := tools.RandomString("ACPTTEST", 8) + ".com."
t.Logf("Attempting to create zone: %s", zoneName)
createOpts := zones.CreateOpts{
Name: zoneName,
Type: "SECONDARY",
Masters: []string{"10.0.0.1"},
}
zone, err := zones.Create(client, createOpts).Extract()
if err != nil {
return zone, err
}
if err := WaitForZoneStatus(client, zone, "ACTIVE"); err != nil {
return zone, err
}
newZone, err := zones.Get(client, zone.ID).Extract()
if err != nil {
return zone, err
}
t.Logf("Created Zone: %s", zoneName)
th.AssertEquals(t, newZone.Name, zoneName)
th.AssertEquals(t, newZone.Masters[0], "10.0.0.1")
return newZone, nil
}
// DeleteRecordSet will delete a specified record set. A fatal error will occur if
// the record set failed to be deleted. This works best when used as a deferred
// function.
func DeleteRecordSet(t *testing.T, client *gophercloud.ServiceClient, rs *recordsets.RecordSet) {
err := recordsets.Delete(client, rs.ZoneID, rs.ID).ExtractErr()
if err != nil {
t.Fatalf("Unable to delete record set %s: %v", rs.ID, err)
}
t.Logf("Deleted record set: %s", rs.ID)
}
// DeleteZone will delete a specified zone. A fatal error will occur if
// the zone failed to be deleted. This works best when used as a deferred
// function.
func DeleteZone(t *testing.T, client *gophercloud.ServiceClient, zone *zones.Zone) {
_, err := zones.Delete(client, zone.ID).Extract()
if err != nil {
t.Fatalf("Unable to delete zone %s: %v", zone.ID, err)
}
t.Logf("Deleted zone: %s", zone.ID)
}
// WaitForRecordSetStatus will poll a record set's status until it either matches
// the specified status or the status becomes ERROR.
func WaitForRecordSetStatus(client *gophercloud.ServiceClient, rs *recordsets.RecordSet, status string) error {
return gophercloud.WaitFor(600, func() (bool, error) {
current, err := recordsets.Get(client, rs.ZoneID, rs.ID).Extract()
if err != nil {
return false, err
}
if current.Status == status {
return true, nil
}
return false, nil
})
}
// WaitForZoneStatus will poll a zone's status until it either matches
// the specified status or the status becomes ERROR.
func WaitForZoneStatus(client *gophercloud.ServiceClient, zone *zones.Zone, status string) error {
return gophercloud.WaitFor(600, func() (bool, error) {
current, err := zones.Get(client, zone.ID).Extract()
if err != nil {
return false, err
}
if current.Status == status {
return true, nil
}
return false, nil
})
}