Skip to content

Commit

Permalink
Self review
Browse files Browse the repository at this point in the history
Signed-off-by: Dainius Serplis <dserplis@vmware.com>
  • Loading branch information
Didainius committed May 10, 2022
1 parent 45d0e9b commit c3ed3a0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* Add additional methods for convenience of NSX-T Org Network DHCP handling
`OpenApiOrgVdcNetwork.GetOpenApiOrgVdcNetworkDhcp`, `OpenApiOrgVdcNetwork.DeletNetworkDhcp`
`OpenApiOrgVdcNetwork.UpdateDhcp` [GH-YYY]
`OpenApiOrgVdcNetwork.UpdateDhcp` [GH-469]
37 changes: 4 additions & 33 deletions govcd/openapi_org_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,41 +132,12 @@ func (vdcGroup *VdcGroup) CreateOpenApiOrgVdcNetwork(orgVdcNetworkConfig *types.
return createOpenApiOrgVdcNetwork(vdcGroup.client, orgVdcNetworkConfig)
}

// UpdateDhcp updates DHCP configuration for specific Org VDC network
func (orgVdcNet *OpenApiOrgVdcNetwork) UpdateDhcp(orgVdcNetworkDhcpConfig *types.OpenApiOrgVdcNetworkDhcp) (*OpenApiOrgVdcNetworkDhcp, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
apiVersion, err := orgVdcNet.client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}

urlRef, err := orgVdcNet.client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, orgVdcNet.OpenApiOrgVdcNetwork.ID))
if err != nil {
return nil, err
}

orgNetDhcpResponse := &OpenApiOrgVdcNetworkDhcp{
OpenApiOrgVdcNetworkDhcp: &types.OpenApiOrgVdcNetworkDhcp{},
client: orgVdcNet.client,
if orgVdcNet.client == nil || orgVdcNet.OpenApiOrgVdcNetwork == nil || orgVdcNet.OpenApiOrgVdcNetwork.ID == "" {
return nil, fmt.Errorf("error - Org VDC network structure must be set and have ID field available")
}

// From v35.0 onwards, if orgVdcNetworkDhcpConfig.LeaseTime or orgVdcNetworkDhcpConfig.Mode are not explicitly
// passed, the API doesn't use any defaults returning an error. Previous API versions were setting
// LeaseTime to 86400 seconds and Mode to EDGE if these values were not supplied. These two conditional
// address the situation.
if orgVdcNetworkDhcpConfig.LeaseTime == nil {
orgVdcNetworkDhcpConfig.LeaseTime = takeIntAddress(86400)
}

if len(orgVdcNetworkDhcpConfig.Mode) == 0 {
orgVdcNetworkDhcpConfig.Mode = "EDGE"
}

err = orgVdcNet.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)
}

return orgNetDhcpResponse, nil
return updateOrgNetworkDhcp(orgVdcNet.client, orgVdcNet.OpenApiOrgVdcNetwork.ID, orgVdcNetworkDhcpConfig)
}

// Update allows to update Org VDC network
Expand Down
76 changes: 39 additions & 37 deletions govcd/openapi_org_network_dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type OpenApiOrgVdcNetworkDhcp struct {
}

// GetOpenApiOrgVdcNetworkDhcp allows to retrieve DHCP configuration for specific Org VDC network
// ID specified as orgNetworkId using OpenAPI
func (orgVdcNet *OpenApiOrgVdcNetwork) GetOpenApiOrgVdcNetworkDhcp() (*OpenApiOrgVdcNetworkDhcp, error) {
client := orgVdcNet.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
Expand Down Expand Up @@ -89,40 +88,7 @@ func (vdc *Vdc) GetOpenApiOrgVdcNetworkDhcp(orgNetworkId string) (*OpenApiOrgVdc
// UpdateOpenApiOrgVdcNetworkDhcp allows to update DHCP configuration for specific Org VDC network
// ID specified as orgNetworkId using OpenAPI
func (vdc *Vdc) UpdateOpenApiOrgVdcNetworkDhcp(orgNetworkId string, orgVdcNetworkDhcpConfig *types.OpenApiOrgVdcNetworkDhcp) (*OpenApiOrgVdcNetworkDhcp, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
apiVersion, err := vdc.client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}

urlRef, err := vdc.client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, orgNetworkId))
if err != nil {
return nil, err
}

orgNetDhcpResponse := &OpenApiOrgVdcNetworkDhcp{
OpenApiOrgVdcNetworkDhcp: &types.OpenApiOrgVdcNetworkDhcp{},
client: vdc.client,
}

// From v35.0 onwards, if orgVdcNetworkDhcpConfig.LeaseTime or orgVdcNetworkDhcpConfig.Mode are not explicitly
// passed, the API doesn't use any defaults returning an error. Previous API versions were setting
// LeaseTime to 86400 seconds and Mode to EDGE if these values were not supplied. These two conditional
// address the situation.
if orgVdcNetworkDhcpConfig.LeaseTime == nil {
orgVdcNetworkDhcpConfig.LeaseTime = takeIntAddress(86400)
}

if len(orgVdcNetworkDhcpConfig.Mode) == 0 {
orgVdcNetworkDhcpConfig.Mode = "EDGE"
}

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)
}

return orgNetDhcpResponse, nil
return updateOrgNetworkDhcp(vdc.client, orgNetworkId, orgVdcNetworkDhcpConfig)
}

// DeleteOpenApiOrgVdcNetworkDhcp allows to perform HTTP DELETE request on DHCP pool configuration for specified Org VDC
Expand Down Expand Up @@ -152,8 +118,7 @@ func (vdc *Vdc) DeleteOpenApiOrgVdcNetworkDhcp(orgNetworkId string) error {
return nil
}

// DeleteOpenApiOrgVdcNetworkDhcp allows to perform HTTP DELETE request on DHCP pool configuration for specified Org VDC
// Network ID
// DeletNetworkDhcp allows to perform HTTP DELETE request on DHCP pool configuration for Org network
func (orgVdcNet *OpenApiOrgVdcNetwork) DeletNetworkDhcp() error {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
apiVersion, err := orgVdcNet.client.getOpenApiHighestElevatedVersion(endpoint)
Expand All @@ -178,3 +143,40 @@ func (orgVdcNet *OpenApiOrgVdcNetwork) DeletNetworkDhcp() error {

return nil
}

func updateOrgNetworkDhcp(client *Client, orgNetworkId string, orgVdcNetworkDhcpConfig *types.OpenApiOrgVdcNetworkDhcp) (*OpenApiOrgVdcNetworkDhcp, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}

urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, orgNetworkId))
if err != nil {
return nil, err
}

orgNetDhcpResponse := &OpenApiOrgVdcNetworkDhcp{
OpenApiOrgVdcNetworkDhcp: &types.OpenApiOrgVdcNetworkDhcp{},
client: client,
}

// From v35.0 onwards, if orgVdcNetworkDhcpConfig.LeaseTime or orgVdcNetworkDhcpConfig.Mode are not explicitly
// passed, the API doesn't use any defaults returning an error. Previous API versions were setting
// LeaseTime to 86400 seconds and Mode to EDGE if these values were not supplied. These two conditional
// address the situation.
if orgVdcNetworkDhcpConfig.LeaseTime == nil {
orgVdcNetworkDhcpConfig.LeaseTime = takeIntAddress(86400)
}

if len(orgVdcNetworkDhcpConfig.Mode) == 0 {
orgVdcNetworkDhcpConfig.Mode = "EDGE"
}

err = 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)
}

return orgNetDhcpResponse, nil
}

0 comments on commit c3ed3a0

Please sign in to comment.