backend.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // +build ecs
  2. /*
  3. Copyright 2020 Docker, Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package ecs
  15. import (
  16. "context"
  17. ecsplugin "github.com/docker/ecs-plugin/pkg/amazon/backend"
  18. "github.com/docker/api/backend"
  19. "github.com/docker/api/compose"
  20. "github.com/docker/api/containers"
  21. apicontext "github.com/docker/api/context"
  22. "github.com/docker/api/context/cloud"
  23. "github.com/docker/api/context/store"
  24. "github.com/docker/api/errdefs"
  25. )
  26. const backendType = store.EcsContextType
  27. // ContextParams options for creating AWS context
  28. type ContextParams struct {
  29. Description string
  30. Region string
  31. Profile string
  32. AwsID string
  33. AwsSecret string
  34. }
  35. func init() {
  36. backend.Register(backendType, backendType, service, getCloudService)
  37. }
  38. func service(ctx context.Context) (backend.Service, error) {
  39. contextStore := store.ContextStore(ctx)
  40. currentContext := apicontext.CurrentContext(ctx)
  41. var ecsContext store.EcsContext
  42. if err := contextStore.GetEndpoint(currentContext, &ecsContext); err != nil {
  43. return nil, err
  44. }
  45. return getEcsAPIService(ecsContext)
  46. }
  47. func getEcsAPIService(ecsCtx store.EcsContext) (*ecsAPIService, error) {
  48. backend, err := ecsplugin.NewBackend(ecsCtx.Profile, ecsCtx.Region)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return &ecsAPIService{
  53. ctx: ecsCtx,
  54. composeBackend: backend,
  55. }, nil
  56. }
  57. type ecsAPIService struct {
  58. ctx store.EcsContext
  59. composeBackend *ecsplugin.Backend
  60. }
  61. func (a *ecsAPIService) ContainerService() containers.Service {
  62. return nil
  63. }
  64. func (a *ecsAPIService) ComposeService() compose.Service {
  65. return a.composeBackend
  66. }
  67. func getCloudService() (cloud.Service, error) {
  68. return ecsCloudService{}, nil
  69. }
  70. type ecsCloudService struct {
  71. }
  72. func (a ecsCloudService) Login(ctx context.Context, params interface{}) error {
  73. return errdefs.ErrNotImplemented
  74. }
  75. func (a ecsCloudService) Logout(ctx context.Context) error {
  76. return errdefs.ErrNotImplemented
  77. }
  78. func (a ecsCloudService) CreateContextData(ctx context.Context, params interface{}) (interface{}, string, error) {
  79. contextHelper := newContextCreateHelper()
  80. createOpts := params.(ContextParams)
  81. return contextHelper.createContextData(ctx, createOpts)
  82. }