Skip to content

Commit

Permalink
Add DNS option to the NSX-T Segment DHCP server (#465)
Browse files Browse the repository at this point in the history
* Add DnsServers option to types.OpenApiOrgVdcNetworkDhcp

Signed-off-by: Miguel Sama <msama@vmware.com>
  • Loading branch information
Miguel Sama authored Apr 29, 2022
1 parent 2914d58 commit 011d36c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions .changes/v2.16.0/465-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add support for `DnsServers` on `OpenApiOrgVdcNetworkDhcp` struct [GH-465]
4 changes: 4 additions & 0 deletions govcd/openapi_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ var endpointElevatedApiVersions = map[string][]string{
"35.2", // Deprecates Action field in favor of ActionValue
"36.2", // Adds 3 new fields - Comments, SourceGroupsExcluded, and DestinationGroupsExcluded
},
types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp: {
//"32.0", // Basic minimum required version
"36.1", // Adds support for dnsServers
},
}

// checkOpenApiEndpointCompatibility checks if VCD version (to which the client is connected) is sufficient to work with
Expand Down
15 changes: 6 additions & 9 deletions govcd/openapi_org_network_dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (vdc *Vdc) GetOpenApiOrgVdcNetworkDhcp(orgNetworkId string) (*OpenApiOrgVdc
queryParameters := queryParameterFilterAnd("orgVdc.id=="+vdc.Vdc.ID, params)

endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
Expand All @@ -46,7 +46,7 @@ func (vdc *Vdc) GetOpenApiOrgVdcNetworkDhcp(orgNetworkId string) (*OpenApiOrgVdc
client: client,
}

err = client.OpenApiGetItem(minimumApiVersion, urlRef, queryParameters, orgNetDhcp.OpenApiOrgVdcNetworkDhcp, nil)
err = client.OpenApiGetItem(apiVersion, urlRef, queryParameters, orgNetDhcp.OpenApiOrgVdcNetworkDhcp, nil)
if err != nil {
return nil, err
}
Expand All @@ -58,7 +58,7 @@ func (vdc *Vdc) GetOpenApiOrgVdcNetworkDhcp(orgNetworkId string) (*OpenApiOrgVdc
// ID specified as orgNetworkId using OpenAPI
func (vdc *Vdc) UpdateOpenApiOrgVdcNetworkDhcp(orgNetworkId string, orgVdcNetworkDhcpConfig *types.OpenApiOrgVdcNetworkDhcp) (*OpenApiOrgVdcNetworkDhcp, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
minimumApiVersion, err := vdc.client.checkOpenApiEndpointCompatibility(endpoint)
apiVersion, err := vdc.client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
Expand All @@ -85,7 +85,7 @@ func (vdc *Vdc) UpdateOpenApiOrgVdcNetworkDhcp(orgNetworkId string, orgVdcNetwor
orgVdcNetworkDhcpConfig.Mode = "EDGE"
}

err = vdc.client.OpenApiPutItem(minimumApiVersion, urlRef, nil, orgVdcNetworkDhcpConfig, orgNetDhcpResponse.OpenApiOrgVdcNetworkDhcp, nil)
err = vdc.client.OpenApiPutItem(apiVersion, urlRef, nil, orgVdcNetworkDhcpConfig, orgNetDhcpResponse.OpenApiOrgVdcNetworkDhcp, nil)
if err != nil {
return nil, fmt.Errorf("error updating Org VDC network DHCP configuration: %s", err)
}
Expand All @@ -95,12 +95,9 @@ func (vdc *Vdc) UpdateOpenApiOrgVdcNetworkDhcp(orgNetworkId string, orgVdcNetwor

// DeleteOpenApiOrgVdcNetworkDhcp allows to perform HTTP DELETE request on DHCP pool configuration for specified Org VDC
// Network ID
//
// Note. VCD Versions before 10.2 do not allow to perform "DELETE" on DHCP pool and will return error. The way to
// remove DHCP configuration is to recreate Org VDC network itself.
func (vdc *Vdc) DeleteOpenApiOrgVdcNetworkDhcp(orgNetworkId string) error {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
minimumApiVersion, err := vdc.client.checkOpenApiEndpointCompatibility(endpoint)
apiVersion, err := vdc.client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return err
}
Expand All @@ -114,7 +111,7 @@ func (vdc *Vdc) DeleteOpenApiOrgVdcNetworkDhcp(orgNetworkId string) error {
return err
}

err = vdc.client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil, nil)
err = vdc.client.OpenApiDeleteItem(apiVersion, urlRef, nil, nil)

if err != nil {
return fmt.Errorf("error deleting Org VDC network DHCP configuration: %s", err)
Expand Down
19 changes: 13 additions & 6 deletions govcd/openapi_org_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,18 +399,25 @@ func nsxtRoutedDhcpConfig(check *C, vdc *Vdc, orgNetId string) {
},
},
},
DnsServers: []string{
"8.8.8.8",
"8.8.4.4",
},
}

// In API versions lower than 36.1, dnsServers list does not exist
if vdc.client.APIVCDMaxVersionIs("< 36.1") {
dhcpDefinition.DnsServers = []string{}
}

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

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

// VCD Versions before 10.2 do not allow to perform "DELETE" on DHCP pool
// To remove DHCP configuration one must remove Org VDC network itself.
if vdc.client.APIVCDMaxVersionIs(">= 35.0") {
err = vdc.DeleteOpenApiOrgVdcNetworkDhcp(orgNetId)
check.Assert(err, IsNil)
}
err = vdc.DeleteOpenApiOrgVdcNetworkDhcp(orgNetId)
check.Assert(err, IsNil)

}

func runOpenApiOrgVdcNetworkWithVdcGroupTest(check *C, vcd *TestVCD, orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork, expectNetworkType string, dhcpFunc dhcpConfigFunc) {
Expand Down
6 changes: 6 additions & 0 deletions types/v56/nsxt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ type OpenApiOrgVdcNetworkDhcp struct {
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 []string `json:"dnsServers,omitempty"`
}

// OpenApiOrgVdcNetworkDhcpIpRange is a type alias to fit naming
Expand Down

0 comments on commit 011d36c

Please sign in to comment.