forked from vmware/go-vcloud-director
/
nsxt_firewall_group.go
376 lines (308 loc) · 14.6 KB
/
nsxt_firewall_group.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright 2021 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/
package govcd
import (
"errors"
"fmt"
"net/url"
"github.com/reski-rukmantiyo/go-vcloud-director/v2/types/v56"
)
// NsxtFirewallGroup uses OpenAPI endpoint to operate NSX-T Security Groups and IP Sets which use
// the same Firewall Group API endpoint
//
// IP sets are groups of objects to which the firewall rules apply. Combining multiple objects into
// IP sets helps reduce the total number of firewall rules to be created.
//
// Security groups are groups of Org Vdc networks to which distributed firewall rules apply.
// Grouping networks helps you to reduce the total number of distributed firewall rules to be
// created.
type NsxtFirewallGroup struct {
NsxtFirewallGroup *types.NsxtFirewallGroup
client *Client
}
// CreateNsxtFirewallGroup allows users to create NSX-T Firewall Group
func (vdc *Vdc) CreateNsxtFirewallGroup(firewallGroupConfig *types.NsxtFirewallGroup) (*NsxtFirewallGroup, error) {
return createNsxtFirewallGroup(vdc.client, firewallGroupConfig)
}
// CreateNsxtFirewallGroup allows users to create NSX-T Firewall Group
func (egw *NsxtEdgeGateway) CreateNsxtFirewallGroup(firewallGroupConfig *types.NsxtFirewallGroup) (*NsxtFirewallGroup, error) {
return createNsxtFirewallGroup(egw.client, firewallGroupConfig)
}
// GetAllNsxtFirewallGroups allows users to retrieve all Firewall Groups for Org
// firewallGroupType can be one of the following:
// * types.FirewallGroupTypeSecurityGroup - for NSX-T Security Groups
// * types.FirewallGroupTypeIpSet - for NSX-T IP Sets
// * "" (empty) - search will not be limited and will get both - IP Sets and Security Groups
//
// It is possible to add additional filtering by using queryParameters of type 'url.Values'.
// One special filter is `_context==` filtering. Value can be one of the following:
//
// * Org Vdc Network ID (_context==networkId) - Returns all the firewall groups which the specified
// network is a member of.
//
// * Edge Gateway ID (_context==edgeGatewayId) - Returns all the firewall groups which are available
// to the specific edge gateway. Or use a shorthand NsxtEdgeGateway.GetAllNsxtFirewallGroups() which
// automatically injects this filter.
//
// * Network Provider ID (_context==networkProviderId) - Returns all the firewall groups which are
// available under a specific network provider. This context requires system admin privilege.
// 'networkProviderId' is NSX-T manager ID
func (org *Org) GetAllNsxtFirewallGroups(queryParameters url.Values, firewallGroupType string) ([]*NsxtFirewallGroup, error) {
queryParams := copyOrNewUrlValues(queryParameters)
if firewallGroupType != "" {
queryParams = queryParameterFilterAnd("type=="+firewallGroupType, queryParams)
}
return getAllNsxtFirewallGroups(org.client, queryParams)
}
// GetAllNsxtFirewallGroups allows users to retrieve all NSX-T Firewall Groups
func (vdc *Vdc) GetAllNsxtFirewallGroups(queryParameters url.Values, firewallGroupType string) ([]*NsxtFirewallGroup, error) {
if vdc.IsNsxv() {
return nil, errors.New("only NSX-T VDCs support Firewall Groups")
}
return getAllNsxtFirewallGroups(vdc.client, queryParameters)
}
// GetAllNsxtFirewallGroups allows users to retrieve all NSX-T Firewall Groups in a particular Edge Gateway
// firewallGroupType can be one of the following:
// * types.FirewallGroupTypeSecurityGroup - for NSX-T Security Groups
// * types.FirewallGroupTypeIpSet - for NSX-T IP Sets
// * "" (empty) - search will not be limited and will get both - IP Sets and Security Groups
func (egw *NsxtEdgeGateway) GetAllNsxtFirewallGroups(queryParameters url.Values, firewallGroupType string) ([]*NsxtFirewallGroup, error) {
queryParams := copyOrNewUrlValues(queryParameters)
if firewallGroupType != "" {
queryParams = queryParameterFilterAnd("type=="+firewallGroupType, queryParams)
}
// Automatically inject Edge Gateway filter because this is an Edge Gateway scoped query
queryParams = queryParameterFilterAnd("_context=="+egw.EdgeGateway.ID, queryParams)
return getAllNsxtFirewallGroups(egw.client, queryParams)
}
// GetNsxtFirewallGroupByName allows users to retrieve Firewall Group by Name
// firewallGroupType can be one of the following:
// * types.FirewallGroupTypeSecurityGroup - for NSX-T Security Groups
// * types.FirewallGroupTypeIpSet - for NSX-T IP Sets
// * "" (empty) - search will not be limited and will get both - IP Sets and Security Groups
//
// Note. One might get an error if IP Set and Security Group exist with the same name (two objects
// of the same type cannot exist) and firewallGroupType is left empty.
func (org *Org) GetNsxtFirewallGroupByName(name, firewallGroupType string) (*NsxtFirewallGroup, error) {
queryParameters := url.Values{}
if firewallGroupType != "" {
queryParameters = queryParameterFilterAnd("type=="+firewallGroupType, queryParameters)
}
return getNsxtFirewallGroupByName(org.client, name, queryParameters)
}
// GetNsxtFirewallGroupByName allows users to retrieve Firewall Group by Name
// firewallGroupType can be one of the following:
// * types.FirewallGroupTypeSecurityGroup - for NSX-T Security Groups
// * types.FirewallGroupTypeIpSet - for NSX-T IP Sets
// * "" (empty) - search will not be limited and will get both - IP Sets and Security Groups
//
// Note. One might get an error if IP Set and Security Group exist with the same name (two objects
// of the same type cannot exist) and firewallGroupType is left empty.
func (vdc *Vdc) GetNsxtFirewallGroupByName(name, firewallGroupType string) (*NsxtFirewallGroup, error) {
queryParameters := url.Values{}
if firewallGroupType != "" {
queryParameters = queryParameterFilterAnd("type=="+firewallGroupType, queryParameters)
}
return getNsxtFirewallGroupByName(vdc.client, name, queryParameters)
}
// GetNsxtFirewallGroupByName allows users to retrieve Firewall Group by Name in a particular Edge Gateway
// firewallGroupType can be one of the following:
// * types.FirewallGroupTypeSecurityGroup - for NSX-T Security Groups
// * types.FirewallGroupTypeIpSet - for NSX-T IP Sets
// * "" (empty) - search will not be limited and will get both - IP Sets and Security Groups
//
// Note. One might get an error if IP Set and Security Group exist with the same name (two objects
// of the same type cannot exist) and firewallGroupType is left empty.
func (egw *NsxtEdgeGateway) GetNsxtFirewallGroupByName(name string, firewallGroupType string) (*NsxtFirewallGroup, error) {
queryParameters := url.Values{}
if firewallGroupType != "" {
queryParameters = queryParameterFilterAnd("type=="+firewallGroupType, queryParameters)
}
// Automatically inject Edge Gateway filter because this is an Edge Gateway scoped query
queryParameters = queryParameterFilterAnd("_context=="+egw.EdgeGateway.ID, queryParameters)
return getNsxtFirewallGroupByName(egw.client, name, queryParameters)
}
// GetNsxtFirewallGroupById retrieves NSX-T Firewall Group by ID
func (org *Org) GetNsxtFirewallGroupById(id string) (*NsxtFirewallGroup, error) {
return getNsxtFirewallGroupById(org.client, id)
}
// GetNsxtFirewallGroupById retrieves NSX-T Firewall Group by ID
func (vdc *Vdc) GetNsxtFirewallGroupById(id string) (*NsxtFirewallGroup, error) {
return getNsxtFirewallGroupById(vdc.client, id)
}
// GetNsxtFirewallGroupById retrieves NSX-T Firewall Group by ID
func (egw *NsxtEdgeGateway) GetNsxtFirewallGroupById(id string) (*NsxtFirewallGroup, error) {
return getNsxtFirewallGroupById(egw.client, id)
}
// Update allows users to update NSX-T Firewall Group
func (firewallGroup *NsxtFirewallGroup) Update(firewallGroupConfig *types.NsxtFirewallGroup) (*NsxtFirewallGroup, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointFirewallGroups
minimumApiVersion, err := firewallGroup.client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
}
if firewallGroupConfig.ID == "" {
return nil, fmt.Errorf("cannot update NSX-T Firewall Group without ID")
}
urlRef, err := firewallGroup.client.OpenApiBuildEndpoint(endpoint, firewallGroupConfig.ID)
if err != nil {
return nil, err
}
returnObject := &NsxtFirewallGroup{
NsxtFirewallGroup: &types.NsxtFirewallGroup{},
client: firewallGroup.client,
}
err = firewallGroup.client.OpenApiPutItem(minimumApiVersion, urlRef, nil, firewallGroupConfig, returnObject.NsxtFirewallGroup, nil)
if err != nil {
return nil, fmt.Errorf("error updating NSX-T firewall group: %s", err)
}
return returnObject, nil
}
// Delete allows users to delete NSX-T Firewall Group
func (firewallGroup *NsxtFirewallGroup) Delete() error {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointFirewallGroups
minimumApiVersion, err := firewallGroup.client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return err
}
if firewallGroup.NsxtFirewallGroup.ID == "" {
return fmt.Errorf("cannot delete NSX-T Firewall Group without ID")
}
urlRef, err := firewallGroup.client.OpenApiBuildEndpoint(endpoint, firewallGroup.NsxtFirewallGroup.ID)
if err != nil {
return err
}
err = firewallGroup.client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil, nil)
if err != nil {
return fmt.Errorf("error deleting NSX-T Firewall Group: %s", err)
}
return nil
}
// GetAssociatedVms allows users to retrieve a list of references to child VMs (with vApps when they exist).
//
// Note. Only Security Groups have associated VMs. Executing it on an IP Set will return an error
// similar to: "only Security Groups have associated VMs. This Firewall Group has type 'IP_SET'"
func (firewallGroup *NsxtFirewallGroup) GetAssociatedVms() ([]*types.NsxtFirewallGroupMemberVms, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointFirewallGroups
minimumApiVersion, err := firewallGroup.client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
}
if firewallGroup.NsxtFirewallGroup.ID == "" {
return nil, fmt.Errorf("cannot retrieve associated VMs for NSX-T Firewall Group without ID")
}
if !firewallGroup.IsSecurityGroup() {
return nil, fmt.Errorf("only Security Groups have associated VMs. This Firewall Group has type '%s'",
firewallGroup.NsxtFirewallGroup.Type)
}
urlRef, err := firewallGroup.client.OpenApiBuildEndpoint(endpoint, firewallGroup.NsxtFirewallGroup.ID, "/associatedVMs")
if err != nil {
return nil, err
}
associatedVms := []*types.NsxtFirewallGroupMemberVms{{}}
err = firewallGroup.client.OpenApiGetAllItems(minimumApiVersion, urlRef, nil, &associatedVms, nil)
if err != nil {
return nil, fmt.Errorf("error retrieving associated VMs: %s", err)
}
return associatedVms, nil
}
// IsSecurityGroup allows users to check if Firewall Group is a Security Group
func (firewallGroup *NsxtFirewallGroup) IsSecurityGroup() bool {
return firewallGroup.NsxtFirewallGroup.Type == types.FirewallGroupTypeSecurityGroup
}
// IsIpSet allows users to check if Firewall Group is an IP Set
func (firewallGroup *NsxtFirewallGroup) IsIpSet() bool {
return firewallGroup.NsxtFirewallGroup.Type == types.FirewallGroupTypeIpSet
}
func getNsxtFirewallGroupByName(client *Client, name string, queryParameters url.Values) (*NsxtFirewallGroup, error) {
queryParams := copyOrNewUrlValues(queryParameters)
queryParams = queryParameterFilterAnd("name=="+name, queryParams)
allGroups, err := getAllNsxtFirewallGroups(client, queryParams)
if err != nil {
return nil, fmt.Errorf("could not find NSX-T Firewall Group with name '%s': %s", name, err)
}
if len(allGroups) == 0 {
return nil, fmt.Errorf("%s: expected exactly one NSX-T Firewall Group with name '%s'. Got %d", ErrorEntityNotFound, name, len(allGroups))
}
if len(allGroups) > 1 {
return nil, fmt.Errorf("expected exactly one NSX-T Firewall Group with name '%s'. Got %d", name, len(allGroups))
}
// TODO API V36.0 - maybe it is fixed
// There is a bug that not all data is present (e.g. missing IpAddresses field for IP_SET) when
// using "getAll" endpoint therefore after finding the object by name we must retrieve it once
// again using its direct endpoint.
//
// return allGroups[0], nil
return getNsxtFirewallGroupById(client, allGroups[0].NsxtFirewallGroup.ID)
}
func getNsxtFirewallGroupById(client *Client, id string) (*NsxtFirewallGroup, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointFirewallGroups
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
}
if id == "" {
return nil, fmt.Errorf("empty NSX-T Firewall Group ID specified")
}
urlRef, err := client.OpenApiBuildEndpoint(endpoint, id)
if err != nil {
return nil, err
}
fwGroup := &NsxtFirewallGroup{
NsxtFirewallGroup: &types.NsxtFirewallGroup{},
client: client,
}
err = client.OpenApiGetItem(minimumApiVersion, urlRef, nil, fwGroup.NsxtFirewallGroup, nil)
if err != nil {
return nil, err
}
return fwGroup, nil
}
func getAllNsxtFirewallGroups(client *Client, queryParameters url.Values) ([]*NsxtFirewallGroup, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointFirewallGroups
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
}
// This Object does not follow regular REST scheme and for get the endpoint must be
// 1.0.0/firewallGroups/summaries therefore bellow "summaries" is appended to the path
urlRef, err := client.OpenApiBuildEndpoint(endpoint, "summaries")
if err != nil {
return nil, err
}
typeResponses := []*types.NsxtFirewallGroup{{}}
err = client.OpenApiGetAllItems(minimumApiVersion, urlRef, queryParameters, &typeResponses, nil)
if err != nil {
return nil, err
}
// Wrap all typeResponses into NsxtEdgeGateway types with client
wrappedResponses := make([]*NsxtFirewallGroup, len(typeResponses))
for sliceIndex := range typeResponses {
wrappedResponses[sliceIndex] = &NsxtFirewallGroup{
NsxtFirewallGroup: typeResponses[sliceIndex],
client: client,
}
}
return wrappedResponses, nil
}
func createNsxtFirewallGroup(client *Client, firewallGroupConfig *types.NsxtFirewallGroup) (*NsxtFirewallGroup, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointFirewallGroups
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
}
urlRef, err := client.OpenApiBuildEndpoint(endpoint)
if err != nil {
return nil, err
}
returnObject := &NsxtFirewallGroup{
NsxtFirewallGroup: &types.NsxtFirewallGroup{},
client: client,
}
err = client.OpenApiPostItem(minimumApiVersion, urlRef, nil, firewallGroupConfig, returnObject.NsxtFirewallGroup, nil)
if err != nil {
return nil, fmt.Errorf("error creating NSX-T Firewall Group: %s", err)
}
return returnObject, nil
}