New session now expects a struct with everything it needs and a context (#228)

to pass along
This commit is contained in:
Jonathan Leibiusky
2017-11-28 12:14:50 -03:00
committed by Marcos Nils
parent 08f1ead2a9
commit da6a55fb5c
10 changed files with 51 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@@ -11,6 +12,7 @@ import (
"github.com/play-with-docker/play-with-docker/config" "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/provisioner"
"github.com/play-with-docker/play-with-docker/pwd/types"
) )
type NewSessionResponse struct { type NewSessionResponse struct {
@@ -75,7 +77,8 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
duration = playground.DefaultSessionDuration 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 err != nil {
if provisioner.OutOfCapacity(err) { if provisioner.OutOfCapacity(err) {
http.Redirect(rw, req, "/ooc", http.StatusFound) http.Redirect(rw, req, "/ooc", http.StatusFound)

View File

@@ -1,6 +1,7 @@
package provisioner package provisioner
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"net/url" "net/url"
@@ -20,7 +21,7 @@ func NewOverlaySessionProvisioner(df docker.FactoryApi) SessionProvisionerApi {
return &overlaySessionProvisioner{dockerFactory: df} 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) dockerClient, err := p.dockerFactory.GetForSession(s)
if err != nil { if err != nil {
// We assume we are out of capacity // We assume we are out of capacity

View File

@@ -1,6 +1,7 @@
package provisioner package provisioner
import ( import (
"context"
"errors" "errors"
"io" "io"
"net" "net"
@@ -27,7 +28,7 @@ type InstanceProvisionerApi interface {
} }
type SessionProvisionerApi 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 SessionClose(session *types.Session) error
} }

View File

@@ -1,6 +1,7 @@
package pwd package pwd
import ( import (
"context"
"testing" "testing"
"time" "time"
@@ -45,7 +46,8 @@ func TestClientNew(t *testing.T) {
playground := &types.Playground{Id: "foobar"} 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) assert.Nil(t, err)
client := p.ClientNew("foobar", session) client := p.ClientNew("foobar", session)
@@ -85,7 +87,8 @@ func TestClientCount(t *testing.T) {
p.generator = _g p.generator = _g
playground := &types.Playground{Id: "foobar"} 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) assert.Nil(t, err)
p.ClientNew("foobar", session) p.ClientNew("foobar", session)
@@ -127,7 +130,8 @@ func TestClientResizeViewPort(t *testing.T) {
p.generator = _g p.generator = _g
playground := &types.Playground{Id: "foobar"} 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) assert.Nil(t, err)
client := p.ClientNew("foobar", session) client := p.ClientNew("foobar", session)
_s.On("ClientFindBySessionId", "aaaabbbbcccc").Return([]*types.Client{client}, nil) _s.On("ClientFindBySessionId", "aaaabbbbcccc").Return([]*types.Client{client}, nil)

View File

@@ -1,6 +1,7 @@
package pwd package pwd
import ( import (
"context"
"fmt" "fmt"
"testing" "testing"
"time" "time"
@@ -74,7 +75,8 @@ func TestInstanceNew(t *testing.T) {
_s.On("PlaygroundGet", "foobar").Return(playground, nil) _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) assert.Nil(t, err)
expectedInstance := types.Instance{ expectedInstance := types.Instance{
@@ -143,7 +145,8 @@ func TestInstanceNew_WithNotAllowedImage(t *testing.T) {
p.generator = _g p.generator = _g
playground := &types.Playground{Id: "foobar"} 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) assert.Nil(t, err)
@@ -213,7 +216,8 @@ func TestInstanceNew_WithCustomHostname(t *testing.T) {
p.generator = _g p.generator = _g
playground := &types.Playground{Id: "foobar"} 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) assert.Nil(t, err)
expectedInstance := types.Instance{ expectedInstance := types.Instance{

View File

@@ -1,9 +1,9 @@
package pwd package pwd
import ( import (
"context"
"io" "io"
"net" "net"
"time"
"github.com/play-with-docker/play-with-docker/pwd/types" "github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
@@ -13,8 +13,8 @@ type Mock struct {
mock.Mock mock.Mock
} }
func (m *Mock) SessionNew(playground *types.Playground, userId string, duration time.Duration, stack string, stackName, imageName string) (*types.Session, error) { func (m *Mock) SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) {
args := m.Called(duration, stack, stackName, imageName) args := m.Called(ctx, config)
return args.Get(0).(*types.Session), args.Error(1) return args.Get(0).(*types.Session), args.Error(1)
} }

View File

@@ -1,6 +1,7 @@
package pwd package pwd
import ( import (
"context"
"errors" "errors"
"io" "io"
"net" "net"
@@ -72,7 +73,7 @@ func SessionNotEmpty(e error) bool {
} }
type PWDApi interface { 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 SessionClose(session *types.Session) error
SessionGetSmallestViewPort(sessionId string) types.ViewPort SessionGetSmallestViewPort(sessionId string) types.ViewPort
SessionDeployStack(session *types.Session) error SessionDeployStack(session *types.Session) error

View File

@@ -44,29 +44,30 @@ type SessionSetupInstanceConf struct {
Tls bool `json:"tls"` 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()) defer observeAction("SessionNew", time.Now())
s := &types.Session{} s := &types.Session{}
s.Id = p.generator.NewId() s.Id = p.generator.NewId()
s.CreatedAt = time.Now() s.CreatedAt = time.Now()
s.ExpiresAt = s.CreatedAt.Add(duration) s.ExpiresAt = s.CreatedAt.Add(config.Duration)
s.Ready = true s.Ready = true
s.Stack = stack s.Stack = config.Stack
s.UserId = userId s.UserId = config.UserId
s.PlaygroundId = playground.Id s.PlaygroundId = config.Playground.Id
if s.Stack != "" { if s.Stack != "" {
s.Ready = false s.Ready = false
} }
stackName := config.StackName
if stackName == "" { if stackName == "" {
stackName = "pwd" stackName = "pwd"
} }
s.StackName = stackName s.StackName = stackName
s.ImageName = imageName s.ImageName = config.ImageName
log.Printf("NewSession id=[%s]\n", s.Id) 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) log.Println(err)
return nil, err return nil, err
} }

View File

@@ -1,6 +1,7 @@
package pwd package pwd
import ( import (
"context"
"testing" "testing"
"time" "time"
@@ -47,7 +48,8 @@ func TestSessionNew(t *testing.T) {
before := time.Now() before := time.Now()
playground := &types.Playground{Id: "foobar"} 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.Nil(t, e)
assert.NotNil(t, s) 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.WithinDuration(t, s.ExpiresAt, before.Add(time.Hour), time.Second)
assert.True(t, s.Ready) 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, "stackPath", s.Stack)
assert.Equal(t, "stackName", s.StackName) assert.Equal(t, "stackName", s.StackName)
@@ -133,7 +136,8 @@ func TestSessionSetup(t *testing.T) {
p := NewPWD(_f, _e, _s, sp, ipf) p := NewPWD(_f, _e, _s, sp, ipf)
p.generator = _g 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) assert.Nil(t, e)
err := p.SessionSetup(s, SessionSetupConf{ err := p.SessionSetup(s, SessionSetupConf{

View File

@@ -4,6 +4,15 @@ import (
"time" "time"
) )
type SessionConfig struct {
Playground *Playground
UserId string
Duration time.Duration
Stack string
StackName string
ImageName string
}
type Session struct { type Session struct {
Id string `json:"id" bson:"id"` Id string `json:"id" bson:"id"`
CreatedAt time.Time `json:"created_at" bson:"created_at"` CreatedAt time.Time `json:"created_at" bson:"created_at"`