backend.go 3.7 KB

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