Skip to content

Commit

Permalink
Added GetServiceBinding to the broker client
Browse files Browse the repository at this point in the history
  • Loading branch information
ddraganovv committed Feb 10, 2025
1 parent 047fcb8 commit c182ff1
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 31 deletions.
27 changes: 27 additions & 0 deletions controllers/controllers/services/osbapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,33 @@ func (c *Client) Unbind(ctx context.Context, payload UnbindPayload) (UnbindRespo
return response, nil
}

func (c *Client) GetServiceBinding(ctx context.Context, payload BindPayload) (BindingResponse, error) {
statusCode, respBytes, err := c.newBrokerRequester().
forBroker(c.broker).
sendRequest(
ctx,
"/v2/service_instances/"+payload.InstanceID+"/service_bindings/"+payload.BindingID,
http.MethodGet,
nil,
nil,
)
if err != nil {
return BindingResponse{}, fmt.Errorf("fetching service binding failed: %w", err)
}

if statusCode == http.StatusNotFound {
return BindingResponse{}, UnrecoverableError{Status: statusCode}
}

response := BindingResponse{}
err = json.Unmarshal(respBytes, &response)
if err != nil {
return BindingResponse{}, fmt.Errorf("failed to unmarshal response: %w", err)
}

return response, nil
}

func payloadToReader(payload any) (io.Reader, error) {
if payload == nil {
return nil, nil
Expand Down
54 changes: 54 additions & 0 deletions controllers/controllers/services/osbapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -619,6 +620,59 @@ var _ = Describe("OSBAPI Client", func() {
})
})

Describe("GetServiceBinding", func() {
var (
bindingResp osbapi.BindingResponse
getBindErr error
)
BeforeEach(func() {
brokerServer.WithResponse(
"/v2/service_instances/{instance_id}/service_bindings/{binding_id}",
map[string]any{
"parameters": map[string]string{
"billing-account": "abcde12345",
},
},
http.StatusOK,
)
})
JustBeforeEach(func() {
bindingResp, getBindErr = brokerClient.GetServiceBinding(ctx, osbapi.BindPayload{
InstanceID: "my-service-instance",
BindingID: "my-binding-id",
})
})

It("gets the service binding", func() {
Expect(getBindErr).NotTo(HaveOccurred())

requests := brokerServer.ServedRequests()
Expect(requests).To(HaveLen(1))
Expect(requests[0].Method).To(Equal(http.MethodGet))
Expect(requests[0].URL.Path).To(Equal("/v2/service_instances/my-service-instance/service_bindings/my-binding-id"))

Expect(bindingResp).To(Equal(osbapi.BindingResponse{
Parameters: map[string]any{
"billing-account": "abcde12345",
},
}))
})

When("the service binding does not exist", func() {
BeforeEach(func() {
brokerServer = brokerServer.WithResponse(
"/v2/service_instances/{instance_id}/service_bindings/{binding_id}",
nil,
http.StatusNotFound,
)
})

It("returns an error", func() {
Expect(getBindErr).To(MatchError(ContainSubstring(fmt.Sprintf("The server responded with status: %d", http.StatusNotFound))))
})
})
})

Describe("GetServiceBindingLastOperation", func() {
var (
lastOpResp osbapi.LastOperationResponse
Expand Down
1 change: 1 addition & 0 deletions controllers/controllers/services/osbapi/clientfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type BrokerClient interface {
Bind(context.Context, BindPayload) (BindResponse, error)
Unbind(context.Context, UnbindPayload) (UnbindResponse, error)
GetServiceBindingLastOperation(context.Context, GetBindingLastOperationRequest) (LastOperationResponse, error)
GetServiceBinding(ctx context.Context, payload BindPayload) (BindingResponse, error)
}

//counterfeiter:generate -o fake -fake-name BrokerClientFactory code.cloudfoundry.org/korifi/controllers/controllers/services/osbapi.BrokerClientFactory
Expand Down
26 changes: 13 additions & 13 deletions controllers/controllers/services/osbapi/fake/broker_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions controllers/controllers/services/osbapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ type BindResponse struct {
IsAsync bool
}

type BindingResponse struct {
Parameters map[string]any `json:"parameters"`
}

type BindResource struct {
AppGUID string `json:"app_guid"`
}
Expand Down
35 changes: 17 additions & 18 deletions controllers/controllers/workloads/build/fake/delegate_reconciler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/assets/sample-broker-golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func main() {
http.HandleFunc("GET /v2/service_instances/{id}/last_operation", getLastOperationHandler)

http.HandleFunc("PUT /v2/service_instances/{instance_id}/service_bindings/{binding_id}", bindHandler)
http.HandleFunc("GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}", getBindingHandler)
http.HandleFunc("DELETE /v2/service_instances/{instance_id}/service_bindings/{binding_id}", unbindHandler)
http.HandleFunc("GET /v2/service_instances/{instance_id}/service_bindings/{binding_id}/last_operation", getLastOperationHandler)

Expand Down Expand Up @@ -107,6 +108,21 @@ func deprovisionServiceInstanceHandler(w http.ResponseWriter, r *http.Request) {
asyncOperation(w, fmt.Sprintf("deprovision-%s", r.PathValue("id")), "{}")
}

func getBindingHandler(w http.ResponseWriter, r *http.Request) {
logRequest(r)

if status, err := checkCredentials(w, r); err != nil {
respond(w, status, fmt.Sprintf("Credentials check failed: %v", err))
return
}

respond(w, http.StatusOK, `{
"parameters": {
"billing-account": "abcde12345"
}
}`)
}

func bindHandler(w http.ResponseWriter, r *http.Request) {
logRequest(r)

Expand Down

0 comments on commit c182ff1

Please sign in to comment.