github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v2action/service_plan.go (about)

     1  package v2action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     7  )
     8  
     9  type ServicePlan ccv2.ServicePlan
    10  
    11  func (actor Actor) GetServicePlan(servicePlanGUID string) (ServicePlan, Warnings, error) {
    12  	servicePlan, warnings, err := actor.CloudControllerClient.GetServicePlan(servicePlanGUID)
    13  	return ServicePlan(servicePlan), Warnings(warnings), err
    14  }
    15  
    16  // GetServicePlansForService returns a list of plans associated with the service and the broker if provided
    17  func (actor Actor) GetServicePlansForService(serviceName, brokerName string) ([]ServicePlan, Warnings, error) {
    18  	service, allWarnings, err := actor.GetServiceByNameAndBrokerName(serviceName, brokerName)
    19  	if err != nil {
    20  		return []ServicePlan{}, allWarnings, err
    21  	}
    22  
    23  	servicePlans, warnings, err := actor.CloudControllerClient.GetServicePlans(ccv2.Filter{
    24  		Type:     constant.ServiceGUIDFilter,
    25  		Operator: constant.EqualOperator,
    26  		Values:   []string{service.GUID},
    27  	})
    28  	allWarnings = append(allWarnings, warnings...)
    29  	if err != nil {
    30  		return []ServicePlan{}, allWarnings, err
    31  	}
    32  
    33  	var plansToReturn []ServicePlan
    34  	for _, plan := range servicePlans {
    35  		plansToReturn = append(plansToReturn, ServicePlan(plan))
    36  	}
    37  
    38  	return plansToReturn, allWarnings, nil
    39  }
    40  
    41  func (actor Actor) getServicePlanForServiceInSpace(servicePlanName, serviceName, spaceGUID, brokerName string) (ServicePlan, Warnings, error) {
    42  	services, allWarnings, err := actor.getServicesByNameForSpace(serviceName, spaceGUID)
    43  	if err != nil {
    44  		return ServicePlan{}, allWarnings, err
    45  	}
    46  
    47  	if len(services) == 0 {
    48  		return ServicePlan{}, Warnings(allWarnings), actionerror.ServiceNotFoundError{Name: serviceName}
    49  	}
    50  
    51  	if len(services) > 1 && brokerName == "" {
    52  		return ServicePlan{}, Warnings(allWarnings), actionerror.DuplicateServiceError{Name: serviceName}
    53  	}
    54  
    55  	service, err := findServiceByBrokerName(services, serviceName, brokerName)
    56  	if err != nil {
    57  		return ServicePlan{}, allWarnings, err
    58  	}
    59  
    60  	plans, warnings, err := actor.CloudControllerClient.GetServicePlans(ccv2.Filter{
    61  		Type:     constant.ServiceGUIDFilter,
    62  		Operator: constant.EqualOperator,
    63  		Values:   []string{service.GUID},
    64  	})
    65  
    66  	allWarnings = append(allWarnings, warnings...)
    67  	if err != nil {
    68  		return ServicePlan{}, allWarnings, err
    69  	}
    70  
    71  	for _, plan := range plans {
    72  		if servicePlanName == plan.Name {
    73  			return ServicePlan(plan), allWarnings, err
    74  		}
    75  	}
    76  
    77  	return ServicePlan{}, allWarnings, actionerror.ServicePlanNotFoundError{PlanName: servicePlanName, OfferingName: serviceName}
    78  }
    79  
    80  func findServiceByBrokerName(services []Service, serviceName, brokerName string) (Service, error) {
    81  	if brokerName == "" && len(services) == 1 {
    82  		return services[0], nil
    83  	}
    84  
    85  	for _, s := range services {
    86  		if s.ServiceBrokerName == brokerName {
    87  			return s, nil
    88  		}
    89  	}
    90  
    91  	return Service{}, actionerror.ServiceAndBrokerCombinationNotFoundError{ServiceName: serviceName, BrokerName: brokerName}
    92  }