Skip to content

Commit 45c9546

Browse files
authored
Merge pull request #50 from yanndegat/add_sweeper
Add sweepers on ovh_domain_zone resources
2 parents 40053c2 + 716c36f commit 45c9546

4 files changed

+240
-88
lines changed

ovh/ovh_sweeper_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform/helper/resource"
9+
10+
"github.com/ovh/go-ovh/ovh"
11+
)
12+
13+
func TestMain(m *testing.M) {
14+
resource.TestMain(m)
15+
}
16+
17+
// sharedClientForRegion returns a common OVHClient setup needed for the sweeper
18+
// functions for a given region
19+
func sharedClientForRegion(region string) (*ovh.Client, error) {
20+
v := os.Getenv("OVH_ENDPOINT")
21+
if v == "" {
22+
return nil, fmt.Errorf("OVH_ENDPOINT must be set")
23+
}
24+
25+
v = os.Getenv("OVH_APPLICATION_KEY")
26+
if v == "" {
27+
return nil, fmt.Errorf("OVH_APPLICATION_KEY must be set")
28+
}
29+
30+
v = os.Getenv("OVH_APPLICATION_SECRET")
31+
if v == "" {
32+
return nil, fmt.Errorf("OVH_APPLICATION_SECRET must be set")
33+
}
34+
35+
v = os.Getenv("OVH_CONSUMER_KEY")
36+
if v == "" {
37+
return nil, fmt.Errorf("OVH_CONSUMER_KEY must be set")
38+
}
39+
40+
config := Config{
41+
Endpoint: os.Getenv("OVH_ENDPOINT"),
42+
ApplicationKey: os.Getenv("OVH_APPLICATION_KEY"),
43+
ApplicationSecret: os.Getenv("OVH_APPLICATION_SECRET"),
44+
ConsumerKey: os.Getenv("OVH_CONSUMER_KEY"),
45+
}
46+
47+
if err := config.loadAndValidate(); err != nil {
48+
return nil, fmt.Errorf("couln't load OVH Client: %s", err)
49+
50+
}
51+
52+
return config.OVHClient, nil
53+
}

ovh/resource_ovh_domain_zone_record_test.go

Lines changed: 91 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,85 @@ import (
88

99
"github.com/hashicorp/terraform/helper/resource"
1010
"github.com/hashicorp/terraform/terraform"
11+
"log"
12+
"strings"
13+
"time"
1114
)
1215

