-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_iploadbalancing_tcp_route.go
204 lines (183 loc) · 5.95 KB
/
resource_iploadbalancing_tcp_route.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
package ovh
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceIPLoadbalancingTcpRoute() *schema.Resource {
return &schema.Resource{
Create: resourceIPLoadbalancingTcpRouteCreate,
Read: resourceIPLoadbalancingTcpRouteRead,
Update: resourceIPLoadbalancingTcpRouteUpdate,
Delete: resourceIPLoadbalancingTcpRouteDelete,
Importer: &schema.ResourceImporter{
State: resourceIpLoadbalancingTcpRouteImportState,
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Description: "The internal name of your IP load balancing",
Required: true,
ForceNew: true,
},
"action": {
Type: schema.TypeList,
Description: "Action triggered when all rules match",
Required: true,
ForceNew: false,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target": {
Type: schema.TypeString,
Description: "Farm ID for \"farm\" action type, empty for others",
Optional: true,
},
"type": {
Type: schema.TypeString,
Description: "Action to trigger if all the rules of this route matches",
Required: true,
},
},
},
},
"display_name": {
Type: schema.TypeString,
Description: "Human readable name for your route, this field is for you",
Optional: true,
},
"frontend_id": {
Type: schema.TypeInt,
Description: "Route traffic for this frontend",
Optional: true,
Computed: true,
},
"weight": {
Type: schema.TypeInt,
Description: "Route priority ([0..255]). 0 if null. Highest priority routes are evaluated last. Only the first matching route will trigger an action",
Optional: true,
Computed: true,
},
//computed
"status": {
Type: schema.TypeString,
Description: "Route status. Routes in \"ok\" state are ready to operate",
Computed: true,
},
"rules": {
Type: schema.TypeList,
Description: "List of rules to match to trigger action",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"field": {
Type: schema.TypeString,
Description: "Name of the field to match like \"protocol\" or \"host\". See \"/ipLoadbalancing/{serviceName}/route/availableRules\" for a list of available rules",
Computed: true,
},
"match": {
Type: schema.TypeString,
Description: "Matching operator. Not all operators are available for all fields. See \"/availableRules\"",
Computed: true,
},
"negate": {
Type: schema.TypeBool,
Description: "Invert the matching operator effect",
Computed: true,
},
"pattern": {
Type: schema.TypeString,
Description: "Value to match against this match. Interpretation if this field depends on the match and field",
Computed: true,
},
"rule_id": {
Type: schema.TypeInt,
Description: "Id of your rule",
Computed: true,
},
"sub_field": {
Type: schema.TypeString,
Description: "Name of sub-field, if applicable. This may be a Cookie or Header name for instance",
Computed: true,
},
},
},
},
},
}
}
func resourceIpLoadbalancingTcpRouteImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
splitId := strings.SplitN(givenId, "/", 2)
if len(splitId) != 2 {
return nil, fmt.Errorf("Import Id is not service_name/route id formatted")
}
serviceName := splitId[0]
routeId := splitId[1]
d.SetId(routeId)
d.Set("service_name", serviceName)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceIPLoadbalancingTcpRouteCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
route := (&IPLoadbalancingTcpRouteOpts{}).FromResource(d)
resp := &IPLoadbalancingTcpRoute{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/route",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Post(endpoint, route, resp); err != nil {
return fmt.Errorf("calling POST %s :\n\t %s", endpoint, err.Error())
}
d.SetId(fmt.Sprintf("%d", resp.RouteId))
return resourceIPLoadbalancingTcpRouteRead(d, meta)
}
func resourceIPLoadbalancingTcpRouteRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
r := &IPLoadbalancingTcpRoute{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/route/%s",
url.PathEscape(serviceName),
url.PathEscape(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() {
if k != "route_id" {
d.Set(k, v)
}
}
return nil
}
func resourceIPLoadbalancingTcpRouteUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
route := (&IPLoadbalancingTcpRouteOpts{}).FromResource(d)
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/route/%s",
url.PathEscape(serviceName),
url.PathEscape(d.Id()),
)
if err := config.OVHClient.Put(endpoint, route, nil); err != nil {
return fmt.Errorf("calling PUT %s:\n\t %s", endpoint, err.Error())
}
return resourceIPLoadbalancingTcpRouteRead(d, meta)
}
func resourceIPLoadbalancingTcpRouteDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/route/%s",
url.PathEscape(serviceName),
url.PathEscape(d.Id()),
)
if err := config.OVHClient.Delete(endpoint, nil); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
d.SetId("")
return nil
}