Skip to content

Commit

Permalink
New session now expects a struct with everything it needs and a conte…
Browse files Browse the repository at this point in the history
…xt (play-with-docker#228)

to pass along
  • Loading branch information
xetorthio authored and marcosnils committed Nov 28, 2017
1 parent 08f1ead commit da6a55f
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 23 deletions.
5 changes: 4 additions & 1 deletion handlers/new_session.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"context"
"encoding/json"
"fmt"
"log"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion provisioner/overlay.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provisioner

import (
"context"
"fmt"
"log"
"net/url"
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion provisioner/provisioner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provisioner

import (
"context"
"errors"
"io"
"net"
Expand All @@ -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
}

Expand Down
10 changes: 7 additions & 3 deletions pwd/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pwd

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions pwd/instance_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pwd

import (
"context"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions pwd/mock.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion pwd/pwd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pwd

import (
"context"
"errors"
"io"
"net"
Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions pwd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 7 additions & 3 deletions pwd/session_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pwd

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand Down
9 changes: 9 additions & 0 deletions pwd/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit da6a55f

Please sign in to comment.