github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/actor/v2action/service_binding.go (about)

     1  package v2action
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     7  )
     8  
     9  // ServiceBinding represents the link between a service instance and an
    10  // application.
    11  type ServiceBinding ccv2.ServiceBinding
    12  
    13  // ServiceBindingNotFoundError is returned when a service binding cannot be
    14  // found.
    15  type ServiceBindingNotFoundError struct {
    16  	AppGUID             string
    17  	ServiceInstanceGUID string
    18  }
    19  
    20  func (e ServiceBindingNotFoundError) Error() string {
    21  	return fmt.Sprintf("Service binding for application GUID '%s', and service instance GUID '%s' not found.", e.AppGUID, e.ServiceInstanceGUID)
    22  }
    23  
    24  // GetServiceBindingByApplicationAndServiceInstance returns a service binding
    25  // given an application GUID and and service instance GUID.
    26  func (actor Actor) GetServiceBindingByApplicationAndServiceInstance(appGUID string, serviceInstanceGUID string) (ServiceBinding, Warnings, error) {
    27  	serviceBindings, warnings, err := actor.CloudControllerClient.GetServiceBindings([]ccv2.Query{
    28  		ccv2.Query{
    29  			Filter:   ccv2.AppGUIDFilter,
    30  			Operator: ccv2.EqualOperator,
    31  			Value:    appGUID,
    32  		},
    33  		ccv2.Query{
    34  			Filter:   ccv2.ServiceInstanceGUIDFilter,
    35  			Operator: ccv2.EqualOperator,
    36  			Value:    serviceInstanceGUID,
    37  		},
    38  	})
    39  
    40  	if err != nil {
    41  		return ServiceBinding{}, Warnings(warnings), err
    42  	}
    43  
    44  	if len(serviceBindings) == 0 {
    45  		return ServiceBinding{}, Warnings(warnings), ServiceBindingNotFoundError{
    46  			AppGUID:             appGUID,
    47  			ServiceInstanceGUID: serviceInstanceGUID,
    48  		}
    49  	}
    50  
    51  	return ServiceBinding(serviceBindings[0]), Warnings(warnings), err
    52  }
    53  
    54  // UnbindServiceBySpace deletes the service binding between an application and
    55  // service instance for a given space.
    56  func (actor Actor) UnbindServiceBySpace(appName string, serviceInstanceName string, spaceGUID string) (Warnings, error) {
    57  	var allWarnings Warnings
    58  
    59  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    60  	allWarnings = append(allWarnings, warnings...)
    61  	if err != nil {
    62  		return allWarnings, err
    63  	}
    64  
    65  	serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, spaceGUID)
    66  	allWarnings = append(allWarnings, warnings...)
    67  	if err != nil {
    68  		return allWarnings, err
    69  	}
    70  
    71  	serviceBinding, warnings, err := actor.GetServiceBindingByApplicationAndServiceInstance(app.GUID, serviceInstance.GUID)
    72  	allWarnings = append(allWarnings, warnings...)
    73  	if err != nil {
    74  		return allWarnings, err
    75  	}
    76  
    77  	ccWarnings, err := actor.CloudControllerClient.DeleteServiceBinding(serviceBinding.GUID)
    78  	allWarnings = append(allWarnings, ccWarnings...)
    79  
    80  	return allWarnings, err
    81  }