1
0

backend.go 3.1 KB

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