Skip to content

Commit

Permalink
Cleanup Services interfaces (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthenw committed Jun 25, 2018
1 parent b77b97b commit 28ded18
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ vendor
*.swp
*.swo
*.db
default.etcd/
default.etcd*
coverage.txt
profile.out
tests/testing.etcd*
4 changes: 2 additions & 2 deletions event/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package event

// Service represents service for managing event types.
type Service interface {
CreateEventType(eventType *Type) (*Type, error)
GetEventType(space string, name TypeName) (*Type, error)
GetEventTypes(space string) (Types, error)
ListEventTypes(space string) (Types, error)
CreateEventType(eventType *Type) (*Type, error)
UpdateEventType(newEventType *Type) (*Type, error)
DeleteEventType(space string, name TypeName) error
}
6 changes: 3 additions & 3 deletions function/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package function

// Service represents service for managing functions.
type Service interface {
RegisterFunction(fn *Function) (*Function, error)
UpdateFunction(fn *Function) (*Function, error)
GetFunction(space string, id ID) (*Function, error)
GetFunctions(space string) (Functions, error)
ListFunctions(space string) (Functions, error)
CreateFunction(fn *Function) (*Function, error)
UpdateFunction(fn *Function) (*Function, error)
DeleteFunction(space string, id ID) error
}
10 changes: 5 additions & 5 deletions httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (h HTTPAPI) listEventTypes(w http.ResponseWriter, r *http.Request, params h
encoder := json.NewEncoder(w)

space := params.ByName("space")
types, err := h.EventTypes.GetEventTypes(space)
types, err := h.EventTypes.ListEventTypes(space)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
encoder.Encode(&Response{Errors: []Error{{Message: err.Error()}}})
Expand Down Expand Up @@ -232,7 +232,7 @@ func (h HTTPAPI) listFunctions(w http.ResponseWriter, r *http.Request, params ht
encoder := json.NewEncoder(w)

space := params.ByName("space")
fns, err := h.Functions.GetFunctions(space)
fns, err := h.Functions.ListFunctions(space)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
encoder.Encode(&Response{Errors: []Error{{Message: err.Error()}}})
Expand All @@ -258,7 +258,7 @@ func (h HTTPAPI) createFunction(w http.ResponseWriter, r *http.Request, params h
}

fn.Space = params.ByName("space")
output, err := h.Functions.RegisterFunction(fn)
output, err := h.Functions.CreateFunction(fn)
if err != nil {
if _, ok := err.(*function.ErrFunctionValidation); ok {
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -343,7 +343,7 @@ func (h HTTPAPI) listSubscriptions(w http.ResponseWriter, r *http.Request, param
encoder := json.NewEncoder(w)

space := params.ByName("space")
subs, err := h.Subscriptions.GetSubscriptions(space)
subs, err := h.Subscriptions.ListSubscriptions(space)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
encoder.Encode(&Response{Errors: []Error{{Message: err.Error()}}})
Expand Down Expand Up @@ -484,7 +484,7 @@ func (h HTTPAPI) listCORS(w http.ResponseWriter, r *http.Request, params httprou
encoder := json.NewEncoder(w)

space := params.ByName("space")
configs, err := h.CORSes.GetCORSes(space)
configs, err := h.CORSes.ListCORS(space)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
encoder.Encode(&Response{Errors: []Error{{Message: err.Error()}}})
Expand Down
20 changes: 10 additions & 10 deletions httpapi/httpapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestGetEventTypes(t *testing.T) {
Space: "default",
Name: event.TypeName("test.event"),
}}
eventTypes.EXPECT().GetEventTypes("default").Return(returnedList, nil)
eventTypes.EXPECT().ListEventTypes("default").Return(returnedList, nil)

resp := request(router, http.MethodGet, "/v1/spaces/default/eventtypes", nil)

Expand All @@ -88,7 +88,7 @@ func TestGetEventTypes(t *testing.T) {
})

t.Run("internal error", func(t *testing.T) {
eventTypes.EXPECT().GetEventTypes(gomock.Any()).Return(nil, errors.New("processing failed"))
eventTypes.EXPECT().ListEventTypes(gomock.Any()).Return(nil, errors.New("processing failed"))

resp := request(router, http.MethodGet, "/v1/spaces/default/eventtypes", nil)

Expand Down Expand Up @@ -356,7 +356,7 @@ func TestGetFunctions(t *testing.T) {
ProviderType: httpprovider.Type,
Provider: &httpprovider.HTTP{},
}}
functions.EXPECT().GetFunctions("default").Return(returnedList, nil)
functions.EXPECT().ListFunctions("default").Return(returnedList, nil)

resp := request(router, http.MethodGet, "/v1/spaces/default/functions", nil)

Expand All @@ -368,7 +368,7 @@ func TestGetFunctions(t *testing.T) {
})

t.Run("internal error", func(t *testing.T) {
functions.EXPECT().GetFunctions(gomock.Any()).Return(nil, errors.New("processing failed"))
functions.EXPECT().ListFunctions(gomock.Any()).Return(nil, errors.New("processing failed"))

resp := request(router, http.MethodGet, "/v1/spaces/default/functions", nil)

Expand All @@ -395,7 +395,7 @@ func TestRegisterFunction(t *testing.T) {
URL: "http://example.com",
},
}
functions.EXPECT().RegisterFunction(fn).Return(fn, nil)
functions.EXPECT().CreateFunction(fn).Return(fn, nil)

resp := request(router, http.MethodPost, "/v1/spaces/test1/functions", fnPayload)

Expand All @@ -408,7 +408,7 @@ func TestRegisterFunction(t *testing.T) {
})

t.Run("function already exists", func(t *testing.T) {
functions.EXPECT().RegisterFunction(gomock.Any()).
functions.EXPECT().CreateFunction(gomock.Any()).
Return(nil, &function.ErrFunctionAlreadyRegistered{ID: function.ID("func1")})

resp := request(router, http.MethodPost, "/v1/spaces/default/functions", fnPayload)
Expand All @@ -420,7 +420,7 @@ func TestRegisterFunction(t *testing.T) {
})

t.Run("validation error", func(t *testing.T) {
functions.EXPECT().RegisterFunction(gomock.Any()).
functions.EXPECT().CreateFunction(gomock.Any()).
Return(nil, &function.ErrFunctionValidation{Message: "wrong function ID format"})

payload := []byte(`{"functionID":"/","type":"http","provider":{"url":"http://test.com"}}}`)
Expand All @@ -442,7 +442,7 @@ func TestRegisterFunction(t *testing.T) {
})

t.Run("internal error", func(t *testing.T) {
functions.EXPECT().RegisterFunction(gomock.Any()).Return(nil, errors.New("processing error"))
functions.EXPECT().CreateFunction(gomock.Any()).Return(nil, errors.New("processing error"))

resp := request(router, http.MethodPost, "/v1/spaces/default/functions", fnPayload)

Expand Down Expand Up @@ -693,7 +693,7 @@ func TestGetCORSes(t *testing.T) {
Space: "default",
ID: cors.ID("GET%2Fhello"),
}}
corses.EXPECT().GetCORSes("default").Return(returnedList, nil)
corses.EXPECT().ListCORS("default").Return(returnedList, nil)

resp := request(router, http.MethodGet, "/v1/spaces/default/cors", nil)

Expand All @@ -705,7 +705,7 @@ func TestGetCORSes(t *testing.T) {
})

t.Run("internal error", func(t *testing.T) {
corses.EXPECT().GetCORSes(gomock.Any()).Return(nil, errors.New("processing failed"))
corses.EXPECT().ListCORS(gomock.Any()).Return(nil, errors.New("processing failed"))

resp := request(router, http.MethodGet, "/v1/spaces/default/cors", nil)

Expand Down
4 changes: 2 additions & 2 deletions libkv/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func (service Service) GetCORS(space string, id cors.ID) (*cors.CORS, error) {
return &config, nil
}

// GetCORSes returns an array of all CORS configuration in the space.
func (service Service) GetCORSes(space string) (cors.CORSes, error) {
// ListCORS returns an array of all CORS configuration in the space.
func (service Service) ListCORS(space string) (cors.CORSes, error) {
configs := []*cors.CORS{}

kvs, err := service.CORSStore.List(spacePath(space), &store.ReadOptions{Consistent: true})
Expand Down
8 changes: 4 additions & 4 deletions libkv/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestGetCORS(t *testing.T) {
})
}

func TestGetCORSes(t *testing.T) {
func TestListCORS(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand All @@ -163,7 +163,7 @@ func TestGetCORSes(t *testing.T) {
db.EXPECT().List("default/", &store.ReadOptions{Consistent: true}).Return(kvs, nil)
service := &Service{CORSStore: db, Log: zap.NewNop()}

list, err := service.GetCORSes("default")
list, err := service.ListCORS("default")

assert.Nil(t, err)
assert.Equal(t, cors.CORSes{testConfig}, list)
Expand All @@ -174,7 +174,7 @@ func TestGetCORSes(t *testing.T) {
db.EXPECT().List(gomock.Any(), gomock.Any()).Return([]*store.KVPair{}, errors.New("KV list err"))
service := &Service{CORSStore: db, Log: zap.NewNop()}

_, err := service.GetCORSes("default")
_, err := service.ListCORS("default")

assert.EqualError(t, err, "KV list err")
})
Expand All @@ -184,7 +184,7 @@ func TestGetCORSes(t *testing.T) {
db.EXPECT().List(gomock.Any(), gomock.Any()).Return([]*store.KVPair{}, errors.New("Key not found in store"))
service := &Service{CORSStore: db, Log: zap.NewNop()}

list, _ := service.GetCORSes("default")
list, _ := service.ListCORS("default")

assert.Equal(t, cors.CORSes{}, list)
})
Expand Down
6 changes: 3 additions & 3 deletions libkv/eventtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (service Service) GetEventType(space string, name event.TypeName) (*event.T
return &eventType, nil
}

// GetEventTypes returns an array of all event types in the space.
func (service Service) GetEventTypes(space string) (event.Types, error) {
// ListEventTypes returns an array of all event types in the space.
func (service Service) ListEventTypes(space string) (event.Types, error) {
types := []*event.Type{}

kvs, err := service.EventTypeStore.List(spacePath(space), &store.ReadOptions{Consistent: true})
Expand Down Expand Up @@ -132,7 +132,7 @@ func (service Service) UpdateEventType(newEventType *event.Type) (*event.Type, e

// DeleteEventType deletes event type from the configuration.
func (service Service) DeleteEventType(space string, name event.TypeName) error {
subs, err := service.GetSubscriptions(space)
subs, err := service.ListSubscriptions(space)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions libkv/eventtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestGetEventType(t *testing.T) {
})
}

func TestGetEventTypes(t *testing.T) {
func TestListEventTypes(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand All @@ -134,7 +134,7 @@ func TestGetEventTypes(t *testing.T) {
db.EXPECT().List("default/", &store.ReadOptions{Consistent: true}).Return(kvs, nil)
service := &Service{EventTypeStore: db, Log: zap.NewNop()}

list, err := service.GetEventTypes("default")
list, err := service.ListEventTypes("default")

assert.Nil(t, err)
assert.Equal(t, event.Types{testEventType}, list)
Expand All @@ -145,7 +145,7 @@ func TestGetEventTypes(t *testing.T) {
db.EXPECT().List(gomock.Any(), gomock.Any()).Return([]*store.KVPair{}, errors.New("KV list err"))
service := &Service{EventTypeStore: db, Log: zap.NewNop()}

_, err := service.GetEventTypes("default")
_, err := service.ListEventTypes("default")

assert.EqualError(t, err, "KV list err")
})
Expand All @@ -155,7 +155,7 @@ func TestGetEventTypes(t *testing.T) {
db.EXPECT().List(gomock.Any(), gomock.Any()).Return([]*store.KVPair{}, errors.New("Key not found in store"))
service := &Service{EventTypeStore: db, Log: zap.NewNop()}

list, _ := service.GetEventTypes("default")
list, _ := service.ListEventTypes("default")

assert.Equal(t, event.Types{}, list)
})
Expand Down
10 changes: 5 additions & 5 deletions libkv/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func (key FunctionKey) String() string {
return key.Space + "/" + string(key.ID)
}

// RegisterFunction registers function in configuration.
func (service Service) RegisterFunction(fn *function.Function) (*function.Function, error) {
// CreateFunction registers function in configuration.
func (service Service) CreateFunction(fn *function.Function) (*function.Function, error) {
if err := validateFunction(fn); err != nil {
return nil, err
}
Expand Down Expand Up @@ -94,8 +94,8 @@ func (service Service) GetFunction(space string, id function.ID) (*function.Func
return &fn, nil
}

// GetFunctions returns an array of all functions in the space.
func (service Service) GetFunctions(space string) (function.Functions, error) {
// ListFunctions returns an array of all functions in the space.
func (service Service) ListFunctions(space string) (function.Functions, error) {
fns := []*function.Function{}

kvs, err := service.FunctionStore.List(spacePath(space), &store.ReadOptions{Consistent: true})
Expand All @@ -119,7 +119,7 @@ func (service Service) GetFunctions(space string) (function.Functions, error) {

// DeleteFunction deletes function from the registry.
func (service Service) DeleteFunction(space string, id function.ID) error {
subs, err := service.GetSubscriptions(space)
subs, err := service.ListSubscriptions(space)
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions libkv/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
_ "github.com/serverless/event-gateway/providers/http"
)

func TestRegisterFunction(t *testing.T) {
func TestCreateFunction(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand All @@ -32,7 +32,7 @@ func TestRegisterFunction(t *testing.T) {
db.EXPECT().AtomicPut("default/testid", payload, nil, nil).Return(true, nil, nil)
service := &Service{FunctionStore: db, Log: zap.NewNop()}

_, err := service.RegisterFunction(fn)
_, err := service.CreateFunction(fn)

assert.Nil(t, err)
})
Expand All @@ -42,7 +42,7 @@ func TestRegisterFunction(t *testing.T) {
db.EXPECT().Get("default/testid", gomock.Any()).Return(nil, nil)
service := &Service{FunctionStore: db, Log: zap.NewNop()}

_, err := service.RegisterFunction(fn)
_, err := service.CreateFunction(fn)

assert.Equal(t, err, &function.ErrFunctionAlreadyRegistered{ID: "testid"})
})
Expand All @@ -53,7 +53,7 @@ func TestRegisterFunction(t *testing.T) {
db.EXPECT().AtomicPut(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil, errors.New("KV put error"))
service := &Service{FunctionStore: db, Log: zap.NewNop()}

_, err := service.RegisterFunction(fn)
_, err := service.CreateFunction(fn)

assert.EqualError(t, err, "KV put error")
})
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestGetFunctions(t *testing.T) {
db.EXPECT().List("default/", &store.ReadOptions{Consistent: true}).Return(kvs, nil)
service := &Service{FunctionStore: db, Log: zap.NewNop()}

list, err := service.GetFunctions("default")
list, err := service.ListFunctions("default")

assert.Nil(t, err)
assert.Equal(t, function.Functions{{
Expand All @@ -178,7 +178,7 @@ func TestGetFunctions(t *testing.T) {
db.EXPECT().List("default/", gomock.Any()).Return([]*store.KVPair{}, errors.New("KV list err"))
service := &Service{FunctionStore: db, Log: zap.NewNop()}

_, err := service.GetFunctions("default")
_, err := service.ListFunctions("default")

assert.EqualError(t, err, "KV list err")
})
Expand All @@ -188,7 +188,7 @@ func TestGetFunctions(t *testing.T) {
db.EXPECT().List("default/", gomock.Any()).Return([]*store.KVPair{}, errors.New("Key not found in store"))
service := &Service{FunctionStore: db, Log: zap.NewNop()}

list, _ := service.GetFunctions("default")
list, _ := service.ListFunctions("default")

assert.Equal(t, function.Functions{}, list)
})
Expand Down
4 changes: 2 additions & 2 deletions libkv/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (service Service) DeleteSubscription(space string, id subscription.ID) erro
return nil
}

// GetSubscriptions returns array of all Subscription.
func (service Service) GetSubscriptions(space string) (subscription.Subscriptions, error) {
// ListSubscriptions returns array of all Subscription.
func (service Service) ListSubscriptions(space string) (subscription.Subscriptions, error) {
subs := []*subscription.Subscription{}

kvs, err := service.SubscriptionStore.List(spacePath(space), &store.ReadOptions{Consistent: true})
Expand Down
Loading

0 comments on commit 28ded18

Please sign in to comment.