Skip to content

Commit

Permalink
Adding a fake broker server
Browse files Browse the repository at this point in the history
This is a continuation of
kubernetes-retired#533, and
is a pre-requisite for
kubernetes-retired#923
  • Loading branch information
arschles committed Jun 9, 2017
1 parent cbfa39b commit bdda355
Show file tree
Hide file tree
Showing 69 changed files with 6,264 additions and 92 deletions.
73 changes: 70 additions & 3 deletions glide.lock

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

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ import:
version: v1.1.0
- package: github.com/onsi/ginkgo
version: v1.3.1
- package: github.com/pivotal-cf/brokerapi
- package: code.cloudfoundry.org/lager
12 changes: 12 additions & 0 deletions pkg/brokerapi/fake/server/bind_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package server

import (
"github.com/pivotal-cf/brokerapi"
)

// BindRequest is the struct to contain details of a bind request
type BindRequest struct {
InstanceID string
BindingID string
Details brokerapi.BindDetails
}
44 changes: 44 additions & 0 deletions pkg/brokerapi/fake/server/convert_catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package server

import (
pkgbrokerapi "github.com/kubernetes-incubator/service-catalog/pkg/brokerapi"
"github.com/pivotal-cf/brokerapi"
)

// ConvertCatalog converts a (github.com/kubernetes-incubator/service-catalog/pkg/brokerapi).Catalog
// to an array of brokerapi.Services
func ConvertCatalog(cat *pkgbrokerapi.Catalog) []brokerapi.Service {
ret := make([]brokerapi.Service, len(cat.Services))
for i, svc := range cat.Services {
ret[i] = convertService(svc)
}
return ret
}

func convertService(svc *pkgbrokerapi.Service) brokerapi.Service {
return brokerapi.Service{
ID: svc.ID,
Name: svc.Name,
Description: svc.Description,
Bindable: svc.Bindable,
Tags: svc.Tags,
PlanUpdatable: svc.PlanUpdateable,
Plans: convertPlans(svc.Plans),
// TODO: convert Requires, Metadata, DashboardClient
}
}

func convertPlans(plans []pkgbrokerapi.ServicePlan) []brokerapi.ServicePlan {
ret := make([]brokerapi.ServicePlan, len(plans))
for i, plan := range plans {
ret[i] = brokerapi.ServicePlan{
ID: plan.ID,
Name: plan.Name,
Description: plan.Description,
Free: &plan.Free,
Bindable: plan.Bindable,
// TODO: convert Metadata
}
}
return ret
}
17 changes: 17 additions & 0 deletions pkg/brokerapi/fake/server/create_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package server

import (
"net/http/httptest"

"github.com/kubernetes-incubator/service-catalog/pkg/brokerapi"
"github.com/kubernetes-incubator/service-catalog/pkg/brokerapi/openservicebroker"
)

// NewCreateFunc creates a new brokerapi.CreateFunc according to a broker server running
// in srv
func NewCreateFunc(srv *httptest.Server, user, pass string) brokerapi.CreateFunc {
// type CreateFunc func(name, url, username, password string) BrokerClient
return brokerapi.CreateFunc(func(name, url, username, password string) brokerapi.BrokerClient {
return openservicebroker.NewClient("testclient", srv.URL, user, pass)
})
}
11 changes: 11 additions & 0 deletions pkg/brokerapi/fake/server/deprovision_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package server

import (
"github.com/pivotal-cf/brokerapi"
)

// DeprovisionRequest is the struct to contain details of a single deprovision request
type DeprovisionRequest struct {
InstanceID string
Details brokerapi.DeprovisionDetails
}
110 changes: 110 additions & 0 deletions pkg/brokerapi/fake/server/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package server

import (
"context"

"github.com/pivotal-cf/brokerapi"
)

// Handler is a fake implementation oif a brokerapi.ServiceBroker
type Handler struct {
Catalog []brokerapi.Service
CatalogRequests int

ProvisionResp brokerapi.ProvisionedServiceSpec
ProvisionRespError error
ProvisionRequests []ProvisionRequest

DeprovisionResp brokerapi.DeprovisionServiceSpec
DeprovisonRespErr error
DeprovisionRequests []DeprovisionRequest

BindResp brokerapi.Binding
BindRespErr error
BindRequests []BindRequest

UnbindRespErr error
UnbindRequests []UnbindRequest

UpdateResp brokerapi.UpdateServiceSpec
UpdateRespErr error
UpdateRequests []UpdateRequest

LastOperationResp brokerapi.LastOperation
LastOperationRespErr error
LastOperationRequests []LastOperationRequest
}

// NewHandler creates a new fake server handler
func NewHandler() *Handler {
return &Handler{}
}

