volumes.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/volumes"
  18. "github.com/docker/compose-cli/pkg/api"
  19. "github.com/awslabs/goformation/v4/cloudformation"
  20. "github.com/awslabs/goformation/v4/cloudformation/efs"
  21. "github.com/compose-spec/compose-go/types"
  22. "github.com/pkg/errors"
  23. )
  24. func (b *ecsAPIService) createNFSMountTarget(project *types.Project, resources awsResources, template *cloudformation.Template) {
  25. for volume := range project.Volumes {
  26. for _, subnet := range resources.subnets {
  27. name := fmt.Sprintf("%sNFSMountTargetOn%s", normalizeResourceName(volume), normalizeResourceName(subnet.ID()))
  28. template.Resources[name] = &efs.MountTarget{
  29. FileSystemId: resources.filesystems[volume].ID(),
  30. SecurityGroups: resources.allSecurityGroups(),
  31. SubnetId: subnet.ID(),
  32. }
  33. }
  34. }
  35. }
  36. func (b *ecsAPIService) mountTargets(volume string, resources awsResources) []string {
  37. var refs []string
  38. for _, subnet := range resources.subnets {
  39. refs = append(refs, fmt.Sprintf("%sNFSMountTargetOn%s", normalizeResourceName(volume), normalizeResourceName(subnet.ID())))
  40. }
  41. return refs
  42. }
  43. func (b *ecsAPIService) createAccessPoints(project *types.Project, r awsResources, template *cloudformation.Template) {
  44. for name, volume := range project.Volumes {
  45. n := fmt.Sprintf("%sAccessPoint", normalizeResourceName(name))
  46. uid := volume.DriverOpts["uid"]
  47. gid := volume.DriverOpts["gid"]
  48. permissions := volume.DriverOpts["permissions"]
  49. path := volume.DriverOpts["root_directory"]
  50. ap := efs.AccessPoint{
  51. AccessPointTags: []efs.AccessPoint_AccessPointTag{
  52. {
  53. Key: api.ProjectLabel,
  54. Value: project.Name,
  55. },
  56. {
  57. Key: api.VolumeLabel,
  58. Value: name,
  59. },
  60. {
  61. Key: "Name",
  62. Value: volume.Name,
  63. },
  64. },
  65. FileSystemId: r.filesystems[name].ID(),
  66. }
  67. if uid != "" {
  68. ap.PosixUser = &efs.AccessPoint_PosixUser{
  69. Uid: uid,
  70. Gid: gid,
  71. }
  72. }
  73. if path != "" {
  74. root := efs.AccessPoint_RootDirectory{
  75. Path: path,
  76. }
  77. ap.RootDirectory = &root
  78. if uid != "" {
  79. root.CreationInfo = &efs.AccessPoint_CreationInfo{
  80. OwnerUid: uid,
  81. OwnerGid: gid,
  82. Permissions: permissions,
  83. }
  84. }
  85. }
  86. template.Resources[n] = &ap
  87. }
  88. }
  89. // VolumeCreateOptions hold EFS filesystem creation options
  90. type VolumeCreateOptions struct {
  91. KmsKeyID string
  92. PerformanceMode string
  93. ProvisionedThroughputInMibps float64
  94. ThroughputMode string
  95. }
  96. type ecsVolumeService struct {
  97. backend *ecsAPIService
  98. }
  99. func (e ecsVolumeService) List(ctx context.Context) ([]volumes.Volume, error) {
  100. filesystems, err := e.backend.aws.ListFileSystems(ctx, nil)
  101. if err != nil {
  102. return nil, err
  103. }
  104. var vol []volumes.Volume
  105. for _, fs := range filesystems {
  106. vol = append(vol, volumes.Volume{
  107. ID: fs.ID(),
  108. Description: fs.ARN(),
  109. })
  110. }
  111. return vol, nil
  112. }
  113. func (e ecsVolumeService) Create(ctx context.Context, name string, options interface{}) (volumes.Volume, error) {
  114. fs, err := e.backend.aws.CreateFileSystem(ctx, map[string]string{
  115. "Name": name,
  116. }, options.(VolumeCreateOptions))
  117. return volumes.Volume{
  118. ID: fs.ID(),
  119. Description: fs.ARN(),
  120. }, err
  121. }
  122. func (e ecsVolumeService) Delete(ctx context.Context, volumeID string, options interface{}) error {
  123. return e.backend.aws.DeleteFileSystem(ctx, volumeID)
  124. }
  125. func (e ecsVolumeService) Inspect(ctx context.Context, volumeID string) (volumes.Volume, error) {
  126. ok, err := e.backend.aws.ResolveFileSystem(ctx, volumeID)
  127. if ok == nil {
  128. err = errors.Wrapf(api.ErrNotFound, "filesystem %q does not exists", volumeID)
  129. }
  130. return volumes.Volume{
  131. ID: volumeID,
  132. Description: ok.ARN(),
  133. }, err
  134. }