network.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package amazon
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/service/ec2"
  7. "github.com/compose-spec/compose-go/types"
  8. "github.com/docker/ecs-plugin/pkg/compose"
  9. "github.com/sirupsen/logrus"
  10. )
  11. // GetDefaultVPC retrieve the default VPC for AWS account
  12. func (c client) GetDefaultVPC() (*string, error) {
  13. logrus.Debug("Retrieve default VPC")
  14. vpcs, err := c.EC2.DescribeVpcs(&ec2.DescribeVpcsInput{
  15. Filters: []*ec2.Filter{
  16. {
  17. Name: aws.String("isDefault"),
  18. Values: []*string{aws.String("true")},
  19. },
  20. },
  21. })
  22. if err != nil {
  23. return nil, err
  24. }
  25. if len(vpcs.Vpcs) == 0 {
  26. return nil, fmt.Errorf("account has not default VPC")
  27. }
  28. return vpcs.Vpcs[0].VpcId, nil
  29. }
  30. // GetSubNets retrieve default subnets for a VPC
  31. func (c client) GetSubNets(vpc *string) ([]string, error) {
  32. logrus.Debug("Retrieve SubNets")
  33. subnets, err := c.EC2.DescribeSubnets(&ec2.DescribeSubnetsInput{
  34. DryRun: nil,
  35. Filters: []*ec2.Filter{
  36. {
  37. Name: aws.String("vpc-id"),
  38. Values: []*string{vpc},
  39. },
  40. {
  41. Name: aws.String("default-for-az"),
  42. Values: []*string{aws.String("true")},
  43. },
  44. },
  45. })
  46. if err != nil {
  47. return nil, err
  48. }
  49. ids := []string{}
  50. for _, subnet := range subnets.Subnets {
  51. ids = append(ids, *subnet.SubnetId)
  52. }
  53. return ids, nil
  54. }
  55. // CreateSecurityGroup create a security group for the project
  56. func (c client) CreateSecurityGroup(project *compose.Project, vpc *string) (*string, error) {
  57. logrus.Debug("Create Security Group")
  58. name := fmt.Sprintf("%s Security Group", project.Name)
  59. securityGroup, err := c.EC2.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
  60. Description: aws.String(name),
  61. GroupName: aws.String(name),
  62. VpcId: vpc,
  63. })
  64. if err != nil {
  65. return nil, err
  66. }
  67. _, err = c.EC2.CreateTags(&ec2.CreateTagsInput{
  68. Resources: []*string{securityGroup.GroupId},
  69. Tags: []*ec2.Tag{
  70. {
  71. Key: aws.String("Name"),
  72. Value: aws.String(name),
  73. },
  74. {
  75. Key: aws.String(ProjectTag),
  76. Value: aws.String(project.Name),
  77. },
  78. },
  79. })
  80. if err != nil {
  81. return nil, err
  82. }
  83. return securityGroup.GroupId, nil
  84. }
  85. func (c *client) ExposePort(securityGroup *string, port types.ServicePortConfig) error {
  86. logrus.Debugf("Authorize ingress port %d/%s\n", port.Published, port.Protocol)
  87. _, err := c.EC2.AuthorizeSecurityGroupIngress(&ec2.AuthorizeSecurityGroupIngressInput{
  88. GroupId: securityGroup,
  89. IpPermissions: []*ec2.IpPermission{
  90. {
  91. IpProtocol: aws.String(strings.ToUpper(port.Protocol)),
  92. IpRanges: []*ec2.IpRange{
  93. {
  94. CidrIp: aws.String("0.0.0.0/0"),
  95. },
  96. },
  97. FromPort: aws.Int64(int64(port.Target)),
  98. ToPort: aws.Int64(int64(port.Target)),
  99. },
  100. },
  101. })
  102. return err
  103. }