github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/route_service_binding_repository.go (about) 1 package api 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io" 8 "strings" 9 10 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 11 "code.cloudfoundry.org/cli/cf/net" 12 ) 13 14 //go:generate counterfeiter . RouteServiceBindingRepository 15 16 type RouteServiceBindingRepository interface { 17 Bind(instanceGUID, routeGUID string, userProvided bool, parameters string) error 18 Unbind(instanceGUID, routeGUID string, userProvided bool) error 19 } 20 21 type CloudControllerRouteServiceBindingRepository struct { 22 config coreconfig.Reader 23 gateway net.Gateway 24 } 25 26 func NewCloudControllerRouteServiceBindingRepository(config coreconfig.Reader, gateway net.Gateway) CloudControllerRouteServiceBindingRepository { 27 return CloudControllerRouteServiceBindingRepository{ 28 config: config, 29 gateway: gateway, 30 } 31 } 32 33 func (repo CloudControllerRouteServiceBindingRepository) Bind( 34 instanceGUID string, 35 routeGUID string, 36 userProvided bool, 37 opaqueParams string, 38 ) error { 39 var rs io.ReadSeeker 40 if opaqueParams != "" { 41 opaqueJSON := json.RawMessage(opaqueParams) 42 s := struct { 43 Parameters *json.RawMessage `json:"parameters"` 44 }{ 45 &opaqueJSON, 46 } 47 48 jsonBytes, err := json.Marshal(s) 49 if err != nil { 50 return err 51 } 52 53 rs = bytes.NewReader(jsonBytes) 54 } else { 55 rs = strings.NewReader("") 56 } 57 58 return repo.gateway.UpdateResourceSync( 59 repo.config.APIEndpoint(), 60 getPath(instanceGUID, routeGUID, userProvided), 61 rs, 62 ) 63 } 64 65 func (repo CloudControllerRouteServiceBindingRepository) Unbind(instanceGUID, routeGUID string, userProvided bool) error { 66 path := getPath(instanceGUID, routeGUID, userProvided) 67 return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path) 68 } 69 70 func getPath(instanceGUID, routeGUID string, userProvided bool) string { 71 var resource string 72 if userProvided { 73 resource = "user_provided_service_instances" 74 } else { 75 resource = "service_instances" 76 } 77 78 return fmt.Sprintf("/v2/%s/%s/routes/%s", resource, instanceGUID, routeGUID) 79 }