Skip to content

Commit

Permalink
Merge pull request #68 from bilalcaliskan/devel
Browse files Browse the repository at this point in the history
fix(transferacceleration): reduce duplicated code usage
  • Loading branch information
bilalcaliskan authored Jun 11, 2023
2 parents 0c0f1d9 + 4d98238 commit 0c5a7fe
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 111 deletions.
26 changes: 2 additions & 24 deletions cmd/transferacceleration/set/disabled/disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
},
}
)
25 changes: 1 addition & 24 deletions cmd/transferacceleration/set/enabled/enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
)
9 changes: 7 additions & 2 deletions cmd/transferacceleration/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 0 additions & 15 deletions cmd/transferacceleration/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package utils

import (
"errors"
"fmt"

"github.com/aws/aws-sdk-go/service/s3/s3iface"
rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"
options2 "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/options"
"github.com/bilalcaliskan/s3-manager/internal/logging"
"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/aws/aws-sdk-go/service/s3"
)

const (
Expand All @@ -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)
Expand Down
40 changes: 0 additions & 40 deletions cmd/transferacceleration/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
34 changes: 32 additions & 2 deletions internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"bytes"
"errors"
"fmt"
"log"
"regexp"
"strings"
Expand Down Expand Up @@ -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":
Expand All @@ -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) {
Expand Down
87 changes: 83 additions & 4 deletions internal/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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() {
Expand Down

0 comments on commit 0c5a7fe

Please sign in to comment.