forked from vmware/go-vcloud-director
/
nsxt_alb_service_engine_groups.go
256 lines (208 loc) · 9.16 KB
/
nsxt_alb_service_engine_groups.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
/*
* Copyright 2021 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/
package govcd
import (
"errors"
"fmt"
"net/url"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
// NsxtAlbServiceEngineGroup provides virtual service management capabilities for tenants. This entity can be created
// by referencing a backing importable service engine group - NsxtAlbImportableServiceEngineGroups.
//
// A service engine group is an isolation domain that also defines shared service engine properties, such as size,
// network access, and failover. Resources in a service engine group can be used for different virtual services,
// depending on your tenant needs. These resources cannot be shared between different service engine groups.
type NsxtAlbServiceEngineGroup struct {
NsxtAlbServiceEngineGroup *types.NsxtAlbServiceEngineGroup
vcdClient *VCDClient
}
// GetAllAlbServiceEngineGroups retrieves NSX-T ALB Service Engines with possible filters
//
// Context is not mandatory for this resource. Supported contexts are:
// * Gateway ID (_context==gatewayId) - returns all Load Balancer Service Engine Groups that are accessible to the
// gateway.
// * Assignable Gateway ID (_context=gatewayId;_context==assignable) returns all Load Balancer Service Engine Groups
// that are assignable to the gateway. This filters out any Load Balancer Service Engine groups that are already
// assigned to the gateway or assigned to another gateway if the reservation type is 'DEDICATED’.
func (vcdClient *VCDClient) GetAllAlbServiceEngineGroups(context string, queryParameters url.Values) ([]*NsxtAlbServiceEngineGroup, error) {
client := vcdClient.Client
if !client.IsSysAdmin {
return nil, errors.New("handling NSX-T ALB Service Engine Groups require System user")
}
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbServiceEngineGroups
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
urlRef, err := client.OpenApiBuildEndpoint(endpoint)
if err != nil {
return nil, err
}
queryParams := copyOrNewUrlValues(queryParameters)
if context != "" {
queryParams = queryParameterFilterAnd(fmt.Sprintf("_context==%s", context), queryParams)
}
typeResponses := []*types.NsxtAlbServiceEngineGroup{{}}
err = client.OpenApiGetAllItems(apiVersion, urlRef, queryParams, &typeResponses, nil)
if err != nil {
return nil, err
}
wrappedResponses := make([]*NsxtAlbServiceEngineGroup, len(typeResponses))
for sliceIndex := range typeResponses {
wrappedResponses[sliceIndex] = &NsxtAlbServiceEngineGroup{
NsxtAlbServiceEngineGroup: typeResponses[sliceIndex],
vcdClient: vcdClient,
}
}
return wrappedResponses, nil
}
// GetAlbServiceEngineGroupByName returns NSX-T ALB Service Engine by Name
// Context is not mandatory for this resource. Supported contexts are:
// * Gateway ID (_context==gatewayId) - returns all Load Balancer Service Engine Groups that are accessible to the
// gateway.
// * Assignable Gateway ID (_context=gatewayId;_context==assignable) returns all Load Balancer Service Engine Groups
// that are assignable to the gateway. This filters out any Load Balancer Service Engine groups that are already
// assigned to the gateway or assigned to another gateway if the reservation type is 'DEDICATED’.
func (vcdClient *VCDClient) GetAlbServiceEngineGroupByName(optionalContext, name string) (*NsxtAlbServiceEngineGroup, error) {
queryParams := copyOrNewUrlValues(nil)
if optionalContext != "" {
queryParams = queryParameterFilterAnd(fmt.Sprintf("_context==%s", optionalContext), queryParams)
}
queryParams.Add("filter", fmt.Sprintf("name==%s", name))
albSeGroups, err := vcdClient.GetAllAlbServiceEngineGroups("", queryParams)
if err != nil {
return nil, fmt.Errorf("error retrieving NSX-T ALB Service Engine Group By Name '%s': %s", name, err)
}
if len(albSeGroups) == 0 {
return nil, fmt.Errorf("%s", ErrorEntityNotFound)
}
if len(albSeGroups) > 1 {
return nil, fmt.Errorf("more than 1 NSX-T ALB Service Engine Group with Name '%s' found", name)
}
return albSeGroups[0], nil
}
// GetAlbServiceEngineGroupById returns importable NSX-T ALB Cloud by ID
func (vcdClient *VCDClient) GetAlbServiceEngineGroupById(id string) (*NsxtAlbServiceEngineGroup, error) {
client := vcdClient.Client
if !client.IsSysAdmin {
return nil, errors.New("handling NSX-T ALB Service Engine Groups require System user")
}
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbServiceEngineGroups
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
urlRef, err := client.OpenApiBuildEndpoint(endpoint, id)
if err != nil {
return nil, err
}
typeResponse := &types.NsxtAlbServiceEngineGroup{}
err = client.OpenApiGetItem(apiVersion, urlRef, nil, &typeResponse, nil)
if err != nil {
return nil, err
}
wrappedResponse := &NsxtAlbServiceEngineGroup{
NsxtAlbServiceEngineGroup: typeResponse,
vcdClient: vcdClient,
}
return wrappedResponse, nil
}
func (vcdClient *VCDClient) CreateNsxtAlbServiceEngineGroup(albServiceEngineGroup *types.NsxtAlbServiceEngineGroup) (*NsxtAlbServiceEngineGroup, error) {
client := vcdClient.Client
if !client.IsSysAdmin {
return nil, errors.New("handling NSX-T ALB Service Engine Groups require System user")
}
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbServiceEngineGroups
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
urlRef, err := client.OpenApiBuildEndpoint(endpoint)
if err != nil {
return nil, err
}
returnObject := &NsxtAlbServiceEngineGroup{
NsxtAlbServiceEngineGroup: &types.NsxtAlbServiceEngineGroup{},
vcdClient: vcdClient,
}
err = client.OpenApiPostItem(apiVersion, urlRef, nil, albServiceEngineGroup, returnObject.NsxtAlbServiceEngineGroup, nil)
if err != nil {
return nil, fmt.Errorf("error creating NSX-T ALB Service Engine Group: %s", err)
}
return returnObject, nil
}
// Update updates existing ALB Controller with new supplied albControllerConfig configuration
func (nsxtAlbServiceEngineGroup *NsxtAlbServiceEngineGroup) Update(albSEGroupConfig *types.NsxtAlbServiceEngineGroup) (*NsxtAlbServiceEngineGroup, error) {
client := nsxtAlbServiceEngineGroup.vcdClient.Client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbServiceEngineGroups
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
if albSEGroupConfig.ID == "" {
return nil, fmt.Errorf("cannot update NSX-T ALB Service Engine Group without ID")
}
urlRef, err := client.OpenApiBuildEndpoint(endpoint, albSEGroupConfig.ID)
if err != nil {
return nil, err
}
responseAlbController := &NsxtAlbServiceEngineGroup{
NsxtAlbServiceEngineGroup: &types.NsxtAlbServiceEngineGroup{},
vcdClient: nsxtAlbServiceEngineGroup.vcdClient,
}
err = client.OpenApiPutItem(apiVersion, urlRef, nil, albSEGroupConfig, responseAlbController.NsxtAlbServiceEngineGroup, nil)
if err != nil {
return nil, fmt.Errorf("error updating NSX-T ALB Service Engine Group: %s", err)
}
return responseAlbController, nil
}
// Delete deletes NSX-T ALB Service Engine Group configuration
func (nsxtAlbServiceEngineGroup *NsxtAlbServiceEngineGroup) Delete() error {
client := nsxtAlbServiceEngineGroup.vcdClient.Client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbServiceEngineGroups
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return err
}
if nsxtAlbServiceEngineGroup.NsxtAlbServiceEngineGroup.ID == "" {
return fmt.Errorf("cannot delete NSX-T ALB Service Engine Group without ID")
}
urlRef, err := client.OpenApiBuildEndpoint(endpoint, nsxtAlbServiceEngineGroup.NsxtAlbServiceEngineGroup.ID)
if err != nil {
return err
}
err = client.OpenApiDeleteItem(apiVersion, urlRef, nil, nil)
if err != nil {
return fmt.Errorf("error deleting NSX-T ALB Service Engine Group: %s", err)
}
return nil
}
// Sync syncs a specified Load Balancer Service Engine Group. It requests the HA mode and the maximum number of
// supported Virtual Services for this Service Engine Group from the Load Balancer, and updates vCD's local record of
// these properties.
func (nsxtAlbServiceEngineGroup *NsxtAlbServiceEngineGroup) Sync() error {
client := nsxtAlbServiceEngineGroup.vcdClient.Client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbServiceEngineGroups
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return err
}
if nsxtAlbServiceEngineGroup.NsxtAlbServiceEngineGroup.ID == "" {
return fmt.Errorf("cannot sync NSX-T ALB Service Engine Group without ID")
}
urlRef, err := client.OpenApiBuildEndpoint(endpoint, nsxtAlbServiceEngineGroup.NsxtAlbServiceEngineGroup.ID, "/sync")
if err != nil {
return err
}
task, err := client.OpenApiPostItemAsync(apiVersion, urlRef, nil, nil)
if err != nil {
return fmt.Errorf("error syncing NSX-T ALB Service Engine Group: %s", err)
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf("sync task for NSX-T ALB Service Engine Group failed: %s", err)
}
return nil
}