backend.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. "fmt"
  17. "github.com/docker/compose-cli/api/backend"
  18. "github.com/docker/compose-cli/api/cloud"
  19. "github.com/docker/compose-cli/api/compose"
  20. "github.com/docker/compose-cli/api/containers"
  21. apicontext "github.com/docker/compose-cli/api/context"
  22. "github.com/docker/compose-cli/api/context/store"
  23. "github.com/docker/compose-cli/api/resources"
  24. "github.com/docker/compose-cli/api/secrets"
  25. "github.com/docker/compose-cli/api/volumes"
  26. "github.com/docker/compose-cli/errdefs"
  27. "github.com/aws/aws-sdk-go/aws"
  28. "github.com/aws/aws-sdk-go/aws/session"
  29. )
  30. const backendType = store.EcsContextType
  31. // ContextParams options for creating AWS context
  32. type ContextParams struct {
  33. Name string
  34. Description string
  35. AccessKey string
  36. SecretKey string
  37. Profile string
  38. Region string
  39. CredsFromEnv bool
  40. }
  41. func (c ContextParams) haveRequiredEnvVars() bool {
  42. if c.Profile != "" {
  43. return true
  44. }
  45. if c.AccessKey != "" && c.SecretKey != "" {
  46. return true
  47. }
  48. return false
  49. }
  50. func init() {
  51. backend.Register(backendType, backendType, service, getCloudService)
  52. }
  53. func service(ctx context.Context) (backend.Service, error) {
  54. contextStore := store.ContextStore(ctx)
  55. currentContext := apicontext.CurrentContext(ctx)
  56. var ecsContext store.EcsContext
  57. if err := contextStore.GetEndpoint(currentContext, &ecsContext); err != nil {
  58. return nil, err
  59. }
  60. return getEcsAPIService(ecsContext)
  61. }
  62. func getEcsAPIService(ecsCtx store.EcsContext) (*ecsAPIService, error) {
  63. region := ""
  64. profile := ecsCtx.Profile
  65. if ecsCtx.CredentialsFromEnv {
  66. env := getEnvVars()
  67. if !env.haveRequiredEnvVars() {
  68. return nil, fmt.Errorf("context requires credentials to be passed as environment variables")
  69. }
  70. profile = env.Profile
  71. region = env.Region
  72. }
  73. if region == "" {
  74. r, err := getRegion(profile)
  75. if err != nil {
  76. return nil, err
  77. }
  78. region = r
  79. }
  80. sess, err := session.NewSessionWithOptions(session.Options{
  81. Profile: profile,
  82. SharedConfigState: session.SharedConfigEnable,
  83. Config: aws.Config{
  84. Region: aws.String(region),
  85. },
  86. })
  87. if err != nil {
  88. return nil, err
  89. }
  90. sdk := newSDK(sess)
  91. return &ecsAPIService{
  92. ctx: ecsCtx,
  93. Region: region,
  94. aws: sdk,
  95. }, nil
  96. }
  97. type ecsAPIService struct {
  98. ctx store.EcsContext
  99. Region string
  100. aws API
  101. }
  102. func (b *ecsAPIService) ContainerService() containers.Service {
  103. return nil
  104. }
  105. func (b *ecsAPIService) ComposeService() compose.Service {
  106. return b
  107. }
  108. func (b *ecsAPIService) SecretsService() secrets.Service {
  109. return b
  110. }
  111. func (b *ecsAPIService) VolumeService() volumes.Service {
  112. return ecsVolumeService{backend: b}
  113. }
  114. func (b *ecsAPIService) ResourceService() resources.Service {
  115. return nil
  116. }
  117. func getCloudService() (cloud.Service, error) {
  118. return ecsCloudService{}, nil
  119. }
  120. type ecsCloudService struct {
  121. }
  122. func (a ecsCloudService) Login(ctx context.Context, params interface{}) error {
  123. return errdefs.ErrNotImplemented
  124. }
  125. func (a ecsCloudService) Logout(ctx context.Context) error {
  126. return errdefs.ErrNotImplemented
  127. }
  128. func (a ecsCloudService) CreateContextData(ctx context.Context, params interface{}) (interface{}, string, error) {
  129. contextHelper := newContextCreateHelper()
  130. createOpts := params.(ContextParams)
  131. return contextHelper.createContextData(ctx, createOpts)
  132. }