Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bucket exists #46

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# These owners will be the default owners for everything in
# the repo. Unless a later match takes precedence,

* @schucly @arttor @fa-at-pulsit
* @schucly @arttor @fa-at-pulsit @aiivashchenko
66 changes: 43 additions & 23 deletions service/worker/handler/bucket_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,9 @@ func (s *svc) HandleBucketCreate(ctx context.Context, t *asynq.Task) (err error)
}

// 1. create bucket
_, err = toClient.AWS().CreateBucketWithContext(ctx, &s3.CreateBucketInput{
Bucket: &p.Bucket,
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: &p.Location,
},
})
if err != nil && (dom.ErrContains(err, "region", "location")) {
defaultRegion := toClient.Config().DefaultRegion
if defaultRegion == "" {
defaultRegion = s.clients.DefaultRegion()
}
logger.Warn().Msgf("unable to create bucket: invalid region %q: retry with default region %q", p.Location, defaultRegion)
_, err = toClient.AWS().CreateBucketWithContext(ctx, &s3.CreateBucketInput{
Bucket: &p.Bucket,
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: &defaultRegion,
},
})
}
err = s.createBucketIfNotExists(ctx, toClient, p)
if err != nil {
if !dom.ErrContains(err, "BucketAlreadyExists") {
return err
}
// else BucketAlreadyExists - ok
return err
}

// 2. copy tags
Expand Down Expand Up @@ -145,6 +124,47 @@ func (s *svc) HandleBucketCreate(ctx context.Context, t *asynq.Task) (err error)
return nil
}

func (s *svc) createBucketIfNotExists(ctx context.Context, toClient s3client.Client, p tasks.BucketCreatePayload) error {
ctx = log.WithBucket(ctx, p.Bucket)
logger := zerolog.Ctx(ctx)
// check if bucket already exists:
_, err := toClient.AWS().HeadBucketWithContext(ctx, &s3.HeadBucketInput{
Bucket: &p.Bucket,
})
if err == nil {
// already exists
return nil
}

// create bucket
_, err = toClient.AWS().CreateBucketWithContext(ctx, &s3.CreateBucketInput{
Bucket: &p.Bucket,
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: &p.Location,
},
})
if err != nil && (dom.ErrContains(err, "region", "location")) {
defaultRegion := toClient.Config().DefaultRegion
if defaultRegion == "" {
defaultRegion = s.clients.DefaultRegion()
}
logger.Warn().Msgf("unable to create bucket: invalid region %q: retry with default region %q", p.Location, defaultRegion)
_, err = toClient.AWS().CreateBucketWithContext(ctx, &s3.CreateBucketInput{
Bucket: &p.Bucket,
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: &defaultRegion,
},
})
}
if err != nil {
if !dom.ErrContains(err, "BucketAlreadyExists") {
return err
}
// else BucketAlreadyExists - ok
}
return nil
}

func (s *svc) bucketCopyLC(ctx context.Context, fromClient, toClient s3client.Client, p tasks.BucketCreatePayload) error {
logger := zerolog.Ctx(ctx)
if !features.Lifecycle(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion test/migration/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestMain(m *testing.M) {
go func() {
app := dom.AppInfo{
Version: "test",
App: "proxy",
App: "worker",
AppID: xid.New().String(),
}
workerCtx, cancelFn := context.WithCancel(ctx)
Expand Down
Loading