-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathresource_iploadbalancing_test.go
211 lines (183 loc) · 5.67 KB
/
resource_iploadbalancing_test.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package ovh
import (
"fmt"
"log"
"net/url"
"strings"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/ovh/go-ovh/ovh"
)
const testAccIpLoadbalancingBasic = `
data "ovh_order_cart" "mycart" {
ovh_subsidiary = "fr"
description = "%s"
}
data "ovh_order_cart_product_plan" "iplb" {
cart_id = data.ovh_order_cart.mycart.id
price_capacity = "renew"
product = "ipLoadbalancing"
plan_code = "iplb-lb1"
}
data "ovh_order_cart_product_options_plan" "bhs" {
cart_id = data.ovh_order_cart_product_plan.iplb.cart_id
price_capacity = data.ovh_order_cart_product_plan.iplb.price_capacity
product = data.ovh_order_cart_product_plan.iplb.product
plan_code = data.ovh_order_cart_product_plan.iplb.plan_code
options_plan_code = "iplb-zone-lb1-rbx"
}
resource "ovh_iploadbalancing" "iplb-lb1" {
ovh_subsidiary = data.ovh_order_cart.mycart.ovh_subsidiary
display_name = "%s"
plan {
duration = data.ovh_order_cart_product_plan.iplb.selected_price.0.duration
plan_code = data.ovh_order_cart_product_plan.iplb.plan_code
pricing_mode = data.ovh_order_cart_product_plan.iplb.selected_price.0.pricing_mode
}
plan_option {
duration = data.ovh_order_cart_product_options_plan.bhs.selected_price.0.duration
plan_code = data.ovh_order_cart_product_options_plan.bhs.plan_code
pricing_mode = data.ovh_order_cart_product_options_plan.bhs.selected_price.0.pricing_mode
}
}
`
const testAccIpLoadbalancingInternal = `
resource "ovh_iploadbalancing" "iplb-internal" {
ovh_subsidiary = "fr"
display_name = "%s"
plan {
catalog_name = "iplb_private_beta"
duration = "P1M"
plan_code = "iplb-service-monitoring"
pricing_mode = "default"
}
plan_option {
catalog_name = "iplb_private_beta"
duration = "P1M"
plan_code = "iplb-zone-gra"
pricing_mode = "default"
}
}
`
func init() {
resource.AddTestSweepers("ovh_iploadbalancing", &resource.Sweeper{
Name: "ovh_iploadbalancing",
F: testSweepIpLoadbalancing,
})
}
func testSweepIpLoadbalancing(region string) error {
config, err := sharedConfigForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
serviceNames := make([]string, 0)
if err := config.OVHClient.Get("/ipLoadbalancing", &serviceNames); err != nil {
return fmt.Errorf("Error calling GET /ipLoadbalancing:\n\t %q", err)
}
if len(serviceNames) == 0 {
log.Print("[DEBUG] No ipLoadbalancing to sweep")
return nil
}
for _, serviceName := range serviceNames {
r := &IpLoadbalancing{}
log.Printf("[DEBUG] Will get ipLoadbalancing: %v", serviceName)
endpoint := fmt.Sprintf(
"/ipLoadbalancing/%s",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Get(endpoint, r); err != nil {
return fmt.Errorf("calling Get %s:\n\t %q", endpoint, err)
}
if r.DisplayName == nil || !strings.HasPrefix(*r.DisplayName, test_prefix) {
continue
}
log.Printf("[DEBUG] Will delete ipLoadbalancing: %v", serviceName)
terminate := func() (string, error) {
log.Printf("[DEBUG] Will terminate ipLoadbalancing %s", serviceName)
endpoint := fmt.Sprintf(
"/ipLoadbalancing/%s/terminate",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Post(endpoint, nil, nil); err != nil {
if errOvh, ok := err.(*ovh.APIError); ok && (errOvh.Code == 404 || errOvh.Code == 460) {
return "", nil
}
return "", fmt.Errorf("calling Post %s:\n\t %q", endpoint, err)
}
return serviceName, nil
}
confirmTerminate := func(token string) error {
log.Printf("[DEBUG] Will confirm termination of ipLoadbalancing %s", serviceName)
endpoint := fmt.Sprintf(
"/ipLoadbalancing/%s/confirmTermination",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Post(endpoint, &IpLoadbalancingConfirmTerminationOpts{Token: token}, nil); err != nil {
return fmt.Errorf("calling Post %s:\n\t %q", endpoint, err)
}
return nil
}
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
if err := orderDelete(nil, config, terminate, confirmTerminate); err != nil {
return resource.RetryableError(err)
}
// Successful delete
return nil
})
if err != nil {
return err
}
}
return nil
}
func TestAccResourceIpLoadbalancing_basic(t *testing.T) {
desc := acctest.RandomWithPrefix(test_prefix)
config := fmt.Sprintf(
testAccIpLoadbalancingBasic,
desc,
desc,
)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckOrderIpLoadbalancing(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"ovh_iploadbalancing.iplb-lb1", "ipv4"),
resource.TestCheckResourceAttr(
"ovh_iploadbalancing.iplb-lb1", "display_name", desc),
resource.TestCheckResourceAttrSet(
"ovh_iploadbalancing.iplb-lb1", "urn"),
),
},
},
})
}
func TestAccResourceIpLoadbalancing_internal(t *testing.T) {
desc := acctest.RandomWithPrefix(test_prefix)
config := fmt.Sprintf(
testAccIpLoadbalancingInternal,
desc,
)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckOrderIpLoadbalancing(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"ovh_iploadbalancing.iplb-internal", "ipv4"),
resource.TestCheckResourceAttr(
"ovh_iploadbalancing.iplb-internal", "display_name", desc),
resource.TestCheckResourceAttrSet(
"ovh_iploadbalancing.iplb-internal", "urn"),
),
},
},
})
}