Pass region to create s3 bucket into

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-11-03 15:34:46 +01:00
parent 3f184f7552
commit 10a384d35b
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
4 changed files with 56 additions and 51 deletions

View File

@ -42,8 +42,8 @@ type API interface {
GetSubNets(ctx context.Context, vpcID string) ([]awsResource, error)
GetRoleArn(ctx context.Context, name string) (string, error)
StackExists(ctx context.Context, name string) (bool, error)
CreateStack(ctx context.Context, name string, template []byte) error
CreateChangeSet(ctx context.Context, name string, template []byte) (string, error)
CreateStack(ctx context.Context, name string, region string, template []byte) error
CreateChangeSet(ctx context.Context, name string, region string, template []byte) (string, error)
UpdateStack(ctx context.Context, changeset string) error
WaitStackComplete(ctx context.Context, name string, operation int) error
GetStackID(ctx context.Context, name string) (string, error)

View File

@ -66,18 +66,18 @@ func (mr *MockAPIMockRecorder) CheckVPC(arg0, arg1 interface{}) *gomock.Call {
}
// CreateChangeSet mocks base method
func (m *MockAPI) CreateChangeSet(arg0 context.Context, arg1 string, arg2 []byte) (string, error) {
func (m *MockAPI) CreateChangeSet(arg0 context.Context, arg1, arg2 string, arg3 []byte) (string, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "CreateChangeSet", arg0, arg1, arg2)
ret := m.ctrl.Call(m, "CreateChangeSet", arg0, arg1, arg2, arg3)
ret0, _ := ret[0].(string)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// CreateChangeSet indicates an expected call of CreateChangeSet
func (mr *MockAPIMockRecorder) CreateChangeSet(arg0, arg1, arg2 interface{}) *gomock.Call {
func (mr *MockAPIMockRecorder) CreateChangeSet(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateChangeSet", reflect.TypeOf((*MockAPI)(nil).CreateChangeSet), arg0, arg1, arg2)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateChangeSet", reflect.TypeOf((*MockAPI)(nil).CreateChangeSet), arg0, arg1, arg2, arg3)
}
// CreateCluster mocks base method
@ -126,17 +126,17 @@ func (mr *MockAPIMockRecorder) CreateSecret(arg0, arg1 interface{}) *gomock.Call
}
// CreateStack mocks base method
func (m *MockAPI) CreateStack(arg0 context.Context, arg1 string, arg2 []byte) error {
func (m *MockAPI) CreateStack(arg0 context.Context, arg1, arg2 string, arg3 []byte) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "CreateStack", arg0, arg1, arg2)
ret := m.ctrl.Call(m, "CreateStack", arg0, arg1, arg2, arg3)
ret0, _ := ret[0].(error)
return ret0
}
// CreateStack indicates an expected call of CreateStack
func (mr *MockAPIMockRecorder) CreateStack(arg0, arg1, arg2 interface{}) *gomock.Call {
func (mr *MockAPIMockRecorder) CreateStack(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateStack", reflect.TypeOf((*MockAPI)(nil).CreateStack), arg0, arg1, arg2)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateStack", reflect.TypeOf((*MockAPI)(nil).CreateStack), arg0, arg1, arg2, arg3)
}
// DeleteAutoscalingGroup mocks base method

View File

