forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_clc_load_balancer.go
129 lines (120 loc) · 3.26 KB
/
resource_clc_load_balancer.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
package clc
import (
"fmt"
"log"
"time"
clc "github.com/CenturyLinkCloud/clc-sdk"
"github.com/CenturyLinkCloud/clc-sdk/lb"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceCLCLoadBalancer() *schema.Resource {
return &schema.Resource{
Create: resourceCLCLoadBalancerCreate,
Read: resourceCLCLoadBalancerRead,
Update: resourceCLCLoadBalancerUpdate,
Delete: resourceCLCLoadBalancerDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"data_center": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
// optional
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "enabled",
},
// computed
"ip_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceCLCLoadBalancerCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clc.Client)
dc := d.Get("data_center").(string)
name := d.Get("name").(string)
desc := d.Get("description").(string)
status := d.Get("status").(string)
r1 := lb.LoadBalancer{
Name: name,
Description: desc,
Status: status,
}
l, err := client.LB.Create(dc, r1)
if err != nil {
return fmt.Errorf("Failed creating load balancer under %v/%v: %v", dc, name, err)
}
d.SetId(l.ID)
return resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := client.LB.Get(dc, l.ID)
if err != nil {
return resource.RetryableError(err)
}
err = resourceCLCLoadBalancerRead(d, meta)
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
}
func resourceCLCLoadBalancerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clc.Client)
dc := d.Get("data_center").(string)
id := d.Id()
resp, err := client.LB.Get(dc, id)
if err != nil {
log.Printf("[INFO] Failed finding load balancer %v/%v. Marking destroyed", dc, id)
d.SetId("")
return nil
}
d.Set("description", resp.Description)
d.Set("ip_address", resp.IPaddress)
d.Set("status", resp.Status)
d.Set("pools", resp.Pools)
d.Set("links", resp.Links)
return nil
}
func resourceCLCLoadBalancerUpdate(d *schema.ResourceData, meta interface{}) error {
update := lb.LoadBalancer{}
client := meta.(*clc.Client)
dc := d.Get("data_center").(string)
id := d.Id()
if d.HasChange("name") {
update.Name = d.Get("name").(string)
}
if d.HasChange("description") {
update.Description = d.Get("description").(string)
}
if d.HasChange("status") {
update.Status = d.Get("status").(string)
}
if update.Name != "" || update.Description != "" || update.Status != "" {
err := client.LB.Update(dc, id, update)
if err != nil {
return fmt.Errorf("Failed updating load balancer under %v/%v: %v", dc, id, err)
}
}
return resourceCLCLoadBalancerRead(d, meta)
}
func resourceCLCLoadBalancerDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clc.Client)
dc := d.Get("data_center").(string)
id := d.Id()
err := client.LB.Delete(dc, id)
if err != nil {
return fmt.Errorf("Failed deleting loadbalancer %v: %v", id, err)
}
return nil
}