-
Notifications
You must be signed in to change notification settings - Fork 76
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 support for NSX-V Edge Gateway DHCP relay settings #271
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
55b2600
Add support for NSX-V Edge Gateway DHCP relay settings
Didainius f65f5df
Add changelog
Didainius 25e41fa
Adress comments, tune 'Reset' function
Didainius 9b6c255
Improve Reset() docstring
Didainius 6410887
Address comments
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2019 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. | ||
*/ | ||
|
||
package govcd | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/vmware/go-vcloud-director/v2/types/v56" | ||
) | ||
|
||
// UpdateDhcpRelay updates DHCP relay settings for a particular edge gateway and returns them. The | ||
// feature itself enables you to leverage your existing DHCP infrastructure from within NSX without | ||
// any interruption to the IP address management in your environment. DHCP messages are relayed from | ||
// virtual machine(s) to the designated DHCP server(s) in the physical world. This enables IP | ||
// addresses within NSX to continue to be in sync with IP addresses in other environments. | ||
func (egw *EdgeGateway) UpdateDhcpRelay(dhcpRelayConfig *types.EdgeDhcpRelay) (*types.EdgeDhcpRelay, error) { | ||
if !egw.HasAdvancedNetworking() { | ||
return nil, fmt.Errorf("only advanced edge gateways support DHCP relay") | ||
} | ||
|
||
httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeDhcpRelayPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get Edge Gateway API endpoint: %s", err) | ||
} | ||
// We expect to get http.StatusNoContent or if not an error of type types.NSXError | ||
_, err = egw.client.ExecuteRequestWithCustomError(httpPath, http.MethodPut, types.AnyXMLMime, | ||
"error setting DHCP relay settings: %s", dhcpRelayConfig, &types.NSXError{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return egw.GetDhcpRelay() | ||
} | ||
|
||
// GetDhcpRelay retrieves a structure of *types.EdgeDhcpRelay with all DHCP relay settings present | ||
// on a particular edge gateway. | ||
func (egw *EdgeGateway) GetDhcpRelay() (*types.EdgeDhcpRelay, error) { | ||
if !egw.HasAdvancedNetworking() { | ||
return nil, fmt.Errorf("only advanced edge gateways support DHCP relay") | ||
} | ||
response := &types.EdgeDhcpRelay{} | ||
|
||
httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeDhcpRelayPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get Edge Gateway API endpoint: %s", err) | ||
} | ||
|
||
// This query Edge gaateway DHCP relay using proxied NSX-V API | ||
_, err = egw.client.ExecuteRequest(httpPath, http.MethodGet, types.AnyXMLMime, | ||
"unable to read edge gateway DHCP relay configuration: %s", nil, response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
// ResetDhcpRelay removes all configuration by sending a DELETE request for DHCP relay configuration | ||
// endpoint | ||
func (egw *EdgeGateway) ResetDhcpRelay() error { | ||
if !egw.HasAdvancedNetworking() { | ||
return fmt.Errorf("only advanced edge gateways support DHCP relay") | ||
} | ||
|
||
httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeDhcpRelayPath) | ||
if err != nil { | ||
return fmt.Errorf("could not get Edge Gateway API endpoint: %s", err) | ||
} | ||
|
||
// Send a DELETE request to DHCP relay configuration endpoint | ||
_, err = egw.client.ExecuteRequestWithCustomError(httpPath, http.MethodDelete, types.AnyXMLMime, | ||
"unable to reset edge gateway DHCP relay configuration: %s", nil, &types.NSXError{}) | ||
return err | ||
} |
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,105 @@ | ||
// +build nsxv functional ALL | ||
|
||
/* | ||
* Copyright 2019 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. | ||
*/ | ||
|
||
package govcd | ||
|
||
import ( | ||
"github.com/vmware/go-vcloud-director/v2/types/v56" | ||
. "gopkg.in/check.v1" | ||
) | ||
|
||
// Test_NsxvDhcpRelay tests out Edge gateway DHCP relay configuration settings. It does the following: | ||
// 1. Creates an IP set to ensure it can be used in DHCP relay configuration settings. | ||
// 2. Creates a DHCP relay configuration with multiple objects and checks. Adds a task for cleanup. | ||
// Note. Because DHCP relay configuration is not an object, but a setting of edge gateway - there is | ||
// nothing to delete | ||
// 3. Resets the DHCP relay configuration and checks that it was removed | ||
// 4. Creates another DHCP relay with different configuration (using only IP sets) and manually | ||
// specifying IP address on edge gateway interface (vNic) | ||
// 6. Resets the DHCP relay configuration and checks that it was removed | ||
func (vcd *TestVCD) Test_NsxvDhcpRelay(check *C) { | ||
if vcd.config.VCD.EdgeGateway == "" { | ||
check.Skip("Skipping test because no edge gateway given") | ||
} | ||
|
||
edge, err := vcd.vdc.GetEdgeGatewayByName(vcd.config.VCD.EdgeGateway, false) | ||
check.Assert(err, IsNil) | ||
check.Assert(edge.EdgeGateway.Name, Equals, vcd.config.VCD.EdgeGateway) | ||
|
||
// Setup IP set for testing purposes and add it to cleanup list | ||
createdIpSet, err := testCreateIpSet("dhcp-relay-test", vcd.vdc) | ||
check.Assert(err, IsNil) | ||
parentEntity := vcd.org.Org.Name + "|" + vcd.vdc.Vdc.Name | ||
AddToCleanupList(createdIpSet.Name, "ipSet", parentEntity, check.TestName()) | ||
|
||
// Lookup vNic index for our org network | ||
vNicIndex, _, err := edge.GetAnyVnicIndexByNetworkName(vcd.config.VCD.Network.Net1) | ||
check.Assert(err, IsNil) | ||
|
||
dhcpRelayConfig := &types.EdgeDhcpRelay{ | ||
RelayServer: &types.EdgeDhcpRelayServer{ | ||
IpAddress: []string{"1.1.1.1"}, | ||
Fqdns: []string{"servergroups.domainname.com", "servergroups.otherdomainname.com"}, | ||
GroupingObjectId: []string{createdIpSet.ID}, | ||
}, | ||
RelayAgents: &types.EdgeDhcpRelayAgents{ | ||
Agents: []types.EdgeDhcpRelayAgent{ | ||
types.EdgeDhcpRelayAgent{ | ||
VnicIndex: vNicIndex, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
createdRelayConfig, err := edge.UpdateDhcpRelay(dhcpRelayConfig) | ||
check.Assert(err, IsNil) | ||
|
||
parentEntity = vcd.org.Org.Name + "|" + vcd.vdc.Vdc.Name + "|" + vcd.config.VCD.EdgeGateway | ||
// DHCP relay config is being prepended (added to beginning) of cleanup list because it must be | ||
// delete first so that above created dependent IP sets can be cleaned up when their turn comes | ||
PrependToCleanupList(check.TestName(), "dhcpRelayConfig", parentEntity, check.TestName()) | ||
|
||
// Cache Gateway auto-assigned address to try specifying it afterwards | ||
vNicDefaultGateway := createdRelayConfig.RelayAgents.Agents[0].GatewayInterfaceAddress | ||
|
||
readRelayConfig, err := edge.GetDhcpRelay() | ||
check.Assert(err, IsNil) | ||
check.Assert(readRelayConfig, DeepEquals, createdRelayConfig) | ||
|
||
// Patch auto-assigned IP so that structure comparison works | ||
dhcpRelayConfig.RelayAgents.Agents[0].GatewayInterfaceAddress = readRelayConfig.RelayAgents.Agents[0].GatewayInterfaceAddress | ||
check.Assert(readRelayConfig.RelayServer, DeepEquals, dhcpRelayConfig.RelayServer) | ||
check.Assert(readRelayConfig.RelayAgents, DeepEquals, dhcpRelayConfig.RelayAgents) | ||
|
||
// Reset DHCP relay and ensure no settings are present | ||
err = edge.ResetDhcpRelay() | ||
check.Assert(err, IsNil) | ||
read, err := edge.GetDhcpRelay() | ||
check.Assert(err, IsNil) | ||
check.Assert(read.RelayServer, IsNil) | ||
check.Assert(read.RelayAgents, IsNil) | ||
|
||
// Second attempt - insert gateway interface IP address during creation to prevent | ||
// auto-assignment | ||
dhcpRelayConfig.RelayAgents.Agents[0].GatewayInterfaceAddress = vNicDefaultGateway | ||
// Remove IP addresses and FQDNs to only use IP sets for specifying DHCP servers addresses | ||
dhcpRelayConfig.RelayServer.IpAddress = nil | ||
dhcpRelayConfig.RelayServer.Fqdns = nil | ||
|
||
createdRelayConfig2, err := edge.UpdateDhcpRelay(dhcpRelayConfig) | ||
check.Assert(err, IsNil) | ||
readRelayConfig2, err := edge.GetDhcpRelay() | ||
check.Assert(err, IsNil) | ||
check.Assert(readRelayConfig2, DeepEquals, createdRelayConfig2) | ||
|
||
// Reset DHCP relay and ensure no settings are present | ||
err = edge.ResetDhcpRelay() | ||
check.Assert(err, IsNil) | ||
read, err = edge.GetDhcpRelay() | ||
check.Assert(err, IsNil) | ||
check.Assert(read.RelayServer, IsNil) | ||
check.Assert(read.RelayAgents, IsNil) | ||
} |
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
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.
Please add a comment about using
PrependToCleanupList
instead of Add, for future referenceThere 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.
Added