github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/service_binding.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 // ServiceBinding represents the link between a service instance and an 10 // application. 11 type ServiceBinding ccv2.ServiceBinding 12 13 func (serviceBinding ServiceBinding) IsInProgress() bool { 14 return serviceBinding.LastOperation.State == constant.LastOperationInProgress 15 } 16 17 // BindServiceByApplicationAndServiceInstance binds the service instance to an application. 18 func (actor Actor) BindServiceByApplicationAndServiceInstance(appGUID string, serviceInstanceGUID string) (Warnings, error) { 19 _, warnings, err := actor.CloudControllerClient.CreateServiceBinding(appGUID, serviceInstanceGUID, "", false, nil) 20 21 return Warnings(warnings), err 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, bindingName string, parameters map[string]interface{}) (ServiceBinding, 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 ServiceBinding{}, allWarnings, err 31 } 32 33 serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, spaceGUID) 34 allWarnings = append(allWarnings, warnings...) 35 if err != nil { 36 return ServiceBinding{}, allWarnings, err 37 } 38 39 serviceBinding, ccv2Warnings, err := actor.CloudControllerClient.CreateServiceBinding(app.GUID, serviceInstance.GUID, bindingName, true, parameters) 40 allWarnings = append(allWarnings, ccv2Warnings...) 41 42 return ServiceBinding(serviceBinding), 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( 49 ccv2.Filter{ 50 Type: constant.AppGUIDFilter, 51 Operator: constant.EqualOperator, 52 Values: []string{appGUID}, 53 }, 54 ccv2.Filter{ 55 Type: constant.ServiceInstanceGUIDFilter, 56 Operator: constant.EqualOperator, 57 Values: []string{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), actionerror.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) (ServiceBinding, 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 ServiceBinding{}, allWarnings, err 84 } 85 86 serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, spaceGUID) 87 allWarnings = append(allWarnings, warnings...) 88 if err != nil { 89 return ServiceBinding{}, 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 ServiceBinding{}, allWarnings, err 96 } 97 98 deletedBinding, ccWarnings, err := actor.CloudControllerClient.DeleteServiceBinding(serviceBinding.GUID, true) 99 allWarnings = append(allWarnings, ccWarnings...) 100 101 return ServiceBinding(deletedBinding), allWarnings, err 102 } 103 104 func (actor Actor) GetServiceBindingsByServiceInstance(serviceInstanceGUID string) ([]ServiceBinding, Warnings, error) { 105 serviceBindings, warnings, err := actor.CloudControllerClient.GetServiceInstanceServiceBindings(serviceInstanceGUID) 106 if err != nil { 107 return nil, Warnings(warnings), err 108 } 109 110 allServiceBindings := []ServiceBinding{} 111 for _, serviceBinding := range serviceBindings { 112 allServiceBindings = append(allServiceBindings, ServiceBinding(serviceBinding)) 113 } 114 115 return allServiceBindings, Warnings(warnings), nil 116 } 117 118 func (actor Actor) GetServiceBindingsByUserProvidedServiceInstance(userProvidedServiceInstanceGUID string) ([]ServiceBinding, Warnings, error) { 119 serviceBindings, warnings, err := actor.CloudControllerClient.GetUserProvidedServiceInstanceServiceBindings(userProvidedServiceInstanceGUID) 120 if err != nil { 121 return nil, Warnings(warnings), err 122 } 123 allServiceBindings := []ServiceBinding{} 124 for _, serviceBinding := range serviceBindings { 125 allServiceBindings = append(allServiceBindings, ServiceBinding(serviceBinding)) 126 } 127 128 return allServiceBindings, Warnings(warnings), nil 129 }