16+
func init() {
17+
resource.AddTestSweepers("ovh_domain_zone_record", &resource.Sweeper{
18+
Name: "ovh_domain_zone_record",
19+
F: testSweepDomainZoneRecord,
20+
})
21+
}
22+
23+
func testSweepDomainZoneRecord(region string) error {
24+
client, err := sharedClientForRegion(region)
25+
if err != nil {
26+
return fmt.Errorf("error getting client: %s", err)
27+
}
28+
29+
zoneName := os.Getenv("OVH_ZONE")
30+
if zoneName == "" {
31+
return fmt.Errorf("OVH_ZONE must be set")
32+
}
33+
34+
dz := &DomainZone{}
35+
36+
if err := client.Get(fmt.Sprintf("/domain/zone/%s", zoneName), &dz); err != nil {
37+
return fmt.Errorf("Error calling /domain/zone/%s:\n\t %q", zoneName, err)
38+
}
39+
40+
records := make([]int64, 0)
41+
if err := client.Get(fmt.Sprintf("/domain/zone/%s/record", zoneName), &records); err != nil {
42+
return fmt.Errorf("Error calling /domain/zone/%s:\n\t %q", zoneName, err)
43+
}
44+
45+
if len(records) == 0 {
46+
log.Print("[DEBUG] No record to sweep")
47+
return nil
48+
}
49+
50+
for _, rec := range records {
51+
record := &OvhDomainZoneRecord{}
52+
53+
if err := client.Get(fmt.Sprintf("/domain/zone/%s/record/%v", zoneName, rec), &record); err != nil {
54+
return fmt.Errorf("Error calling /domain/zone/%s/record/%v:\n\t %q", zoneName, rec, err)
55+
}
56+
57+
if !strings.HasPrefix(record.SubDomain, test_prefix) {
58+
continue
59+
}
60+
61+
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
62+
if err := client.Delete(fmt.Sprintf("/domain/zone/%s/record/%v", zoneName, rec), nil); err != nil {
63+
return resource.RetryableError(err)
64+
}
65+
// Successful delete
66+
return nil
67+
})
68+
if err != nil {
69+
return err
70+
}
71+
}
72+
73+
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
74+
err := client.Post(
75+
fmt.Sprintf("/domain/zone/%s/refresh", zoneName),
76+
nil,
77+
nil,
78+
)
79+
80+
if err != nil {
81+
return resource.RetryableError(fmt.Errorf("Error refresh OVH Zone: %s", err))
82+
}
83+
// Successful refresh
84+
return nil
85+
})
86+
87+
return nil
88+
}
89+
1390
func TestAccOvhDomainZoneRecord_Basic(t *testing.T) {
1491
var record OvhDomainZoneRecord
1592
zone := os.Getenv("OVH_ZONE")
@@ -20,12 +97,11 @@ func TestAccOvhDomainZoneRecord_Basic(t *testing.T) {
2097
CheckDestroy: testAccCheckOvhDomainZoneRecordDestroy,
2198
Steps: []resource.TestStep{
2299
resource.TestStep{
23-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_basic, zone),
100+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_basic, zone, test_prefix),
24101
Check: resource.ComposeTestCheckFunc(
25102
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
26-
testAccCheckOvhDomainZoneRecordAttributes(&record),
27103
resource.TestCheckResourceAttr(
28-
"ovh_domain_zone_record.foobar", "subdomain", "terraform"),
104+
"ovh_domain_zone_record.foobar", "subdomain", test_prefix),
29105
resource.TestCheckResourceAttr(
30106
"ovh_domain_zone_record.foobar", "zone", zone),
31107
resource.TestCheckResourceAttr(
@@ -48,12 +124,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
48124
CheckDestroy: testAccCheckOvhDomainZoneRecordDestroy,
49125
Steps: []resource.TestStep{
50126
resource.TestStep{
51-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_basic, zone),
127+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_basic, zone, test_prefix),
52128
Check: resource.ComposeTestCheckFunc(
53129
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
54-
testAccCheckOvhDomainZoneRecordAttributes(&record),
55130
resource.TestCheckResourceAttr(
56-
"ovh_domain_zone_record.foobar", "subdomain", "terraform"),
131+
"ovh_domain_zone_record.foobar", "subdomain", test_prefix),
57132
resource.TestCheckResourceAttr(
58133
"ovh_domain_zone_record.foobar", "zone", zone),
59134
resource.TestCheckResourceAttr(
@@ -63,12 +138,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
63138
),
64139
},
65140
resource.TestStep{
66-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_1, zone),
141+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_1, zone, test_prefix),
67142
Check: resource.ComposeTestCheckFunc(
68143
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
69-
testAccCheckOvhDomainZoneRecordAttributesUpdated_1(&record),
70144
resource.TestCheckResourceAttr(
71-
"ovh_domain_zone_record.foobar", "subdomain", "terraform"),
145+
"ovh_domain_zone_record.foobar", "subdomain", test_prefix),
72146
resource.TestCheckResourceAttr(
73147
"ovh_domain_zone_record.foobar", "zone", zone),
74148
resource.TestCheckResourceAttr(
@@ -78,12 +152,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
78152
),
79153
},
80154
resource.TestStep{
81-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_2, zone),
155+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_2, zone, test_prefix),
82156
Check: resource.ComposeTestCheckFunc(
83157
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
84-
testAccCheckOvhDomainZoneRecordAttributesUpdated_2(&record),
85158
resource.TestCheckResourceAttr(
86-
"ovh_domain_zone_record.foobar", "subdomain", "terraform2"),
159+
"ovh_domain_zone_record.foobar", "subdomain", fmt.Sprintf("%s2", test_prefix)),
87160
resource.TestCheckResourceAttr(
88161
"ovh_domain_zone_record.foobar", "zone", zone),
89162
resource.TestCheckResourceAttr(
@@ -93,12 +166,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
93166
),
94167
},
95168
resource.TestStep{
96-
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_3, zone),
169+
Config: fmt.Sprintf(testAccCheckOvhDomainZoneRecordConfig_new_value_3, zone, test_prefix),
97170
Check: resource.ComposeTestCheckFunc(
98171
testAccCheckOvhDomainZoneRecordExists("ovh_domain_zone_record.foobar", &record),
99-
testAccCheckOvhDomainZoneRecordAttributesUpdated_3(&record),
100172
resource.TestCheckResourceAttr(
101-
"ovh_domain_zone_record.foobar", "subdomain", "terraform3"),
173+
"ovh_domain_zone_record.foobar", "subdomain", fmt.Sprintf("%s3", test_prefix)),
102174
resource.TestCheckResourceAttr(
103175
"ovh_domain_zone_record.foobar", "zone", zone),
104176
resource.TestCheckResourceAttr(
@@ -166,65 +238,10 @@ func testAccCheckOvhDomainZoneRecordExists(n string, record *OvhDomainZoneRecord
166238
}
167239
}
168240