@ -21,11 +21,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/go-uuid"
"strings"
"time"
@ -36,6 +31,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
@ -54,11 +50,15 @@ import (
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/secretsmanager/secretsmanageriface"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@ -197,11 +197,9 @@ func (s sdk) GetSubNets(ctx context.Context, vpcID string) ([]awsResource, error
return nil, err
}
for _, subnet := range subnets.Subnets {
id := aws.StringValue(subnet.SubnetId)
logrus.Debugf("Found SubNet %s", id)
ids = append(ids, existingAWSResource{
arn: aws.StringValue(subnet.SubnetArn),
id: id,
id: aws.StringValue(subnet.SubnetId),
})
}
@ -238,10 +236,17 @@ func (s sdk) StackExists(ctx context.Context, name string) (bool, error) {
type uploadedTemplateFunc func(ctx context.Context, name string, url string) (string, error)
func (s sdk) withTemplate(ctx context.Context, name string, template []byte, fn uploadedTemplateFunc) (string, error) {
func (s sdk) withTemplate(ctx context.Context, name string, template []byte, region string, fn uploadedTemplateFunc) (string, error) {
logrus.Debug("Create s3 bucket to store cloudformation template")
var configuration *s3.CreateBucketConfiguration
if region != "us-east-1" {
configuration = &s3.CreateBucketConfiguration{
LocationConstraint: aws.String(region),
}
}
_, err := s.S3.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String("com.docker.compose"),
Bucket: aws.String("com.docker.compose." + region),
CreateBucketConfiguration: configuration,
})
if err != nil {
ae, ok := err.(awserr.Error)
@ -261,7 +266,7 @@ func (s sdk) withTemplate(ctx context.Context, name string, template []byte, fn
upload, err := s.uploader.UploadWithContext(ctx, &s3manager.UploadInput{
Key: aws.String(key),
Body: bytes.NewReader(template),
Bucket: aws.String("com.docker.compose"),
Bucket: aws.String("com.docker.compose." + region),
ContentType: aws.String("application/json"),
Tagging: aws.String(name),
})
@ -270,7 +275,7 @@ func (s sdk) withTemplate(ctx context.Context, name string, template []byte, fn
return "", err
}
defer s.S3.DeleteObjects(&s3.DeleteObjectsInput{
defer s.S3.DeleteObjects(&s3.DeleteObjectsInput{ //nolint: errcheck
Bucket: aws.String("com.docker.compose"),
Delete: &s3.Delete{
Objects: []*s3.ObjectIdentifier{
@ -285,10 +290,10 @@ func (s sdk) withTemplate(ctx context.Context, name string, template []byte, fn
return fn(ctx, name, upload.Location)
}
func (s sdk) CreateStack(ctx context.Context, name string, template []byte) error {
func (s sdk) CreateStack(ctx context.Context, name string, region string, template []byte) error {
logrus.Debug("Create CloudFormation stack")
stackId, err := s.withTemplate(ctx, name, template, func(ctx context.Context, name string, url string) (string, error) {
stackID, err := s.withTemplate(ctx, name, template, region, func(ctx context.Context, name string, url string) (string, error) {
stack, err := s.CF.CreateStackWithContext(ctx, &cloudformation.CreateStackInput{
OnFailure: aws.String("DELETE"),
StackName: aws.String(name),
@ -309,14 +314,14 @@ func (s sdk) CreateStack(ctx context.Context, name string, template []byte) erro
}
return aws.StringValue(stack.StackId), nil
})
logrus.Debugf("Stack %s created", stackId)
logrus.Debugf("Stack %s created", stackID)
return err
}
func (s sdk) CreateChangeSet(ctx context.Context, name string, template []byte) (string, error) {
func (s sdk) CreateChangeSet(ctx context.Context, name string, region string, template []byte) (string, error) {
logrus.Debug("Create CloudFormation Changeset")
changeset, err := s.withTemplate(ctx, name, template, func(ctx context.Context, name string, url string) (string, error) {
changeset, err := s.withTemplate(ctx, name, template, region, func(ctx context.Context, name string, url string) (string, error) {
update := fmt.Sprintf("Update%s", time.Now().Format("2006-01-02-15-04-05"))
changeset, err := s.CF.CreateChangeSetWithContext(ctx, &cloudformation.CreateChangeSetInput{
ChangeSetName: aws.String(update),

View File

@ -44,7 +44,7 @@ func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach b
operation := stackCreate
if update {
operation = stackUpdate
changeset, err := b.aws.CreateChangeSet(ctx, project.Name, template)
changeset, err := b.aws.CreateChangeSet(ctx, project.Name, b.Region, template)
if err != nil {
return err
}
@ -53,7 +53,7 @@ func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach b
return err
}
} else {
err = b.aws.CreateStack(ctx, project.Name, template)
err = b.aws.CreateStack(ctx, project.Name, b.Region, template)
if err != nil {
return err
}