pivotal-cf / brokerapi

A Go package for implementing the V2 Open Service Broker API
Apache License 2.0
84 stars 69 forks source link

Requirement to expose additional API endpoints #206

Closed chanjaljayaram closed 2 years ago

chanjaljayaram commented 2 years ago

Our Platform expects certain additional API endpoints (other than those in the Open Service Broker specs). For instance one of those endpoints would be v2/service_instances/{instance_id}/name which would return a service name that the Platform expects the Broker is provide.

With brokerapi.New returning an http.Handler we cannot configure additional routes. I was able to modify the brokerapi.New definition as below func New(serviceBroker ServiceBroker, logger lager.Logger, brokerCredentials BrokerCredentials) *mux.Router and then configure additional routes in my code like this brokerAPI := brokerapi.New(ServiceBroker, logger, brokerCredential) brokerAPI.HandleFunc("/v2/service_instances/{instance_id}/name", ServiceBroker.GetInstanceName).Methods("GET") http.Handle("/", brokerAPI) logger.Fatal("http-listen", http.ListenAndServe(":8080", nil))

Would this update make sense? If there is another way of implementing this use case (without having to update the brokerapi codebase) could you please let me know?

cf-gitbot commented 2 years ago

We have created an issue in Pivotal Tracker to manage this:

https://www.pivotaltracker.com/story/show/182441266

The labels on this github issue will be updated when the story is started.

FelisiaM commented 2 years ago

Hi @chanjaljayaram

I think what you want to achieve can be done by implementing combing a few methods that already exist in the BrokerAPI. These methods exist in the api_options.go and allow you to create a new broker with a custom definition. Essentially you want to implement something similar to NewWithOptions() and use your router to which you can attach more routes to handle.

It should look something like:

type config struct {
    router       *mux.Router
    customRouter bool
    logger       lager.Logger
}

cfg := config{
   logger: logger,
}
router := mux.NewRouter()
// configure router middleware e.g. https://github.com/pivotal-cf/brokerapi/blob/31b2e3865befe6c2f3b95d668234f96894349d12/api_options.go#L66-L71 

brokerapi.WithOptions(brokerapi.WithRouter(router), brokerapi.WithBrokerCredentials(brokerCredentials))
brokerapi.AttachRoutes(cfg1.router, serviceBroker, logger)
router.HandleFunc("/v2/service_instances/{instance_id}/name", ServiceBroker.GetInstanceName).Methods("GET")

Let me know if you give it a try and if this works.

Thanks, Felisia

chanjaljayaram commented 2 years ago

Hi Felisia

It worked with the following updates:

"github.com/pivotal-cf/brokerapi/v8" "github.com/pivotal-cf/brokerapi/v8/domain"

router := mux.NewRouter() brokerapi.WithOptions(brokerapi.WithRouter(router), brokerapi.WithBrokerCredentials(brokerapi.BrokerCredentials{Username: "admin", Password: "admin"})) brokerapi.AttachRoutes(router, ServiceBroker, logger) router.HandleFunc("/v2/service_instances/{instance_id}/name", ServiceBroker.GetInstanceName).Methods("GET") router.HandleFunc("/v2/service_instances/{instance_id}/name", ServiceBroker.GetInstanceName).Methods("GET") http.Handle("/", router)

Thanks, Chanjal