github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/openapi_org_network.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  	"strconv"
    11  
    12  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    13  )
    14  
    15  const labelOrgVdcNetworkSegmentProfile = "Org VDC Network Segment Profile"
    16  
    17  // OpenApiOrgVdcNetwork uses OpenAPI endpoint to operate both - NSX-T and NSX-V Org VDC networks
    18  type OpenApiOrgVdcNetwork struct {
    19  	OpenApiOrgVdcNetwork *types.OpenApiOrgVdcNetwork
    20  	client               *Client
    21  }
    22  
    23  // GetOpenApiOrgVdcNetworkById allows to retrieve both - NSX-T and NSX-V Org VDC networks
    24  func (org *Org) GetOpenApiOrgVdcNetworkById(id string) (*OpenApiOrgVdcNetwork, error) {
    25  	// Inject Org ID filter to perform filtering on server side
    26  	params := url.Values{}
    27  	filterParams := queryParameterFilterAnd("orgRef.id=="+org.Org.ID, params)
    28  	return getOpenApiOrgVdcNetworkById(org.client, id, filterParams)
    29  }
    30  
    31  // GetOpenApiOrgVdcNetworkByNameAndOwnerId allows to retrieve both - NSX-T and NSX-V Org VDC networks
    32  // by network name and Owner (VDC or VDC Group) ID
    33  func (org *Org) GetOpenApiOrgVdcNetworkByNameAndOwnerId(name, ownerId string) (*OpenApiOrgVdcNetwork, error) {
    34  	// Inject Org ID filter to perform filtering on server side
    35  	queryParameters := url.Values{}
    36  	queryParameters = queryParameterFilterAnd(fmt.Sprintf("name==%s;ownerRef.id==%s", name, ownerId), queryParameters)
    37  
    38  	allEdges, err := getAllOpenApiOrgVdcNetworks(org.client, queryParameters)
    39  	if err != nil {
    40  		return nil, fmt.Errorf("unable to retrieve Org VDC network by name '%s' in Owner '%s': %s", name, ownerId, err)
    41  	}
    42  
    43  	return returnSingleOpenApiOrgVdcNetwork(name, allEdges)
    44  }
    45  
    46  // GetOpenApiOrgVdcNetworkById allows to retrieve both - NSX-T and NSX-V Org VDC networks
    47  func (vdc *Vdc) GetOpenApiOrgVdcNetworkById(id string) (*OpenApiOrgVdcNetwork, error) {
    48  	return getOrgVdcNetworkById(vdc.client, id, vdc.Vdc.ID)
    49  }
    50  
    51  // GetOpenApiOrgVdcNetworkById allows to retrieve both - NSX-T and NSX-V Org VDC Group networks
    52  func (vdcGroup *VdcGroup) GetOpenApiOrgVdcNetworkById(id string) (*OpenApiOrgVdcNetwork, error) {
    53  	return getOrgVdcNetworkById(vdcGroup.client, id, vdcGroup.VdcGroup.Id)
    54  }
    55  
    56  // getOrgVdcNetworkById allows to retrieve both - NSX-T and NSX-V Org VDC Group networks
    57  func getOrgVdcNetworkById(client *Client, id, ownerId string) (*OpenApiOrgVdcNetwork, error) {
    58  	// Inject Vdc ID filter to perform filtering on server side
    59  	params := url.Values{}
    60  	filterParams := queryParameterFilterAnd("ownerRef.id=="+ownerId, params)
    61  	egw, err := getOpenApiOrgVdcNetworkById(client, id, filterParams)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	return egw, nil
    67  }
    68  
    69  // GetOpenApiOrgVdcNetworkByName allows to retrieve both - NSX-T and NSX-V Org VDC networks
    70  func (vdc *Vdc) GetOpenApiOrgVdcNetworkByName(name string) (*OpenApiOrgVdcNetwork, error) {
    71  	queryParameters := url.Values{}
    72  	queryParameters.Add("filter", fmt.Sprintf("name==%s", name))
    73  
    74  	allEdges, err := vdc.GetAllOpenApiOrgVdcNetworks(queryParameters)
    75  	if err != nil {
    76  		return nil, fmt.Errorf("unable to retrieve Org VDC network by name '%s': %s", name, err)
    77  	}
    78  
    79  	return returnSingleOpenApiOrgVdcNetwork(name, allEdges)
    80  }
    81  
    82  // GetOpenApiOrgVdcNetworkByName allows to retrieve both - NSX-T and NSX-V Org VDC networks
    83  func (vdcGroup *VdcGroup) GetOpenApiOrgVdcNetworkByName(name string) (*OpenApiOrgVdcNetwork, error) {
    84  	queryParameters := url.Values{}
    85  	queryParameters.Add("filter", fmt.Sprintf("name==%s", name))
    86  
    87  	allEdges, err := vdcGroup.GetAllOpenApiOrgVdcNetworks(queryParameters)
    88  	if err != nil {
    89  		return nil, fmt.Errorf("unable to retrieve Org VDC network by name '%s': %s", name, err)
    90  	}
    91  
    92  	return returnSingleOpenApiOrgVdcNetwork(name, allEdges)
    93  }
    94  
    95  // GetAllOpenApiOrgVdcNetworks allows to retrieve all NSX-T or NSX-V Org VDC networks in Org
    96  //
    97  // Note. If pageSize > 32 it will be limited to maximum of 32 in this function because API validation does not allow for
    98  // higher number
    99  func (org *Org) GetAllOpenApiOrgVdcNetworks(queryParameters url.Values) ([]*OpenApiOrgVdcNetwork, error) {
   100  	filteredQueryParams := queryParameterFilterAnd("orgRef.id=="+org.Org.ID, queryParameters)
   101  	return getAllOpenApiOrgVdcNetworks(org.client, filteredQueryParams)
   102  }
   103  
   104  // GetAllOpenApiOrgVdcNetworks allows to retrieve all NSX-T or NSX-V Org VDC networks in Vdc
   105  //
   106  // Note. If pageSize > 32 it will be limited to maximum of 32 in this function because API validation does not allow for
   107  // higher number
   108  func (vdc *Vdc) GetAllOpenApiOrgVdcNetworks(queryParameters url.Values) ([]*OpenApiOrgVdcNetwork, error) {
   109  	filteredQueryParams := queryParameterFilterAnd("ownerRef.id=="+vdc.Vdc.ID, queryParameters)
   110  	return getAllOpenApiOrgVdcNetworks(vdc.client, filteredQueryParams)
   111  }
   112  
   113  // GetAllOpenApiOrgVdcNetworks allows to retrieve all NSX-T or NSX-V Org VDC networks in Vdc
   114  //
   115  // Note. If pageSize > 32 it will be limited to maximum of 32 in this function because API validation does not allow for
   116  // higher number
   117  func (vdcGroup *VdcGroup) GetAllOpenApiOrgVdcNetworks(queryParameters url.Values) ([]*OpenApiOrgVdcNetwork, error) {
   118  	filteredQueryParams := queryParameterFilterAnd("ownerRef.id=="+vdcGroup.VdcGroup.Id, queryParameters)
   119  	return getAllOpenApiOrgVdcNetworks(vdcGroup.client, filteredQueryParams)
   120  }
   121  
   122  // CreateOpenApiOrgVdcNetwork allows to create NSX-T or NSX-V Org VDC network
   123  func (org *Org) CreateOpenApiOrgVdcNetwork(orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork) (*OpenApiOrgVdcNetwork, error) {
   124  	return createOpenApiOrgVdcNetwork(org.client, orgVdcNetworkConfig)
   125  }
   126  
   127  // CreateOpenApiOrgVdcNetwork allows to create NSX-T or NSX-V Org VDC network
   128  func (vdc *Vdc) CreateOpenApiOrgVdcNetwork(orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork) (*OpenApiOrgVdcNetwork, error) {
   129  	return createOpenApiOrgVdcNetwork(vdc.client, orgVdcNetworkConfig)
   130  }
   131  
   132  // CreateOpenApiOrgVdcNetwork allows to create NSX-T or NSX-V Org VDC network
   133  func (vdcGroup *VdcGroup) CreateOpenApiOrgVdcNetwork(orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork) (*OpenApiOrgVdcNetwork, error) {
   134  	return createOpenApiOrgVdcNetwork(vdcGroup.client, orgVdcNetworkConfig)
   135  }
   136  
   137  // UpdateDhcp updates DHCP configuration for specific Org VDC network
   138  func (orgVdcNet *OpenApiOrgVdcNetwork) UpdateDhcp(orgVdcNetworkDhcpConfig *types.OpenApiOrgVdcNetworkDhcp) (*OpenApiOrgVdcNetworkDhcp, error) {
   139  	if orgVdcNet.client == nil || orgVdcNet.OpenApiOrgVdcNetwork == nil || orgVdcNet.OpenApiOrgVdcNetwork.ID == "" {
   140  		return nil, fmt.Errorf("error - Org VDC network structure must be set and have ID field available")
   141  	}
   142  	return updateOrgNetworkDhcp(orgVdcNet.client, orgVdcNet.OpenApiOrgVdcNetwork.ID, orgVdcNetworkDhcpConfig)
   143  }
   144  
   145  // Update allows to update Org VDC network
   146  func (orgVdcNet *OpenApiOrgVdcNetwork) Update(OrgVdcNetworkConfig *types.OpenApiOrgVdcNetwork) (*OpenApiOrgVdcNetwork, error) {
   147  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks
   148  	minimumApiVersion, err := orgVdcNet.client.checkOpenApiEndpointCompatibility(endpoint)
   149  	if err != nil {
   150  		return nil, err
   151  	}
   152  
   153  	if OrgVdcNetworkConfig.ID == "" {
   154  		return nil, fmt.Errorf("cannot update Org VDC network without ID")
   155  	}
   156  
   157  	urlRef, err := orgVdcNet.client.OpenApiBuildEndpoint(endpoint, OrgVdcNetworkConfig.ID)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  
   162  	returnEgw := &OpenApiOrgVdcNetwork{
   163  		OpenApiOrgVdcNetwork: &types.OpenApiOrgVdcNetwork{},
   164  		client:               orgVdcNet.client,
   165  	}
   166  
   167  	err = orgVdcNet.client.OpenApiPutItem(minimumApiVersion, urlRef, nil, OrgVdcNetworkConfig, returnEgw.OpenApiOrgVdcNetwork, nil)
   168  	if err != nil {
   169  		return nil, fmt.Errorf("error updating Org VDC network: %s", err)
   170  	}
   171  
   172  	return returnEgw, nil
   173  }
   174  
   175  // Delete allows to delete Org VDC network
   176  func (orgVdcNet *OpenApiOrgVdcNetwork) Delete() error {
   177  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks
   178  	minimumApiVersion, err := orgVdcNet.client.checkOpenApiEndpointCompatibility(endpoint)
   179  	if err != nil {
   180  		return err
   181  	}
   182  
   183  	if orgVdcNet.OpenApiOrgVdcNetwork.ID == "" {
   184  		return fmt.Errorf("cannot delete Org VDC network without ID")
   185  	}
   186  
   187  	urlRef, err := orgVdcNet.client.OpenApiBuildEndpoint(endpoint, orgVdcNet.OpenApiOrgVdcNetwork.ID)
   188  	if err != nil {
   189  		return err
   190  	}
   191  
   192  	err = orgVdcNet.client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil, nil)
   193  
   194  	if err != nil {
   195  		return fmt.Errorf("error deleting Org VDC network: %s", err)
   196  	}
   197  
   198  	return nil
   199  }
   200  
   201  // GetType returns type of Org VDC network
   202  func (orgVdcNet *OpenApiOrgVdcNetwork) GetType() string {
   203  	return orgVdcNet.OpenApiOrgVdcNetwork.NetworkType
   204  }
   205  
   206  // IsIsolated returns true if the network type is isolated (NSX-V and NSX-T)
   207  func (orgVdcNet *OpenApiOrgVdcNetwork) IsIsolated() bool {
   208  	return orgVdcNet.GetType() == types.OrgVdcNetworkTypeIsolated
   209  }
   210  
   211  // IsRouted returns true if the network type is isolated (NSX-V and NSX-T)
   212  func (orgVdcNet *OpenApiOrgVdcNetwork) IsRouted() bool {
   213  	return orgVdcNet.GetType() == types.OrgVdcNetworkTypeRouted
   214  }
   215  
   216  // IsImported returns true if the network type is imported (NSX-T only)
   217  func (orgVdcNet *OpenApiOrgVdcNetwork) IsImported() bool {
   218  	return orgVdcNet.GetType() == types.OrgVdcNetworkTypeOpaque
   219  }
   220  
   221  // IsDirect returns true if the network type is direct (NSX-V only)
   222  func (orgVdcNet *OpenApiOrgVdcNetwork) IsDirect() bool {
   223  	return orgVdcNet.GetType() == types.OrgVdcNetworkTypeDirect
   224  }
   225  
   226  // IsNsxt returns true if the network is backed by NSX-T
   227  func (orgVdcNet *OpenApiOrgVdcNetwork) IsNsxt() bool {
   228  
   229  	// orgVdcNet.OpenApiOrgVdcNetwork.OrgVdcIsNsxTBacked returns `true` only if network is a member
   230  	// of VDC (not VDC Group) therefore an additional check for `BackingNetworkType` is required
   231  
   232  	return orgVdcNet.OpenApiOrgVdcNetwork.OrgVdcIsNsxTBacked ||
   233  		orgVdcNet.OpenApiOrgVdcNetwork.BackingNetworkType == types.OpenApiOrgVdcNetworkBackingTypeNsxt
   234  }
   235  
   236  // IsDhcpEnabled returns true if DHCP is enabled for NSX-T Org VDC network, false otherwise
   237  func (orgVdcNet *OpenApiOrgVdcNetwork) IsDhcpEnabled() bool {
   238  	if !orgVdcNet.IsNsxt() {
   239  		return false
   240  	}
   241  
   242  	dhcpConfig, err := orgVdcNet.GetOpenApiOrgVdcNetworkDhcp()
   243  	if err != nil {
   244  		return false
   245  	}
   246  
   247  	if dhcpConfig == nil || dhcpConfig.OpenApiOrgVdcNetworkDhcp == nil || dhcpConfig.OpenApiOrgVdcNetworkDhcp.Enabled == nil || !*dhcpConfig.OpenApiOrgVdcNetworkDhcp.Enabled {
   248  		return false
   249  	}
   250  
   251  	return true
   252  }
   253  
   254  // getOpenApiOrgVdcNetworkById is a private parent for wrapped functions:
   255  // func (org *Org) GetOpenApiOrgVdcNetworkById(id string) (*OpenApiOrgVdcNetwork, error)
   256  // func (vdc *Vdc) GetOpenApiOrgVdcNetworkById(id string) (*OpenApiOrgVdcNetwork, error)
   257  func getOpenApiOrgVdcNetworkById(client *Client, id string, queryParameters url.Values) (*OpenApiOrgVdcNetwork, error) {
   258  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks
   259  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   260  	if err != nil {
   261  		return nil, err
   262  	}
   263  
   264  	if id == "" {
   265  		return nil, fmt.Errorf("empty Org VDC network ID")
   266  	}
   267  
   268  	urlRef, err := client.OpenApiBuildEndpoint(endpoint, id)
   269  	if err != nil {
   270  		return nil, err
   271  	}
   272  
   273  	egw := &OpenApiOrgVdcNetwork{
   274  		OpenApiOrgVdcNetwork: &types.OpenApiOrgVdcNetwork{},
   275  		client:               client,
   276  	}
   277  
   278  	err = client.OpenApiGetItem(minimumApiVersion, urlRef, queryParameters, egw.OpenApiOrgVdcNetwork, nil)
   279  	if err != nil {
   280  		return nil, err
   281  	}
   282  
   283  	return egw, nil
   284  }
   285  
   286  // returnSingleOpenApiOrgVdcNetwork helps to reduce code duplication for `GetOpenApiOrgVdcNetworkByName` functions with different
   287  // receivers
   288  func returnSingleOpenApiOrgVdcNetwork(name string, allEdges []*OpenApiOrgVdcNetwork) (*OpenApiOrgVdcNetwork, error) {
   289  	if len(allEdges) > 1 {
   290  		return nil, fmt.Errorf("got more than one Org VDC network by name '%s' %d", name, len(allEdges))
   291  	}
   292  
   293  	if len(allEdges) < 1 {
   294  		return nil, fmt.Errorf("%s: got zero Org VDC networks by name '%s'", ErrorEntityNotFound, name)
   295  	}
   296  
   297  	return allEdges[0], nil
   298  }
   299  
   300  // getAllOpenApiOrgVdcNetworks is a private parent for wrapped functions:
   301  // func (vdc *Vdc) GetAllOpenApiOrgVdcNetworks(queryParameters url.Values) ([]*OpenApiOrgVdcNetwork, error)
   302  //
   303  // Note. If pageSize > 32 it will be limited to maximum of 32 in this function because API validation does not allow
   304  // higher number
   305  func getAllOpenApiOrgVdcNetworks(client *Client, queryParameters url.Values) ([]*OpenApiOrgVdcNetwork, error) {
   306  
   307  	// Enforce maximum pageSize to be 32 as API endpoint throws error if it is > 32
   308  	pageSizeString := queryParameters.Get("pageSize")
   309  
   310  	switch pageSizeString {
   311  	// If no pageSize is specified it must be set to 32 as by default low level API function OpenApiGetAllItems sets 128
   312  	case "":
   313  		queryParameters.Set("pageSize", "32")
   314  
   315  	// If pageSize is specified ensure it is not >32
   316  	default:
   317  		pageSizeValue, err := strconv.Atoi(pageSizeString)
   318  		if err != nil {
   319  			return nil, fmt.Errorf("error parsing pageSize value: %s", err)
   320  		}
   321  		if pageSizeString != "" && pageSizeValue > 32 {
   322  			queryParameters.Set("pageSize", "32")
   323  		}
   324  	}
   325  
   326  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks
   327  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   328  	if err != nil {
   329  		return nil, err
   330  	}
   331  
   332  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
   333  	if err != nil {
   334  		return nil, err
   335  	}
   336  
   337  	typeResponses := []*types.OpenApiOrgVdcNetwork{{}}
   338  	err = client.OpenApiGetAllItems(minimumApiVersion, urlRef, queryParameters, &typeResponses, nil)
   339  	if err != nil {
   340  		return nil, err
   341  	}
   342  
   343  	// Wrap all typeResponses into OpenApiOrgVdcNetwork types with client
   344  	wrappedResponses := make([]*OpenApiOrgVdcNetwork, len(typeResponses))
   345  	for sliceIndex := range typeResponses {
   346  		wrappedResponses[sliceIndex] = &OpenApiOrgVdcNetwork{
   347  			OpenApiOrgVdcNetwork: typeResponses[sliceIndex],
   348  			client:               client,
   349  		}
   350  	}
   351  
   352  	return wrappedResponses, nil
   353  }
   354  
   355  // createOpenApiOrgVdcNetwork is wrapped by public CreateOpenApiOrgVdcNetwork methods
   356  func createOpenApiOrgVdcNetwork(client *Client, OrgVdcNetworkConfig *types.OpenApiOrgVdcNetwork) (*OpenApiOrgVdcNetwork, error) {
   357  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks
   358  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   359  	if err != nil {
   360  		return nil, err
   361  	}
   362  
   363  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
   364  	if err != nil {
   365  		return nil, err
   366  	}
   367  
   368  	returnEgw := &OpenApiOrgVdcNetwork{
   369  		OpenApiOrgVdcNetwork: &types.OpenApiOrgVdcNetwork{},
   370  		client:               client,
   371  	}
   372  
   373  	err = client.OpenApiPostItem(minimumApiVersion, urlRef, nil, OrgVdcNetworkConfig, returnEgw.OpenApiOrgVdcNetwork, nil)
   374  	if err != nil {
   375  		return nil, fmt.Errorf("error creating Org VDC network: %s", err)
   376  	}
   377  
   378  	return returnEgw, nil
   379  }
   380  
   381  // GetSegmentProfile retrieves Segment Profile configuration for a single Org VDC Network
   382  func (orgVdcNet *OpenApiOrgVdcNetwork) GetSegmentProfile() (*types.OrgVdcNetworkSegmentProfiles, error) {
   383  	c := crudConfig{
   384  		endpoint:       types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworkSegmentProfiles,
   385  		endpointParams: []string{orgVdcNet.OpenApiOrgVdcNetwork.ID},
   386  		entityLabel:    labelOrgVdcNetworkSegmentProfile,
   387  	}
   388  	return getInnerEntity[types.OrgVdcNetworkSegmentProfiles](orgVdcNet.client, c)
   389  }
   390  
   391  // UpdateSegmentProfile updates a Segment Profile with a given configuration
   392  func (orgVdcNet *OpenApiOrgVdcNetwork) UpdateSegmentProfile(entityConfig *types.OrgVdcNetworkSegmentProfiles) (*types.OrgVdcNetworkSegmentProfiles, error) {
   393  	c := crudConfig{
   394  		endpoint:       types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworkSegmentProfiles,
   395  		endpointParams: []string{orgVdcNet.OpenApiOrgVdcNetwork.ID},
   396  		entityLabel:    labelOrgVdcNetworkSegmentProfile,
   397  	}
   398  	return updateInnerEntity(orgVdcNet.client, c, entityConfig)
   399  }