@@ -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_redirection" , & resource.Sweeper {
18
+ Name : "ovh_domain_zone_redirection" ,
19
+ F : testSweepDomainZoneRedirection ,
20
+ })
21
+ }
22
+
23
+ func testSweepDomainZoneRedirection (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
+ redirections := make ([]int64 , 0 )
41
+ if err := client .Get (fmt .Sprintf ("/domain/zone/%s/redirection" , zoneName ), & redirections ); err != nil {
42
+ return fmt .Errorf ("Error calling /domain/zone/%s:\n \t %q" , zoneName , err )
43
+ }
44
+
45
+ if len (redirections ) == 0 {
46
+ log .Print ("[DEBUG] No redirection to sweep" )
47
+ return nil
48
+ }
49
+
50
+ for _ , rec := range redirections {
51
+ redirection := & OvhDomainZoneRedirection {}
52
+
53
+ if err := client .Get (fmt .Sprintf ("/domain/zone/%s/redirection/%v" , zoneName , rec ), & redirection ); err != nil {
54
+ return fmt .Errorf ("Error calling /domain/zone/%s/redirection/%v:\n \t %q" , zoneName , rec , err )
55
+ }
56
+
57
+ if ! strings .HasPrefix (redirection .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/redirection/%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 TestAccOvhDomainZoneRedirection_Basic (t * testing.T ) {
14
91
var redirection OvhDomainZoneRedirection
15
92
zone := os .Getenv ("OVH_ZONE" )
@@ -20,11 +97,11 @@ func TestAccOvhDomainZoneRedirection_Basic(t *testing.T) {
20
97
CheckDestroy : testAccCheckOvhDomainZoneRedirectionDestroy ,
21
98
Steps : []resource.TestStep {
22
99
resource.TestStep {
23
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_basic , zone ),
100
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_basic , zone , test_prefix ),
24
101
Check : resource .ComposeTestCheckFunc (
25
102
testAccCheckOvhDomainZoneRedirectionExists ("ovh_domain_zone_redirection.foobar" , & redirection ),
26
103
resource .TestCheckResourceAttr (
27
- "ovh_domain_zone_redirection.foobar" , "subdomain" , "terraform" ),
104
+ "ovh_domain_zone_redirection.foobar" , "subdomain" , test_prefix ),
28
105
resource .TestCheckResourceAttr (
29
106
"ovh_domain_zone_redirection.foobar" , "zone" , zone ),
30
107
resource .TestCheckResourceAttr (
@@ -47,47 +124,47 @@ func TestAccOvhDomainZoneRedirection_Updated(t *testing.T) {
47
124
CheckDestroy : testAccCheckOvhDomainZoneRedirectionDestroy ,
48
125
Steps : []resource.TestStep {
49
126
resource.TestStep {
50
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_basic , zone ),
127
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_basic , zone , test_prefix ),
51
128
Check : resource .ComposeTestCheckFunc (
52
129
testAccCheckOvhDomainZoneRedirectionExists ("ovh_domain_zone_redirection.foobar" , & redirection ),
53
130
resource .TestCheckResourceAttr (
54
- "ovh_domain_zone_redirection.foobar" , "subdomain" , "terraform" ),
131
+ "ovh_domain_zone_redirection.foobar" , "subdomain" , test_prefix ),
55
132
resource .TestCheckResourceAttr (
56
133
"ovh_domain_zone_redirection.foobar" , "zone" , zone ),
57
134
resource .TestCheckResourceAttr (
58
135
"ovh_domain_zone_redirection.foobar" , "target" , "https://terraform.net" ),
59
136
),
60
137
},
61
138
resource.TestStep {
62
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_new_value_1 , zone ),
139
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_new_value_1 , zone , test_prefix ),
63
140
Check : resource .ComposeTestCheckFunc (
64
141
testAccCheckOvhDomainZoneRedirectionExists ("ovh_domain_zone_redirection.foobar" , & redirection ),
65
142
resource .TestCheckResourceAttr (
66
- "ovh_domain_zone_redirection.foobar" , "subdomain" , "terraform" ),
143
+ "ovh_domain_zone_redirection.foobar" , "subdomain" , test_prefix ),
67
144
resource .TestCheckResourceAttr (
68
145
"ovh_domain_zone_redirection.foobar" , "zone" , zone ),
69
146
resource .TestCheckResourceAttr (
70
147
"ovh_domain_zone_redirection.foobar" , "target" , "https://terraform.io" ),
71
148
),
72
149
},
73
150
resource.TestStep {
74
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_new_value_2 , zone ),
151
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_new_value_2 , zone , test_prefix ),
75
152
Check : resource .ComposeTestCheckFunc (
76
153
testAccCheckOvhDomainZoneRedirectionExists ("ovh_domain_zone_redirection.foobar" , & redirection ),
77
154
resource .TestCheckResourceAttr (
78
- "ovh_domain_zone_redirection.foobar" , "subdomain" , "terraform2" ),
155
+ "ovh_domain_zone_redirection.foobar" , "subdomain" , fmt . Sprintf ( "%s2" , test_prefix ) ),
79
156
resource .TestCheckResourceAttr (
80
157
"ovh_domain_zone_redirection.foobar" , "zone" , zone ),
81
158
resource .TestCheckResourceAttr (
82
159
"ovh_domain_zone_redirection.foobar" , "target" , "https://terraform.io" ),
83
160
),
84
161
},
85
162
resource.TestStep {
86
- Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_new_value_3 , zone ),
163
+ Config : fmt .Sprintf (testAccCheckOvhDomainZoneRedirectionConfig_new_value_3 , zone , test_prefix ),
87
164
Check : resource .ComposeTestCheckFunc (
88
165
testAccCheckOvhDomainZoneRedirectionExists ("ovh_domain_zone_redirection.foobar" , & redirection ),
89
166
resource .TestCheckResourceAttr (
90
- "ovh_domain_zone_redirection.foobar" , "subdomain" , "terraform3" ),
167
+ "ovh_domain_zone_redirection.foobar" , "subdomain" , fmt . Sprintf ( "%s3" , test_prefix ) ),
91
168
resource .TestCheckResourceAttr (
92
169
"ovh_domain_zone_redirection.foobar" , "zone" , zone ),
93
170
resource .TestCheckResourceAttr (
@@ -156,15 +233,15 @@ func testAccCheckOvhDomainZoneRedirectionExists(n string, redirection *OvhDomain
156
233
const testAccCheckOvhDomainZoneRedirectionConfig_basic = `
157
234
resource "ovh_domain_zone_redirection" "foobar" {
158
235
zone = "%s"
159
- subdomain = "terraform "
236
+ subdomain = "%s "
160
237
target = "https://terraform.net"
161
238
type = "visible"
162
239
}`
163
240
164
241
const testAccCheckOvhDomainZoneRedirectionConfig_new_value_1 = `
165
242
resource "ovh_domain_zone_redirection" "foobar" {
166
243
zone = "%s"
167
- subdomain = "terraform "
244
+ subdomain = "%s "
168
245
target = "https://terraform.io"
169
246
type = "visible"
170
247
}
@@ -173,7 +250,7 @@ resource "ovh_domain_zone_redirection" "foobar" {
173
250
const testAccCheckOvhDomainZoneRedirectionConfig_new_value_2 = `
174
251
resource "ovh_domain_zone_redirection" "foobar" {
175
252
zone = "%s"
176
- subdomain = "terraform2 "
253
+ subdomain = "%s2 "
177
254
target = "https://terraform.io"
178
255
type = "visible"
179
256
}
@@ -182,7 +259,7 @@ resource "ovh_domain_zone_redirection" "foobar" {
182
259
const testAccCheckOvhDomainZoneRedirectionConfig_new_value_3 = `
183
260
resource "ovh_domain_zone_redirection" "foobar" {
184
261
zone = "%s"
185
- subdomain = "terraform3 "
262
+ subdomain = "%s3 "
186
263
target = "https://terraform.com"
187
264
type = "visible"
188
265
}`
0 commit comments