-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NSX-T Firewall support #381
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
78b036b
Add NSX-T Firewall support
Didainius 84e2ac5
merge master
Didainius f05dff4
Add appliction port profile references to firewall test
Didainius 5f9cfa9
Remove leftover comment
Didainius 5a1e58d
Move helper function to common test functions file
Didainius 15a38b7
Remove comments
Didainius 6e94dc6
Merge master
Didainius e75ec1e
Rename 'DeleteX' methods to 'DeleteRuleX'
Didainius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright 2021 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. | ||
*/ | ||
|
||
package govcd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/vmware/go-vcloud-director/v2/types/v56" | ||
) | ||
|
||
// NsxtFirewall contains a types.NsxtFirewallRuleContainer which encloses three types of rules - | ||
// system, default and user defined rules. User defined rules are the only ones that can be modified, others are | ||
// read-only. | ||
type NsxtFirewall struct { | ||
NsxtFirewallRuleContainer *types.NsxtFirewallRuleContainer | ||
client *Client | ||
// edgeGatewayId is stored for usage in NsxtFirewall receiver functions | ||
edgeGatewayId string | ||
} | ||
|
||
// UpdateNsxtFirewall allows user to set new firewall rules or update existing ones. The API does not have POST endpoint | ||
// and always uses PUT endpoint for creating and updating. | ||
func (egw *NsxtEdgeGateway) UpdateNsxtFirewall(firewallRules *types.NsxtFirewallRuleContainer) (*NsxtFirewall, error) { | ||
client := egw.client | ||
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointNsxtFirewallRules | ||
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Insert Edge Gateway ID into endpoint path edgeGateways/%s/firewall/rules | ||
urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, egw.EdgeGateway.ID)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
returnObject := &NsxtFirewall{ | ||
NsxtFirewallRuleContainer: &types.NsxtFirewallRuleContainer{}, | ||
client: client, | ||
edgeGatewayId: egw.EdgeGateway.ID, | ||
} | ||
|
||
err = client.OpenApiPutItem(minimumApiVersion, urlRef, nil, firewallRules, returnObject.NsxtFirewallRuleContainer) | ||
if err != nil { | ||
return nil, fmt.Errorf("error setting NSX-T Firewall: %s", err) | ||
} | ||
|
||
return returnObject, nil | ||
} | ||
|
||
// GetNsxtFirewall retrieves all firewall rules system, default and user defined rules | ||
func (egw *NsxtEdgeGateway) GetNsxtFirewall() (*NsxtFirewall, error) { | ||
client := egw.client | ||
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointNsxtFirewallRules | ||
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Insert Edge Gateway ID into endpoint path edgeGateways/%s/firewall/rules | ||
urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, egw.EdgeGateway.ID)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
returnObject := &NsxtFirewall{ | ||
NsxtFirewallRuleContainer: &types.NsxtFirewallRuleContainer{}, | ||
client: client, | ||
edgeGatewayId: egw.EdgeGateway.ID, | ||
} | ||
|
||
err = client.OpenApiGetItem(minimumApiVersion, urlRef, nil, returnObject.NsxtFirewallRuleContainer) | ||
if err != nil { | ||
return nil, fmt.Errorf("error retrieving NSX-T Firewall rules: %s", err) | ||
} | ||
|
||
// Store Edge Gateway ID for later operations | ||
returnObject.edgeGatewayId = egw.EdgeGateway.ID | ||
|
||
return returnObject, nil | ||
} | ||
|
||
// DeleteAll allows users to delete all NSX-T Firewall rules in a particular Edge Gateway | ||
func (firewall *NsxtFirewall) DeleteAll() error { | ||
|
||
if firewall.edgeGatewayId == "" { | ||
return fmt.Errorf("missing Edge Gateway ID") | ||
} | ||
|
||
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointNsxtFirewallRules | ||
minimumApiVersion, err := firewall.client.checkOpenApiEndpointCompatibility(endpoint) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
urlRef, err := firewall.client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, firewall.edgeGatewayId)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = firewall.client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error deleting all NSX-T Firewall Rules: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DeleteById allows users to delete NSX-T Firewall Rule By ID | ||
func (firewall *NsxtFirewall) DeleteById(id string) error { | ||
if id == "" { | ||
return fmt.Errorf("empty ID specified") | ||
} | ||
|
||
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointNsxtFirewallRules | ||
minimumApiVersion, err := firewall.client.checkOpenApiEndpointCompatibility(endpoint) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
urlRef, err := firewall.client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, firewall.edgeGatewayId), "/", id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = firewall.client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error deleting NSX-T Firewall Rule with ID '%s': %s", id, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though unlrelated to this PR, I just noticed this line. Do we break the semver aspect of the govcd versioning here?
CC: @dataclouder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep - this happened as part of #372