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

[WIP] Change UpdateTemplate function declaration #10

Closed
6 changes: 4 additions & 2 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/fatih/structs"
)

// NewDefaultRouter sets up a mux.Router with the registry routes
Expand Down Expand Up @@ -166,7 +167,7 @@ func (registry Registry) templateUpdate(w http.ResponseWriter, r *http.Request)
panic(err)
}

var tmpl Template
var tmpl Template

if err := json.Unmarshal(body, &tmpl); err != nil {
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -182,7 +183,8 @@ func (registry Registry) templateUpdate(w http.ResponseWriter, r *http.Request)

tmpl.Name = templateName

tmpl, err = registry.UpdateTemplate(tmpl)
tmpl, err = registry.UpdateTemplate(templateName, structs.Map(tmpl))

if err != nil {
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(TemplateNotFoundError); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http/httptest"
"testing"

"github.com/binder-project/binder-registry/mocks"
"github.com/binder-project/binder-registry/mocks"
"github.com/gorilla/mux"
)

Expand Down
15 changes: 8 additions & 7 deletions inmemstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,24 @@ func (store InMemoryStore) ListTemplates() ([]Template, error) {
}

// UpdateTemplate will allow for updating ImageName and Command
func (store InMemoryStore) UpdateTemplate(tmpl Template) (Template, error) {
updatedTemplate, ok := store.templateMap[tmpl.Name]
func (store InMemoryStore) UpdateTemplate(name string,
update map[string]interface {}) (Template, error) {
updatedTemplate, ok := store.templateMap[name]
if !ok {
return Template{}, UnavailableTemplateError
}

// For now we allow updates to image name and command
if tmpl.ImageName != "" {
updatedTemplate.ImageName = tmpl.ImageName
if updatedName, ok := update["name"]; ok {
updatedTemplate.ImageName = updatedName.(string)
}
if tmpl.Command != "" {
updatedTemplate.Command = tmpl.Command
if command, ok := update["command"]; ok {
updatedTemplate.Command = command.(string)
}

updatedTemplate.TimeModified = time.Now().UTC()

store.templateMap[tmpl.Name] = updatedTemplate
store.templateMap[name] = updatedTemplate

return updatedTemplate, nil
}
5 changes: 3 additions & 2 deletions inmemstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestListTemplates(t *testing.T) {

func TestUpdateTemplate(t *testing.T) {
store := NewInMemoryStore()
_, err := store.UpdateTemplate(Template{Name: "NotHere", ImageName: "cool/app"})
_, err := store.UpdateTemplate("NotHere", make(map[string]interface{}))
equals(t, err, UnavailableTemplateError)

template := Template{
Expand All @@ -107,7 +107,8 @@ func TestUpdateTemplate(t *testing.T) {
updatedTemplate.ImageName = "nteract/poster"
nequals(t, updatedTemplate, registeredTemplate)

actualTemplate, err := store.UpdateTemplate(updatedTemplate)
actualTemplate, err := store.UpdateTemplate("Test1", map[string]interface{}{
"image-name": "nteract/poster"})
ok(t, err)

// TODO: Mock time
Expand Down
2 changes: 1 addition & 1 deletion middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s PanicStore) ListTemplates() ([]Template, error) {
panic("NO LISTING TEMPLATES FOR YOU")
}

func (s PanicStore) UpdateTemplate(tmpl Template) (Template, error) {
func (s PanicStore) UpdateTemplate(name string, update map[string]interface{}) (Template, error) {
panic("NO UPDATING TEMPLATES FOR YOU")
}

Expand Down
2 changes: 1 addition & 1 deletion mongostore.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func contains(list []string, item string) bool {
}

func (store MongoStore) UpdateTemplate(name string,
update map[string]string) error {
update map[string]interface {}) error {
TEMPLATE_KEYS := []string{"name", "image-name", "command", "limits",
"time-created", "time-modified", "redirect-uri",
"container-ip", "bind-port"}
Expand Down
2 changes: 1 addition & 1 deletion mongostore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestMongoUpdateTemplate(t *testing.T) {
_, err := store.RegisterTemplate(tmpl)

if (err == nil) {
updates := make(map[string]string)
updates := make(map[string]interface{})
updates["name"] = "Updated!"
err := store.UpdateTemplate(tmpl.Name, updates)

Expand Down
119 changes: 67 additions & 52 deletions registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,72 +71,87 @@ func matchedAPIError(tb testing.TB, expected APIErrorResponse, w *httptest.Respo
equals(tb, expected.Message, apiError.Message)
}

/**
Autogenerated with mockery -name=store
There's a few changes though. Buyer beware.
**/
/*

*/
type mockStore struct {
mock.Mock
registerCall map[string]RegisterReceiver
mock.Mock
registerCall map[string]RegisterReceiver
}

type RegisterReceiver struct {
Input Template
OutputTemplate Template
OutputError error
Actual Template
Input Template
OutputTemplate Template
OutputError error
Actual Template
}

func (_m *mockStore) GetTemplate(name string) (Template, error) {
ret := _m.Called(name)

var r0 Template
if rf, ok := ret.Get(0).(func(string) Template); ok {
r0 = rf(name)
} else {
r0 = ret.Get(0).(Template)
}

var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(name)
} else {
r1 = ret.Error(1)
}

return r0, r1
ret := _m.Called(name)

var r0 Template
if rf, ok := ret.Get(0).(func(string) Template); ok {
r0 = rf(name)
} else {
r0 = ret.Get(0).(Template)
}

var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(name)
} else {
r1 = ret.Error(1)
}

return r0, r1
}

func (_m *mockStore) RegisterTemplate(tmpl Template) (Template, error) {
receiver := _m.registerCall[tmpl.Name]
receiver.Actual = tmpl
receiver := _m.registerCall[tmpl.Name]
receiver.Actual = tmpl

return receiver.OutputTemplate, receiver.OutputError
return receiver.OutputTemplate, receiver.OutputError
}

func (_m *mockStore) ListTemplates() ([]Template, error) {
ret := _m.Called()

var r0 []Template
if rf, ok := ret.Get(0).(func() []Template); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]Template)
}
}
ret := _m.Called()

var r0 []Template
if rf, ok := ret.Get(0).(func() []Template); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]Template)
}
}

var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}

return r0, r1
}

var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
func (_m *mockStore) UpdateTemplate(name string, update map[string]interface{}) (Template, error) {
ret := _m.Called(name, update)

return r0, r1
}
func (_m *mockStore) UpdateTemplate(tmpl Template) (Template, error) {
receiver := _m.registerCall[tmpl.Name]
receiver.Actual = tmpl
var r0 Template
if rf, ok := ret.Get(0).(func(string, map[string]interface{}) Template); ok {
r0 = rf(name, update)
} else {
r0 = ret.Get(0).(Template)
}

var r1 error
if rf, ok := ret.Get(1).(func(string, map[string]interface{}) error); ok {
r1 = rf(name, update)
} else {
r1 = ret.Error(1)
}

return receiver.OutputTemplate, receiver.OutputError
return r0, r1
}

2 changes: 1 addition & 1 deletion store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Store interface {
ListTemplates() ([]Template, error)

// UpdateTemplate will allow for updating ImageName and Command
UpdateTemplate(tmpl Template) (Template, error)
UpdateTemplate(name string, update map[string]interface {}) (Template, error)
}

// AuthStore is an interface for connecting to some authentication endpoint,
Expand Down