github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/nsxv_dhcprelay.go (about)

     1  /*
     2   * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     3   */
     4  
     5  package govcd
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  
    11  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    12  )
    13  
    14  // UpdateDhcpRelay updates DHCP relay settings for a particular edge gateway and returns them. The
    15  // feature itself enables you to leverage your existing DHCP infrastructure from within NSX without
    16  // any interruption to the IP address management in your environment. DHCP messages are relayed from
    17  // virtual machine(s) to the designated DHCP server(s) in the physical world. This enables IP
    18  // addresses within NSX to continue to be in sync with IP addresses in other environments.
    19  func (egw *EdgeGateway) UpdateDhcpRelay(dhcpRelayConfig *types.EdgeDhcpRelay) (*types.EdgeDhcpRelay, error) {
    20  	if !egw.HasAdvancedNetworking() {
    21  		return nil, fmt.Errorf("only advanced edge gateways support DHCP relay")
    22  	}
    23  
    24  	httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeDhcpRelayPath)
    25  	if err != nil {
    26  		return nil, fmt.Errorf("could not get Edge Gateway API endpoint: %s", err)
    27  	}
    28  	// We expect to get http.StatusNoContent or if not an error of type types.NSXError
    29  	_, err = egw.client.ExecuteRequestWithCustomError(httpPath, http.MethodPut, types.AnyXMLMime,
    30  		"error setting DHCP relay settings: %s", dhcpRelayConfig, &types.NSXError{})
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	return egw.GetDhcpRelay()
    36  }
    37  
    38  // GetDhcpRelay retrieves a structure of *types.EdgeDhcpRelay with all DHCP relay settings present
    39  // on a particular edge gateway.
    40  func (egw *EdgeGateway) GetDhcpRelay() (*types.EdgeDhcpRelay, error) {
    41  	if !egw.HasAdvancedNetworking() {
    42  		return nil, fmt.Errorf("only advanced edge gateways support DHCP relay")
    43  	}
    44  	response := &types.EdgeDhcpRelay{}
    45  
    46  	httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeDhcpRelayPath)
    47  	if err != nil {
    48  		return nil, fmt.Errorf("could not get Edge Gateway API endpoint: %s", err)
    49  	}
    50  
    51  	// This query Edge gateway DHCP relay using proxied NSX-V API
    52  	_, err = egw.client.ExecuteRequest(httpPath, http.MethodGet, types.AnyXMLMime,
    53  		"unable to read edge gateway DHCP relay configuration: %s", nil, response)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	return response, nil
    59  }
    60  
    61  // ResetDhcpRelay removes all configuration by sending a DELETE request for DHCP relay configuration
    62  // endpoint
    63  func (egw *EdgeGateway) ResetDhcpRelay() error {
    64  	if !egw.HasAdvancedNetworking() {
    65  		return fmt.Errorf("only advanced edge gateways support DHCP relay")
    66  	}
    67  
    68  	httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeDhcpRelayPath)
    69  	if err != nil {
    70  		return fmt.Errorf("could not get Edge Gateway API endpoint: %s", err)
    71  	}
    72  
    73  	// Send a DELETE request to DHCP relay configuration endpoint
    74  	_, err = egw.client.ExecuteRequestWithCustomError(httpPath, http.MethodDelete, types.AnyXMLMime,
    75  		"unable to reset edge gateway DHCP relay configuration: %s", nil, &types.NSXError{})
    76  	return err
    77  }