github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/ip_space_org_assignment.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  // IpSpaceOrgAssignment handles Custom Quotas (name in UI) for a particular Org. They complement
    15  // default quotas which are being set in IP Space itself.
    16  // The behavior of IpSpaceOrgAssignment is specific - whenever an NSX-T Edge Gateway backed by
    17  // Provider gateway using IP Spaces is being created - Org Assignment is created implicitly. One can
    18  // look up that assignment by IP Space and Org to update `types.IpSpaceOrgAssignment.CustomQuotas`
    19  // field
    20  type IpSpaceOrgAssignment struct {
    21  	IpSpaceOrgAssignment *types.IpSpaceOrgAssignment
    22  	IpSpaceId            string
    23  
    24  	vcdClient *VCDClient
    25  }
    26  
    27  // GetAllOrgAssignments retrieves all IP Space Org assignments within an IP Space
    28  //
    29  // Note. Org assignments are implicitly created after NSX-T Edge Gateway backed by Provider gateway
    30  // using IP Spaces is being created.
    31  func (ipSpace *IpSpace) GetAllOrgAssignments(queryParameters url.Values) ([]*IpSpaceOrgAssignment, error) {
    32  	client := ipSpace.vcdClient.Client
    33  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceOrgAssignments
    34  	apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	queryParams := queryParameterFilterAnd(fmt.Sprintf("ipSpaceRef.id==%s", ipSpace.IpSpace.ID), queryParameters)
    44  	typeResponses := []*types.IpSpaceOrgAssignment{{}}
    45  	err = client.OpenApiGetAllItems(apiVersion, urlRef, queryParams, &typeResponses, nil)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	// Wrap all typeResponses into IpSpaceOrgAssignment types with client
    51  	results := make([]*IpSpaceOrgAssignment, len(typeResponses))
    52  	for sliceIndex := range typeResponses {
    53  		results[sliceIndex] = &IpSpaceOrgAssignment{
    54  			IpSpaceOrgAssignment: typeResponses[sliceIndex],
    55  			IpSpaceId:            ipSpace.IpSpace.ID,
    56  			vcdClient:            ipSpace.vcdClient,
    57  		}
    58  	}
    59  
    60  	return results, nil
    61  }
    62  
    63  // GetOrgAssignmentById retrieves IP Space Org Assignment with a given ID
    64  func (ipSpace *IpSpace) GetOrgAssignmentById(id string) (*IpSpaceOrgAssignment, error) {
    65  	if id == "" {
    66  		return nil, fmt.Errorf("IP Space Org Assignment lookup requires ID")
    67  	}
    68  
    69  	client := ipSpace.vcdClient.Client
    70  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceOrgAssignments
    71  	apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	urlRef, err := client.OpenApiBuildEndpoint(endpoint, id)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	response := &IpSpaceOrgAssignment{
    82  		IpSpaceOrgAssignment: &types.IpSpaceOrgAssignment{},
    83  		IpSpaceId:            ipSpace.IpSpace.ID,
    84  		vcdClient:            ipSpace.vcdClient,
    85  	}
    86  
    87  	err = client.OpenApiGetItem(apiVersion, urlRef, nil, response.IpSpaceOrgAssignment, nil)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	return response, nil
    93  }
    94  
    95  // GetOrgAssignmentById retrieves IP Space Org Assignment with a given Org Name
    96  func (ipSpace *IpSpace) GetOrgAssignmentByOrgName(orgName string) (*IpSpaceOrgAssignment, error) {
    97  	if orgName == "" {
    98  		return nil, fmt.Errorf("name of Org is required")
    99  	}
   100  	queryParams := queryParameterFilterAnd(fmt.Sprintf("orgRef.name==%s", orgName), nil)
   101  	results, err := ipSpace.GetAllOrgAssignments(queryParams)
   102  	if err != nil {
   103  		return nil, fmt.Errorf("error retrieving IP Space Org Assignments by Org Name: %s", err)
   104  	}
   105  
   106  	singleResult, err := oneOrError("Org Name", orgName, results)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	return singleResult, nil
   112  }
   113  
   114  // GetOrgAssignmentById retrieves IP Space Org Assignment with a given Org ID
   115  func (ipSpace *IpSpace) GetOrgAssignmentByOrgId(orgId string) (*IpSpaceOrgAssignment, error) {
   116  	if orgId == "" {
   117  		return nil, fmt.Errorf("organization ID is required")
   118  	}
   119  	queryParams := queryParameterFilterAnd(fmt.Sprintf("orgRef.id==%s", orgId), nil)
   120  	results, err := ipSpace.GetAllOrgAssignments(queryParams)
   121  	if err != nil {
   122  		return nil, fmt.Errorf("error retrieving IP Space Org Assignments by Org ID: %s", err)
   123  	}
   124  
   125  	singleResult, err := oneOrError("Org ID", orgId, results)
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	return singleResult, nil
   131  }
   132  
   133  // Update Org Assignment
   134  func (ipSpaceOrgAssignment *IpSpaceOrgAssignment) Update(ipSpaceOrgAssignmentConfig *types.IpSpaceOrgAssignment) (*IpSpaceOrgAssignment, error) {
   135  	client := ipSpaceOrgAssignment.vcdClient.Client
   136  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceOrgAssignments
   137  	apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	ipSpaceOrgAssignmentConfig.ID = ipSpaceOrgAssignment.IpSpaceOrgAssignment.ID
   143  	urlRef, err := client.OpenApiBuildEndpoint(endpoint, ipSpaceOrgAssignmentConfig.ID)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	result := &IpSpaceOrgAssignment{
   149  		IpSpaceOrgAssignment: &types.IpSpaceOrgAssignment{},
   150  		IpSpaceId:            ipSpaceOrgAssignment.IpSpaceId,
   151  		vcdClient:            ipSpaceOrgAssignment.vcdClient,
   152  	}
   153  
   154  	err = client.OpenApiPutItem(apiVersion, urlRef, nil, ipSpaceOrgAssignmentConfig, result.IpSpaceOrgAssignment, nil)
   155  	if err != nil {
   156  		return nil, fmt.Errorf("error updating IP Space Org Assignment: %s", err)
   157  	}
   158  
   159  	return result, nil
   160  }