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

     1  /*
     2   * Copyright 2021 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     3   */
     4  
     5  package govcd
     6  
     7  import (
     8  	"fmt"
     9  	"net/url"
    10  
    11  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    12  )
    13  
    14  // NsxtAlbPool defines configuration of a single NSX-T ALB Pool. Pools maintain the list of servers assigned to them and
    15  // perform health monitoring, load balancing, persistence. A pool may only be used or referenced by only one virtual
    16  // service at a time.
    17  type NsxtAlbPool struct {
    18  	NsxtAlbPool *types.NsxtAlbPool
    19  	vcdClient   *VCDClient
    20  }
    21  
    22  // GetAllAlbPoolSummaries retrieves partial information for type `NsxtAlbPool`, but it is the only way to retrieve all ALB
    23  // pools for Edge Gateway
    24  func (vcdClient *VCDClient) GetAllAlbPoolSummaries(edgeGatewayId string, queryParameters url.Values) ([]*NsxtAlbPool, error) {
    25  	client := vcdClient.Client
    26  
    27  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbPoolSummaries
    28  	apiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, edgeGatewayId))
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	typeResponses := []*types.NsxtAlbPool{{}}
    39  	err = client.OpenApiGetAllItems(apiVersion, urlRef, queryParameters, &typeResponses, nil)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	// Wrap all typeResponses into NsxtAlbPool types with client
    45  	wrappedResponses := make([]*NsxtAlbPool, len(typeResponses))
    46  	for sliceIndex := range typeResponses {
    47  		wrappedResponses[sliceIndex] = &NsxtAlbPool{
    48  			NsxtAlbPool: typeResponses[sliceIndex],
    49  			vcdClient:   vcdClient,
    50  		}
    51  	}
    52  
    53  	return wrappedResponses, nil
    54  }
    55  
    56  // GetAllAlbPools uses GetAllAlbPoolSummaries behind the scenes and the fetches complete data for all ALB Pools. This
    57  // has performance penalty because each ALB Pool is fetched individually.
    58  func (vcdClient *VCDClient) GetAllAlbPools(edgeGatewayId string, queryParameters url.Values) ([]*NsxtAlbPool, error) {
    59  	allAlbPoolSummaries, err := vcdClient.GetAllAlbPoolSummaries(edgeGatewayId, queryParameters)
    60  	if err != nil {
    61  		return nil, fmt.Errorf("error retrieving all ALB Pool summaries: %s", err)
    62  	}
    63  
    64  	// Loop over all Summaries and retrieve complete information
    65  	allAlbPools := make([]*NsxtAlbPool, len(allAlbPoolSummaries))
    66  	for index := range allAlbPoolSummaries {
    67  
    68  		allAlbPools[index], err = vcdClient.GetAlbPoolById(allAlbPoolSummaries[index].NsxtAlbPool.ID)
    69  		if err != nil {
    70  			return nil, fmt.Errorf("error retrieving complete ALB Pool: %s", err)
    71  		}
    72  
    73  	}
    74  
    75  	return allAlbPools, nil
    76  }
    77  
    78  // GetAlbPoolByName fetches ALB Pool By Name
    79  func (vcdClient *VCDClient) GetAlbPoolByName(edgeGatewayId string, name string) (*NsxtAlbPool, error) {
    80  	queryParameters := copyOrNewUrlValues(nil)
    81  	queryParameters.Add("filter", "name=="+name)
    82  
    83  	allAlbPools, err := vcdClient.GetAllAlbPools(edgeGatewayId, queryParameters)
    84  	if err != nil {
    85  		return nil, fmt.Errorf("error retrieving ALB Pool with Name '%s': %s", name, err)
    86  	}
    87  
    88  	if len(allAlbPools) == 0 {
    89  		return nil, fmt.Errorf("%s: could not find ALB Pool with Name '%s'", ErrorEntityNotFound, name)
    90  	}
    91  
    92  	if len(allAlbPools) > 1 {
    93  		return nil, fmt.Errorf("found more than 1 ALB Pool with Name '%s'", name)
    94  	}
    95  
    96  	return allAlbPools[0], nil
    97  }
    98  
    99  // GetAlbPoolById fetches ALB Pool By Id
   100  func (vcdClient *VCDClient) GetAlbPoolById(id string) (*NsxtAlbPool, error) {
   101  	client := vcdClient.Client
   102  
   103  	if id == "" {
   104  		return nil, fmt.Errorf("ID is required to lookup NSX-T ALB Pool by ID")
   105  	}
   106  
   107  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbPools
   108  	apiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	urlRef, err := client.OpenApiBuildEndpoint(endpoint, id)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	typeResponse := &types.NsxtAlbPool{}
   119  	err = client.OpenApiGetItem(apiVersion, urlRef, nil, &typeResponse, nil)
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	wrappedResponse := &NsxtAlbPool{
   125  		NsxtAlbPool: typeResponse,
   126  		vcdClient:   vcdClient,
   127  	}
   128  
   129  	return wrappedResponse, nil
   130  }
   131  
   132  // CreateNsxtAlbPool creates NSX-T ALB Pool based on supplied configuration
   133  func (vcdClient *VCDClient) CreateNsxtAlbPool(albPoolConfig *types.NsxtAlbPool) (*NsxtAlbPool, error) {
   134  	client := vcdClient.Client
   135  
   136  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbPools
   137  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	returnObject := &NsxtAlbPool{
   148  		NsxtAlbPool: &types.NsxtAlbPool{},
   149  		vcdClient:   vcdClient,
   150  	}
   151  
   152  	err = client.OpenApiPostItem(minimumApiVersion, urlRef, nil, albPoolConfig, returnObject.NsxtAlbPool, nil)
   153  	if err != nil {
   154  		return nil, fmt.Errorf("error creating NSX-T ALB Pool: %s", err)
   155  	}
   156  
   157  	return returnObject, nil
   158  }
   159  
   160  // Update updates NSX-T ALB Pool based on supplied configuration
   161  func (nsxtAlbPool *NsxtAlbPool) Update(albPoolConfig *types.NsxtAlbPool) (*NsxtAlbPool, error) {
   162  	client := nsxtAlbPool.vcdClient.Client
   163  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbPools
   164  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   165  	if err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	if albPoolConfig.ID == "" {
   170  		return nil, fmt.Errorf("cannot update NSX-T ALB Pool without ID")
   171  	}
   172  
   173  	urlRef, err := client.OpenApiBuildEndpoint(endpoint, albPoolConfig.ID)
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  
   178  	responseAlbController := &NsxtAlbPool{
   179  		NsxtAlbPool: &types.NsxtAlbPool{},
   180  		vcdClient:   nsxtAlbPool.vcdClient,
   181  	}
   182  
   183  	err = client.OpenApiPutItem(minimumApiVersion, urlRef, nil, albPoolConfig, responseAlbController.NsxtAlbPool, nil)
   184  	if err != nil {
   185  		return nil, fmt.Errorf("error updating NSX-T ALB Pool: %s", err)
   186  	}
   187  
   188  	return responseAlbController, nil
   189  }
   190  
   191  // Delete deletes NSX-T ALB Pool
   192  func (nsxtAlbPool *NsxtAlbPool) Delete() error {
   193  	client := nsxtAlbPool.vcdClient.Client
   194  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbPools
   195  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   196  	if err != nil {
   197  		return err
   198  	}
   199  
   200  	if nsxtAlbPool.NsxtAlbPool.ID == "" {
   201  		return fmt.Errorf("cannot delete NSX-T ALB Pool without ID")
   202  	}
   203  
   204  	urlRef, err := client.OpenApiBuildEndpoint(endpoint, nsxtAlbPool.NsxtAlbPool.ID)
   205  	if err != nil {
   206  		return err
   207  	}
   208  
   209  	err = client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil, nil)
   210  	if err != nil {
   211  		return fmt.Errorf("error deleting NSX-T ALB Pool: %s", err)
   212  	}
   213  
   214  	return nil
   215  }