github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/service_app_binding.go (about) 1 package v7action 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/ccv3" 7 "code.cloudfoundry.org/cli/resources" 8 "code.cloudfoundry.org/cli/types" 9 "code.cloudfoundry.org/cli/util/railway" 10 ) 11 12 type CreateServiceAppBindingParams struct { 13 SpaceGUID string 14 ServiceInstanceName string 15 AppName string 16 BindingName string 17 Parameters types.OptionalObject 18 } 19 20 type DeleteServiceAppBindingParams struct { 21 SpaceGUID string 22 ServiceInstanceName string 23 AppName string 24 } 25 26 func (actor Actor) CreateServiceAppBinding(params CreateServiceAppBindingParams) (chan PollJobEvent, Warnings, error) { 27 var ( 28 serviceInstance resources.ServiceInstance 29 app resources.Application 30 jobURL ccv3.JobURL 31 stream chan PollJobEvent 32 ) 33 34 warnings, err := railway.Sequentially( 35 func() (warnings ccv3.Warnings, err error) { 36 serviceInstance, _, warnings, err = actor.getServiceInstanceByNameAndSpace(params.ServiceInstanceName, params.SpaceGUID) 37 return 38 }, 39 func() (warnings ccv3.Warnings, err error) { 40 app, warnings, err = actor.CloudControllerClient.GetApplicationByNameAndSpace(params.AppName, params.SpaceGUID) 41 return 42 }, 43 func() (warnings ccv3.Warnings, err error) { 44 jobURL, warnings, err = actor.createServiceAppBinding(serviceInstance.GUID, app.GUID, params.BindingName, params.Parameters) 45 return 46 }, 47 func() (warnings ccv3.Warnings, err error) { 48 stream = actor.PollJobToEventStream(jobURL) 49 return 50 }, 51 ) 52 53 switch err.(type) { 54 case nil: 55 return stream, Warnings(warnings), nil 56 case ccerror.ApplicationNotFoundError: 57 return nil, Warnings(warnings), actionerror.ApplicationNotFoundError{Name: params.AppName} 58 default: 59 return nil, Warnings(warnings), err 60 } 61 } 62 63 func (actor Actor) DeleteServiceAppBinding(params DeleteServiceAppBindingParams) (chan PollJobEvent, Warnings, error) { 64 var ( 65 serviceInstance resources.ServiceInstance 66 app resources.Application 67 binding resources.ServiceCredentialBinding 68 jobURL ccv3.JobURL 69 stream chan PollJobEvent 70 ) 71 72 warnings, err := railway.Sequentially( 73 func() (warnings ccv3.Warnings, err error) { 74 serviceInstance, _, warnings, err = actor.getServiceInstanceByNameAndSpace(params.ServiceInstanceName, params.SpaceGUID) 75 return 76 }, 77 func() (warnings ccv3.Warnings, err error) { 78 app, warnings, err = actor.CloudControllerClient.GetApplicationByNameAndSpace(params.AppName, params.SpaceGUID) 79 return 80 }, 81 func() (warnings ccv3.Warnings, err error) { 82 binding, warnings, err = actor.getServiceAppBinding(serviceInstance.GUID, app.GUID) 83 return 84 }, 85 func() (warnings ccv3.Warnings, err error) { 86 jobURL, warnings, err = actor.CloudControllerClient.DeleteServiceCredentialBinding(binding.GUID) 87 return 88 }, 89 func() (warnings ccv3.Warnings, err error) { 90 stream = actor.PollJobToEventStream(jobURL) 91 return 92 }, 93 ) 94 95 switch err.(type) { 96 case nil: 97 return stream, Warnings(warnings), nil 98 case ccerror.ApplicationNotFoundError: 99 return nil, Warnings(warnings), actionerror.ApplicationNotFoundError{Name: params.AppName} 100 default: 101 return nil, Warnings(warnings), err 102 } 103 } 104 105 func (actor Actor) createServiceAppBinding(serviceInstanceGUID, appGUID, bindingName string, parameters types.OptionalObject) (ccv3.JobURL, ccv3.Warnings, error) { 106 jobURL, warnings, err := actor.CloudControllerClient.CreateServiceCredentialBinding(resources.ServiceCredentialBinding{ 107 Type: resources.AppBinding, 108 Name: bindingName, 109 ServiceInstanceGUID: serviceInstanceGUID, 110 AppGUID: appGUID, 111 Parameters: parameters, 112 }) 113 switch err.(type) { 114 case nil: 115 return jobURL, warnings, nil 116 case ccerror.ResourceAlreadyExistsError: 117 return "", warnings, actionerror.ResourceAlreadyExistsError{Message: err.Error()} 118 default: 119 return "", warnings, err 120 } 121 } 122 123 func (actor Actor) getServiceAppBinding(serviceInstanceGUID, appGUID string) (resources.ServiceCredentialBinding, ccv3.Warnings, error) { 124 bindings, warnings, err := actor.CloudControllerClient.GetServiceCredentialBindings( 125 ccv3.Query{Key: ccv3.TypeFilter, Values: []string{"app"}}, 126 ccv3.Query{Key: ccv3.ServiceInstanceGUIDFilter, Values: []string{serviceInstanceGUID}}, 127 ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{appGUID}}, 128 ) 129 130 switch { 131 case err != nil: 132 return resources.ServiceCredentialBinding{}, warnings, err 133 case len(bindings) == 0: 134 return resources.ServiceCredentialBinding{}, warnings, actionerror.ServiceBindingNotFoundError{ 135 AppGUID: appGUID, 136 ServiceInstanceGUID: serviceInstanceGUID, 137 } 138 default: 139 return bindings[0], warnings, nil 140 } 141 }