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

Rethinking Service Registry, much refactoring #5

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
83 changes: 0 additions & 83 deletions broker/bind.go

This file was deleted.

68 changes: 54 additions & 14 deletions broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package broker

import (
"context"
"errors"
"fmt"

"code.cloudfoundry.org/lager"
brokerapi "github.com/pivotal-cf/brokerapi/domain"
brokerapi "github.com/pivotal-cf/brokerapi/v7/domain"
"github.com/starkandwayne/scs-broker/broker/implementation"
"github.com/starkandwayne/scs-broker/config"
)

Expand All @@ -14,25 +15,17 @@ const (
)

type SCSBroker struct {
Config config.Config
Logger lager.Logger
}

func (broker *SCSBroker) GetServiceByServiceID(serviceID string) (config.Service, error) {
for _, service := range broker.Config.Services {
if service.ServiceID == serviceID {
return service, nil
}
}

return config.Service{}, fmt.Errorf("No valid service found for %s", serviceID)
func New() *SCSBroker {
return &SCSBroker{}
}

func (broker *SCSBroker) Services(ctx context.Context) ([]brokerapi.Service, error) {

services := []brokerapi.Service{}

for _, service := range broker.Config.Services {
for _, service := range config.Parsed.Services {
brokerService := brokerapi.Service{
ID: service.ServiceID,
Name: service.ServiceName,
Expand All @@ -49,7 +42,7 @@ func (broker *SCSBroker) Services(ctx context.Context) ([]brokerapi.Service, err
}},
Metadata: &brokerapi.ServiceMetadata{
DisplayName: service.ServiceName,
ImageUrl: fmt.Sprintf("data:image/png;base64,%s", broker.Config.IconImage),
ImageUrl: fmt.Sprintf("data:image/png;base64,%s", config.Parsed.IconImage),
},
Tags: []string{
"snw",
Expand All @@ -60,5 +53,52 @@ func (broker *SCSBroker) Services(ctx context.Context) ([]brokerapi.Service, err
}

return services, nil
}

func (broker *SCSBroker) Provision(ctx context.Context, instanceID string, details brokerapi.ProvisionDetails, asyncAllowed bool) (spec brokerapi.ProvisionedServiceSpec, err error) {
return implementation.
ByServiceID(details.ServiceID).
Provision(ctx, instanceID, details, asyncAllowed)
}

func (broker *SCSBroker) Deprovision(ctx context.Context, instanceID string, details brokerapi.DeprovisionDetails, asyncAllowed bool) (brokerapi.DeprovisionServiceSpec, error) {
return implementation.
ByServiceID(details.ServiceID).
Deprovision(ctx, instanceID, details, asyncAllowed)
}

func (broker *SCSBroker) Update(ctx context.Context, instanceID string, details brokerapi.UpdateDetails, asyncAllowed bool) (brokerapi.UpdateServiceSpec, error) {
return implementation.
ByServiceID(details.ServiceID).
Update(ctx, instanceID, details, asyncAllowed)
}

func (broker *SCSBroker) Bind(ctx context.Context, instanceID string, bindingID string, details brokerapi.BindDetails, asyncAllowed bool) (brokerapi.Binding, error) {
return implementation.
ByServiceID(details.ServiceID).
Bind(ctx, instanceID, bindingID, details, asyncAllowed)
}

func (broker *SCSBroker) Unbind(ctx context.Context, instanceID string, bindingID string, details brokerapi.UnbindDetails, asyncAllowed bool) (brokerapi.UnbindSpec, error) {
return implementation.
ByServiceID(details.ServiceID).
Unbind(ctx, instanceID, bindingID, details, asyncAllowed)
}

// Here be unimplemented dragons

func (broker *SCSBroker) LastOperation(ctx context.Context, instanceID string, details brokerapi.PollDetails) (brokerapi.LastOperation, error) {
return brokerapi.LastOperation{}, errors.New("not implemented")
}

func (broker *SCSBroker) GetBinding(ctx context.Context, instanceID, bindingID string) (brokerapi.GetBindingSpec, error) {
return brokerapi.GetBindingSpec{}, errors.New("not implemented")
}

func (broker *SCSBroker) GetInstance(ctx context.Context, instanceID string) (brokerapi.GetInstanceDetailsSpec, error) {
return brokerapi.GetInstanceDetailsSpec{}, errors.New("not implemented")
}

func (broker *SCSBroker) LastBindingOperation(ctx context.Context, instanceID, bindingID string, details brokerapi.PollDetails) (brokerapi.LastOperation, error) {
return brokerapi.LastOperation{}, errors.New("not implemented")
}
41 changes: 0 additions & 41 deletions broker/client.go

This file was deleted.

87 changes: 87 additions & 0 deletions broker/configserver/bind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package configserver

import (
"context"
"fmt"

"github.com/cloudfoundry-community/go-uaa"
brokerapi "github.com/pivotal-cf/brokerapi/v7/domain"
"github.com/starkandwayne/scs-broker/broker/utilities"
"github.com/starkandwayne/scs-broker/client"
"github.com/starkandwayne/scs-broker/config"
"github.com/starkandwayne/scs-broker/logger"
)

func (broker *Broker) Bind(ctx context.Context, instanceID, bindingID string, details brokerapi.BindDetails, asyncAllowed bool) (brokerapi.Binding, error) {
dummy := brokerapi.Binding{}

logger.Info("Bind: GetUAAClient")

api, err := client.GetUaaClient()
if err != nil {
logger.Info("Bind: Error in getting client")
return dummy, err
}

clientId := utilities.MakeClientIdForBinding(details.ServiceID, bindingID)
password := utilities.GenClientPassword()

uaaClient := uaa.Client{
ClientID: clientId,
AuthorizedGrantTypes: []string{"client_credentials"},
Authorities: []string{fmt.Sprintf("%s.%v.read", details.ServiceID, instanceID)},
DisplayName: clientId,
ClientSecret: password,
}

logger.Info("Bind: got client info")
logger.Info("Bind: Create Client")
_, err = api.CreateClient(uaaClient)
if err != nil {
logger.Info("Bind: Error in CreateClient")
return dummy, err
}

logger.Info("Bind: GetClient")
cfClient, err := client.GetClient()
if err != nil {
logger.Info("Bind: Error in GetClient")
return dummy, err
}

logger.Info("Bind: Get Info")
info, _, _, err := cfClient.GetInfo()
if err != nil {
logger.Info("Bind: Error in Get Info")

return dummy, err
}

logger.Info("Bind: GetApplicationByNameAndSpace")

app, _, err := cfClient.GetApplicationByNameAndSpace(utilities.MakeAppName(details.ServiceID, instanceID), config.Parsed.InstanceSpaceGUID)
if err != nil {
logger.Info("Bind: Error in GetApplicationByNameAndSpace")
return dummy, err
}

logger.Info("Bind: GetApplicationRoutes")
routes, _, err := cfClient.GetApplicationRoutes(app.GUID)
if err != nil {
logger.Info("Bind: Error in GetApplicationRoutes")
return dummy, err
}

logger.Info("Bind: Building binding Credentials")

creds := map[string]string{
"uri": fmt.Sprintf("https://%v", routes[0].URL),
"access_token_uri": fmt.Sprintf("%v/oauth/token", info.UAA()),
"client_id": clientId,
"client_secret": password,
}

logger.Info("Bind: Return")

return brokerapi.Binding{Credentials: creds}, nil
}
7 changes: 7 additions & 0 deletions broker/configserver/broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package configserver

type Broker struct{}

func New() *Broker {
return &Broker{}
}
Loading