-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource_iploadbalancing.go
247 lines (216 loc) · 6.9 KB
/
resource_iploadbalancing.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package ovh
import (
"fmt"
"log"
"net/url"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/go-ovh/ovh"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)
func resourceIpLoadbalancing() *schema.Resource {
return &schema.Resource{
Create: resourceIpLoadbalancingCreate,
Update: resourceIpLoadbalancingUpdate,
Read: resourceIpLoadbalancingRead,
Delete: resourceIpLoadbalancingDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
return []*schema.ResourceData{d}, nil
},
},
Schema: resourceIpLoadbalancingSchema(),
}
}
func resourceIpLoadbalancingSchema() map[string]*schema.Schema {
schema := map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Description: "Set the name displayed in ManagerV6 for your iplb (max 50 chars)",
Optional: true,
Computed: true,
},
"ssl_configuration": {
Type: schema.TypeString,
Description: "Modern oldest compatible clients : Firefox 27, Chrome 30, IE 11 on Windows 7, Edge, Opera 17, Safari 9, Android 5.0, and Java 8. Intermediate oldest compatible clients : Firefox 1, Chrome 1, IE 7, Opera 5, Safari 1, Windows XP IE8, Android 2.3, Java 7. Intermediate if null.",
Optional: true,
Computed: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateStringEnum(strings.ToLower(v.(string)), []string{
"intermediate",
"modern",
})
if err != nil {
errors = append(errors, err)
}
return
},
},
//computed
"ipv6": {
Type: schema.TypeString,
Description: "The IPV6 associated to your IP load balancing. DEPRECATED.",
Computed: true,
},
"ipv4": {
Type: schema.TypeString,
Description: "The IPV4 associated to your IP load balancing",
Computed: true,
},
"zone": {
Type: schema.TypeList,
Description: "Location where your service is",
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"service_name": {
Type: schema.TypeString,
Description: "The internal name of your IP load balancing",
Computed: true,
},
"ip_loadbalancing": {
Type: schema.TypeString,
Description: "Your IP load balancing",
Computed: true,
},
"state": {
Type: schema.TypeString,
Description: "Current state of your IP",
Computed: true,
},
"offer": {
Type: schema.TypeString,
Description: "The offer of your IP load balancing",
Computed: true,
},
"vrack_eligibility": {
Type: schema.TypeBool,
Description: "Vrack eligibility",
Computed: true,
},
"vrack_name": {
Type: schema.TypeString,
Description: "Name of the vRack on which the current Load Balancer is attached to, as it is named on vRack product",
Computed: true,
},
// additional exported attributes
"metrics_token": {
Type: schema.TypeString,
Description: "The metrics token associated with your IP load balancing",
Sensitive: true,
Computed: true,
},
"orderable_zone": {
Type: schema.TypeSet,
Computed: true,
Description: "Available additional zone for your Load Balancer",
Set: func(v interface{}) int {
r := v.(map[string]interface{})
return hashcode.String(r["name"].(string))
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The zone three letter code",
Computed: true,
},
"plan_code": {
Type: schema.TypeString,
Description: "The billing planCode for this zone",
Computed: true,
},
},
},
},
}
for k, v := range genericOrderSchema(true) {
schema[k] = v
}
return schema
}
func resourceIpLoadbalancingCreate(d *schema.ResourceData, meta interface{}) error {
if err := orderCreate(d, meta, "ipLoadbalancing"); err != nil {
return fmt.Errorf("Could not order ipLoadbalancing: %q", err)
}
return resourceIpLoadbalancingUpdate(d, meta)
}
func resourceIpLoadbalancingUpdate(d *schema.ResourceData, meta interface{}) error {
_, details, err := orderRead(d, meta)
if err != nil {
return fmt.Errorf("Could not read ipLoadbalancing order: %q", err)
}
var serviceName string
if strings.Contains(details[0].Domain, "-zone-") {
serviceName = strings.Split(details[0].Domain, "-zone-")[0]
} else {
serviceName = details[0].Domain
}
config := meta.(*Config)
log.Printf("[DEBUG] Will update ipLoadbalancing: %s", serviceName)
opts := (&IpLoadbalancingUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf("/ipLoadbalancing/%s", serviceName)
if err := config.OVHClient.Put(endpoint, opts, nil); err != nil {
return fmt.Errorf("calling Put %s: %q", endpoint, err)
}
return resourceIpLoadbalancingRead(d, meta)
}
func resourceIpLoadbalancingRead(d *schema.ResourceData, meta interface{}) error {
_, details, err := orderRead(d, meta)
if err != nil {
return fmt.Errorf("Could not read ipLoadbalancing order: %q", err)
}
var serviceName string
if strings.Contains(details[0].Domain, "-zone-") {
serviceName = strings.Split(details[0].Domain, "-zone-")[0]
} else {
serviceName = details[0].Domain
}
config := meta.(*Config)
log.Printf("[DEBUG] Will read ipLoadbalancing: %s", serviceName)
r := &IpLoadbalancing{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s", serviceName)
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 resourceIpLoadbalancingDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
id := d.Id()
serviceName := d.Get("service_name").(string)
terminate := func() (string, error) {
log.Printf("[DEBUG] Will terminate ipLoadbalancing %s for order %s", serviceName, id)
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 for order %s", serviceName, id)
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
}
if err := orderDelete(d, meta, terminate, confirmTerminate); err != nil {
return err
}
return nil
}