Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GetServiceBinding to the broker client #3787

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions controllers/controllers/services/osbapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,36 @@ 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,
map[string]string{
"service_id": payload.ServiceId,
"plan_id": payload.PlanID,
},
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
62 changes: 62 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,67 @@ 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",
BindRequest: osbapi.BindRequest{
ServiceId: "my-service-offering-id",
PlanID: "my-plan-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(requests[0].URL.Query()).To(BeEquivalentTo(map[string][]string{
"service_id": {"my-service-offering-id"},
"plan_id": {"my-plan-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
Loading