@@ -8,8 +8,85 @@ 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
+ 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
+
13
90
func TestAccOvhDomainZoneRecord_Basic (t * testing.T ) {
14
91
var record OvhDomainZoneRecord
15
92
zone := os .Getenv ("OVH_ZONE" )
@@ -20,12 +97,11 @@ func TestAccOvhDomainZoneRecord_Basic(t *testing.T) {
20
97
CheckDestroy : testAccCheckOvhDomainZoneRecordDestroy ,
21
98
Steps : []resource.TestStep {
22
99
resource.TestStep {
23
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_basic , zone ),
100
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_basic , zone , test_prefix ),
24
101
Check : resource .ComposeTestCheckFunc (
25
102
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
26
- testAccCheckOvhDomainZoneRecordAttributes (& record ),
27
103
resource .TestCheckResourceAttr (
28
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform" ),
104
+ "ovh_domain_zone_record.foobar" , "subdomain" , test_prefix ),
29
105
resource .TestCheckResourceAttr (
30
106
"ovh_domain_zone_record.foobar" , "zone" , zone ),
31
107
resource .TestCheckResourceAttr (
@@ -48,12 +124,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
48
124
CheckDestroy : testAccCheckOvhDomainZoneRecordDestroy ,
49
125
Steps : []resource.TestStep {
50
126
resource.TestStep {
51
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_basic , zone ),
127
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_basic , zone , test_prefix ),
52
128
Check : resource .ComposeTestCheckFunc (
53
129
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
54
- testAccCheckOvhDomainZoneRecordAttributes (& record ),
55
130
resource .TestCheckResourceAttr (
56
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform" ),
131
+ "ovh_domain_zone_record.foobar" , "subdomain" , test_prefix ),
57
132
resource .TestCheckResourceAttr (
58
133
"ovh_domain_zone_record.foobar" , "zone" , zone ),
59
134
resource .TestCheckResourceAttr (
@@ -63,12 +138,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
63
138
),
64
139
},
65
140
resource.TestStep {
66
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_1 , zone ),
141
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_1 , zone , test_prefix ),
67
142
Check : resource .ComposeTestCheckFunc (
68
143
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
69
- testAccCheckOvhDomainZoneRecordAttributesUpdated_1 (& record ),
70
144
resource .TestCheckResourceAttr (
71
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform" ),
145
+ "ovh_domain_zone_record.foobar" , "subdomain" , test_prefix ),
72
146
resource .TestCheckResourceAttr (
73
147
"ovh_domain_zone_record.foobar" , "zone" , zone ),
74
148
resource .TestCheckResourceAttr (
@@ -78,12 +152,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
78
152
),
79
153
},
80
154
resource.TestStep {
81
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_2 , zone ),
155
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_2 , zone , test_prefix ),
82
156
Check : resource .ComposeTestCheckFunc (
83
157
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
84
- testAccCheckOvhDomainZoneRecordAttributesUpdated_2 (& record ),
85
158
resource .TestCheckResourceAttr (
86
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform2" ),
159
+ "ovh_domain_zone_record.foobar" , "subdomain" , fmt . Sprintf ( "%s2" , test_prefix ) ),
87
160
resource .TestCheckResourceAttr (
88
161
"ovh_domain_zone_record.foobar" , "zone" , zone ),
89
162
resource .TestCheckResourceAttr (
@@ -93,12 +166,11 @@ func TestAccOvhDomainZoneRecord_Updated(t *testing.T) {
93
166
),
94
167
},
95
168
resource.TestStep {
96
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_3 , zone ),
169
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRecordConfig_new_value_3 , zone , test_prefix ),
97
170
Check : resource .ComposeTestCheckFunc (
98
171
testAccCheckOvhDomainZoneRecordExists ("ovh_domain_zone_record.foobar" , & record ),
99
- testAccCheckOvhDomainZoneRecordAttributesUpdated_3 (& record ),
100
172
resource .TestCheckResourceAttr (
101
- "ovh_domain_zone_record.foobar" , "subdomain" , "terraform3" ),
173
+ "ovh_domain_zone_record.foobar" , "subdomain" , fmt . Sprintf ( "%s3" , test_prefix ) ),
102
174
resource .TestCheckResourceAttr (
103
175
"ovh_domain_zone_record.foobar" , "zone" , zone ),
104
176
resource .TestCheckResourceAttr (
@@ -166,65 +238,10 @@ func testAccCheckOvhDomainZoneRecordExists(n string, record *OvhDomainZoneRecord
166
238
}
167
239
}
168
240
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
241
const testAccCheckOvhDomainZoneRecordConfig_basic = `
225
242
resource "ovh_domain_zone_record" "foobar" {
226
243
zone = "%s"
227
- subdomain = "terraform "
244
+ subdomain = "%s "
228
245
target = "192.168.0.10"
229
246
fieldtype = "A"
230
247
ttl = 3600
@@ -233,7 +250,7 @@ resource "ovh_domain_zone_record" "foobar" {
233
250
const testAccCheckOvhDomainZoneRecordConfig_new_value_1 = `
234
251
resource "ovh_domain_zone_record" "foobar" {
235
252
zone = "%s"
236
- subdomain = "terraform "
253
+ subdomain = "%s "
237
254
target = "192.168.0.11"
238
255
fieldtype = "A"
239
256
ttl = 3600
@@ -242,7 +259,7 @@ resource "ovh_domain_zone_record" "foobar" {
242
259
const testAccCheckOvhDomainZoneRecordConfig_new_value_2 = `
243
260
resource "ovh_domain_zone_record" "foobar" {
244
261
zone = "%s"
245
- subdomain = "terraform2 "
262
+ subdomain = "%s2 "
246
263
target = "192.168.0.11"
247
264
fieldtype = "A"
248
265
ttl = 3600
@@ -251,7 +268,7 @@ resource "ovh_domain_zone_record" "foobar" {
251
268
const testAccCheckOvhDomainZoneRecordConfig_new_value_3 = `
252
269
resource "ovh_domain_zone_record" "foobar" {
253
270
zone = "%s"
254
- subdomain = "terraform3 "
271
+ subdomain = "%s3 "
255
272
target = "192.168.0.13"
256
273
fieldtype = "A"
257
274
ttl = 3604
0 commit comments