@@ -8,8 +8,89 @@ import (
8
8
9
9
"github.com/hashicorp/terraform/helper/resource"
10
10
"github.com/hashicorp/terraform/terraform"
11
+ "log"
12
+ "strings"
13
+ "time"
11
14
)
12
15
16
+ const (
17
+ test_prefix = "testacc-terraform"
18
+ )
19
+
20
+ func init () {
21
+ resource .AddTestSweepers ("ovh_domain_zone_record" , & resource.Sweeper {
22
+ Name : "ovh_domain_zone_record" ,
23
+ F : testSweepDomainZoneRecord ,
24
+ })
25
+ }
26
+
27
+ func testSweepDomainZoneRecord (region string ) error {
28
+ client , err := sharedClientForRegion (region )
29
+ if err != nil {
30
+ return fmt .Errorf ("error getting client: %s" , err )
31
+ }
32
+
33
+ zoneName := os .Getenv ("OVH_ZONE" )
34
+ if zoneName == "" {
35
+ return fmt .Errorf ("OVH_ZONE must be set" )
36
+ }
37
+
38
+ dz := & DomainZone {}
39
+
40
+ if err := client .Get (fmt .Sprintf ("/domain/zone/%s" , zoneName ), & dz ); err != nil {
41
+ return fmt .Errorf ("Error calling /domain/zone/%s:\n \t %q" , zoneName , err )
42
+ }
43
+
44
+ records := make ([]int64 , 0 )
45
+ if err := client .Get (fmt .Sprintf ("/domain/zone/%s/record" , zoneName ), & records ); err != nil {
46
+ return fmt .Errorf ("Error calling /domain/zone/%s:\n \t %q" , zoneName , err )
47
+ }
48
+
49
+ if len (records ) == 0 {
50
+ log .Print ("[DEBUG] No record to sweep" )
51
+ return nil
52
+ }
53
+
54
+ for _ , rec := range records {
55
+ record := & OvhDomainZoneRecord {}
56
+
57
+ if err := client .Get (fmt .Sprintf ("/domain/zone/%s/record/%v" , zoneName , rec ), & record ); err != nil {
58
+ return fmt .Errorf ("Error calling /domain/zone/%s/record/%v:\n \t %q" , zoneName , rec , err )
59
+ }
60
+
61
+ if ! strings .HasPrefix (record .SubDomain , test_prefix ) {
62
+ continue
63
+ }
64
+
65
+ err = resource .Retry (5 * time .Minute , func () * resource.RetryError {
66
+ if err := client .Delete (fmt .Sprintf ("/domain/zone/%s/record/%v" , zoneName , rec ), nil ); err != nil {
67
+ return resource .RetryableError (err )
68
+ }
69
+ // Successful delete
70
+ return nil
71
+ })
72
+ if err != nil {
73
+ return err
74
+ }
75
+ }
76
+
77
+ err = resource .Retry (5 * time .Minute , func () * resource.RetryError {
78
+ err := client .Post (
79
+ fmt .Sprintf ("/domain/zone/%s/refresh" , zoneName ),
80
+ nil ,
81
+ nil ,
82
+ )
83
+
84
+ if err != nil {
85
+ return resource .RetryableError (fmt .Errorf ("Error refresh OVH Zone: %s" , err ))
86
+ }
87
+ // Successful refresh
88
+ return nil
89
+ })
90
+
91
+ return nil
92
+ }
93
+
13
94
func TestAccOvhDomainZoneRecord_Basic (t * testing.T ) {
14
95
var record OvhDomainZoneRecord
15
96
zone := os .Getenv ("OVH_ZONE" )
@@ -20,12 +101,11 @@ func TestAccOvhDomainZoneRecord_Basic(t *testing.T) {
20
101
CheckDestroy : testAccCheckOvhDomainZoneRecordDestroy ,
21
102
Steps : []resource.TestStep {
22
103
resource.TestStep {
23
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_basic , zone ),
104
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_basic , zone , test_prefix ),
24
105
Check : resource .ComposeTestCheckFunc (
25
106
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
26
- testAccCheckOvhDomainZoneRecordAttributes (& record ),
27
107
resource .TestCheckResourceAttr (
28
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform" ),
108
+ "ovh_domain_zone_record.foobar" , "subdomain" , test_prefix ),
29
109
resource .TestCheckResourceAttr (
30
110
"ovh_domain_zone_record.foobar" , "zone" , zone ),
31
111
resource .TestCheckResourceAttr (
@@ -48,12 +128,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
48
128
CheckDestroy : testAccCheckOvhDomainZoneRecordDestroy ,
49
129
Steps : []resource.TestStep {
50
130
resource.TestStep {
51
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_basic , zone ),
131
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_basic , zone , test_prefix ),
52
132
Check : resource .ComposeTestCheckFunc (
53
133
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
54
- testAccCheckOvhDomainZoneRecordAttributes (& record ),
55
134
resource .TestCheckResourceAttr (
56
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform" ),
135
+ "ovh_domain_zone_record.foobar" , "subdomain" , test_prefix ),
57
136
resource .TestCheckResourceAttr (
58
137
"ovh_domain_zone_record.foobar" , "zone" , zone ),
59
138
resource .TestCheckResourceAttr (
@@ -63,12 +142,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
63
142
),
64
143
},
65
144
resource.TestStep {
66
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_1 , zone ),
145
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_1 , zone , test_prefix ),
67
146
Check : resource .ComposeTestCheckFunc (
68
147
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
69
- testAccCheckOvhDomainZoneRecordAttributesUpdated_1 (& record ),
70
148
resource .TestCheckResourceAttr (
71
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform" ),
149
+ "ovh_domain_zone_record.foobar" , "subdomain" , test_prefix ),
72
150
resource .TestCheckResourceAttr (
73
151
"ovh_domain_zone_record.foobar" , "zone" , zone ),
74
152
resource .TestCheckResourceAttr (
@@ -78,12 +156,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
78
156
),
79
157
},
80
158
resource.TestStep {
81
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_2 , zone ),
159
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_2 , zone , test_prefix ),
82
160
Check : resource .ComposeTestCheckFunc (
83
161
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
84
- testAccCheckOvhDomainZoneRecordAttributesUpdated_2 (& record ),
85
162
resource .TestCheckResourceAttr (
86
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform2" ),
163
+ "ovh_domain_zone_record.foobar" , "subdomain" , fmt . Sprintf ( "%s2" , test_prefix ) ),
87
164
resource .TestCheckResourceAttr (
88
165
"ovh_domain_zone_record.foobar" , "zone" , zone ),
89
166
resource .TestCheckResourceAttr (
@@ -93,12 +170,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
93
170
),
94
171
},
95
172
resource.TestStep {
96
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_3 , zone ),
173
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_3 , zone , test_prefix ),
97
174
Check : resource .ComposeTestCheckFunc (
98
175
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
99
- testAccCheckOvhDomainZoneRecordAttributesUpdated_3 (& record ),
100
176
resource .TestCheckResourceAttr (
101
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform3" ),
177
+ "ovh_domain_zone_record.foobar" , "subdomain" , fmt . Sprintf ( "%s3" , test_prefix ) ),
102
178
resource .TestCheckResourceAttr (
103
179
"ovh_domain_zone_record.foobar" , "zone" , zone ),
104
180
resource .TestCheckResourceAttr (
@@ -166,65 +242,10 @@ func testAccCheckOvhDomainZoneRecordExists(n string, record *OvhDomainZoneRecord
166
242
}
167
243
}
168
244
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
-
224
245
const testAccCheckOvhDomainZoneRecordConfig_basic = `
225
246
resource "ovh_domain_zone_record" "foobar" {
226
247
zone = "%s"
227
- subdomain = "terraform "
248
+ subdomain = "%s "
228
249
target = "192.168.0.10"
229
250
fieldtype = "A"
230
251
ttl = 3600
@@ -233,7 +254,7 @@ resource "ovh_domain_zone_record" "foobar" {
233
254
const testAccCheckOvhDomainZoneRecordConfig_new_value_1 = `
234
255
resource "ovh_domain_zone_record" "foobar" {
235
256
zone = "%s"
236
- subdomain = "terraform "
257
+ subdomain = "%s "
237
258
target = "192.168.0.11"
238
259
fieldtype = "A"
239
260
ttl = 3600
@@ -242,7 +263,7 @@ resource "ovh_domain_zone_record" "foobar" {
242
263
const testAccCheckOvhDomainZoneRecordConfig_new_value_2 = `
243
264
resource "ovh_domain_zone_record" "foobar" {
244
265
zone = "%s"
245
- subdomain = "terraform2 "
266
+ subdomain = "%s2 "
246
267
target = "192.168.0.11"
247
268
fieldtype = "A"
248
269
ttl = 3600
@@ -251,7 +272,7 @@ resource "ovh_domain_zone_record" "foobar" {
251
272
const testAccCheckOvhDomainZoneRecordConfig_new_value_3 = `
252
273
resource "ovh_domain_zone_record" "foobar" {
253
274
zone = "%s"
254
- subdomain = "terraform3 "
275
+ subdomain = "%s3 "
255
276
target = "192.168.0.13"
256
277
fieldtype = "A"
257
278
ttl = 3604
0 commit comments