diff --git a/.changes/v2.16.0/yyy-improvements.md b/.changes/v2.16.0/469-improvements.md similarity index 79% rename from .changes/v2.16.0/yyy-improvements.md rename to .changes/v2.16.0/469-improvements.md index 86f156d7c..8a5c23d12 100644 --- a/.changes/v2.16.0/yyy-improvements.md +++ b/.changes/v2.16.0/469-improvements.md @@ -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] diff --git a/govcd/openapi_org_network.go b/govcd/openapi_org_network.go index f6b1f2b69..bcfe3f980 100644 --- a/govcd/openapi_org_network.go +++ b/govcd/openapi_org_network.go @@ -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 diff --git a/govcd/openapi_org_network_dhcp.go b/govcd/openapi_org_network_dhcp.go index ed7bc5aeb..479744450 100644 --- a/govcd/openapi_org_network_dhcp.go +++ b/govcd/openapi_org_network_dhcp.go @@ -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 @@ -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 @@ -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) @@ -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 +}