backend.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/compose"
  18. "github.com/docker/compose-cli/api/containers"
  19. "github.com/docker/compose-cli/api/resources"
  20. "github.com/docker/compose-cli/api/secrets"
  21. "github.com/docker/compose-cli/api/volumes"
  22. "github.com/docker/compose-cli/backend"
  23. apicontext "github.com/docker/compose-cli/context"
  24. "github.com/docker/compose-cli/context/cloud"
  25. "github.com/docker/compose-cli/context/store"
  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. Region string
  36. Profile string
  37. }
  38. func init() {
  39. backend.Register(backendType, backendType, service, getCloudService)
  40. }
  41. func service(ctx context.Context) (backend.Service, error) {
  42. contextStore := store.ContextStore(ctx)
  43. currentContext := apicontext.CurrentContext(ctx)
  44. var ecsContext store.EcsContext
  45. if err := contextStore.GetEndpoint(currentContext, &ecsContext); err != nil {
  46. return nil, err
  47. }
  48. return getEcsAPIService(ecsContext)
  49. }
  50. func getEcsAPIService(ecsCtx store.EcsContext) (*ecsAPIService, error) {
  51. if ecsCtx.CredentialsFromEnv {
  52. creds := getEnvVars()
  53. if !creds.HaveRequiredCredentials() {
  54. return nil, fmt.Errorf(`context requires credentials to be passed as environment variable.`)
  55. }
  56. }
  57. sess, err := session.NewSessionWithOptions(session.Options{
  58. Profile: ecsCtx.Profile,
  59. SharedConfigState: session.SharedConfigEnable,
  60. Config: aws.Config{
  61. Region: aws.String(ecsCtx.Region),
  62. },
  63. })
  64. if err != nil {
  65. return nil, err
  66. }
  67. sdk := newSDK(sess)
  68. return &ecsAPIService{
  69. ctx: ecsCtx,
  70. Region: ecsCtx.Region,
  71. aws: sdk,
  72. }, nil
  73. }
  74. type ecsAPIService struct {
  75. ctx store.EcsContext
  76. Region string
  77. aws API
  78. }
  79. func (b *ecsAPIService) ContainerService() containers.Service {
  80. return nil
  81. }
  82. func (b *ecsAPIService) ComposeService() compose.Service {
  83. return b
  84. }
  85. func (b *ecsAPIService) SecretsService() secrets.Service {
  86. return b
  87. }
  88. func (b *ecsAPIService) VolumeService() volumes.Service {
  89. return ecsVolumeService{backend: b}
  90. }
  91. func (b *ecsAPIService) ResourceService() resources.Service {
  92. return nil
  93. }
  94. func getCloudService() (cloud.Service, error) {
  95. return ecsCloudService{}, nil
  96. }
  97. type ecsCloudService struct {
  98. }
  99. func (a ecsCloudService) Login(ctx context.Context, params interface{}) error {
  100. return errdefs.ErrNotImplemented
  101. }
  102. func (a ecsCloudService) Logout(ctx context.Context) error {
  103. return errdefs.ErrNotImplemented
  104. }
  105. func (a ecsCloudService) CreateContextData(ctx context.Context, params interface{}) (interface{}, string, error) {
  106. contextHelper := newContextCreateHelper()
  107. createOpts := params.(ContextParams)
  108. return contextHelper.createContextData(ctx, createOpts)
  109. }