-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource_iploadbalancing_http_farm_server.go
227 lines (199 loc) · 6.75 KB
/
resource_iploadbalancing_http_farm_server.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package ovh
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceIpLoadbalancingHttpFarmServer() *schema.Resource {
return &schema.Resource{
Create: resourceIpLoadbalancingHttpFarmServerCreate,
Read: resourceIpLoadbalancingHttpFarmServerRead,
Update: resourceIpLoadbalancingHttpFarmServerUpdate,
Delete: resourceIpLoadbalancingHttpFarmServerDelete,
Importer: &schema.ResourceImporter{
State: resourceIpLoadbalancingHttpFarmServerImportState,
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"farm_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
},
"address": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
ip := v.(string)
if net.ParseIP(ip).To4() == nil {
errors = append(errors, fmt.Errorf("Address %s is not an IPv4", ip))
}
return
},
},
"ssl": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"cookie": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Optional: true,
},
"proxy_protocol_version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateStringEnum(v.(string), []string{"v1", "v2", "v2-ssl", "v2-ssl-cn"})
if err != nil {
errors = append(errors, err)
}
return
},
},
"chain": {
Type: schema.TypeString,
Optional: true,
},
"weight": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
},
"probe": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"backup": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"status": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateStringEnum(v.(string), []string{"active", "inactive"})
if err != nil {
errors = append(errors, err)
}
return
},
},
},
}
}
func resourceIpLoadbalancingHttpFarmServerImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
splitId := strings.SplitN(givenId, "/", 3)
if len(splitId) != 3 {
return nil, fmt.Errorf("Import Id is not service_name/farm_id/server id formatted")
}
serviceName := splitId[0]
farmId, err := strconv.Atoi(splitId[1])
if err != nil {
return nil, fmt.Errorf("Couldn't cast farmId %d to int: %s", farmId, err.Error())
}
serverId := splitId[2]
d.SetId(serverId)
d.Set("farm_id", farmId)
d.Set("service_name", serviceName)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceIpLoadbalancingHttpFarmServerCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
newBackendServer := &IpLoadbalancingFarmServerCreateOpts{
DisplayName: helpers.GetNilStringPointerFromData(d, "display_name"),
Address: d.Get("address").(string),
Port: helpers.GetNilIntPointerFromData(d, "port"),
ProxyProtocolVersion: helpers.GetNilStringPointerFromData(d, "proxy_protocol_version"),
Chain: helpers.GetNilStringPointerFromData(d, "chain"),
Weight: helpers.GetNilIntPointerFromData(d, "weight"),
Probe: helpers.GetNilBoolPointerFromData(d, "probe"),
Ssl: helpers.GetNilBoolPointerFromData(d, "ssl"),
Backup: helpers.GetNilBoolPointerFromData(d, "backup"),
Status: d.Get("status").(string),
}
service := d.Get("service_name").(string)
farmid := d.Get("farm_id").(int)
r := &IpLoadbalancingFarmServer{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/http/farm/%d/server", service, farmid)
err := config.OVHClient.Post(endpoint, newBackendServer, r)
if err != nil {
return fmt.Errorf("calling POST %s with %d:\n\t %s", endpoint, farmid, err.Error())
}
//set id
d.SetId(fmt.Sprintf("%d", r.ServerId))
return resourceIpLoadbalancingHttpFarmServerRead(d, meta)
}
func resourceIpLoadbalancingHttpFarmServerRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
service := d.Get("service_name").(string)
farmid := d.Get("farm_id").(int)
r := &IpLoadbalancingFarmServer{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/http/farm/%d/server/%s", service, farmid, d.Id())
if err := config.OVHClient.Get(endpoint, r); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
// set resource attributes
for k, v := range r.ToMap() {
d.Set(k, v)
}
return nil
}
func resourceIpLoadbalancingHttpFarmServerUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
update := &IpLoadbalancingFarmServerUpdateOpts{
DisplayName: helpers.GetNilStringPointerFromData(d, "display_name"),
Address: helpers.GetNilStringPointerFromData(d, "address"),
Port: helpers.GetNilIntPointerFromData(d, "port"),
ProxyProtocolVersion: helpers.GetNilStringPointerFromData(d, "proxy_protocol_version"),
Chain: helpers.GetNilStringPointerFromData(d, "chain"),
Weight: helpers.GetNilIntPointerFromData(d, "weight"),
Probe: helpers.GetNilBoolPointerFromData(d, "probe"),
Ssl: helpers.GetNilBoolPointerFromData(d, "ssl"),
Backup: helpers.GetNilBoolPointerFromData(d, "backup"),
Status: helpers.GetNilStringPointerFromData(d, "status"),
}
service := d.Get("service_name").(string)
farmid := d.Get("farm_id").(int)
r := &IpLoadbalancingFarmServer{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/http/farm/%d/server/%s", service, farmid, d.Id())
err := config.OVHClient.Put(endpoint, update, r)
if err != nil {
return fmt.Errorf("calling PUT %s with %d:\n\t %s", endpoint, farmid, err.Error())
}
return resourceIpLoadbalancingHttpFarmServerRead(d, meta)
}
func resourceIpLoadbalancingHttpFarmServerDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
service := d.Get("service_name").(string)
farmid := d.Get("farm_id").(int)
r := &IpLoadbalancingFarmServer{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/http/farm/%d/server/%s", service, farmid, d.Id())
err := config.OVHClient.Delete(endpoint, r)
if err != nil {
return fmt.Errorf("calling DELETE %s :\n\t %s", endpoint, err.Error())
}
d.SetId("")
return nil
}