/
resource_vcd_lb_virtual_server.go
282 lines (249 loc) · 9.71 KB
/
resource_vcd_lb_virtual_server.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package vcd
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
func resourceVcdLBVirtualServer() *schema.Resource {
return &schema.Resource{
Create: resourceVcdLBVirtualServerCreate,
Read: resourceVcdLBVirtualServerRead,
Update: resourceVcdLBVirtualServerUpdate,
Delete: resourceVcdLBVirtualServerDelete,
Importer: &schema.ResourceImporter{
State: resourceVcdLBVirtualServerImport,
},
Schema: map[string]*schema.Schema{
"org": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of organization to use, optional if defined at provider " +
"level. Useful when connected as sysadmin working across different organizations",
},
"vdc": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of VDC to use, optional if defined at provider level",
},
"edge_gateway": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Edge gateway name in which the LB Virtual Server is located",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Unique Virtual Server name",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Virtual Server description",
},
"enabled": {
Type: schema.TypeBool,
Default: true,
Optional: true,
Description: "Defines if the virtual server is enabled",
},
"ip_address": {
Type: schema.TypeString,
Required: true,
Description: "IP address that the load balancer listens on",
ValidateFunc: validation.IsIPAddress,
},
"protocol": {
Type: schema.TypeString,
Required: true,
Description: "Protocol that the virtual server accepts",
ValidateFunc: validateCase("lower"),
},
"port": {
Type: schema.TypeInt,
Required: true,
Description: "Port number that the load balancer listens on",
},
"enable_acceleration": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable virtual server acceleration",
},
"connection_limit": {
Type: schema.TypeInt,
Optional: true,
Description: "Maximum concurrent connections that the virtual server can process",
},
"connection_rate_limit": {
Type: schema.TypeInt,
Optional: true,
Description: "Maximum incoming new connection requests per second",
},
"app_profile_id": {
Type: schema.TypeString,
Optional: true,
Description: "Application profile ID to be associated with the virtual server",
},
"server_pool_id": {
Type: schema.TypeString,
Optional: true,
Description: "The server pool that the load balancer will use",
},
"app_rule_ids": {
Type: schema.TypeList,
Optional: true,
Description: "List of attached application rule IDs",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceVcdLBVirtualServerCreate(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
vcdClient.lockParentEdgeGtw(d)
defer vcdClient.unLockParentEdgeGtw(d)
edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway")
if err != nil {
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
lBVirtualServer, err := getLBVirtualServerType(d)
if err != nil {
return fmt.Errorf("unable to make load balancer virtual server query: %s", err)
}
createdVirtualServer, err := edgeGateway.CreateLbVirtualServer(lBVirtualServer)
if err != nil {
return fmt.Errorf("error creating new load balancer virtual server: %s", err)
}
d.SetId(createdVirtualServer.ID)
return resourceVcdLBVirtualServerRead(d, meta)
}
func resourceVcdLBVirtualServerRead(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway")
if err != nil {
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
readVirtualServer, err := edgeGateway.GetLbVirtualServerById(d.Id())
if err != nil {
d.SetId("")
return fmt.Errorf("unable to find load balancer virtual server with ID %s: %s", d.Id(), err)
}
return setlBVirtualServerData(d, readVirtualServer)
}
func resourceVcdLBVirtualServerUpdate(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
vcdClient.lockParentEdgeGtw(d)
defer vcdClient.unLockParentEdgeGtw(d)
edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway")
if err != nil {
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
updateVirtualServerConfig, err := getLBVirtualServerType(d)
updateVirtualServerConfig.ID = d.Id() // We already know an ID for update and it allows to change name
if err != nil {
return fmt.Errorf("could not create load balancer virtual server type for update: %s", err)
}
updatedVirtualServer, err := edgeGateway.UpdateLbVirtualServer(updateVirtualServerConfig)
if err != nil {
return fmt.Errorf("unable to update load balancer virtual server with ID %s: %s", d.Id(), err)
}
return setlBVirtualServerData(d, updatedVirtualServer)
}
func resourceVcdLBVirtualServerDelete(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
vcdClient.lockParentEdgeGtw(d)
defer vcdClient.unLockParentEdgeGtw(d)
edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway")
if err != nil {
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
err = edgeGateway.DeleteLbVirtualServerById(d.Id())
if err != nil {
return fmt.Errorf("error deleting load balancer virtual server: %s", err)
}
d.SetId("")
return nil
}
// resourceVcdLBVirtualServerImport is responsible for importing the resource.
// The following steps happen as part of import
// 1. The user supplies `terraform import _resource_name_ _the_id_string_` command
// 2. `_the_id_string_` contains a dot formatted path to resource as in the example below
// 3. The functions splits the dot-formatted path and tries to lookup the object
// 4. If the lookup succeeds it sets the ID field for `_resource_name_` resource in statefile
// (the resource must be already defined in .tf config otherwise `terraform import` will complain)
// 5. `terraform refresh` is being implicitly launched. The Read method looks up all other fields
// based on the known ID of object.
//
// Example resource name (_resource_name_): vcd_lb_virtual_server.my-test-virtual-server
// Example import path (_the_id_string_): org.vdc.edge-gw.existing-virtual-server
// Note: the separator can be changed using Provider.import_separator or variable VCD_IMPORT_SEPARATOR
func resourceVcdLBVirtualServerImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
resourceURI := strings.Split(d.Id(), ImportSeparator)
if len(resourceURI) != 4 {
return nil, fmt.Errorf("resource name must be specified as org.vdc.edge-gw.lb-virtual-server")
}
orgName, vdcName, edgeName, virtualServerName := resourceURI[0], resourceURI[1], resourceURI[2], resourceURI[3]
vcdClient := meta.(*VCDClient)
edgeGateway, err := vcdClient.GetEdgeGateway(orgName, vdcName, edgeName)
if err != nil {
return nil, fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
readVirtualServer, err := edgeGateway.GetLbVirtualServerByName(virtualServerName)
if err != nil {
return []*schema.ResourceData{}, fmt.Errorf("unable to find load balancer virtual server with name %s: %s",
d.Id(), err)
}
dSet(d, "org", orgName)
dSet(d, "vdc", vdcName)
dSet(d, "edge_gateway", edgeName)
dSet(d, "name", virtualServerName)
d.SetId(readVirtualServer.ID)
return []*schema.ResourceData{d}, nil
}
// getLBVirtualServerType converts schema.ResourceData to *types.LbVirtualServer and is useful
// for creating API requests
func getLBVirtualServerType(d *schema.ResourceData) (*types.LbVirtualServer, error) {
lbVirtualServer := &types.LbVirtualServer{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Enabled: d.Get("enabled").(bool),
IpAddress: d.Get("ip_address").(string),
Protocol: d.Get("protocol").(string),
Port: d.Get("port").(int),
AccelerationEnabled: d.Get("enable_acceleration").(bool),
ConnectionLimit: d.Get("connection_limit").(int),
ConnectionRateLimit: d.Get("connection_rate_limit").(int),
ApplicationProfileId: d.Get("app_profile_id").(string),
DefaultPoolId: d.Get("server_pool_id").(string),
}
// convert list of app rule ids to slice of strings
var appRuleIds []string
rules := d.Get("app_rule_ids").([]interface{})
for _, rule := range rules {
ruleString := rule.(string)
appRuleIds = append(appRuleIds, ruleString)
}
lbVirtualServer.ApplicationRuleIds = appRuleIds
return lbVirtualServer, nil
}
// setlBVirtualServerData sets object state from *types.LbVirtualServer
func setlBVirtualServerData(d *schema.ResourceData, lBVirtualServer *types.LbVirtualServer) error {
dSet(d, "name", lBVirtualServer.Name)
dSet(d, "description", lBVirtualServer.Description)
dSet(d, "enabled", lBVirtualServer.Enabled)
dSet(d, "ip_address", lBVirtualServer.IpAddress)
dSet(d, "protocol", lBVirtualServer.Protocol)
dSet(d, "port", lBVirtualServer.Port)
dSet(d, "enable_acceleration", lBVirtualServer.AccelerationEnabled)
dSet(d, "connection_limit", lBVirtualServer.ConnectionLimit)
dSet(d, "connection_rate_limit", lBVirtualServer.ConnectionRateLimit)
dSet(d, "app_profile_id", lBVirtualServer.ApplicationProfileId)
dSet(d, "server_pool_id", lBVirtualServer.DefaultPoolId)
return d.Set("app_rule_ids", lBVirtualServer.ApplicationRuleIds)
}