github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/v2action/service_instance.go (about)

     1  package v2action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     8  )
     9  
    10  // ServiceInstance represents an instance of a service.
    11  type ServiceInstance ccv2.ServiceInstance
    12  
    13  func (instance ServiceInstance) IsManaged() bool {
    14  	return ccv2.ServiceInstance(instance).Managed()
    15  }
    16  
    17  func (instance ServiceInstance) IsUserProvided() bool {
    18  	return ccv2.ServiceInstance(instance).UserProvided()
    19  }
    20  
    21  func (actor Actor) GetServiceInstance(guid string) (ServiceInstance, Warnings, error) {
    22  	instance, warnings, err := actor.CloudControllerClient.GetServiceInstance(guid)
    23  	if _, ok := err.(ccerror.ResourceNotFoundError); ok {
    24  		return ServiceInstance{}, Warnings(warnings), actionerror.ServiceInstanceNotFoundError{GUID: guid}
    25  	}
    26  	return ServiceInstance(instance), Warnings(warnings), err
    27  }
    28  
    29  func (actor Actor) GetServiceInstanceByNameAndSpace(name string, spaceGUID string) (ServiceInstance, Warnings, error) {
    30  	serviceInstances, warnings, err := actor.CloudControllerClient.GetSpaceServiceInstances(
    31  		spaceGUID,
    32  		true,
    33  		ccv2.Filter{
    34  			Type:     constant.NameFilter,
    35  			Operator: constant.EqualOperator,
    36  			Values:   []string{name},
    37  		})
    38  
    39  	if err != nil {
    40  		return ServiceInstance{}, Warnings(warnings), err
    41  	}
    42  
    43  	if len(serviceInstances) == 0 {
    44  		return ServiceInstance{}, Warnings(warnings), actionerror.ServiceInstanceNotFoundError{
    45  			Name: name,
    46  		}
    47  	}
    48  
    49  	return ServiceInstance(serviceInstances[0]), Warnings(warnings), nil
    50  }
    51  
    52  func (actor Actor) GetServiceInstancesByApplication(appGUID string) ([]ServiceInstance, Warnings, error) {
    53  	var allWarnings Warnings
    54  	bindings, apiWarnings, err := actor.CloudControllerClient.GetServiceBindings(ccv2.Filter{
    55  		Type:     constant.AppGUIDFilter,
    56  		Operator: constant.EqualOperator,
    57  		Values:   []string{appGUID},
    58  	})
    59  	allWarnings = append(allWarnings, apiWarnings...)
    60  
    61  	if err != nil {
    62  		return nil, allWarnings, err
    63  	}
    64  
    65  	var serviceInstances []ServiceInstance
    66  	for _, binding := range bindings {
    67  		instance, warnings, instanceErr := actor.GetServiceInstance(binding.ServiceInstanceGUID)
    68  		allWarnings = append(allWarnings, warnings...)
    69  		if instanceErr != nil {
    70  			return nil, allWarnings, instanceErr
    71  		}
    72  		serviceInstances = append(serviceInstances, ServiceInstance(instance))
    73  	}
    74  
    75  	return serviceInstances, allWarnings, err
    76  }
    77  
    78  func (actor Actor) GetServiceInstancesBySpace(spaceGUID string) ([]ServiceInstance, Warnings, error) {
    79  	ccv2ServiceInstances, warnings, err := actor.CloudControllerClient.GetSpaceServiceInstances(spaceGUID, true)
    80  
    81  	if err != nil {
    82  		return []ServiceInstance{}, Warnings(warnings), err
    83  	}
    84  
    85  	serviceInstances := make([]ServiceInstance, len(ccv2ServiceInstances))
    86  	for i, ccv2ServiceInstance := range ccv2ServiceInstances {
    87  		serviceInstances[i] = ServiceInstance(ccv2ServiceInstance)
    88  	}
    89  
    90  	return serviceInstances, Warnings(warnings), nil
    91  }