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

     1  /*
     2   * Copyright 2022 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     3   */
     4  
     5  package govcd
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    11  )
    12  
    13  // GetBgpConfiguration retrieves BGP Configuration for NSX-T Edge Gateway
    14  func (egw *NsxtEdgeGateway) GetBgpConfiguration() (*types.EdgeBgpConfig, error) {
    15  	client := egw.client
    16  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointEdgeBgpConfig
    17  	apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	// Insert Edge Gateway ID into endpoint path "edgeGateways/%s/routing/bgp"
    23  	urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, egw.EdgeGateway.ID))
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	returnObject := &types.EdgeBgpConfig{}
    29  
    30  	err = client.OpenApiGetItem(apiVersion, urlRef, nil, returnObject, nil)
    31  	if err != nil {
    32  		return nil, fmt.Errorf("error retrieving NSX-T Edge Gateway BGP Configuration: %s", err)
    33  	}
    34  
    35  	return returnObject, nil
    36  }
    37  
    38  // UpdateBgpConfiguration updates BGP configuration on NSX-T Edge Gateway
    39  //
    40  // Note. Update of BGP configuration requires version to be specified in 'Version' field. This
    41  // function automatically handles it.
    42  func (egw *NsxtEdgeGateway) UpdateBgpConfiguration(bgpConfig *types.EdgeBgpConfig) (*types.EdgeBgpConfig, error) {
    43  	client := egw.client
    44  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointEdgeBgpConfig
    45  	apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	// Insert Edge Gateway ID into endpoint path
    51  	urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, egw.EdgeGateway.ID))
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	// Update of BGP config requires version to be specified. This function automatically handles it.
    57  	existingBgpConfig, err := egw.GetBgpConfiguration()
    58  	if err != nil {
    59  		return nil, fmt.Errorf("error getting NSX-T Edge Gateway BGP Configuration: %s", err)
    60  	}
    61  	bgpConfig.Version = existingBgpConfig.Version
    62  
    63  	returnObject := &types.EdgeBgpConfig{}
    64  
    65  	err = client.OpenApiPutItem(apiVersion, urlRef, nil, bgpConfig, returnObject, nil)
    66  	if err != nil {
    67  		return nil, fmt.Errorf("error setting NSX-T Edge Gateway BGP Configuration: %s", err)
    68  	}
    69  
    70  	return returnObject, nil
    71  }
    72  
    73  // DisableBgpConfiguration performs an `UpdateBgpConfiguration` and preserve all field values as
    74  // they were, but explicitly sets Enabled to false.
    75  func (egw *NsxtEdgeGateway) DisableBgpConfiguration() error {
    76  	// Get existing BGP configuration so that when disabling it - other settings remain as they are
    77  	bgpConfig, err := egw.GetBgpConfiguration()
    78  	if err != nil {
    79  		return fmt.Errorf("error retrieving BGP configuration: %s", err)
    80  	}
    81  	bgpConfig.Enabled = false
    82  
    83  	_, err = egw.UpdateBgpConfiguration(bgpConfig)
    84  	return err
    85  }