-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource_vrack_ip_loadbalancing.go
123 lines (97 loc) · 3.48 KB
/
resource_vrack_ip_loadbalancing.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
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 resourceVrackIpLoadbalancing() *schema.Resource {
return &schema.Resource{
Create: resourceVrackIpLoadbalancingCreate,
Read: resourceVrackIpLoadbalancingRead,
Delete: resourceVrackIpLoadbalancingDelete,
Importer: &schema.ResourceImporter{
State: resourceVrackIpLoadbalancingImportState,
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Description: "The internal name of your vrack",
Required: true,
ForceNew: true,
},
"ip_loadbalancing": {
Type: schema.TypeString,
Description: "Your ipLoadbalancing",
Required: true,
ForceNew: true,
},
},
}
}
func resourceVrackIpLoadbalancingImportState(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/iploadbalancing formatted")
}
serviceName := splitId[0]
ipLoadbalancing := splitId[1]
d.SetId(fmt.Sprintf("%s-%s", serviceName, ipLoadbalancing))
d.Set("service_name", serviceName)
d.Set("ip_loadbalancing", ipLoadbalancing)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceVrackIpLoadbalancingCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
opts := (&VrackIpLoadbalancingCreateOpts{}).FromResource(d)
task := &VrackTask{}
endpoint := fmt.Sprintf("/vrack/%s/ipLoadbalancing", serviceName)
if err := config.OVHClient.Post(endpoint, opts, task); err != nil {
return fmt.Errorf("Error calling POST %s with opts %v:\n\t %q", endpoint, opts, err)
}
if err := waitForVrackTask(task, config.OVHClient); err != nil {
return fmt.Errorf("Error waiting for vrack (%s) to attach dedicated server %v: %s", serviceName, opts, err)
}
//set id
d.SetId(fmt.Sprintf("%s-%s", serviceName, opts.IpLoadbalancing))
return resourceVrackIpLoadbalancingRead(d, meta)
}
func resourceVrackIpLoadbalancingRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vds := &VrackIpLoadbalancing{}
serviceName := d.Get("service_name").(string)
ipLoadbalancing := d.Get("ip_loadbalancing").(string)
endpoint := fmt.Sprintf("/vrack/%s/ipLoadbalancing/%s",
url.PathEscape(serviceName),
url.PathEscape(ipLoadbalancing),
)
if err := config.OVHClient.Get(endpoint, vds); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
d.Set("service_name", vds.Vrack)
d.Set("ip_loadbalancing", vds.IpLoadbalancing)
return nil
}
func resourceVrackIpLoadbalancingDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
ipLoadbalancing := d.Get("ip_loadbalancing").(string)
task := &VrackTask{}
endpoint := fmt.Sprintf("/vrack/%s/ipLoadbalancing/%s",
url.PathEscape(serviceName),
url.PathEscape(ipLoadbalancing),
)
if err := config.OVHClient.Delete(endpoint, task); err != nil {
return fmt.Errorf("Error calling DELETE %s with %s/%s:\n\t %q", endpoint, serviceName, ipLoadbalancing, err)
}
if err := waitForVrackTask(task, config.OVHClient); err != nil {
return fmt.Errorf("Error waiting for vrack (%s) to detach dedicated server (%s): %s", serviceName, ipLoadbalancing, err)
}
d.SetId("")
return nil
}