diff --git a/handlers/new_session.go b/handlers/new_session.go index a6ae265..cdae21d 100644 --- a/handlers/new_session.go +++ b/handlers/new_session.go @@ -1,6 +1,7 @@ package handlers import ( + "context" "encoding/json" "fmt" "log" @@ -11,6 +12,7 @@ import ( "github.com/play-with-docker/play-with-docker/config" "github.com/play-with-docker/play-with-docker/provisioner" + "github.com/play-with-docker/play-with-docker/pwd/types" ) type NewSessionResponse struct { @@ -75,7 +77,8 @@ func NewSession(rw http.ResponseWriter, req *http.Request) { duration = playground.DefaultSessionDuration } - s, err := core.SessionNew(playground, userId, duration, stack, stackName, imageName) + sConfig := types.SessionConfig{Playground: playground, UserId: userId, Duration: duration, Stack: stack, StackName: stackName, ImageName: imageName} + s, err := core.SessionNew(context.Background(), sConfig) if err != nil { if provisioner.OutOfCapacity(err) { http.Redirect(rw, req, "/ooc", http.StatusFound) diff --git a/provisioner/overlay.go b/provisioner/overlay.go index e8b3e93..9f08065 100644 --- a/provisioner/overlay.go +++ b/provisioner/overlay.go @@ -1,6 +1,7 @@ package provisioner import ( + "context" "fmt" "log" "net/url" @@ -20,7 +21,7 @@ func NewOverlaySessionProvisioner(df docker.FactoryApi) SessionProvisionerApi { return &overlaySessionProvisioner{dockerFactory: df} } -func (p *overlaySessionProvisioner) SessionNew(playground *types.Playground, s *types.Session) error { +func (p *overlaySessionProvisioner) SessionNew(ctx context.Context, s *types.Session) error { dockerClient, err := p.dockerFactory.GetForSession(s) if err != nil { // We assume we are out of capacity diff --git a/provisioner/provisioner.go b/provisioner/provisioner.go index b4af881..8167c2f 100644 --- a/provisioner/provisioner.go +++ b/provisioner/provisioner.go @@ -1,6 +1,7 @@ package provisioner import ( + "context" "errors" "io" "net" @@ -27,7 +28,7 @@ type InstanceProvisionerApi interface { } type SessionProvisionerApi interface { - SessionNew(playground *types.Playground, session *types.Session) error + SessionNew(ctx context.Context, session *types.Session) error SessionClose(session *types.Session) error } diff --git a/pwd/client_test.go b/pwd/client_test.go index e7984bd..1ba5487 100644 --- a/pwd/client_test.go +++ b/pwd/client_test.go @@ -1,6 +1,7 @@ package pwd import ( + "context" "testing" "time" @@ -45,7 +46,8 @@ func TestClientNew(t *testing.T) { playground := &types.Playground{Id: "foobar"} - session, err := p.SessionNew(playground, "", time.Hour, "", "", "") + sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""} + session, err := p.SessionNew(context.Background(), sConfig) assert.Nil(t, err) client := p.ClientNew("foobar", session) @@ -85,7 +87,8 @@ func TestClientCount(t *testing.T) { p.generator = _g playground := &types.Playground{Id: "foobar"} - session, err := p.SessionNew(playground, "", time.Hour, "", "", "") + sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""} + session, err := p.SessionNew(context.Background(), sConfig) assert.Nil(t, err) p.ClientNew("foobar", session) @@ -127,7 +130,8 @@ func TestClientResizeViewPort(t *testing.T) { p.generator = _g playground := &types.Playground{Id: "foobar"} - session, err := p.SessionNew(playground, "", time.Hour, "", "", "") + sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""} + session, err := p.SessionNew(context.Background(), sConfig) assert.Nil(t, err) client := p.ClientNew("foobar", session) _s.On("ClientFindBySessionId", "aaaabbbbcccc").Return([]*types.Client{client}, nil) diff --git a/pwd/instance_test.go b/pwd/instance_test.go index cdd38e5..acd0c2d 100644 --- a/pwd/instance_test.go +++ b/pwd/instance_test.go @@ -1,6 +1,7 @@ package pwd import ( + "context" "fmt" "testing" "time" @@ -74,7 +75,8 @@ func TestInstanceNew(t *testing.T) { _s.On("PlaygroundGet", "foobar").Return(playground, nil) - session, err := p.SessionNew(playground, "", time.Hour, "", "", "") + sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""} + session, err := p.SessionNew(context.Background(), sConfig) assert.Nil(t, err) expectedInstance := types.Instance{ @@ -143,7 +145,8 @@ func TestInstanceNew_WithNotAllowedImage(t *testing.T) { p.generator = _g playground := &types.Playground{Id: "foobar"} - session, err := p.SessionNew(playground, "", time.Hour, "", "", "") + sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""} + session, err := p.SessionNew(context.Background(), sConfig) assert.Nil(t, err) @@ -213,7 +216,8 @@ func TestInstanceNew_WithCustomHostname(t *testing.T) { p.generator = _g playground := &types.Playground{Id: "foobar"} - session, err := p.SessionNew(playground, "", time.Hour, "", "", "") + sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""} + session, err := p.SessionNew(context.Background(), sConfig) assert.Nil(t, err) expectedInstance := types.Instance{ diff --git a/pwd/mock.go b/pwd/mock.go index 53b6a88..22539f9 100644 --- a/pwd/mock.go +++ b/pwd/mock.go @@ -1,9 +1,9 @@ package pwd import ( + "context" "io" "net" - "time" "github.com/play-with-docker/play-with-docker/pwd/types" "github.com/stretchr/testify/mock" @@ -13,8 +13,8 @@ type Mock struct { mock.Mock } -func (m *Mock) SessionNew(playground *types.Playground, userId string, duration time.Duration, stack string, stackName, imageName string) (*types.Session, error) { - args := m.Called(duration, stack, stackName, imageName) +func (m *Mock) SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) { + args := m.Called(ctx, config) return args.Get(0).(*types.Session), args.Error(1) } diff --git a/pwd/pwd.go b/pwd/pwd.go index 558c79b..ddf678a 100644 --- a/pwd/pwd.go +++ b/pwd/pwd.go @@ -1,6 +1,7 @@ package pwd import ( + "context" "errors" "io" "net" @@ -72,7 +73,7 @@ func SessionNotEmpty(e error) bool { } type PWDApi interface { - SessionNew(playground *types.Playground, userId string, duration time.Duration, stack string, stackName, imageName string) (*types.Session, error) + SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) SessionClose(session *types.Session) error SessionGetSmallestViewPort(sessionId string) types.ViewPort SessionDeployStack(session *types.Session) error diff --git a/pwd/session.go b/pwd/session.go index 688b41f..228e54a 100644 --- a/pwd/session.go +++ b/pwd/session.go @@ -44,29 +44,30 @@ type SessionSetupInstanceConf struct { Tls bool `json:"tls"` } -func (p *pwd) SessionNew(playground *types.Playground, userId string, duration time.Duration, stack, stackName, imageName string) (*types.Session, error) { +func (p *pwd) SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) { defer observeAction("SessionNew", time.Now()) s := &types.Session{} s.Id = p.generator.NewId() s.CreatedAt = time.Now() - s.ExpiresAt = s.CreatedAt.Add(duration) + s.ExpiresAt = s.CreatedAt.Add(config.Duration) s.Ready = true - s.Stack = stack - s.UserId = userId - s.PlaygroundId = playground.Id + s.Stack = config.Stack + s.UserId = config.UserId + s.PlaygroundId = config.Playground.Id if s.Stack != "" { s.Ready = false } + stackName := config.StackName if stackName == "" { stackName = "pwd" } s.StackName = stackName - s.ImageName = imageName + s.ImageName = config.ImageName log.Printf("NewSession id=[%s]\n", s.Id) - if err := p.sessionProvisioner.SessionNew(playground, s); err != nil { + if err := p.sessionProvisioner.SessionNew(ctx, s); err != nil { log.Println(err) return nil, err } diff --git a/pwd/session_test.go b/pwd/session_test.go index 7a2d2c2..1d68de6 100644 --- a/pwd/session_test.go +++ b/pwd/session_test.go @@ -1,6 +1,7 @@ package pwd import ( + "context" "testing" "time" @@ -47,7 +48,8 @@ func TestSessionNew(t *testing.T) { before := time.Now() playground := &types.Playground{Id: "foobar"} - s, e := p.SessionNew(playground, "", time.Hour, "", "", "") + sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""} + s, e := p.SessionNew(context.Background(), sConfig) assert.Nil(t, e) assert.NotNil(t, s) @@ -58,7 +60,8 @@ func TestSessionNew(t *testing.T) { assert.WithinDuration(t, s.ExpiresAt, before.Add(time.Hour), time.Second) assert.True(t, s.Ready) - s, _ = p.SessionNew(playground, "", time.Hour, "stackPath", "stackName", "imageName") + sConfig = types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "stackPath", StackName: "stackName", ImageName: "imageName"} + s, _ = p.SessionNew(context.Background(), sConfig) assert.Equal(t, "stackPath", s.Stack) assert.Equal(t, "stackName", s.StackName) @@ -133,7 +136,8 @@ func TestSessionSetup(t *testing.T) { p := NewPWD(_f, _e, _s, sp, ipf) p.generator = _g - s, e := p.SessionNew(time.Hour, "", "", "") + sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""} + s, e := p.SessionNew(context.Background(), sConfig) assert.Nil(t, e) err := p.SessionSetup(s, SessionSetupConf{ diff --git a/pwd/types/session.go b/pwd/types/session.go index 67ce933..9816718 100644 --- a/pwd/types/session.go +++ b/pwd/types/session.go @@ -4,6 +4,15 @@ import ( "time" ) +type SessionConfig struct { + Playground *Playground + UserId string + Duration time.Duration + Stack string + StackName string + ImageName string +} + type Session struct { Id string `json:"id" bson:"id"` CreatedAt time.Time `json:"created_at" bson:"created_at"`