Skip to content

Commit dc56680

Browse files
committed
adds iploadbalancing_tcp_farm_server
1 parent 34bffdf commit dc56680

4 files changed

+461
-1
lines changed

ovh/helpers.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ovh
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
func validateStringEnum(value string, enum []string) error {
68
missing := true
@@ -14,3 +16,30 @@ func validateStringEnum(value string, enum []string) error {
1416
}
1517
return nil
1618
}
19+
20+
func getNilBoolPointer(val interface{}) *bool {
21+
if val == nil {
22+
return nil
23+
}
24+
value := val.(bool)
25+
return &value
26+
}
27+
28+
func getNilStringPointer(val interface{}) *string {
29+
if val == nil {
30+
return nil
31+
}
32+
value := val.(string)
33+
if len(value) == 0 {
34+
return nil
35+
}
36+
return &value
37+
}
38+
39+
func getNilIntPointer(val interface{}) *int {
40+
if val == nil {
41+
return nil
42+
}
43+
value := val.(int)
44+
return &value
45+
}

ovh/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func Provider() terraform.ResourceProvider {
4949

5050
ResourcesMap: map[string]*schema.Resource{
5151
"ovh_iploadbalancing_tcp_farm": resourceIpLoadbalancingTcpFarm(),
52+
"ovh_iploadbalancing_tcp_farm_server": resourceIpLoadbalancingTcpFarmServer(),
5253
"ovh_publiccloud_private_network": resourcePublicCloudPrivateNetwork(),
5354
"ovh_publiccloud_private_network_subnet": resourcePublicCloudPrivateNetworkSubnet(),
5455
"ovh_publiccloud_user": resourcePublicCloudUser(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
package ovh
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"net"
8+
9+
"github.com/hashicorp/terraform/helper/schema"
10+
)
11+
12+
type IpLoadbalancingTcpFarmServer struct {
13+
BackendId int `json:"backendId,omitempty"`
14+
ServerId int `json:"serverId,omitempty"`
15+
FarmId int `json:"farmId,omitempty"`
16+
Address *string `json:"address"`
17+
Cookie *string `json:"cookie,omitempty"`
18+
Port *int `json:"port"`
19+
ProxyProtocolVersion *string `json:"proxyProtocolVersion"`
20+
Chain *string `json:"chain"`
21+
Weight *int `json:"weight"`
22+
Probe *bool `json:"probe"`
23+
Ssl *bool `json:"ssl"`
24+
Backup *bool `json:"backup"`
25+
Status *string `json:"status"`
26+
}
27+
28+
func resourceIpLoadbalancingTcpFarmServer() *schema.Resource {
29+
return &schema.Resource{
30+
Create: resourceIpLoadbalancingTcpFarmServerCreate,
31+
Read: resourceIpLoadbalancingTcpFarmServerRead,
32+
Update: resourceIpLoadbalancingTcpFarmServerUpdate,
33+
Delete: resourceIpLoadbalancingTcpFarmServerDelete,
34+
Schema: map[string]*schema.Schema{
35+
"service_name": &schema.Schema{
36+
Type: schema.TypeString,
37+
Required: true,
38+
ForceNew: true,
39+
},
40+
"farm_id": &schema.Schema{
41+
Type: schema.TypeInt,
42+
Required: true,
43+
ForceNew: true,
44+
},
45+
"address": &schema.Schema{
46+
Type: schema.TypeString,
47+
Required: true,
48+
ForceNew: true,
49+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
50+
ip := v.(string)
51+
if net.ParseIP(ip).To4() == nil {
52+
errors = append(errors, fmt.Errorf("Address %s is not an IPv4", ip))
53+
}
54+
return
55+
},
56+
},
57+
"ssl": &schema.Schema{
58+
Type: schema.TypeBool,
59+
Optional: true,
60+
},
61+
"cookie": &schema.Schema{
62+
Type: schema.TypeString,
63+
Computed: true,
64+
},
65+
"port": &schema.Schema{
66+
Type: schema.TypeInt,
67+
Optional: true,
68+
},
69+
"proxy_protocol_version": &schema.Schema{
70+
Type: schema.TypeString,
71+
Optional: true,
72+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
73+
err := validateStringEnum(v.(string), []string{"v1", "v2", "v2-ssl", "v2-ssl-cn"})
74+
if err != nil {
75+
errors = append(errors, err)
76+
}
77+
return
78+
},
79+
},
80+
"chain": &schema.Schema{
81+
Type: schema.TypeString,
82+
Optional: true,
83+
},
84+
"weight": &schema.Schema{
85+
Type: schema.TypeInt,
86+
Optional: true,
87+
Default: 1,
88+
},
89+
"probe": &schema.Schema{
90+
Type: schema.TypeBool,
91+
Optional: true,
92+
},
93+
"backup": &schema.Schema{
94+
Type: schema.TypeBool,
95+
Optional: true,
96+
},
97+
"status": &schema.Schema{
98+
Type: schema.TypeString,
99+
Required: true,
100+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
101+
err := validateStringEnum(v.(string), []string{"active", "inactive"})
102+
if err != nil {
103+
errors = append(errors, err)
104+
}
105+
return
106+
},
107+
},
108+
},
109+
}
110+
}
111+
112+
func resourceIpLoadbalancingTcpFarmServerCreate(d *schema.ResourceData, meta interface{}) error {
113+
config := meta.(*Config)
114+
115+
newBackendServer := &IpLoadbalancingTcpFarmServer{
116+
Address: getNilStringPointer(d.Get("address").(string)),
117+
Port: getNilIntPointer(d.Get("port").(int)),
118+
ProxyProtocolVersion: getNilStringPointer(d.Get("proxy_protocol_version").(string)),
119+
Chain: getNilStringPointer(d.Get("chain").(string)),
120+
Weight: getNilIntPointer(d.Get("weight").(int)),
121+
Probe: getNilBoolPointer(d.Get("probe").(bool)),
122+
Ssl: getNilBoolPointer(d.Get("ssl").(bool)),
123+
Backup: getNilBoolPointer(d.Get("backup").(bool)),
124+
Status: getNilStringPointer(d.Get("status").(string)),
125+
}
126+
127+
service := d.Get("service_name").(string)
128+
farmid := d.Get("farm_id").(int)
129+
r := &IpLoadbalancingTcpFarmServer{}
130+
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server", service, farmid)
131+
132+
err := config.OVHClient.Post(endpoint, newBackendServer, r)
133+
if err != nil {
134+
return fmt.Errorf("calling %s with %d:\n\t %s", endpoint, farmid, err.Error())
135+
}
136+
137+
//set id
138+
d.SetId(fmt.Sprintf("%d", r.ServerId))
139+
140+
return nil
141+
}
142+
143+
func resourceIpLoadbalancingTcpFarmServerRead(d *schema.ResourceData, meta interface{}) error {
144+
config := meta.(*Config)
145+
146+
service := d.Get("service_name").(string)
147+
farmid := d.Get("farm_id").(int)
148+
r := &IpLoadbalancingTcpFarmServer{}
149+
150+
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id())
151+
152+
err := config.OVHClient.Get(endpoint, r)
153+
if err != nil {
154+
return fmt.Errorf("calling %s :\n\t %q", endpoint, err)
155+
}
156+
log.Printf("[DEBUG] Response object from OVH : %v", r)
157+
158+
d.Set("probe", *r.Probe)
159+
d.Set("ssl", *r.Ssl)
160+
d.Set("backup", *r.Backup)
161+
d.Set("address", *r.Address)
162+
if r.Cookie != nil {
163+
d.Set("cookie", *r.Cookie)
164+
}
165+
d.Set("port", *r.Port)
166+
if r.ProxyProtocolVersion != nil {
167+
d.Set("proxy_protocol_version", *r.ProxyProtocolVersion)
168+
}
169+
if r.Chain != nil {
170+
d.Set("chain", *r.Chain)
171+
}
172+
d.Set("weight", *r.Weight)
173+
d.Set("status", *r.Status)
174+
175+
return nil
176+
}
177+
178+
func resourceIpLoadbalancingTcpFarmServerUpdate(d *schema.ResourceData, meta interface{}) error {
179+
config := meta.(*Config)
180+
181+
update := &IpLoadbalancingTcpFarmServer{
182+
Address: getNilStringPointer(d.Get("address").(string)),
183+
Port: getNilIntPointer(d.Get("port").(int)),
184+
ProxyProtocolVersion: getNilStringPointer(d.Get("proxy_protocol_version").(string)),
185+
Chain: getNilStringPointer(d.Get("chain").(string)),
186+
Weight: getNilIntPointer(d.Get("weight").(int)),
187+
Probe: getNilBoolPointer(d.Get("probe").(bool)),
188+
Ssl: getNilBoolPointer(d.Get("ssl").(bool)),
189+
Backup: getNilBoolPointer(d.Get("backup").(bool)),
190+
Status: getNilStringPointer(d.Get("status").(string)),
191+
}
192+
193+
service := d.Get("service_name").(string)
194+
farmid := d.Get("farm_id").(int)
195+
r := &IpLoadbalancingTcpFarmServer{}
196+
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id())
197+
js, _ := json.Marshal(update)
198+
log.Printf("[DEBUG] PUT %s : %v", endpoint, string(js))
199+
err := config.OVHClient.Put(endpoint, update, r)
200+
if err != nil {
201+
return fmt.Errorf("calling %s with %d:\n\t %s", endpoint, farmid, err.Error())
202+
}
203+
return nil
204+
}
205+
206+
func resourceIpLoadbalancingTcpFarmServerDelete(d *schema.ResourceData, meta interface{}) error {
207+
config := meta.(*Config)
208+
209+
service := d.Get("service_name").(string)
210+
farmid := d.Get("farm_id").(int)
211+
212+
r := &IpLoadbalancingTcpFarmServer{}
213+
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id())
214+
215+
err := config.OVHClient.Delete(endpoint, r)
216+
if err != nil {
217+
return fmt.Errorf("calling %s :\n\t %s", endpoint, err.Error())
218+
}
219+
220+
return nil
221+
}

0 commit comments

Comments
 (0)