github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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  // BindServiceBySpace binds the service instance to an application for a given space.
    25  func (actor Actor) BindServiceBySpace(appName string, serviceInstanceName string, spaceGUID string, parameters map[string]interface{}) (Warnings, error) {
    26  	var allWarnings Warnings
    27  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    28  	allWarnings = append(allWarnings, warnings...)
    29  	if err != nil {
    30  		return allWarnings, err
    31  	}
    32  
    33  	serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, spaceGUID)
    34  	allWarnings = append(allWarnings, warnings...)
    35  	if err != nil {
    36  		return allWarnings, err
    37  	}
    38  
    39  	_, ccv2Warnings, err := actor.CloudControllerClient.CreateServiceBinding(app.GUID, serviceInstance.GUID, parameters)
    40  	allWarnings = append(allWarnings, ccv2Warnings...)
    41  
    42  	return allWarnings, err
    43  }
    44  
    45  // GetServiceBindingByApplicationAndServiceInstance returns a service binding
    46  // given an application GUID and and service instance GUID.
    47  func (actor Actor) GetServiceBindingByApplicationAndServiceInstance(appGUID string, serviceInstanceGUID string) (ServiceBinding, Warnings, error) {
    48  	serviceBindings, warnings, err := actor.CloudControllerClient.GetServiceBindings([]ccv2.Query{
    49  		ccv2.Query{
    50  			Filter:   ccv2.AppGUIDFilter,
    51  			Operator: ccv2.EqualOperator,
    52  			Value:    appGUID,
    53  		},
    54  		ccv2.Query{
    55  			Filter:   ccv2.ServiceInstanceGUIDFilter,
    56  			Operator: ccv2.EqualOperator,
    57  			Value:    serviceInstanceGUID,
    58  		},
    59  	})
    60  
    61  	if err != nil {
    62  		return ServiceBinding{}, Warnings(warnings), err
    63  	}
    64  
    65  	if len(serviceBindings) == 0 {
    66  		return ServiceBinding{}, Warnings(warnings), ServiceBindingNotFoundError{
    67  			AppGUID:             appGUID,
    68  			ServiceInstanceGUID: serviceInstanceGUID,
    69  		}
    70  	}
    71  
    72  	return ServiceBinding(serviceBindings[0]), Warnings(warnings), err
    73  }
    74  
    75  // UnbindServiceBySpace deletes the service binding between an application and
    76  // service instance for a given space.
    77  func (actor Actor) UnbindServiceBySpace(appName string, serviceInstanceName string, spaceGUID string) (Warnings, error) {
    78  	var allWarnings Warnings
    79  
    80  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    81  	allWarnings = append(allWarnings, warnings...)
    82  	if err != nil {
    83  		return allWarnings, err
    84  	}
    85  
    86  	serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, spaceGUID)
    87  	allWarnings = append(allWarnings, warnings...)
    88  	if err != nil {
    89  		return allWarnings, err
    90  	}
    91  
    92  	serviceBinding, warnings, err := actor.GetServiceBindingByApplicationAndServiceInstance(app.GUID, serviceInstance.GUID)
    93  	allWarnings = append(allWarnings, warnings...)
    94  	if err != nil {
    95  		return allWarnings, err
    96  	}
    97  
    98  	ccWarnings, err := actor.CloudControllerClient.DeleteServiceBinding(serviceBinding.GUID)
    99  	allWarnings = append(allWarnings, ccWarnings...)
   100  
   101  	return allWarnings, err
   102  }