// Services is the interface implementation of brokerapi.ServiceBroker
func (h *Handler) Services(ctx context.Context) []brokerapi.Service {
h.CatalogRequests++
return h.Catalog
}

// Provision is the interface implementation of brokerapi.ServiceBroker
func (h *Handler) Provision(
ctx context.Context,
instanceID string,
details brokerapi.ProvisionDetails,
asyncAllowed bool,
) (brokerapi.ProvisionedServiceSpec, error) {
h.ProvisionRequests = append(h.ProvisionRequests, ProvisionRequest{
InstanceID: instanceID,
Details: details,
AsyncAllowed: asyncAllowed,
})
return h.ProvisionResp, h.ProvisionRespError
}

// Deprovision is the interface implementation of brokerapi.ServiceBroker
func (h *Handler) Deprovision(context context.Context, instanceID string, details brokerapi.DeprovisionDetails, asyncAllowed bool) (brokerapi.DeprovisionServiceSpec, error) {
h.DeprovisionRequests = append(h.DeprovisionRequests, DeprovisionRequest{
InstanceID: instanceID,
Details: details,
})
return h.DeprovisionResp, h.DeprovisonRespErr
}

// Bind is the interface implementation of brokerapi.ServiceBroker
func (h *Handler) Bind(context context.Context, instanceID, bindingID string, details brokerapi.BindDetails) (brokerapi.Binding, error) {
h.BindRequests = append(h.BindRequests, BindRequest{
InstanceID: instanceID,
BindingID: bindingID,
Details: details,
})
return h.BindResp, h.BindRespErr
}

// Unbind is the interface implementation of brokerapi.ServiceBroker
func (h *Handler) Unbind(context context.Context, instanceID, bindingID string, details brokerapi.UnbindDetails) error {
h.UnbindRequests = append(h.UnbindRequests, UnbindRequest{
InstanceID: instanceID,
BindingID: bindingID,
Details: details,
})
return h.UnbindRespErr
}

// Update is the interface implementation of brokerapi.ServiceBroker
func (h *Handler) Update(context context.Context, instanceID string, details brokerapi.UpdateDetails, asyncAllowed bool) (brokerapi.UpdateServiceSpec, error) {
h.UpdateRequests = append(h.UpdateRequests, UpdateRequest{
InstanceID: instanceID,
Details: details,
AsyncAllowed: asyncAllowed,
})
return h.UpdateResp, h.UpdateRespErr
}

// LastOperation is the interface implementation of brokerapi.ServiceBroker
func (h *Handler) LastOperation(context context.Context, instanceID, operationData string) (brokerapi.LastOperation, error) {
h.LastOperationRequests = append(h.LastOperationRequests, LastOperationRequest{
InstanceID: instanceID,
OperationData: operationData,
})
return h.LastOperationResp, h.LastOperationRespErr
}
9 changes: 9 additions & 0 deletions pkg/brokerapi/fake/server/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package server

import (
"code.cloudfoundry.org/lager"
)

var (
logger = lager.NewLogger("server")
)
6 changes: 6 additions & 0 deletions pkg/brokerapi/fake/server/last_operation_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package server

type LastOperationRequest struct {
InstanceID string
OperationData string
}
12 changes: 12 additions & 0 deletions pkg/brokerapi/fake/server/provision_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package server

import (
"github.com/pivotal-cf/brokerapi"
)

// ProvisionRequest is the struct to house details of a single provision request
type ProvisionRequest struct {
InstanceID string
Details brokerapi.ProvisionDetails
AsyncAllowed bool
}
18 changes: 18 additions & 0 deletions pkg/brokerapi/fake/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package server

import (
"net/http/httptest"

"github.com/pivotal-cf/brokerapi"
)

// Run runs a new test server from the given broker handler and auth credentials
func Run(hdl *Handler, username, password string) *httptest.Server {
httpHandler := brokerapi.New(hdl, logger, brokerapi.BrokerCredentials{
Username: username,
Password: password,
})

srv := httptest.NewServer(httpHandler)
return srv
}
12 changes: 12 additions & 0 deletions pkg/brokerapi/fake/server/unbind_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package server

import (
"github.com/pivotal-cf/brokerapi"
)

// UnbindRequest is the struct to house details of a single unbind request
type UnbindRequest struct{
InstanceID string
BindingID string
Details brokerapi.UnbindDetails
}
12 changes: 12 additions & 0 deletions pkg/brokerapi/fake/server/update_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package server

import (
"github.com/pivotal-cf/brokerapi"
)

// UpdateRequest is the struct that contains details of a single update request
type UpdateRequest struct {
InstanceID string
Details brokerapi.UpdateDetails
AsyncAllowed bool
}
1 change: 1 addition & 0 deletions vendor/bitbucket.org/ww/goautoneg/.hg_archival.txt

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

Loading

0 comments on commit bdda355

Please sign in to comment.