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

     1  /*
     2   * Copyright 2023 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  const labelIpSpaceUplink = "IP Space Uplink"
    15  
    16  // IpSpaceUplink provides the capability to assign one or more IP Spaces as Uplinks to External
    17  // Networks
    18  type IpSpaceUplink struct {
    19  	IpSpaceUplink *types.IpSpaceUplink
    20  	vcdClient     *VCDClient
    21  }
    22  
    23  // wrap is a hidden helper that facilitates the usage of a generic CRUD function
    24  //
    25  //lint:ignore U1000 this method is used in generic functions, but annoys staticcheck
    26  func (i IpSpaceUplink) wrap(inner *types.IpSpaceUplink) *IpSpaceUplink {
    27  	i.IpSpaceUplink = inner
    28  	return &i
    29  }
    30  
    31  // CreateIpSpaceUplink creates an IP Space Uplink with a given configuration
    32  func (vcdClient *VCDClient) CreateIpSpaceUplink(ipSpaceUplinkConfig *types.IpSpaceUplink) (*IpSpaceUplink, error) {
    33  	c := crudConfig{
    34  		endpoint:    types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceUplinks,
    35  		entityLabel: labelIpSpaceUplink,
    36  	}
    37  
    38  	outerType := IpSpaceUplink{vcdClient: vcdClient}
    39  	return createOuterEntity(&vcdClient.Client, outerType, c, ipSpaceUplinkConfig)
    40  }
    41  
    42  // GetAllIpSpaceUplinks retrieves all IP Space Uplinks for a given External Network ID
    43  //
    44  // externalNetworkId is mandatory
    45  func (vcdClient *VCDClient) GetAllIpSpaceUplinks(externalNetworkId string, queryParameters url.Values) ([]*IpSpaceUplink, error) {
    46  	if externalNetworkId == "" {
    47  		return nil, fmt.Errorf("mandatory External Network ID is empty")
    48  	}
    49  
    50  	queryparams := queryParameterFilterAnd(fmt.Sprintf("externalNetworkRef.id==%s", externalNetworkId), queryParameters)
    51  	c := crudConfig{
    52  		endpoint:        types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceUplinks,
    53  		entityLabel:     labelIpSpaceUplink,
    54  		queryParameters: queryparams,
    55  	}
    56  
    57  	outerType := IpSpaceUplink{vcdClient: vcdClient}
    58  	return getAllOuterEntities[IpSpaceUplink, types.IpSpaceUplink](&vcdClient.Client, outerType, c)
    59  }
    60  
    61  // GetIpSpaceUplinkByName retrieves a single IP Space Uplink by Name in a given External Network
    62  func (vcdClient *VCDClient) GetIpSpaceUplinkByName(externalNetworkId, name string) (*IpSpaceUplink, error) {
    63  	queryParams := queryParameterFilterAnd(fmt.Sprintf("name==%s", name), nil)
    64  	allIpSpaceUplinks, err := vcdClient.GetAllIpSpaceUplinks(externalNetworkId, queryParams)
    65  	if err != nil {
    66  		return nil, fmt.Errorf("error getting IP Space Uplink by Name '%s':%s", name, err)
    67  	}
    68  
    69  	return oneOrError("name", name, allIpSpaceUplinks)
    70  }
    71  
    72  // GetIpSpaceUplinkById retrieves IP Space Uplink with a given ID
    73  func (vcdClient *VCDClient) GetIpSpaceUplinkById(id string) (*IpSpaceUplink, error) {
    74  	c := crudConfig{
    75  		endpoint:       types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceUplinks,
    76  		endpointParams: []string{id},
    77  		entityLabel:    labelIpSpaceUplink,
    78  	}
    79  
    80  	outerType := IpSpaceUplink{vcdClient: vcdClient}
    81  	return getOuterEntity[IpSpaceUplink, types.IpSpaceUplink](&vcdClient.Client, outerType, c)
    82  }
    83  
    84  // Update IP Space Uplink
    85  func (ipSpaceUplink *IpSpaceUplink) Update(ipSpaceUplinkConfig *types.IpSpaceUplink) (*IpSpaceUplink, error) {
    86  	c := crudConfig{
    87  		endpoint:       types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceUplinks,
    88  		endpointParams: []string{ipSpaceUplink.IpSpaceUplink.ID},
    89  		entityLabel:    labelIpSpaceUplink,
    90  	}
    91  
    92  	outerType := IpSpaceUplink{vcdClient: ipSpaceUplink.vcdClient}
    93  	return updateOuterEntity(&ipSpaceUplink.vcdClient.Client, outerType, c, ipSpaceUplinkConfig)
    94  }
    95  
    96  // Delete IP Space Uplink
    97  func (ipSpaceUplink *IpSpaceUplink) Delete() error {
    98  	if ipSpaceUplink == nil || ipSpaceUplink.IpSpaceUplink == nil || ipSpaceUplink.IpSpaceUplink.ID == "" {
    99  		return fmt.Errorf("IP Space Uplink must have ID")
   100  	}
   101  
   102  	c := crudConfig{
   103  		endpoint:       types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceUplinks,
   104  		endpointParams: []string{ipSpaceUplink.IpSpaceUplink.ID},
   105  		entityLabel:    labelIpSpaceUplink,
   106  	}
   107  
   108  	return deleteEntityById(&ipSpaceUplink.vcdClient.Client, c)
   109  }