169-
func testAccCheckOvhDomainZoneRecordAttributes(record *OvhDomainZoneRecord) resource.TestCheckFunc {
170-
return func(s *terraform.State) error {
171-
172-
if record.Target != "192.168.0.10" {
173-
return fmt.Errorf("Bad content: %#v", record)
174-
}
175-
176-
return nil
177-
}
178-
}
179-
180-
func testAccCheckOvhDomainZoneRecordAttributesUpdated_1(record *OvhDomainZoneRecord) resource.TestCheckFunc {
181-
return func(s *terraform.State) error {
182-
183-
if record.Target != "192.168.0.11" {
184-
return fmt.Errorf("Bad content: %#v", record)
185-
}
186-
187-
return nil
188-
}
189-
}
190-
191-
func testAccCheckOvhDomainZoneRecordAttributesUpdated_2(record *OvhDomainZoneRecord) resource.TestCheckFunc {
192-
return func(s *terraform.State) error {
193-
194-
if record.Target != "192.168.0.11" {
195-
return fmt.Errorf("Bad content: %#v", record)
196-
}
197-
198-
if record.SubDomain != "terraform2" {
199-
return fmt.Errorf("Bad content: %#v", record)
200-
}
201-
202-
return nil
203-
}
204-
}
205-
206-
func testAccCheckOvhDomainZoneRecordAttributesUpdated_3(record *OvhDomainZoneRecord) resource.TestCheckFunc {
207-
return func(s *terraform.State) error {
208-
209-
if record.Target != "192.168.0.13" {
210-
return fmt.Errorf("Bad content: %#v", record)
211-
}
212-
213-
if record.SubDomain != "terraform3" {
214-
return fmt.Errorf("Bad content: %#v", record)
215-
}
216-
217-
if record.Ttl != 3604 {
218-
return fmt.Errorf("Bad content: %#v", record)
219-
}
220-
return nil
221-
}
222-
}
223-
224241
const testAccCheckOvhDomainZoneRecordConfig_basic = `
225242
resource "ovh_domain_zone_record" "foobar" {
226243
zone = "%s"
227-
subdomain = "terraform"
244+
subdomain = "%s"
228245
target = "192.168.0.10"
229246
fieldtype = "A"
230247
ttl = 3600
@@ -233,7 +250,7 @@ resource "ovh_domain_zone_record" "foobar" {
233250
const testAccCheckOvhDomainZoneRecordConfig_new_value_1 = `
234251
resource "ovh_domain_zone_record" "foobar" {
235252
zone = "%s"
236-
subdomain = "terraform"
253+
subdomain = "%s"
237254
target = "192.168.0.11"
238255
fieldtype = "A"
239256
ttl = 3600
@@ -242,7 +259,7 @@ resource "ovh_domain_zone_record" "foobar" {
242259
const testAccCheckOvhDomainZoneRecordConfig_new_value_2 = `
243260
resource "ovh_domain_zone_record" "foobar" {
244261
zone = "%s"
245-
subdomain = "terraform2"
262+
subdomain = "%s2"
246263
target = "192.168.0.11"
247264
fieldtype = "A"
248265
ttl = 3600
@@ -251,7 +268,7 @@ resource "ovh_domain_zone_record" "foobar" {
251268
const testAccCheckOvhDomainZoneRecordConfig_new_value_3 = `
252269
resource "ovh_domain_zone_record" "foobar" {
253270
zone = "%s"
254-
subdomain = "terraform3"
271+
subdomain = "%s3"
255272
target = "192.168.0.13"
256273
fieldtype = "A"
257274
ttl = 3604

0 commit comments

Comments
 (0)