backend.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package backend
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "github.com/sirupsen/logrus"
  19. "github.com/docker/api/compose"
  20. "github.com/docker/api/containers"
  21. "github.com/docker/api/context/cloud"
  22. )
  23. var (
  24. errNoType = errors.New("backend: no type")
  25. errNoName = errors.New("backend: no name")
  26. errTypeRegistered = errors.New("backend: already registered")
  27. )
  28. type initFunc func(context.Context) (Service, error)
  29. type getCloudServiceFunc func() (cloud.Service, error)
  30. type registeredBackend struct {
  31. name string
  32. backendType string
  33. init initFunc
  34. getCloudService getCloudServiceFunc
  35. }
  36. var backends = struct {
  37. r []*registeredBackend
  38. }{}
  39. // Service aggregates the service interfaces
  40. type Service interface {
  41. ContainerService() containers.Service
  42. ComposeService() compose.Service
  43. }
  44. // Register adds a typed backend to the registry
  45. func Register(name string, backendType string, init initFunc, getCoudService getCloudServiceFunc) {
  46. if name == "" {
  47. logrus.Fatal(errNoName)
  48. }
  49. if backendType == "" {
  50. logrus.Fatal(errNoType)
  51. }
  52. for _, b := range backends.r {
  53. if b.backendType == backendType {
  54. logrus.Fatal(errTypeRegistered)
  55. }
  56. }
  57. backends.r = append(backends.r, &registeredBackend{
  58. name,
  59. backendType,
  60. init,
  61. getCoudService,
  62. })
  63. }
  64. // Get returns the backend registered for a particular type, it returns
  65. // an error if there is no registered backends for the given type.
  66. func Get(ctx context.Context, backendType string) (Service, error) {
  67. for _, b := range backends.r {
  68. if b.backendType == backendType {
  69. return b.init(ctx)
  70. }
  71. }
  72. return nil, fmt.Errorf("backend not found for context %q", backendType)
  73. }
  74. // GetCloudService returns the backend registered for a particular type, it returns
  75. // an error if there is no registered backends for the given type.
  76. func GetCloudService(ctx context.Context, backendType string) (cloud.Service, error) {
  77. for _, b := range backends.r {
  78. if b.backendType == backendType {
  79. return b.getCloudService()
  80. }
  81. }
  82. return nil, fmt.Errorf("backend not found for backend type %s", backendType)
  83. }