2020-04-29 19:57:53 +02:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-04-29 23:39:54 +02:00
|
|
|
"fmt"
|
2020-05-05 15:37:12 +02:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/docker/api/compose"
|
|
|
|
"github.com/docker/api/containers"
|
2020-05-12 13:37:28 +02:00
|
|
|
"github.com/docker/api/context/cloud"
|
2020-04-29 19:57:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-05-04 23:00:21 +02:00
|
|
|
errNoType = errors.New("backend: no type")
|
|
|
|
errNoName = errors.New("backend: no name")
|
|
|
|
errTypeRegistered = errors.New("backend: already registered")
|
2020-04-29 19:57:53 +02:00
|
|
|
)
|
|
|
|
|
2020-05-05 17:14:26 +02:00
|
|
|
type initFunc func(context.Context) (Service, error)
|
2020-05-28 17:37:59 +02:00
|
|
|
type getCloudServiceFunc func() (cloud.Service, error)
|
2020-04-29 19:57:53 +02:00
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
type registeredBackend struct {
|
2020-05-28 17:37:59 +02:00
|
|
|
name string
|
|
|
|
backendType string
|
|
|
|
init initFunc
|
|
|
|
getCloudService getCloudServiceFunc
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var backends = struct {
|
2020-05-04 23:00:21 +02:00
|
|
|
r []*registeredBackend
|
2020-04-29 19:57:53 +02:00
|
|
|
}{}
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
// Service aggregates the service interfaces
|
2020-05-05 15:37:12 +02:00
|
|
|
type Service interface {
|
|
|
|
ContainerService() containers.Service
|
|
|
|
ComposeService() compose.Service
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// Register adds a typed backend to the registry
|
2020-05-28 17:37:59 +02:00
|
|
|
func Register(name string, backendType string, init initFunc, getCoudService getCloudServiceFunc) {
|
2020-04-29 19:57:53 +02:00
|
|
|
if name == "" {
|
2020-05-05 15:37:12 +02:00
|
|
|
logrus.Fatal(errNoName)
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
|
|
|
if backendType == "" {
|
2020-05-05 15:37:12 +02:00
|
|
|
logrus.Fatal(errNoType)
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
|
|
|
for _, b := range backends.r {
|
|
|
|
if b.backendType == backendType {
|
2020-05-05 15:37:12 +02:00
|
|
|
logrus.Fatal(errTypeRegistered)
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
backends.r = append(backends.r, ®isteredBackend{
|
2020-04-29 19:57:53 +02:00
|
|
|
name,
|
|
|
|
backendType,
|
|
|
|
init,
|
2020-05-28 17:37:59 +02:00
|
|
|
getCoudService,
|
2020-04-29 19:57:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// Get returns the backend registered for a particular type, it returns
|
|
|
|
// an error if there is no registered backends for the given type.
|
2020-05-05 17:14:26 +02:00
|
|
|
func Get(ctx context.Context, backendType string) (Service, error) {
|
2020-04-29 19:57:53 +02:00
|
|
|
for _, b := range backends.r {
|
|
|
|
if b.backendType == backendType {
|
|
|
|
return b.init(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:39:54 +02:00
|
|
|
return nil, fmt.Errorf("backend not found for context %q", backendType)
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
2020-05-28 17:37:59 +02:00
|
|
|
|
|
|
|
// GetCloudService returns the backend registered for a particular type, it returns
|
|
|
|
// an error if there is no registered backends for the given type.
|
|
|
|
func GetCloudService(ctx context.Context, backendType string) (cloud.Service, error) {
|
|
|
|
for _, b := range backends.r {
|
|
|
|
if b.backendType == backendType {
|
|
|
|
return b.getCloudService()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("backend not found for backend type %s", backendType)
|
|
|
|
}
|