code.cloudfoundry.org/cli@v7.1.0+incompatible/actor/v2action/service.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  // Service is representation of a service offering.
    10  type Service ccv2.Service
    11  
    12  // Filter is representation of Cloud Controller request query parameters.
    13  type Filter ccv2.Filter
    14  
    15  // ServicesWithPlans is an association between a Service and the plans it offers.
    16  type ServicesWithPlans map[Service][]ServicePlan
    17  
    18  // GetService fetches a service by GUID.
    19  func (actor Actor) GetService(serviceGUID string) (Service, Warnings, error) {
    20  	service, warnings, err := actor.CloudControllerClient.GetService(serviceGUID)
    21  	return Service(service), Warnings(warnings), err
    22  }
    23  
    24  // GetServiceByNameAndBrokerName returns services based on the name and broker provided.
    25  // If a service broker name is provided, only services for that broker will be returned.
    26  // If there are no services, a ServiceNotFoundError will be returned.
    27  //
    28  // NOTE: this will fail for non-admin users if the broker name is specified, as only
    29  // admin users are able to view the list of service brokers.
    30  func (actor Actor) GetServiceByNameAndBrokerName(serviceName, serviceBrokerName string) (Service, Warnings, error) {
    31  	filters := []ccv2.Filter{ccv2.Filter{
    32  		Type:     constant.LabelFilter,
    33  		Operator: constant.EqualOperator,
    34  		Values:   []string{serviceName},
    35  	}}
    36  
    37  	if serviceBrokerName != "" {
    38  		serviceBroker, warnings, err := actor.GetServiceBrokerByName(serviceBrokerName)
    39  		if err != nil {
    40  			return Service{}, warnings, err
    41  		}
    42  
    43  		brokerFilter := ccv2.Filter{
    44  			Type:     constant.ServiceBrokerGUIDFilter,
    45  			Operator: constant.EqualOperator,
    46  			Values:   []string{serviceBroker.GUID},
    47  		}
    48  		filters = append(filters, brokerFilter)
    49  	}
    50  
    51  	services, warnings, err := actor.CloudControllerClient.GetServices(filters...)
    52  	if err != nil {
    53  		return Service{}, Warnings(warnings), err
    54  	}
    55  
    56  	if len(services) == 0 {
    57  		return Service{}, Warnings(warnings), actionerror.ServiceNotFoundError{Name: serviceName}
    58  	}
    59  
    60  	if len(services) > 1 {
    61  		return Service{}, Warnings(warnings), actionerror.DuplicateServiceError{Name: serviceName}
    62  	}
    63  
    64  	return Service(services[0]), Warnings(warnings), nil
    65  }
    66  
    67  func (actor Actor) getServicesByNameForSpace(serviceName, spaceGUID string) ([]Service, Warnings, error) {
    68  	var filters []ccv2.Filter
    69  
    70  	filters = append(filters, ccv2.Filter{
    71  		Type:     constant.LabelFilter,
    72  		Operator: constant.EqualOperator,
    73  		Values:   []string{serviceName},
    74  	})
    75  
    76  	services, warnings, err := actor.CloudControllerClient.GetSpaceServices(spaceGUID, filters...)
    77  	var result []Service
    78  
    79  	if err != nil {
    80  		return result, Warnings(warnings), err
    81  	}
    82  
    83  	for _, service := range services {
    84  		result = append(result, Service(service))
    85  	}
    86  
    87  	return result, Warnings(warnings), nil
    88  }
    89  
    90  // GetServicesWithPlans returns a map of Services to ServicePlans for a particular broker.
    91  // A particular service with associated plans from a broker can be fetched by additionally providing
    92  // a service label filter.
    93  func (actor Actor) GetServicesWithPlans(filters ...Filter) (ServicesWithPlans, Warnings, error) {
    94  	ccv2Filters := []ccv2.Filter{}
    95  	for _, f := range filters {
    96  		ccv2Filters = append(ccv2Filters, ccv2.Filter(f))
    97  	}
    98  
    99  	var allWarnings Warnings
   100  
   101  	services, warnings, err := actor.CloudControllerClient.GetServices(ccv2Filters...)
   102  	allWarnings = append(allWarnings, Warnings(warnings)...)
   103  	if err != nil {
   104  		return nil, allWarnings, err
   105  	}
   106  
   107  	servicesWithPlans := ServicesWithPlans{}
   108  	for _, service := range services {
   109  		servicePlans, warnings, err := actor.CloudControllerClient.GetServicePlans(ccv2.Filter{
   110  			Type:     constant.ServiceGUIDFilter,
   111  			Operator: constant.EqualOperator,
   112  			Values:   []string{service.GUID},
   113  		})
   114  		allWarnings = append(allWarnings, Warnings(warnings)...)
   115  		if err != nil {
   116  			return nil, allWarnings, err
   117  		}
   118  
   119  		plansToReturn := []ServicePlan{}
   120  		for _, plan := range servicePlans {
   121  			plansToReturn = append(plansToReturn, ServicePlan(plan))
   122  		}
   123  
   124  		servicesWithPlans[Service(service)] = plansToReturn
   125  	}
   126  
   127  	return servicesWithPlans, allWarnings, nil
   128  }
   129  
   130  // ServiceExistsWithName returns true if there is an Organization with the
   131  // provided name, otherwise false.
   132  func (actor Actor) ServiceExistsWithName(serviceName string) (bool, Warnings, error) {
   133  	services, warnings, err := actor.CloudControllerClient.GetServices(ccv2.Filter{
   134  		Type:     constant.LabelFilter,
   135  		Operator: constant.EqualOperator,
   136  		Values:   []string{serviceName},
   137  	})
   138  	if err != nil {
   139  		return false, Warnings(warnings), err
   140  	}
   141  
   142  	if len(services) == 0 {
   143  		return false, Warnings(warnings), nil
   144  	}
   145  
   146  	return true, Warnings(warnings), nil
   147  }
   148  
   149  func (actor Actor) PurgeServiceOffering(service Service) (Warnings, error) {
   150  	warnings, err := actor.CloudControllerClient.DeleteService(service.GUID, true)
   151  	if err != nil {
   152  		return Warnings(warnings), err
   153  	}
   154  
   155  	return Warnings(warnings), nil
   156  }