/
resource_ibm_lbaas_health_monitor.go
198 lines (177 loc) · 6.59 KB
/
resource_ibm_lbaas_health_monitor.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package classicinfrastructure
import (
"fmt"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/softlayer/softlayer-go/datatypes"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/sl"
)
func ResourceIBMLbaasHealthMonitor() *schema.Resource {
return &schema.Resource{
Create: resourceIBMLbaasHealthMonitorCreate,
Read: resourceIBMLbaasHealthMonitorRead,
Delete: resourceIBMLbaasHealthMonitorDelete,
Update: resourceIBMLbaasHealthMonitorUpdate,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"HTTP", "HTTPS", "TCP"}),
Description: "Protocol value",
},
"port": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validate.ValidatePortRange(1, 65535),
Description: "Port number",
},
"interval": {
Type: schema.TypeInt,
Optional: true,
Default: 5,
ValidateFunc: validate.ValidateInterval,
Description: "Interval value",
},
"max_retries": {
Type: schema.TypeInt,
Optional: true,
Default: 2,
ValidateFunc: validate.ValidateMaxRetries,
Description: "Maximum retry counts",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 2,
ValidateFunc: validate.ValidateTimeout,
Description: "Timeout in seconds",
},
"url_path": {
Type: schema.TypeString,
Optional: true,
Default: "/",
ValidateFunc: validate.ValidateURLPath,
Description: "URL Path",
},
"monitor_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Monitor ID",
},
"lbaas_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "LBAAS id",
},
},
}
}
func resourceIBMLbaasHealthMonitorCreate(d *schema.ResourceData, meta interface{}) error {
sess := meta.(conns.ClientSession).SoftLayerSession()
healthMonitorService := services.GetNetworkLBaaSHealthMonitorService(sess.SetRetries(0))
lbaasID := d.Get("lbaas_id").(string)
healthMonitors := make([]datatypes.Network_LBaaS_LoadBalancerHealthMonitorConfiguration, 0, 1)
healthMonitor := datatypes.Network_LBaaS_LoadBalancerHealthMonitorConfiguration{
BackendPort: sl.Int(d.Get("port").(int)),
BackendProtocol: sl.String(d.Get("protocol").(string)),
HealthMonitorUuid: sl.String(d.Get("monitor_id").(string)),
Interval: sl.Int(d.Get("interval").(int)),
Timeout: sl.Int(d.Get("timeout").(int)),
MaxRetries: sl.Int(d.Get("max_retries").(int)),
UrlPath: sl.String(d.Get("url_path").(string)),
}
healthMonitors = append(healthMonitors, healthMonitor)
_, err := waitForLbaasLBActive(d, meta)
if err != nil {
return fmt.Errorf("[ERROR] Error waiting for load balancer (%s) to become ready: %s", d.Id(), err)
}
_, err = healthMonitorService.UpdateLoadBalancerHealthMonitors(sl.String(lbaasID), healthMonitors)
if err != nil {
return fmt.Errorf("[ERROR] Error adding health monitors: %#v", err)
}
_, err = waitForLbaasLBActive(d, meta)
if err != nil {
return fmt.Errorf("[ERROR] Error waiting for load balancer (%s) to become ready: %s", d.Id(), err)
}
d.SetId(fmt.Sprintf("%s/%s", lbaasID, d.Get("monitor_id").(string)))
return resourceIBMLbaasHealthMonitorRead(d, meta)
}
func resourceIBMLbaasHealthMonitorRead(d *schema.ResourceData, meta interface{}) error {
sess := meta.(conns.ClientSession).SoftLayerSession()
service := services.GetNetworkLBaaSLoadBalancerService(sess)
parts, err := flex.IdParts(d.Id())
if err != nil {
return err
}
lbaasID := parts[0]
monitorID := parts[1]
result, err := service.Mask("listeners.defaultPool.healthMonitor").GetLoadBalancer(sl.String(lbaasID))
if err != nil {
return fmt.Errorf("[ERROR] Error retrieving load balancer: %s", err)
}
for _, i := range result.Listeners {
if monitorID == *i.DefaultPool.HealthMonitor.Uuid {
d.Set("protocol", *i.Protocol)
d.Set("port", *i.DefaultPool.ProtocolPort)
d.Set("interval", *i.DefaultPool.HealthMonitor.Interval)
d.Set("max_retries", *i.DefaultPool.HealthMonitor.MaxRetries)
d.Set("timeout", *i.DefaultPool.HealthMonitor.Timeout)
if i.DefaultPool.HealthMonitor.UrlPath != nil && *i.Protocol == "HTTP" {
d.Set("url_path", *i.DefaultPool.HealthMonitor.UrlPath)
}
break
}
}
return nil
}
func resourceIBMLbaasHealthMonitorUpdate(d *schema.ResourceData, meta interface{}) error {
sess := meta.(conns.ClientSession).SoftLayerSession()
healthMonitorService := services.GetNetworkLBaaSHealthMonitorService(sess.SetRetries(0))
parts, err := flex.IdParts(d.Id())
if err != nil {
return err
}
lbaasID := parts[0]
monitorID := parts[1]
if d.HasChange("interval") || d.HasChange("timeout") || d.HasChange("max_retries") || d.HasChange("url_path") {
healthMonitors := make([]datatypes.Network_LBaaS_LoadBalancerHealthMonitorConfiguration, 0, 1)
healthMonitor := datatypes.Network_LBaaS_LoadBalancerHealthMonitorConfiguration{
BackendPort: sl.Int(d.Get("port").(int)),
BackendProtocol: sl.String(d.Get("protocol").(string)),
HealthMonitorUuid: sl.String(monitorID),
Interval: sl.Int(d.Get("interval").(int)),
Timeout: sl.Int(d.Get("timeout").(int)),
MaxRetries: sl.Int(d.Get("max_retries").(int)),
UrlPath: sl.String(d.Get("url_path").(string)),
}
healthMonitors = append(healthMonitors, healthMonitor)
_, err = waitForLbaasLBActive(d, meta)
if err != nil {
return fmt.Errorf(
"Error waiting for load balancer (%s) to become ready: %s", d.Id(), err)
}
_, err := healthMonitorService.UpdateLoadBalancerHealthMonitors(sl.String(lbaasID), healthMonitors)
if err != nil {
return fmt.Errorf("[ERROR] Error adding health monitors: %#v", err)
}
_, err = waitForLbaasLBActive(d, meta)
if err != nil {
return fmt.Errorf(
"Error waiting for load balancer (%s) to become ready: %s", d.Id(), err)
}
}
return resourceIBMLbaasHealthMonitorRead(d, meta)
}
func resourceIBMLbaasHealthMonitorDelete(d *schema.ResourceData, meta interface{}) error {
fmt.Println("Health monitor is destroyed only when the corresponding protocol is removed")
d.SetId("")
return nil
}