backend.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 ecs
  14. import (
  15. "context"
  16. "github.com/aws/aws-sdk-go/aws"
  17. "github.com/aws/aws-sdk-go/aws/session"
  18. "github.com/docker/compose-cli/backend"
  19. "github.com/docker/compose-cli/compose"
  20. "github.com/docker/compose-cli/containers"
  21. apicontext "github.com/docker/compose-cli/context"
  22. "github.com/docker/compose-cli/context/cloud"
  23. "github.com/docker/compose-cli/context/store"
  24. "github.com/docker/compose-cli/errdefs"
  25. "github.com/docker/compose-cli/secrets"
  26. )
  27. const backendType = store.EcsContextType
  28. // ContextParams options for creating AWS context
  29. type ContextParams struct {
  30. Description string
  31. Region string
  32. Profile string
  33. AwsID string
  34. AwsSecret string
  35. }
  36. func init() {
  37. backend.Register(backendType, backendType, service, getCloudService)
  38. }
  39. func service(ctx context.Context) (backend.Service, error) {
  40. contextStore := store.ContextStore(ctx)
  41. currentContext := apicontext.CurrentContext(ctx)
  42. var ecsContext store.EcsContext
  43. if err := contextStore.GetEndpoint(currentContext, &ecsContext); err != nil {
  44. return nil, err
  45. }
  46. return getEcsAPIService(ecsContext)
  47. }
  48. func getEcsAPIService(ecsCtx store.EcsContext) (*ecsAPIService, error) {
  49. sess, err := session.NewSessionWithOptions(session.Options{
  50. Profile: ecsCtx.Profile,
  51. SharedConfigState: session.SharedConfigEnable,
  52. Config: aws.Config{
  53. Region: aws.String(ecsCtx.Region),
  54. },
  55. })
  56. if err != nil {
  57. return nil, err
  58. }
  59. return &ecsAPIService{
  60. ctx: ecsCtx,
  61. Region: ecsCtx.Region,
  62. SDK: newSDK(sess),
  63. }, nil
  64. }
  65. type ecsAPIService struct {
  66. ctx store.EcsContext
  67. Region string
  68. SDK sdk
  69. }
  70. func (a *ecsAPIService) ContainerService() containers.Service {
  71. return nil
  72. }
  73. func (a *ecsAPIService) ComposeService() compose.Service {
  74. return a
  75. }
  76. func (a *ecsAPIService) SecretsService() secrets.Service {
  77. return a
  78. }
  79. func getCloudService() (cloud.Service, error) {
  80. return ecsCloudService{}, nil
  81. }
  82. type ecsCloudService struct {
  83. }
  84. func (a ecsCloudService) Login(ctx context.Context, params interface{}) error {
  85. return errdefs.ErrNotImplemented
  86. }
  87. func (a ecsCloudService) Logout(ctx context.Context) error {
  88. return errdefs.ErrNotImplemented
  89. }
  90. func (a ecsCloudService) CreateContextData(ctx context.Context, params interface{}) (interface{}, string, error) {
  91. contextHelper := newContextCreateHelper()
  92. createOpts := params.(ContextParams)
  93. return contextHelper.createContextData(ctx, createOpts)
  94. }