diff --git a/pkg/api-manager/gateway/local/gateway.go b/pkg/api-manager/gateway/local/gateway.go index 2f5a9b787..08afec4df 100644 --- a/pkg/api-manager/gateway/local/gateway.go +++ b/pkg/api-manager/gateway/local/gateway.go @@ -12,8 +12,10 @@ import ( log "github.com/sirupsen/logrus" + apimanager "github.com/vmware/dispatch/pkg/api-manager" "github.com/vmware/dispatch/pkg/api-manager/gateway" "github.com/vmware/dispatch/pkg/client" + entitystore "github.com/vmware/dispatch/pkg/entity-store" "github.com/vmware/dispatch/pkg/errors" "github.com/vmware/dispatch/pkg/http" "github.com/vmware/dispatch/pkg/trace" @@ -23,6 +25,7 @@ import ( type Gateway struct { Server *http.Server + store entitystore.EntityStore fnClient client.FunctionsClient sync.RWMutex @@ -33,15 +36,17 @@ type Gateway struct { } // NewGateway creates a new local API gateway -func NewGateway(functionsClient client.FunctionsClient) (*Gateway, error) { +func NewGateway(store entitystore.EntityStore, functionsClient client.FunctionsClient) (*Gateway, error) { c := &Gateway{ fnClient: functionsClient, Server: http.NewServer(nil), + store: store, apis: make(map[string]*gateway.API), pathLookup: make(map[string][]*gateway.API), hostLookup: make(map[string][]*gateway.API), methodLookup: make(map[string][]*gateway.API), } + c.rebuildCache() return c, nil } @@ -106,6 +111,18 @@ func (g *Gateway) DeleteAPI(ctx context.Context, api *gateway.API) error { // rebuildCache iterates over all configured APIs and populates lookup caches. Could optimized // to only add changes. func (g *Gateway) rebuildCache() { + // Store should only be nil for tests + if g.store != nil { + var apis []*apimanager.API + err := g.store.ListGlobal(context.TODO(), entitystore.Options{}, &apis) + if err != nil { + log.Errorf("error syncing APIs: %v", err) + } + g.apis = make(map[string]*gateway.API) + for _, api := range apis { + g.apis[api.Name] = &api.API + } + } g.hostLookup = make(map[string][]*gateway.API) g.methodLookup = make(map[string][]*gateway.API) g.pathLookup = make(map[string][]*gateway.API) diff --git a/pkg/api-manager/gateway/local/gateway_test.go b/pkg/api-manager/gateway/local/gateway_test.go index 424ff618c..88569d306 100644 --- a/pkg/api-manager/gateway/local/gateway_test.go +++ b/pkg/api-manager/gateway/local/gateway_test.go @@ -28,7 +28,7 @@ func TestGatewayGetRequest(t *testing.T) { fnClient.On("RunFunction", mock.Anything, mock.Anything, mock.Anything).Return( &v1.Run{}, nil, ) - gw, err := NewGateway(fnClient) + gw, err := NewGateway(nil, fnClient) assert.NoError(t, err) api1 := &gateway.API{ @@ -71,7 +71,7 @@ func TestGatewayPostRequest(t *testing.T) { fnClient.On("RunFunction", mock.Anything, mock.Anything, mock.Anything).Return( &v1.Run{}, nil, ) - gw, err := NewGateway(fnClient) + gw, err := NewGateway(nil, fnClient) assert.NoError(t, err) api1 := &gateway.API{ diff --git a/pkg/api-manager/handlers.go b/pkg/api-manager/handlers.go index 81507ea03..db0c02e74 100644 --- a/pkg/api-manager/handlers.go +++ b/pkg/api-manager/handlers.go @@ -56,6 +56,10 @@ func apiModelOntoEntity(organizationID string, m *v1.API) *API { } else { uris = m.Uris } + var methods []string + for _, method := range m.Methods { + methods = append(methods, strings.ToUpper(method)) + } e := API{ BaseEntity: entitystore.BaseEntity{ Name: *m.Name, @@ -70,7 +74,7 @@ func apiModelOntoEntity(organizationID string, m *v1.API) *API { Enabled: m.Enabled, TLS: m.TLS, Hosts: m.Hosts, - Methods: m.Methods, + Methods: methods, Protocols: m.Protocols, URIs: uris, CORS: m.Cors, diff --git a/pkg/dispatchserver/local.go b/pkg/dispatchserver/local.go index b9ba3427e..25fac9b76 100644 --- a/pkg/dispatchserver/local.go +++ b/pkg/dispatchserver/local.go @@ -75,7 +75,7 @@ func runLocal(config *serverConfig) { functionsHandler, functionsShutdown := initFunctions(config, functionsDeps) defer functionsShutdown() - gw, err := local.NewGateway(functions) + gw, err := local.NewGateway(store, functions) if err != nil { log.Fatalf("Error creating API Gateway: %v", err) }