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