diff --git a/cmd/transferacceleration/set/disabled/disabled.go b/cmd/transferacceleration/set/disabled/disabled.go index c0594f9..0d51f4c 100644 --- a/cmd/transferacceleration/set/disabled/disabled.go +++ b/cmd/transferacceleration/set/disabled/disabled.go @@ -3,9 +3,9 @@ package disabled import ( options2 "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/options" "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/utils" + "github.com/bilalcaliskan/s3-manager/internal/aws" "github.com/aws/aws-sdk-go/service/s3/s3iface" - "github.com/bilalcaliskan/s3-manager/internal/aws" "github.com/rs/zerolog" "github.com/spf13/cobra" ) @@ -33,30 +33,8 @@ var ( } transferAccelerationOpts.DesiredState = "disabled" - res, err := aws.GetTransferAcceleration(svc, transferAccelerationOpts) - if err != nil { - logger.Error().Msg(err.Error()) - return err - } - - if err := utils.DecideActualState(res, transferAccelerationOpts); err != nil { - logger.Error().Msg(err.Error()) - return err - } - - if transferAccelerationOpts.DesiredState == transferAccelerationOpts.ActualState { - logger.Warn().Msg("transferr acceleration configuration is already at desired state") - return nil - } - - if _, err := aws.SetTransferAcceleration(svc, transferAccelerationOpts); err != nil { - logger.Error().Msg(err.Error()) - return err - } - - logger.Info().Msg("successfully set transfer acceleration as disabled") - return nil + return aws.SetTransferAcceleration(svc, transferAccelerationOpts, logger) }, } ) diff --git a/cmd/transferacceleration/set/enabled/enabled.go b/cmd/transferacceleration/set/enabled/enabled.go index 69787f7..0edee41 100644 --- a/cmd/transferacceleration/set/enabled/enabled.go +++ b/cmd/transferacceleration/set/enabled/enabled.go @@ -33,30 +33,7 @@ var ( transferAccelerationOpts.DesiredState = "enabled" - res, err := aws.GetTransferAcceleration(svc, transferAccelerationOpts) - if err != nil { - logger.Error().Msg(err.Error()) - return err - } - - if err := utils.DecideActualState(res, transferAccelerationOpts); err != nil { - logger.Error().Msg(err.Error()) - return err - } - - if transferAccelerationOpts.DesiredState == transferAccelerationOpts.ActualState { - logger.Warn().Msg("transferr acceleration configuration is already at desired state") - return nil - } - - if _, err := aws.SetTransferAcceleration(svc, transferAccelerationOpts); err != nil { - logger.Error().Msg(err.Error()) - return err - } - - logger.Info().Msg("successfully set transfer acceleration as enabled") - - return nil + return aws.SetTransferAcceleration(svc, transferAccelerationOpts, logger) }, } ) diff --git a/cmd/transferacceleration/show/show.go b/cmd/transferacceleration/show/show.go index 3dd978c..c922e7e 100644 --- a/cmd/transferacceleration/show/show.go +++ b/cmd/transferacceleration/show/show.go @@ -2,8 +2,8 @@ package show import ( "errors" + "fmt" - "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/utils" "github.com/bilalcaliskan/s3-manager/internal/aws" "github.com/aws/aws-sdk-go/service/s3/s3iface" @@ -52,7 +52,12 @@ var ( return err } - if err := utils.DecideActualState(res, transferAccelerationOpts); err != nil { + if *res.Status == "Enabled" { + transferAccelerationOpts.ActualState = "enabled" + } else if *res.Status == "Suspended" { + transferAccelerationOpts.ActualState = "disabled" + } else { + err := fmt.Errorf("unknown status '%s' returned from AWS SDK", transferAccelerationOpts.ActualState) logger.Error().Msg(err.Error()) return err } diff --git a/cmd/transferacceleration/utils/utils.go b/cmd/transferacceleration/utils/utils.go index 68370f2..145cc3f 100644 --- a/cmd/transferacceleration/utils/utils.go +++ b/cmd/transferacceleration/utils/utils.go @@ -2,7 +2,6 @@ package utils import ( "errors" - "fmt" "github.com/aws/aws-sdk-go/service/s3/s3iface" rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options" @@ -10,8 +9,6 @@ import ( "github.com/bilalcaliskan/s3-manager/internal/logging" "github.com/rs/zerolog" "github.com/spf13/cobra" - - "github.com/aws/aws-sdk-go/service/s3" ) const ( @@ -27,18 +24,6 @@ func CheckArgs(args []string) error { return nil } -func DecideActualState(res *s3.GetBucketAccelerateConfigurationOutput, opts *options2.TransferAccelerationOptions) error { - if *res.Status == "Enabled" { - opts.ActualState = "enabled" - } else if *res.Status == "Suspended" { - opts.ActualState = "disabled" - } else { - return fmt.Errorf(ErrUnknownStatus, opts.ActualState) - } - - return nil -} - func PrepareConstants(cmd *cobra.Command, transferAccelerationOpts *options2.TransferAccelerationOptions) (s3iface.S3API, *options2.TransferAccelerationOptions, zerolog.Logger) { svc := cmd.Context().Value(rootopts.S3SvcKey{}).(s3iface.S3API) rootOpts := cmd.Context().Value(rootopts.OptsKey{}).(*rootopts.RootOptions) diff --git a/cmd/transferacceleration/utils/utils_test.go b/cmd/transferacceleration/utils/utils_test.go index 0e04123..002aa41 100644 --- a/cmd/transferacceleration/utils/utils_test.go +++ b/cmd/transferacceleration/utils/utils_test.go @@ -4,7 +4,6 @@ import ( "context" "testing" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3iface" "github.com/bilalcaliskan/s3-manager/cmd/root/options" @@ -30,45 +29,6 @@ func TestCheckArgsFailure(t *testing.T) { assert.NotNil(t, err) } -func TestDecideActualStateEnabled(t *testing.T) { - res := &s3.GetBucketAccelerateConfigurationOutput{ - Status: aws.String("Enabled"), - } - - rootOpts := options.GetRootOptions() - opts := options3.GetTransferAccelerationOptions() - opts.RootOptions = rootOpts - - err := DecideActualState(res, opts) - assert.Nil(t, err) -} - -func TestDecideActualStateEnabled2(t *testing.T) { - res := &s3.GetBucketAccelerateConfigurationOutput{ - Status: aws.String("Suspended"), - } - - rootOpts := options.GetRootOptions() - opts := options3.GetTransferAccelerationOptions() - opts.RootOptions = rootOpts - - err := DecideActualState(res, opts) - assert.Nil(t, err) -} - -func TestDecideActualStateFailure(t *testing.T) { - res := &s3.GetBucketAccelerateConfigurationOutput{ - Status: aws.String("Suspendedddd"), - } - - rootOpts := options.GetRootOptions() - opts := options3.GetTransferAccelerationOptions() - opts.RootOptions = rootOpts - - err := DecideActualState(res, opts) - assert.NotNil(t, err) -} - func TestPrepareConstants(t *testing.T) { var ( svc s3iface.S3API diff --git a/internal/aws/aws.go b/internal/aws/aws.go index 534f250..067fb4e 100644 --- a/internal/aws/aws.go +++ b/internal/aws/aws.go @@ -3,6 +3,7 @@ package aws import ( "bytes" "errors" + "fmt" "log" "regexp" "strings" @@ -116,7 +117,28 @@ func GetTransferAcceleration(svc s3iface.S3API, opts *options6.TransferAccelerat }) } -func SetTransferAcceleration(svc s3iface.S3API, opts *options6.TransferAccelerationOptions) (res *s3.PutBucketAccelerateConfigurationOutput, err error) { +func SetTransferAcceleration(svc s3iface.S3API, opts *options6.TransferAccelerationOptions, logger zerolog.Logger) error { + res, err := GetTransferAcceleration(svc, opts) + if err != nil { + logger.Error().Msg(err.Error()) + return err + } + + if *res.Status == "Enabled" { + opts.ActualState = "enabled" + } else if *res.Status == "Suspended" { + opts.ActualState = "disabled" + } else { + err := fmt.Errorf("unknown status '%s' returned from AWS SDK", opts.ActualState) + logger.Error().Msg(err.Error()) + return err + } + + if opts.DesiredState == opts.ActualState { + logger.Warn().Msg("transfer acceleration configuration is already at desired state") + return nil + } + var status string switch opts.DesiredState { case "enabled": @@ -125,10 +147,18 @@ func SetTransferAcceleration(svc s3iface.S3API, opts *options6.TransferAccelerat status = "Suspended" } - return svc.PutBucketAccelerateConfiguration(&s3.PutBucketAccelerateConfigurationInput{ + _, err = svc.PutBucketAccelerateConfiguration(&s3.PutBucketAccelerateConfigurationInput{ Bucket: aws.String(opts.BucketName), AccelerateConfiguration: &s3.AccelerateConfiguration{Status: aws.String(status)}, }) + if err != nil { + logger.Error().Msg(err.Error()) + return err + } + + logger.Info().Msgf("successfully set transfer acceleration as %s", opts.DesiredState) + + return nil } func GetBucketPolicy(svc s3iface.S3API, opts *options5.BucketPolicyOptions) (res *s3.GetBucketPolicyOutput, err error) { diff --git a/internal/aws/aws_test.go b/internal/aws/aws_test.go index f8ff80b..4a28bfa 100644 --- a/internal/aws/aws_test.go +++ b/internal/aws/aws_test.go @@ -573,9 +573,11 @@ func TestSetTransferAcceleration(t *testing.T) { taOpts.RootOptions = rootOpts taOpts.DesiredState = "enabled" + + defaultGetBucketAccelerationErr = nil defaultPutBucketAccelerationErr = nil - res, err := SetTransferAcceleration(&mockS3Client{}, taOpts) - assert.NotNil(t, res) + + err := SetTransferAcceleration(&mockS3Client{}, taOpts, logging.GetLogger(taOpts.RootOptions)) assert.Nil(t, err) } @@ -589,12 +591,89 @@ func TestSetTransferAcceleration2(t *testing.T) { taOpts.RootOptions = rootOpts taOpts.DesiredState = "disabled" + + defaultGetBucketAccelerationErr = nil defaultPutBucketAccelerationErr = nil - res, err := SetTransferAcceleration(&mockS3Client{}, taOpts) - assert.NotNil(t, res) + + err := SetTransferAcceleration(&mockS3Client{}, taOpts, logging.GetLogger(taOpts.RootOptions)) + assert.Nil(t, err) +} + +func TestSetTransferAcceleration3(t *testing.T) { + taOpts := options5.GetTransferAccelerationOptions() + defer func() { + taOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + taOpts.RootOptions = rootOpts + + taOpts.DesiredState = "disabled" + + defaultGetBucketAccelerationErr = errors.New("dummy error") + defaultPutBucketAccelerationErr = nil + + err := SetTransferAcceleration(&mockS3Client{}, taOpts, logging.GetLogger(taOpts.RootOptions)) + assert.NotNil(t, err) +} + +func TestSetTransferAcceleration4(t *testing.T) { + taOpts := options5.GetTransferAccelerationOptions() + defer func() { + taOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + taOpts.RootOptions = rootOpts + + taOpts.DesiredState = "enabled" + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + defaultPutBucketAccelerationErr = nil + + err := SetTransferAcceleration(&mockS3Client{}, taOpts, logging.GetLogger(taOpts.RootOptions)) assert.Nil(t, err) } +func TestSetTransferAcceleration5(t *testing.T) { + taOpts := options5.GetTransferAccelerationOptions() + defer func() { + taOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + taOpts.RootOptions = rootOpts + + taOpts.DesiredState = "enabled" + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Suspendedddd") + defaultPutBucketAccelerationErr = nil + + err := SetTransferAcceleration(&mockS3Client{}, taOpts, logging.GetLogger(taOpts.RootOptions)) + assert.NotNil(t, err) +} + +func TestSetTransferAcceleration6(t *testing.T) { + taOpts := options5.GetTransferAccelerationOptions() + defer func() { + taOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + taOpts.RootOptions = rootOpts + + taOpts.DesiredState = "enabled" + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + defaultPutBucketAccelerationErr = errors.New("dummy error") + + err := SetTransferAcceleration(&mockS3Client{}, taOpts, logging.GetLogger(taOpts.RootOptions)) + assert.NotNil(t, err) +} + func TestGetBucketPolicy(t *testing.T) { bpOpts := options6.GetBucketPolicyOptions() defer func() {