Refactor GetForInstance so it doesn't depends on storage

This commit is contained in:
Marcos Lilljedahl
2017-08-11 18:16:14 -03:00
parent 1263f36bc8
commit fb1e50deaf
12 changed files with 52 additions and 42 deletions

View File

@@ -1,6 +1,8 @@
package docker
import "github.com/play-with-docker/play-with-docker/pwd/types"
type FactoryApi interface {
GetForSession(sessionId string) (DockerApi, error)
GetForInstance(sessionId, instanceName string) (DockerApi, error)
GetForInstance(instance *types.Instance) (DockerApi, error)
}

View File

@@ -1,6 +1,9 @@
package docker
import "github.com/stretchr/testify/mock"
import (
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/stretchr/testify/mock"
)
type FactoryMock struct {
mock.Mock
@@ -11,7 +14,7 @@ func (m *FactoryMock) GetForSession(sessionId string) (DockerApi, error) {
return args.Get(0).(DockerApi), args.Error(1)
}
func (m *FactoryMock) GetForInstance(sessionId, instanceName string) (DockerApi, error) {
args := m.Called(sessionId, instanceName)
func (m *FactoryMock) GetForInstance(instance *types.Instance) (DockerApi, error) {
args := m.Called(instance)
return args.Get(0).(DockerApi), args.Error(1)
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/docker/docker/api"
"github.com/docker/docker/client"
"github.com/docker/go-connections/tlsconfig"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/router"
"github.com/play-with-docker/play-with-docker/storage"
)
@@ -52,8 +53,8 @@ func (f *localCachedFactory) GetForSession(sessionId string) (DockerApi, error)
return f.sessionClient, nil
}
func (f *localCachedFactory) GetForInstance(sessionId, instanceName string) (DockerApi, error) {
key := sessionId + instanceName
func (f *localCachedFactory) GetForInstance(instance *types.Instance) (DockerApi, error) {
key := instance.SessionId + instance.Name
f.irw.Lock()
c, found := f.instanceClients[key]
@@ -71,10 +72,6 @@ func (f *localCachedFactory) GetForInstance(sessionId, instanceName string) (Doc
return c.client, nil
}
instance, err := f.storage.InstanceGet(sessionId, instanceName)
if err != nil {
return nil, err
}
// Need to create client to the DinD docker daemon
// We check if the client needs to use TLS
var tlsConfig *tls.Config