github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/nsxt_alb_importable_service_engine_groups.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  	"errors"
     9  	"fmt"
    10  	"net/url"
    11  
    12  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    13  )
    14  
    15  // NsxtAlbImportableServiceEngineGroups provides capability to list all Importable Service Engine Groups available in
    16  // ALB Controller so that they can be consumed by NsxtAlbServiceEngineGroup
    17  //
    18  // Note. The API does not return Importable Service Engine Group once it is consumed.
    19  type NsxtAlbImportableServiceEngineGroups struct {
    20  	NsxtAlbImportableServiceEngineGroups *types.NsxtAlbImportableServiceEngineGroups
    21  	vcdClient                            *VCDClient
    22  }
    23  
    24  // GetAllAlbImportableServiceEngineGroups lists all Importable Service Engine Groups available in ALB Controller
    25  func (vcdClient *VCDClient) GetAllAlbImportableServiceEngineGroups(parentAlbCloudUrn string, queryParameters url.Values) ([]*NsxtAlbImportableServiceEngineGroups, error) {
    26  	client := vcdClient.Client
    27  	if parentAlbCloudUrn == "" {
    28  		return nil, fmt.Errorf("parentAlbCloudUrn is required")
    29  	}
    30  	if !client.IsSysAdmin {
    31  		return nil, errors.New("handling NSX-T ALB Importable Service Engine Groups requires System user")
    32  	}
    33  
    34  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbImportableServiceEngineGroups
    35  	apiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	queryParams := copyOrNewUrlValues(queryParameters)
    46  	queryParams = queryParameterFilterAnd(fmt.Sprintf("_context==%s", parentAlbCloudUrn), queryParams)
    47  	typeResponses := []*types.NsxtAlbImportableServiceEngineGroups{{}}
    48  	err = client.OpenApiGetAllItems(apiVersion, urlRef, queryParams, &typeResponses, nil)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	wrappedResponses := make([]*NsxtAlbImportableServiceEngineGroups, len(typeResponses))
    54  	for sliceIndex := range typeResponses {
    55  		wrappedResponses[sliceIndex] = &NsxtAlbImportableServiceEngineGroups{
    56  			NsxtAlbImportableServiceEngineGroups: typeResponses[sliceIndex],
    57  			vcdClient:                            vcdClient,
    58  		}
    59  	}
    60  
    61  	return wrappedResponses, nil
    62  }
    63  
    64  // GetAlbImportableServiceEngineGroupByName returns importable NSX-T ALB Clouds.
    65  func (vcdClient *VCDClient) GetAlbImportableServiceEngineGroupByName(parentAlbCloudUrn, name string) (*NsxtAlbImportableServiceEngineGroups, error) {
    66  	albClouds, err := vcdClient.GetAllAlbImportableServiceEngineGroups(parentAlbCloudUrn, nil)
    67  	if err != nil {
    68  		return nil, fmt.Errorf("error finding NSX-T ALB Importable Service Engine Group by Name '%s': %s", name, err)
    69  	}
    70  
    71  	// Filtering by Name is not supported by API therefore it must be filtered on client side
    72  	var foundResult bool
    73  	var foundAlbCloud *NsxtAlbImportableServiceEngineGroups
    74  	for i, value := range albClouds {
    75  		if albClouds[i].NsxtAlbImportableServiceEngineGroups.DisplayName == name {
    76  			foundResult = true
    77  			foundAlbCloud = value
    78  			break
    79  		}
    80  	}
    81  
    82  	if !foundResult {
    83  		return nil, fmt.Errorf("%s: could not find NSX-T ALB Importable Service Engine Group by Name %s", ErrorEntityNotFound, name)
    84  	}
    85  
    86  	return foundAlbCloud, nil
    87  }
    88  
    89  // GetAlbImportableServiceEngineGroupById
    90  // Note. ID filtering is performed on client side
    91  func (vcdClient *VCDClient) GetAlbImportableServiceEngineGroupById(parentAlbCloudUrn, id string) (*NsxtAlbImportableServiceEngineGroups, error) {
    92  	albClouds, err := vcdClient.GetAllAlbImportableServiceEngineGroups(parentAlbCloudUrn, nil)
    93  	if err != nil {
    94  		return nil, fmt.Errorf("error finding NSX-T ALB Importable Service Engine Group by ID '%s': %s", id, err)
    95  	}
    96  
    97  	// Filtering by ID is not supported by API therefore it must be filtered on client side
    98  	var foundResult bool
    99  	var foundImportableSEGroups *NsxtAlbImportableServiceEngineGroups
   100  	for i, value := range albClouds {
   101  		if albClouds[i].NsxtAlbImportableServiceEngineGroups.ID == id {
   102  			foundResult = true
   103  			foundImportableSEGroups = value
   104  		}
   105  	}
   106  
   107  	if !foundResult {
   108  		return nil, fmt.Errorf("%s: could not find NSX-T ALB Importable Service Engine Group by ID %s", ErrorEntityNotFound, id)
   109  	}
   110  
   111  	return foundImportableSEGroups, nil
   112  }
   113  
   114  // GetAllAlbImportableServiceEngineGroups lists all Importable Service Engine Groups available in ALB Controller
   115  func (nsxtAlbCloud *NsxtAlbCloud) GetAllAlbImportableServiceEngineGroups(parentAlbCloudUrn string, queryParameters url.Values) ([]*NsxtAlbImportableServiceEngineGroups, error) {
   116  	client := nsxtAlbCloud.vcdClient.Client
   117  	if parentAlbCloudUrn == "" {
   118  		return nil, fmt.Errorf("parentAlbCloudUrn is required")
   119  	}
   120  	if !client.IsSysAdmin {
   121  		return nil, errors.New("handling NSX-T ALB Importable Service Engine Groups requires System user")
   122  	}
   123  
   124  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbImportableServiceEngineGroups
   125  	apiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	queryParams := copyOrNewUrlValues(queryParameters)
   136  	queryParams = queryParameterFilterAnd(fmt.Sprintf("_context==%s", parentAlbCloudUrn), queryParams)
   137  	typeResponses := []*types.NsxtAlbImportableServiceEngineGroups{{}}
   138  	err = client.OpenApiGetAllItems(apiVersion, urlRef, queryParams, &typeResponses, nil)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	wrappedResponses := make([]*NsxtAlbImportableServiceEngineGroups, len(typeResponses))
   144  	for sliceIndex := range typeResponses {
   145  		wrappedResponses[sliceIndex] = &NsxtAlbImportableServiceEngineGroups{
   146  			NsxtAlbImportableServiceEngineGroups: typeResponses[sliceIndex],
   147  			vcdClient:                            nsxtAlbCloud.vcdClient,
   148  		}
   149  	}
   150  
   151  	return wrappedResponses, nil
   152  }
   153  
   154  // GetAlbImportableServiceEngineGroupByName returns importable NSX-T ALB Clouds.
   155  func (nsxtAlbCloud *NsxtAlbCloud) GetAlbImportableServiceEngineGroupByName(parentAlbCloudUrn, name string) (*NsxtAlbImportableServiceEngineGroups, error) {
   156  	albClouds, err := nsxtAlbCloud.vcdClient.GetAllAlbImportableServiceEngineGroups(parentAlbCloudUrn, nil)
   157  	if err != nil {
   158  		return nil, fmt.Errorf("error finding NSX-T ALB Importable Service Engine Group by Name '%s': %s", name, err)
   159  	}
   160  
   161  	// Filtering by ID is not supported by API therefore it must be filtered on client side
   162  	var foundResult bool
   163  	var foundAlbCloud *NsxtAlbImportableServiceEngineGroups
   164  	for i, value := range albClouds {
   165  		if albClouds[i].NsxtAlbImportableServiceEngineGroups.DisplayName == name {
   166  			foundResult = true
   167  			foundAlbCloud = value
   168  			break
   169  		}
   170  	}
   171  
   172  	if !foundResult {
   173  		return nil, fmt.Errorf("%s: could not find NSX-T ALB Importable Service Engine Group by Name %s", ErrorEntityNotFound, name)
   174  	}
   175  
   176  	return foundAlbCloud, nil
   177  }
   178  
   179  // GetAlbImportableServiceEngineGroupById
   180  // Note. ID filtering is performed on client side
   181  func (nsxtAlbCloud *NsxtAlbCloud) GetAlbImportableServiceEngineGroupById(parentAlbCloudUrn, id string) (*NsxtAlbImportableServiceEngineGroups, error) {
   182  	albClouds, err := nsxtAlbCloud.vcdClient.GetAllAlbImportableServiceEngineGroups(parentAlbCloudUrn, nil)
   183  	if err != nil {
   184  		return nil, fmt.Errorf("error finding NSX-T ALB Importable Service Engine Group by ID '%s': %s", id, err)
   185  	}
   186  
   187  	// Filtering by ID is not supported by API therefore it must be filtered on client side
   188  	var foundResult bool
   189  	var foundImportableSEGroups *NsxtAlbImportableServiceEngineGroups
   190  	for i, value := range albClouds {
   191  		if albClouds[i].NsxtAlbImportableServiceEngineGroups.ID == id {
   192  			foundResult = true
   193  			foundImportableSEGroups = value
   194  		}
   195  	}
   196  
   197  	if !foundResult {
   198  		return nil, fmt.Errorf("%s: could not find NSX-T ALB Importable Service Engine Group by ID %s", ErrorEntityNotFound, id)
   199  	}
   200  
   201  	return foundImportableSEGroups, nil
   202  }