Skip to content
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

Documentation and test improvements for NSX-T Network DHCP type #517

Merged
merged 15 commits into from
Nov 15, 2022
1 change: 1 addition & 0 deletions .changes/v2.17.0/517-notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Improve documentation for `types.OpenApiOrgVdcNetworkDhcp` [GH-517]
103 changes: 84 additions & 19 deletions govcd/openapi_org_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func (vcd *TestVCD) Test_NsxtOrgVdcNetworkIsolated(check *C) {
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: check.TestName(),
Description: check.TestName() + "-description",

// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},
NetworkType: types.OrgVdcNetworkTypeIsolated,
Subnets: types.OrgVdcNetworkSubnets{
Values: []types.OrgVdcNetworkSubnetValues{
Expand Down Expand Up @@ -54,8 +51,8 @@ func (vcd *TestVCD) Test_NsxtOrgVdcNetworkIsolated(check *C) {
},
}

runOpenApiOrgVdcNetworkTest(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeIsolated, nil)
runOpenApiOrgVdcNetworkWithVdcGroupTest(check, vcd, orgVdcNetworkConfig, types.OrgVdcNetworkTypeIsolated, nil)
runOpenApiOrgVdcNetworkTest(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeIsolated, []dhcpConfigFunc{nsxtDhcpConfigNetworkMode})
runOpenApiOrgVdcNetworkWithVdcGroupTest(check, vcd, orgVdcNetworkConfig, types.OrgVdcNetworkTypeIsolated, []dhcpConfigFunc{nsxtDhcpConfigNetworkMode})
}

func (vcd *TestVCD) Test_NsxtOrgVdcNetworkRouted(check *C) {
Expand All @@ -68,10 +65,7 @@ func (vcd *TestVCD) Test_NsxtOrgVdcNetworkRouted(check *C) {
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: check.TestName(),
Description: check.TestName() + "-description",

// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},
NetworkType: types.OrgVdcNetworkTypeRouted,

// Connection is used for "routed" network
Expand Down Expand Up @@ -115,8 +109,8 @@ func (vcd *TestVCD) Test_NsxtOrgVdcNetworkRouted(check *C) {
},
}

runOpenApiOrgVdcNetworkTest(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeRouted, nsxtRoutedDhcpConfig)
runOpenApiOrgVdcNetworkWithVdcGroupTest(check, vcd, orgVdcNetworkConfig, types.OrgVdcNetworkTypeRouted, nsxtRoutedDhcpConfig)
runOpenApiOrgVdcNetworkTest(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeRouted, []dhcpConfigFunc{nsxtRoutedDhcpConfigEdgeMode, nsxtDhcpConfigNetworkMode})
runOpenApiOrgVdcNetworkWithVdcGroupTest(check, vcd, orgVdcNetworkConfig, types.OrgVdcNetworkTypeRouted, []dhcpConfigFunc{nsxtRoutedDhcpConfigEdgeMode, nsxtDhcpConfigNetworkMode})
}

func (vcd *TestVCD) Test_NsxtOrgVdcNetworkImported(check *C) {
Expand Down Expand Up @@ -330,7 +324,7 @@ func (vcd *TestVCD) Test_NsxvOrgVdcNetworkDirect(check *C) {
runOpenApiOrgVdcNetworkTest(check, vcd, vcd.vdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeDirect, nil)
}

func runOpenApiOrgVdcNetworkTest(check *C, vcd *TestVCD, vdc *Vdc, orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork, expectNetworkType string, dhcpFunc dhcpConfigFunc) {
func runOpenApiOrgVdcNetworkTest(check *C, vcd *TestVCD, vdc *Vdc, orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork, expectNetworkType string, dhcpFunc []dhcpConfigFunc) {
orgVdcNet, err := vdc.CreateOpenApiOrgVdcNetwork(orgVdcNetworkConfig)
check.Assert(err, IsNil)

Expand Down Expand Up @@ -370,8 +364,8 @@ func runOpenApiOrgVdcNetworkTest(check *C, vcd *TestVCD, vdc *Vdc, orgVdcNetwork
check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.Description, Equals, orgVdcNet.OpenApiOrgVdcNetwork.Description)

// Configure DHCP if specified
if dhcpFunc != nil {
dhcpFunc(check, vcd, vdc, updatedOrgVdcNet.OpenApiOrgVdcNetwork.ID)
for i := range dhcpFunc {
dhcpFunc[i](check, vcd, vdc, updatedOrgVdcNet.OpenApiOrgVdcNetwork.ID)
}
// Delete
err = orgVdcNet.Delete()
Expand All @@ -387,7 +381,7 @@ func runOpenApiOrgVdcNetworkTest(check *C, vcd *TestVCD, vdc *Vdc, orgVdcNetwork

type dhcpConfigFunc func(check *C, vcd *TestVCD, vdc *Vdc, orgNetId string)

func nsxtRoutedDhcpConfig(check *C, vcd *TestVCD, vdc *Vdc, orgNetId string) {
func nsxtRoutedDhcpConfigEdgeMode(check *C, vcd *TestVCD, vdc *Vdc, orgNetId string) {
dhcpDefinition := &types.OpenApiOrgVdcNetworkDhcp{
Enabled: takeBoolPointer(true),
DhcpPools: []types.OpenApiOrgVdcNetworkDhcpPools{
Expand Down Expand Up @@ -435,10 +429,81 @@ func nsxtRoutedDhcpConfig(check *C, vcd *TestVCD, vdc *Vdc, orgNetId string) {
check.Assert(err, IsNil)
check.Assert(len(deletedDhcp.OpenApiOrgVdcNetworkDhcp.DhcpPools), Equals, 0)
check.Assert(len(deletedDhcp.OpenApiOrgVdcNetworkDhcp.DnsServers), Equals, 0)
}

// nsxtDhcpConfigNetworkMode checks DHCP functionality in NETWORK mode.
// It requires that Edge Cluster is set at VDC level therefore this function does it for the
// duration of this test and restores it back
func nsxtDhcpConfigNetworkMode(check *C, vcd *TestVCD, vdc *Vdc, orgNetId string) {
// Only supported in 10.3.1+
if vdc.client.APIVCDMaxVersionIs("< 36.1") {
return
}

// DHCP in NETWORK mode requires Edge Cluster to be set for VDC and cleaned up afterwards
edgeCluster, err := vdc.GetNsxtEdgeClusterByName(vcd.config.VCD.Nsxt.NsxtEdgeCluster)
check.Assert(err, IsNil)
vdcNetworkProfile := &types.VdcNetworkProfile{
ServicesEdgeCluster: &types.VdcNetworkProfileServicesEdgeCluster{
BackingID: edgeCluster.NsxtEdgeCluster.ID,
},
}
_, err = vdc.UpdateVdcNetworkProfile(vdcNetworkProfile)
check.Assert(err, IsNil)
defer func() {
err := vdc.DeleteVdcNetworkProfile()
if err != nil {
check.Errorf("error cleaning up VDC Network Profile: %s", err)
}
}()

dhcpDefinition := &types.OpenApiOrgVdcNetworkDhcp{
Enabled: takeBoolPointer(true),
Mode: "NETWORK",
IPAddress: "2.1.1.252",
DhcpPools: []types.OpenApiOrgVdcNetworkDhcpPools{
{
Enabled: takeBoolPointer(true),
IPRange: types.OpenApiOrgVdcNetworkDhcpIpRange{
StartAddress: "2.1.1.200",
EndAddress: "2.1.1.201",
},
},
},
DnsServers: []string{
"8.8.8.8",
"8.8.4.4",
},
}

updatedDhcp, err := vdc.UpdateOpenApiOrgVdcNetworkDhcp(orgNetId, dhcpDefinition)
check.Assert(err, IsNil)

check.Assert(dhcpDefinition, DeepEquals, updatedDhcp.OpenApiOrgVdcNetworkDhcp)

err = vdc.DeleteOpenApiOrgVdcNetworkDhcp(orgNetId)
check.Assert(err, IsNil)

orgVdcNetwork, err := vcd.org.GetOpenApiOrgVdcNetworkById(orgNetId)
check.Assert(err, IsNil)
check.Assert(orgVdcNetwork, NotNil)

updatedDhcp2, err := orgVdcNetwork.UpdateDhcp(dhcpDefinition)
check.Assert(err, IsNil)
check.Assert(updatedDhcp2, NotNil)

check.Assert(dhcpDefinition, DeepEquals, updatedDhcp2.OpenApiOrgVdcNetworkDhcp)

err = orgVdcNetwork.DeletNetworkDhcp()
check.Assert(err, IsNil)

deletedDhcp, err := orgVdcNetwork.GetOpenApiOrgVdcNetworkDhcp()
check.Assert(err, IsNil)
check.Assert(len(deletedDhcp.OpenApiOrgVdcNetworkDhcp.DhcpPools), Equals, 0)
check.Assert(len(deletedDhcp.OpenApiOrgVdcNetworkDhcp.DnsServers), Equals, 0)
}

func runOpenApiOrgVdcNetworkWithVdcGroupTest(check *C, vcd *TestVCD, orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork, expectNetworkType string, dhcpFunc dhcpConfigFunc) {
func runOpenApiOrgVdcNetworkWithVdcGroupTest(check *C, vcd *TestVCD, orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork, expectNetworkType string, dhcpFunc []dhcpConfigFunc) {
adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
check.Assert(err, IsNil)

Expand Down Expand Up @@ -525,8 +590,8 @@ func runOpenApiOrgVdcNetworkWithVdcGroupTest(check *C, vcd *TestVCD, orgVdcNetwo
check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.Description, Equals, orgVdcNet.OpenApiOrgVdcNetwork.Description)

// Configure DHCP if specified
if dhcpFunc != nil {
dhcpFunc(check, vcd, vdc, updatedOrgVdcNet.OpenApiOrgVdcNetwork.ID)
for i := range dhcpFunc {
dhcpFunc[i](check, vcd, vdc, updatedOrgVdcNet.OpenApiOrgVdcNetwork.ID)
}
// Delete
err = orgVdcNet.Delete()
Expand Down
24 changes: 16 additions & 8 deletions types/v56/nsxt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,28 +201,36 @@ type NsxtImportableSwitch = OpenApiReference

// OpenApiOrgVdcNetworkDhcp allows users to manage DHCP configuration for Org VDC networks by using OpenAPI endpoint
type OpenApiOrgVdcNetworkDhcp struct {
Enabled *bool `json:"enabled,omitempty"`
Enabled *bool `json:"enabled,omitempty"`

// LeaseTime specifies the amount of time in seconds of how long a DHCP IP will be leased out
// for. The minimum is 60s while the maximum is 4,294,967,295s, which is roughly 49,710 days.
LeaseTime *int `json:"leaseTime,omitempty"`
DhcpPools []OpenApiOrgVdcNetworkDhcpPools `json:"dhcpPools,omitempty"`

// Mode describes how the DHCP service is configured for this network. Once a DHCP service has been created, the mode
// attribute cannot be changed. The mode field will default to 'EDGE' if it is not provided. This field only applies
// to networks backed by an NSX-T network provider.
//
// The supported values are EDGE (default) and NETWORK.
// The supported values are EDGE, NETWORK and RELAY (VCD 10.3.1+, API 36.1+).
// * If EDGE is specified, the DHCP service of the edge is used to obtain DHCP IPs.
// * If NETWORK is specified, a DHCP server is created for use by this network. (To use NETWORK
// * If NETWORK is specified, a DHCP server is created for use by this network.
// * If RELAY is specified, all the DHCP client requests will be relayed to Gateway DHCP
// Forwarder service. This mode is only supported for Routed Org vDC Networks.
//
// In order to use DHCP for IPV6, NETWORK mode must be used. Routed networks which are using NETWORK DHCP services can
// be disconnected from the edge gateway and still retain their DHCP configuration, however network using EDGE DHCP
// cannot be disconnected from the gateway until DHCP has been disabled.
// In order to use DHCP for IPV6, NETWORK mode must be used. Routed networks which are using
// NETWORK DHCP services can be disconnected from the edge gateway and still retain their DHCP
// configuration, however DHCP configuration will be removed during connection change for
// networks using EDGE or RELAY DHCP mode.
Mode string `json:"mode,omitempty"`

// IPAddress is only applicable when mode=NETWORK. This will specify IP address of DHCP server in network.
IPAddress string `json:"ipAddress,omitempty"`

// New fields starting with 36.1

// DnsServers are the IPs to be assigned by this DHCP service. The IP type must match the IP type of the subnet on
// which the DHCP config is being created.
// DnsServers are the IPs to be assigned by this DHCP service. The IP type must match the IP
// type of the subnet on which the DHCP config is being created.
DnsServers []string `json:"dnsServers,omitempty"`
}

Expand Down