github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/service_offering.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     6  	"code.cloudfoundry.org/cli/resources"
     7  	"code.cloudfoundry.org/cli/util/lookuptable"
     8  )
     9  
    10  // GetServiceOffering lists service offering with optional filters.
    11  func (client *Client) GetServiceOfferings(query ...Query) ([]resources.ServiceOffering, Warnings, error) {
    12  	var result []resources.ServiceOffering
    13  
    14  	query = append(query, Query{Key: FieldsServiceBroker, Values: []string{"name", "guid"}})
    15  
    16  	included, warnings, err := client.MakeListRequest(RequestParams{
    17  		RequestName:  internal.GetServiceOfferingsRequest,
    18  		Query:        query,
    19  		ResponseBody: resources.ServiceOffering{},
    20  		AppendToList: func(item interface{}) error {
    21  			result = append(result, item.(resources.ServiceOffering))
    22  			return nil
    23  		},
    24  	})
    25  
    26  	brokerNameLookup := lookuptable.NameFromGUID(included.ServiceBrokers)
    27  
    28  	for i, _ := range result {
    29  		result[i].ServiceBrokerName = brokerNameLookup[result[i].ServiceBrokerGUID]
    30  	}
    31  
    32  	return result, warnings, err
    33  }
    34  
    35  func (client *Client) GetServiceOfferingByGUID(guid string) (resources.ServiceOffering, Warnings, error) {
    36  	if guid == "" {
    37  		return resources.ServiceOffering{}, nil, ccerror.ServiceOfferingNotFoundError{}
    38  	}
    39  
    40  	var result resources.ServiceOffering
    41  
    42  	_, warnings, err := client.MakeRequest(RequestParams{
    43  		RequestName:  internal.GetServiceOfferingRequest,
    44  		URIParams:    internal.Params{"service_offering_guid": guid},
    45  		ResponseBody: &result,
    46  	})
    47  
    48  	return result, warnings, err
    49  }
    50  
    51  func (client *Client) GetServiceOfferingByNameAndBroker(serviceOfferingName, serviceBrokerName string) (resources.ServiceOffering, Warnings, error) {
    52  	query := []Query{
    53  		{Key: NameFilter, Values: []string{serviceOfferingName}},
    54  		{Key: PerPage, Values: []string{"2"}},
    55  		{Key: Page, Values: []string{"1"}},
    56  	}
    57  	if serviceBrokerName != "" {
    58  		query = append(query, Query{Key: ServiceBrokerNamesFilter, Values: []string{serviceBrokerName}})
    59  	}
    60  
    61  	offerings, warnings, err := client.GetServiceOfferings(query...)
    62  	if err != nil {
    63  		return resources.ServiceOffering{}, warnings, err
    64  	}
    65  
    66  	switch len(offerings) {
    67  	case 0:
    68  		return resources.ServiceOffering{}, warnings, ccerror.ServiceOfferingNotFoundError{
    69  			ServiceOfferingName: serviceOfferingName,
    70  			ServiceBrokerName:   serviceBrokerName,
    71  		}
    72  	case 1:
    73  		return offerings[0], warnings, nil
    74  	default:
    75  		return resources.ServiceOffering{}, warnings, ccerror.ServiceOfferingNameAmbiguityError{
    76  			ServiceOfferingName: serviceOfferingName,
    77  			ServiceBrokerNames:  extractServiceBrokerNames(offerings),
    78  		}
    79  	}
    80  }
    81  
    82  func (client *Client) PurgeServiceOffering(serviceOfferingGUID string) (Warnings, error) {
    83  	_, warnings, err := client.MakeRequest(RequestParams{
    84  		RequestName: internal.DeleteServiceOfferingRequest,
    85  		URIParams:   internal.Params{"service_offering_guid": serviceOfferingGUID},
    86  		Query:       []Query{{Key: Purge, Values: []string{"true"}}},
    87  	})
    88  	return warnings, err
    89  }
    90  
    91  func extractServiceBrokerNames(offerings []resources.ServiceOffering) (result []string) {
    92  	for _, o := range offerings {
    93  		result = append(result, o.ServiceBrokerName)
    94  	}
    95  	return
    96  }