Skip to content

Commit e208e00

Browse files
committed
ovh_iploadbalancing_tcp_farm
1 parent 66b6758 commit e208e00

5 files changed

+487
-1
lines changed

ovh/helpers.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ovh
2+
3+
import "fmt"
4+
5+
func validateStringEnum(value string, enum []string) error {
6+
missing := true
7+
for _, v := range enum {
8+
if value == v {
9+
missing = false
10+
}
11+
}
12+
if missing {
13+
return fmt.Errorf("Value %s is not among valid values (%s)", value, enum)
14+
}
15+
return nil
16+
}

ovh/provider.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ func Provider() terraform.ResourceProvider {
5252
},
5353

5454
ResourcesMap: map[string]*schema.Resource{
55-
"ovh_domain_zone_record": resourceOvhDomainZoneRecord(),
55+
"ovh_iploadbalancing_tcp_farm": resourceIpLoadbalancingTcpFarm(),
56+
"ovh_domain_zone_record": resourceOvhDomainZoneRecord(),
5657
// New naming schema (issue #23)
5758
"ovh_cloud_network_private": resourcePublicCloudPrivateNetwork(),
5859
"ovh_cloud_network_private_subnet": resourcePublicCloudPrivateNetworkSubnet(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
)
8+
9+
type IpLoadbalancingTcpFarmBackendProbe struct {
10+
Match string `json:"match,omitempty"`
11+
Port int `json:"port,omitempty"`
12+
Interval int `json:"interval,omitempty"`
13+
Negate bool `json:"negate,omitempty"`
14+
Pattern string `json:"pattern,omitempty"`
15+
ForceSsl bool `json:"forceSsl,omitempty"`
16+
URL string `json:"url,omitempty"`
17+
Method string `json:"method,omitempty"`
18+
Type string `json:"type,omitempty"`
19+
}
20+
21+
type IpLoadbalancingTcpFarm struct {
22+
FarmId int `json:"farmId,omitempty"`
23+
Zone string `json:"zone,omitempty"`
24+
VrackNetworkId int `json:"vrackNetworkId,omitempty"`
25+
Port int `json:"port,omitempty"`
26+
Stickiness string `json:"stickiness,omitempty"`
27+
Balance string `json:"balance,omitempty"`
28+
Probe *IpLoadbalancingTcpFarmBackendProbe `json:"probe,omitempty"`
29+
DisplayName string `json:"displayName,omitempty"`
30+
}
31+
32+
func resourceIpLoadbalancingTcpFarm() *schema.Resource {
33+
return &schema.Resource{
34+
Create: resourceIpLoadbalancingTcpFarmCreate,
35+
Read: resourceIpLoadbalancingTcpFarmRead,
36+
Update: resourceIpLoadbalancingTcpFarmUpdate,
37+
Delete: resourceIpLoadbalancingTcpFarmDelete,
38+
39+
Schema: map[string]*schema.Schema{
40+
"service_name": &schema.Schema{
41+
Type: schema.TypeString,
42+
Required: true,
43+
ForceNew: true,
44+
},
45+
"balance": &schema.Schema{
46+
Type: schema.TypeString,
47+
Optional: true,
48+
ForceNew: false,
49+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
50+
err := validateStringEnum(v.(string), []string{"first", "leastconn", "roundrobin", "source"})
51+
if err != nil {
52+
errors = append(errors, err)
53+
}
54+
return
55+
},
56+
},
57+
"display_name": &schema.Schema{
58+
Type: schema.TypeString,
59+
Optional: true,
60+
ForceNew: false,
61+
},
62+
"port": &schema.Schema{
63+
Type: schema.TypeInt,
64+
Optional: true,
65+
ForceNew: false,
66+
},
67+
"stickiness": &schema.Schema{
68+
Type: schema.TypeString,
69+
Optional: true,
70+
ForceNew: false,
71+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
72+
err := validateStringEnum(v.(string), []string{"sourceIp"})
73+
if err != nil {
74+
errors = append(errors, err)
75+
}
76+
return
77+
},
78+
},
79+
"vrack_network_id": &schema.Schema{
80+
Type: schema.TypeInt,
81+
Optional: true,
82+
ForceNew: false,
83+
},
84+
"zone": &schema.Schema{
85+
Type: schema.TypeString,
86+
Required: true,
87+
ForceNew: true,
88+
},
89+
"probe": &schema.Schema{
90+
Type: schema.TypeSet,
91+
Optional: true,
92+
ForceNew: false,
93+
Elem: &schema.Resource{
94+
Schema: map[string]*schema.Schema{
95+
"match": &schema.Schema{
96+
Type: schema.TypeString,
97+
Optional: true,
98+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
99+
err := validateStringEnum(v.(string), []string{"contains", "default", "internal", "matches", "status"})
100+
if err != nil {
101+
errors = append(errors, err)
102+
}
103+
return
104+
},
105+
},
106+
"port": &schema.Schema{
107+
Type: schema.TypeInt,
108+
Optional: true,
109+
},
110+
"interval": &schema.Schema{
111+
Type: schema.TypeInt,
112+
Optional: true,
113+
Default: 30,
114+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
115+
value := v.(int)
116+
if value < 30 || value > 3600 {
117+
errors = append(errors, fmt.Errorf("Probe interval not in 30..3600 range"))
118+
}
119+
return
120+
},
121+
},
122+
"negate": &schema.Schema{
123+
Type: schema.TypeBool,
124+
Optional: true,
125+
},
126+
"pattern": &schema.Schema{
127+
Type: schema.TypeString,
128+
Optional: true,
129+
},
130+
"force_ssl": &schema.Schema{
131+
Type: schema.TypeBool,
132+
Optional: true,
133+
},
134+
"url": &schema.Schema{
135+
Type: schema.TypeString,
136+
Optional: true,
137+
},
138+
"method": &schema.Schema{
139+
Type: schema.TypeString,
140+
Optional: true,
141+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
142+
err := validateStringEnum(v.(string), []string{"GET", "HEAD", "OPTIONS", "internal"})
143+
if err != nil {
144+
errors = append(errors, err)
145+
}
146+
return
147+
},
148+
},
149+
"type": &schema.Schema{
150+
Type: schema.TypeString,
151+
Required: true,
152+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
153+
err := validateStringEnum(v.(string), []string{"http", "internal", "mysql", "oko", "pgsql", "smtp", "tcp"})
154+
if err != nil {
155+
errors = append(errors, err)
156+
}
157+
return
158+
},
159+
},
160+
},
161+
},
162+
},
163+
},
164+
}
165+
}
166+
167+
func resourceIpLoadbalancingTcpFarmCreate(d *schema.ResourceData, meta interface{}) error {
168+
config := meta.(*Config)
169+
170+
probe := &IpLoadbalancingTcpFarmBackendProbe{}
171+
probeSet := d.Get("probe").(*schema.Set)
172+
if probeSet.Len() > 0 {
173+
probeData := probeSet.List()[0].(map[string]interface{})
174+
probe.Match = probeData["match"].(string)
175+
probe.Port = probeData["port"].(int)
176+
probe.Interval = probeData["interval"].(int)
177+
probe.Negate = probeData["negate"].(bool)
178+
probe.Pattern = probeData["pattern"].(string)
179+
probe.ForceSsl = probeData["force_ssl"].(bool)
180+
probe.URL = probeData["url"].(string)
181+
probe.Method = probeData["method"].(string)
182+
probe.Type = probeData["type"].(string)
183+
}
184+
185+
farm := &IpLoadbalancingTcpFarm{
186+
Zone: d.Get("zone").(string),
187+
VrackNetworkId: d.Get("vrack_network_id").(int),
188+
Port: d.Get("port").(int),
189+
Stickiness: d.Get("stickiness").(string),
190+
Balance: d.Get("balance").(string),
191+
Probe: probe,
192+
DisplayName: d.Get("display_name").(string),
193+
}
194+
195+
service := d.Get("service_name").(string)
196+
resp := &IpLoadbalancingTcpFarm{}
197+
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm", service)
198+
199+
err := config.OVHClient.Post(endpoint, farm, resp)
200+
if err != nil {
201+
return fmt.Errorf("calling POST %s :\n\t %s", endpoint, err.Error())
202+
}
203+
204+
d.SetId(fmt.Sprintf("%d", resp.FarmId))
205+
206+
return nil
207+
}
208+
209+
func resourceIpLoadbalancingTcpFarmRead(d *schema.ResourceData, meta interface{}) error {
210+
config := meta.(*Config)
211+
service := d.Get("service_name").(string)
212+
r := &IpLoadbalancingTcpFarm{}
213+
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%s", service, d.Id())
214+
215+
err := config.OVHClient.Get(endpoint, &r)
216+
if err != nil {
217+
return fmt.Errorf("calling %s:\n\t %s", endpoint, err.Error())
218+
}
219+
220+
d.Set("display_name", r.DisplayName)
221+
222+
return nil
223+
}
224+
225+
func resourceIpLoadbalancingTcpFarmUpdate(d *schema.ResourceData, meta interface{}) error {
226+
config := meta.(*Config)
227+
service := d.Get("service_name").(string)
228+
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%s", service, d.Id())
229+
230+
probe := &IpLoadbalancingTcpFarmBackendProbe{}
231+
probeSet := d.Get("probe").(*schema.Set)
232+
if probeSet.Len() > 0 {
233+
probeData := probeSet.List()[0].(map[string]interface{})
234+
probe.Match = probeData["match"].(string)
235+
probe.Port = probeData["port"].(int)
236+
probe.Interval = probeData["interval"].(int)
237+
probe.Negate = probeData["negate"].(bool)
238+
probe.Pattern = probeData["pattern"].(string)
239+
probe.ForceSsl = probeData["force_ssl"].(bool)
240+
probe.URL = probeData["url"].(string)
241+
probe.Method = probeData["method"].(string)
242+
probe.Type = probeData["type"].(string)
243+
}
244+
245+
farm := &IpLoadbalancingTcpFarm{
246+
VrackNetworkId: d.Get("vrack_network_id").(int),
247+
Port: d.Get("port").(int),
248+
Stickiness: d.Get("stickiness").(string),
249+
Balance: d.Get("balance").(string),
250+
Probe: probe,
251+
DisplayName: d.Get("display_name").(string),
252+
}
253+
254+
err := config.OVHClient.Put(endpoint, farm, nil)
255+
if err != nil {
256+
return fmt.Errorf("calling %s:\n\t %s", endpoint, err.Error())
257+
}
258+
259+
return nil
260+
}
261+
262+
func resourceIpLoadbalancingTcpFarmDelete(d *schema.ResourceData, meta interface{}) error {
263+
config := meta.(*Config)
264+
265+
service := d.Get("service_name").(string)
266+
r := &IpLoadbalancingTcpFarm{}
267+
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%s", service, d.Id())
268+
269+
err := config.OVHClient.Delete(endpoint, &r)
270+
if err != nil {
271+
return fmt.Errorf("Error calling %s: %s \n", endpoint, err.Error())
272+
}
273+
274+
return nil
275+
}

0 commit comments

Comments
 (0)