diff --git a/TODOLIST.md b/TODOLIST.md index f107419..8c5eb1c 100644 --- a/TODOLIST.md +++ b/TODOLIST.md @@ -1,11 +1,13 @@ - [x] versioning - [x] search/file - [x] tags -- [NO] storageclass -- [NO] encryption -- [ ] search/text (all files, single file etc.) -- [ ] bucketpolicy -- [ ] objectlock +- [x] search/text (all files, single file etc.) +- [x] bucketpolicy +- [x] transferacceleration +- [ ] mfadelete - [ ] re-enable prompt/select logic - [ ] fix code smells - [ ] fix duplications +- [ ] ~~storageclass~~ +- [ ] ~~encryption~~ +- [ ] ~~objectlock~~ diff --git a/cmd/root/root.go b/cmd/root/root.go index 88f103a..aec51fa 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration" + "github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy" "github.com/bilalcaliskan/s3-manager/cmd/tags" @@ -36,6 +38,7 @@ func init() { rootCmd.AddCommand(versioning.VersioningCmd) rootCmd.AddCommand(tags.TagsCmd) rootCmd.AddCommand(bucketpolicy.BucketPolicyCmd) + rootCmd.AddCommand(transferacceleration.TransferAccelerationCmd) } var ( diff --git a/cmd/transferacceleration/options/options.go b/cmd/transferacceleration/options/options.go new file mode 100644 index 0000000..25cae24 --- /dev/null +++ b/cmd/transferacceleration/options/options.go @@ -0,0 +1,50 @@ +package options + +import ( + "github.com/bilalcaliskan/s3-manager/cmd/root/options" +) + +type TransferAccelerationOptsKey struct{} + +var ( + //substringRunner prompt.PromptRunner = prompt.GetPromptRunner("Provide text to search", nil) + //extensionRunner prompt.PromptRunner = prompt.GetPromptRunner("Provide target file extensions (comma seperated)", nil) + transferAccelerationOpts = &TransferAccelerationOptions{} +) + +// TransferAccelerationOptions contains frequent command line and application options. +type TransferAccelerationOptions struct { + // ActualState is state + ActualState string + // DesiredState is state + DesiredState string + *options.RootOptions +} + +// GetTransferAccelerationOptions returns the pointer of FindOptions +func GetTransferAccelerationOptions() *TransferAccelerationOptions { + return transferAccelerationOpts +} + +func (opts *TransferAccelerationOptions) SetZeroValues() { + opts.ActualState = "Enabled" + opts.DesiredState = "enabled" + opts.RootOptions.SetZeroValues() +} + +/*func (opts *ConfigureOptions) PromptInteractiveValues() error { + res, err := substringRunner.Run() + if err != nil { + return err + } + opts.Foo = res + + res, err = extensionRunner.Run() + if err != nil { + return err + } + opts.FileExtensions = res + + return nil +} +*/ diff --git a/cmd/transferacceleration/options/options_test.go b/cmd/transferacceleration/options/options_test.go new file mode 100644 index 0000000..01ab1bd --- /dev/null +++ b/cmd/transferacceleration/options/options_test.go @@ -0,0 +1,23 @@ +package options + +import ( + "testing" + + "github.com/bilalcaliskan/s3-manager/cmd/root/options" + + "github.com/stretchr/testify/assert" +) + +func TestGetTransferAccelerationOptions(t *testing.T) { + opts := GetTransferAccelerationOptions() + assert.NotNil(t, opts) +} + +func TestTransferAccelerationOptions_SetZeroValues(t *testing.T) { + rootOpts := options.GetRootOptions() + opts := GetTransferAccelerationOptions() + opts.RootOptions = rootOpts + assert.NotNil(t, opts) + + opts.SetZeroValues() +} diff --git a/cmd/transferacceleration/set/disabled/disabled.go b/cmd/transferacceleration/set/disabled/disabled.go new file mode 100644 index 0000000..c0594f9 --- /dev/null +++ b/cmd/transferacceleration/set/disabled/disabled.go @@ -0,0 +1,62 @@ +package disabled + +import ( + options2 "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/options" + "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/utils" + + "github.com/aws/aws-sdk-go/service/s3/s3iface" + "github.com/bilalcaliskan/s3-manager/internal/aws" + "github.com/rs/zerolog" + "github.com/spf13/cobra" +) + +func init() { + transferAccelerationOpts = options2.GetTransferAccelerationOptions() +} + +var ( + svc s3iface.S3API + logger zerolog.Logger + transferAccelerationOpts *options2.TransferAccelerationOptions + DisabledCmd = &cobra.Command{ + Use: "disabled", + Short: "", + SilenceUsage: true, + SilenceErrors: true, + RunE: func(cmd *cobra.Command, args []string) error { + svc, transferAccelerationOpts, logger = utils.PrepareConstants(cmd, transferAccelerationOpts) + + if err := utils.CheckArgs(args); err != nil { + logger.Error(). + Msg(err.Error()) + return err + } + + 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 + }, + } +) diff --git a/cmd/transferacceleration/set/disabled/disabled_test.go b/cmd/transferacceleration/set/disabled/disabled_test.go new file mode 100644 index 0000000..c689614 --- /dev/null +++ b/cmd/transferacceleration/set/disabled/disabled_test.go @@ -0,0 +1,292 @@ +package disabled + +import ( + "context" + "errors" + "testing" + + "github.com/bilalcaliskan/s3-manager/cmd/versioning/set/utils" + + "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" + internalaws "github.com/bilalcaliskan/s3-manager/internal/aws" + "github.com/stretchr/testify/assert" +) + +var ( + defaultGetBucketAccelerationOutput = &s3.GetBucketAccelerateConfigurationOutput{ + Status: aws.String("Enabled"), + } + defaultGetBucketAccelerationErr error + defaultPutBucketAccelerationOutput = &s3.PutBucketAccelerateConfigurationOutput{} + defaultPutBucketAccelerationErr error +) + +func createSvc(rootOpts *options.RootOptions) (*s3.S3, error) { + return internalaws.CreateAwsService(rootOpts) +} + +// Define a mock struct to be used in your unit tests +type mockS3Client struct { + s3iface.S3API +} + +func (m *mockS3Client) GetBucketAccelerateConfiguration(input *s3.GetBucketAccelerateConfigurationInput) (*s3.GetBucketAccelerateConfigurationOutput, error) { + return defaultGetBucketAccelerationOutput, defaultGetBucketAccelerationErr +} + +func (m *mockS3Client) PutBucketAccelerateConfiguration(input *s3.PutBucketAccelerateConfigurationInput) (*s3.PutBucketAccelerateConfigurationOutput, error) { + return defaultPutBucketAccelerationOutput, defaultPutBucketAccelerationErr +} + +func TestExecuteTooManyArguments(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + DisabledCmd.SetContext(ctx) + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) + + args := []string{"enabled"} + DisabledCmd.SetArgs(args) + + err = DisabledCmd.Execute() + assert.NotNil(t, err) + assert.Equal(t, utils.ErrTooManyArguments, err.Error()) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +/*func TestExecuteWrongArguments(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + DisabledCmd.SetContext(ctx) + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) + + args := []string{"eeenabled"} + DisabledCmd.SetArgs(args) + + err = DisabledCmd.Execute() + assert.NotNil(t, err) + assert.Equal(t, ErrWrongArgumentProvided, err.Error()) + + rootOpts.SetZeroValues() + versioningOpts.SetZeroValues() +}*/ + +/* + func TestExecuteNoArgument(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + DisabledCmd.SetContext(ctx) + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) + + DisabledCmd.SetArgs([]string{}) + err = DisabledCmd.Execute() + assert.NotNil(t, err) + assert.Equal(t, ErrNoArgument, err.Error()) + + rootOpts.SetZeroValues() + versioningOpts.SetZeroValues() + } +*/ +func TestExecuteSuccessAlreadyDisabled(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + DisabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + defaultPutBucketAccelerationErr = nil + + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) + + DisabledCmd.SetArgs([]string{}) + err := DisabledCmd.Execute() + assert.Nil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteSuccess(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + DisabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Enabled") + defaultPutBucketAccelerationErr = nil + + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) + + DisabledCmd.SetArgs([]string{}) + err := DisabledCmd.Execute() + assert.Nil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteGetBucketAccelerationErr(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + DisabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = errors.New("dummy error") + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + defaultPutBucketAccelerationErr = nil + + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) + + DisabledCmd.SetArgs([]string{}) + err := DisabledCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteSetBucketAccelerationErr(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + DisabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Enabled") + defaultPutBucketAccelerationErr = errors.New("dummy error") + + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) + + DisabledCmd.SetArgs([]string{}) + err := DisabledCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteUnknownErr(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + DisabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Enableddd") + defaultPutBucketAccelerationErr = errors.New("dummy error") + + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) + + DisabledCmd.SetArgs([]string{}) + err := DisabledCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +/* +func TestExecuteSuccessEnabledWrongVersioning(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + DisabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + defaultPutBucketAccelerationErr = nil + + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) + DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) + + DisabledCmd.SetArgs([]string{}) + err := DisabledCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} +*/ diff --git a/cmd/transferacceleration/set/enabled/enabled.go b/cmd/transferacceleration/set/enabled/enabled.go new file mode 100644 index 0000000..69787f7 --- /dev/null +++ b/cmd/transferacceleration/set/enabled/enabled.go @@ -0,0 +1,62 @@ +package enabled + +import ( + "github.com/aws/aws-sdk-go/service/s3/s3iface" + "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/rs/zerolog" + "github.com/spf13/cobra" +) + +func init() { + transferAccelerationOpts = options.GetTransferAccelerationOptions() +} + +var ( + svc s3iface.S3API + logger zerolog.Logger + transferAccelerationOpts *options.TransferAccelerationOptions + EnabledCmd = &cobra.Command{ + Use: "enabled", + Short: "", + SilenceUsage: true, + SilenceErrors: true, + RunE: func(cmd *cobra.Command, args []string) error { + svc, transferAccelerationOpts, logger = utils.PrepareConstants(cmd, transferAccelerationOpts) + + if err := utils.CheckArgs(args); err != nil { + logger.Error(). + Msg(err.Error()) + return err + } + + 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 + }, + } +) diff --git a/cmd/transferacceleration/set/enabled/enabled_test.go b/cmd/transferacceleration/set/enabled/enabled_test.go new file mode 100644 index 0000000..f47b7cc --- /dev/null +++ b/cmd/transferacceleration/set/enabled/enabled_test.go @@ -0,0 +1,261 @@ +package enabled + +import ( + "context" + "errors" + "testing" + + "github.com/bilalcaliskan/s3-manager/cmd/versioning/set/utils" + + "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" + internalaws "github.com/bilalcaliskan/s3-manager/internal/aws" + "github.com/stretchr/testify/assert" +) + +var ( + defaultGetBucketAccelerationOutput = &s3.GetBucketAccelerateConfigurationOutput{ + Status: aws.String("Enabled"), + } + defaultGetBucketAccelerationErr error + defaultPutBucketAccelerationOutput = &s3.PutBucketAccelerateConfigurationOutput{} + defaultPutBucketAccelerationErr error +) + +func createSvc(rootOpts *options.RootOptions) (*s3.S3, error) { + return internalaws.CreateAwsService(rootOpts) +} + +// Define a mock struct to be used in your unit tests +type mockS3Client struct { + s3iface.S3API +} + +func (m *mockS3Client) GetBucketAccelerateConfiguration(input *s3.GetBucketAccelerateConfigurationInput) (*s3.GetBucketAccelerateConfigurationOutput, error) { + return defaultGetBucketAccelerationOutput, defaultGetBucketAccelerationErr +} + +func (m *mockS3Client) PutBucketAccelerateConfiguration(input *s3.PutBucketAccelerateConfigurationInput) (*s3.PutBucketAccelerateConfigurationOutput, error) { + return defaultPutBucketAccelerationOutput, defaultPutBucketAccelerationErr +} + +func TestExecuteTooManyArguments(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + EnabledCmd.SetContext(ctx) + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.S3SvcKey{}, svc)) + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.OptsKey{}, rootOpts)) + + args := []string{"enabled"} + EnabledCmd.SetArgs(args) + + err = EnabledCmd.Execute() + assert.NotNil(t, err) + assert.Equal(t, utils.ErrTooManyArguments, err.Error()) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +/*func TestExecuteWrongArguments(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + EnabledCmd.SetContext(ctx) + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.S3SvcKey{}, svc)) + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.OptsKey{}, rootOpts)) + + args := []string{"eeenabled"} + EnabledCmd.SetArgs(args) + + err = EnabledCmd.Execute() + assert.NotNil(t, err) + assert.Equal(t, ErrWrongArgumentProvided, err.Error()) + + rootOpts.SetZeroValues() + versioningOpts.SetZeroValues() +}*/ + +/* + func TestExecuteNoArgument(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + EnabledCmd.SetContext(ctx) + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.S3SvcKey{}, svc)) + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.OptsKey{}, rootOpts)) + + EnabledCmd.SetArgs([]string{}) + err = EnabledCmd.Execute() + assert.NotNil(t, err) + assert.Equal(t, ErrNoArgument, err.Error()) + + rootOpts.SetZeroValues() + versioningOpts.SetZeroValues() + } +*/ +func TestExecuteSuccessAlreadyEnabled(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + EnabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Enabled") + defaultPutBucketAccelerationErr = nil + + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.S3SvcKey{}, svc)) + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.OptsKey{}, rootOpts)) + + EnabledCmd.SetArgs([]string{}) + err := EnabledCmd.Execute() + assert.Nil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteSuccess(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + EnabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + defaultPutBucketAccelerationErr = nil + + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.S3SvcKey{}, svc)) + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.OptsKey{}, rootOpts)) + + EnabledCmd.SetArgs([]string{}) + err := EnabledCmd.Execute() + assert.Nil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteGetBucketAccelerationErr(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + EnabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = errors.New("dummy error") + defaultPutBucketAccelerationErr = nil + + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.S3SvcKey{}, svc)) + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.OptsKey{}, rootOpts)) + + EnabledCmd.SetArgs([]string{}) + err := EnabledCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteSetBucketAccelerationErr(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + EnabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + defaultPutBucketAccelerationErr = errors.New("dummy error") + + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.S3SvcKey{}, svc)) + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.OptsKey{}, rootOpts)) + + EnabledCmd.SetArgs([]string{}) + err := EnabledCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteUnknownErr(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + EnabledCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Enableddd") + defaultPutBucketAccelerationErr = errors.New("dummy error") + + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.S3SvcKey{}, svc)) + EnabledCmd.SetContext(context.WithValue(EnabledCmd.Context(), options.OptsKey{}, rootOpts)) + + EnabledCmd.SetArgs([]string{}) + err := EnabledCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} diff --git a/cmd/transferacceleration/set/set.go b/cmd/transferacceleration/set/set.go new file mode 100644 index 0000000..4580d9a --- /dev/null +++ b/cmd/transferacceleration/set/set.go @@ -0,0 +1,21 @@ +package set + +import ( + "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/set/disabled" + "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/set/enabled" + "github.com/spf13/cobra" +) + +func init() { + SetCmd.AddCommand(enabled.EnabledCmd) + SetCmd.AddCommand(disabled.DisabledCmd) +} + +var ( + SetCmd = &cobra.Command{ + Use: "set", + Short: "", + SilenceUsage: true, + SilenceErrors: true, + } +) diff --git a/cmd/transferacceleration/set/set_test.go b/cmd/transferacceleration/set/set_test.go new file mode 100644 index 0000000..26eb414 --- /dev/null +++ b/cmd/transferacceleration/set/set_test.go @@ -0,0 +1,11 @@ +package set + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSetCmd(t *testing.T) { + assert.NotNil(t, SetCmd) +} diff --git a/cmd/transferacceleration/show/show.go b/cmd/transferacceleration/show/show.go new file mode 100644 index 0000000..3dd978c --- /dev/null +++ b/cmd/transferacceleration/show/show.go @@ -0,0 +1,65 @@ +package show + +import ( + "errors" + + "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/utils" + "github.com/bilalcaliskan/s3-manager/internal/aws" + + "github.com/aws/aws-sdk-go/service/s3/s3iface" + rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options" + "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/options" + "github.com/bilalcaliskan/s3-manager/internal/logging" + "github.com/rs/zerolog" + "github.com/spf13/cobra" +) + +func init() { + transferAccelerationOpts = options.GetTransferAccelerationOptions() +} + +var ( + svc s3iface.S3API + logger zerolog.Logger + transferAccelerationOpts *options.TransferAccelerationOptions + ShowCmd = &cobra.Command{ + Use: "show", + Short: "", + SilenceUsage: true, + SilenceErrors: true, + RunE: func(cmd *cobra.Command, args []string) (err error) { + rootOpts := cmd.Context().Value(rootopts.OptsKey{}).(*rootopts.RootOptions) + svc = cmd.Context().Value(rootopts.S3SvcKey{}).(s3iface.S3API) + + transferAccelerationOpts.RootOptions = rootOpts + logger = logging.GetLogger(rootOpts) + + if len(args) > 0 { + err = errors.New("too many arguments provided") + logger.Error(). + Msg(err.Error()) + return err + } + + /*ta, err := aws.GetTransferAcceleration(svc, transferAccelerationOpts) + if err != nil { + return err + }*/ + + 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 + } + + logger.Info().Msgf("current transfer acceleration configuration is %s", transferAccelerationOpts.ActualState) + + return nil + }, + } +) diff --git a/cmd/transferacceleration/show/show_test.go b/cmd/transferacceleration/show/show_test.go new file mode 100644 index 0000000..777e11d --- /dev/null +++ b/cmd/transferacceleration/show/show_test.go @@ -0,0 +1,251 @@ +package show + +import ( + "context" + "errors" + "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" + internalaws "github.com/bilalcaliskan/s3-manager/internal/aws" + "github.com/stretchr/testify/assert" +) + +var ( + defaultGetBucketAccelerationOutput = &s3.GetBucketAccelerateConfigurationOutput{ + Status: aws.String("Enabled"), + } + defaultGetBucketAccelerationErr error +) + +func createSvc(rootOpts *options.RootOptions) (*s3.S3, error) { + return internalaws.CreateAwsService(rootOpts) +} + +// Define a mock struct to be used in your unit tests +type mockS3Client struct { + s3iface.S3API +} + +func (m *mockS3Client) GetBucketAccelerateConfiguration(input *s3.GetBucketAccelerateConfigurationInput) (*s3.GetBucketAccelerateConfigurationOutput, error) { + return defaultGetBucketAccelerationOutput, defaultGetBucketAccelerationErr +} + +func TestExecuteTooManyArguments(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + ShowCmd.SetContext(ctx) + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.S3SvcKey{}, svc)) + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.OptsKey{}, rootOpts)) + + args := []string{"enabled"} + ShowCmd.SetArgs(args) + + err = ShowCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +/*func TestExecuteWrongArguments(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + ShowCmd.SetContext(ctx) + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.S3SvcKey{}, svc)) + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.OptsKey{}, rootOpts)) + + args := []string{"eeenabled"} + ShowCmd.SetArgs(args) + + err = ShowCmd.Execute() + assert.NotNil(t, err) + assert.Equal(t, ErrWrongArgumentProvided, err.Error()) + + rootOpts.SetZeroValues() + versioningOpts.SetZeroValues() +}*/ + +/* + func TestExecuteNoArgument(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + ShowCmd.SetContext(ctx) + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.S3SvcKey{}, svc)) + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.OptsKey{}, rootOpts)) + + ShowCmd.SetArgs([]string{}) + err = ShowCmd.Execute() + assert.NotNil(t, err) + assert.Equal(t, ErrNoArgument, err.Error()) + + rootOpts.SetZeroValues() + versioningOpts.SetZeroValues() + } +*/ +func TestExecuteSuccessAlreadyDisabled(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + ShowCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.S3SvcKey{}, svc)) + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.OptsKey{}, rootOpts)) + + ShowCmd.SetArgs([]string{}) + err := ShowCmd.Execute() + assert.Nil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteSuccess(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + ShowCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Enabled") + + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.S3SvcKey{}, svc)) + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.OptsKey{}, rootOpts)) + + ShowCmd.SetArgs([]string{}) + err := ShowCmd.Execute() + assert.Nil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteGetBucketAccelerationErr(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + ShowCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = errors.New("dummy error") + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.S3SvcKey{}, svc)) + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.OptsKey{}, rootOpts)) + + ShowCmd.SetArgs([]string{}) + err := ShowCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +func TestExecuteUnknownErr(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + ShowCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Enableddd") + + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.S3SvcKey{}, svc)) + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.OptsKey{}, rootOpts)) + + ShowCmd.SetArgs([]string{}) + err := ShowCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} + +/* +func TestExecuteSuccessEnabledWrongVersioning(t *testing.T) { + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + ctx := context.Background() + ShowCmd.SetContext(ctx) + + mockSvc := &mockS3Client{} + svc = mockSvc + + defaultGetBucketAccelerationErr = nil + defaultGetBucketAccelerationOutput.Status = aws.String("Suspended") + defaultPutBucketAccelerationErr = nil + + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.S3SvcKey{}, svc)) + ShowCmd.SetContext(context.WithValue(ShowCmd.Context(), options.OptsKey{}, rootOpts)) + + ShowCmd.SetArgs([]string{}) + err := ShowCmd.Execute() + assert.NotNil(t, err) + + rootOpts.SetZeroValues() + transferAccelerationOpts.SetZeroValues() +} +*/ diff --git a/cmd/transferacceleration/transferacceleration.go b/cmd/transferacceleration/transferacceleration.go new file mode 100644 index 0000000..16e8679 --- /dev/null +++ b/cmd/transferacceleration/transferacceleration.go @@ -0,0 +1,21 @@ +package transferacceleration + +import ( + "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/set" + "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/show" + "github.com/spf13/cobra" +) + +func init() { + TransferAccelerationCmd.AddCommand(show.ShowCmd) + TransferAccelerationCmd.AddCommand(set.SetCmd) +} + +var ( + TransferAccelerationCmd = &cobra.Command{ + Use: "transferacceleration", + Short: "", + SilenceUsage: true, + SilenceErrors: true, + } +) diff --git a/cmd/transferacceleration/transferacceleration_test.go b/cmd/transferacceleration/transferacceleration_test.go new file mode 100644 index 0000000..f66ec84 --- /dev/null +++ b/cmd/transferacceleration/transferacceleration_test.go @@ -0,0 +1,11 @@ +package transferacceleration + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTransferAccelerationCmd(t *testing.T) { + assert.NotNil(t, TransferAccelerationCmd) +} diff --git a/cmd/transferacceleration/utils/utils.go b/cmd/transferacceleration/utils/utils.go new file mode 100644 index 0000000..68370f2 --- /dev/null +++ b/cmd/transferacceleration/utils/utils.go @@ -0,0 +1,50 @@ +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 ( + ErrTooManyArguments = "too many arguments. please provide just 'enabled' or 'disabled'" + ErrUnknownStatus = "unknown status '%s' returned from AWS SDK" +) + +func CheckArgs(args []string) error { + if len(args) != 0 { + return errors.New(ErrTooManyArguments) + } + + 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) + transferAccelerationOpts.RootOptions = rootOpts + + logger := logging.GetLogger(transferAccelerationOpts.RootOptions) + + return svc, transferAccelerationOpts, logger +} diff --git a/cmd/transferacceleration/utils/utils_test.go b/cmd/transferacceleration/utils/utils_test.go new file mode 100644 index 0000000..0e04123 --- /dev/null +++ b/cmd/transferacceleration/utils/utils_test.go @@ -0,0 +1,99 @@ +package utils + +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" + options3 "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/options" + internalaws "github.com/bilalcaliskan/s3-manager/internal/aws" + "github.com/rs/zerolog" + "github.com/spf13/cobra" + + "github.com/stretchr/testify/assert" +) + +func createSvc(rootOpts *options.RootOptions) (*s3.S3, error) { + return internalaws.CreateAwsService(rootOpts) +} + +func TestCheckArgsSuccess(t *testing.T) { + err := CheckArgs([]string{}) + assert.Nil(t, err) +} + +func TestCheckArgsFailure(t *testing.T) { + err := CheckArgs([]string{"foo"}) + 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 + taOpts *options3.TransferAccelerationOptions + logger zerolog.Logger + ) + + cmd := &cobra.Command{} + cmd.SetContext(context.Background()) + + rootOpts := options.GetRootOptions() + rootOpts.AccessKey = "thisisaccesskey" + rootOpts.SecretKey = "thisissecretkey" + rootOpts.Region = "thisisregion" + rootOpts.BucketName = "thisisbucketname" + + svc, err := createSvc(rootOpts) + assert.NotNil(t, svc) + assert.Nil(t, err) + + cmd.SetContext(context.WithValue(context.Background(), options.OptsKey{}, rootOpts)) + cmd.SetContext(context.WithValue(cmd.Context(), options.S3SvcKey{}, svc)) + + svc, taOpts, logger = PrepareConstants(cmd, options3.GetTransferAccelerationOptions()) + assert.NotNil(t, svc) + assert.NotNil(t, taOpts) + assert.NotNil(t, logger) +} diff --git a/internal/aws/aws.go b/internal/aws/aws.go index 72133a4..534f250 100644 --- a/internal/aws/aws.go +++ b/internal/aws/aws.go @@ -8,6 +8,8 @@ import ( "strings" "sync" + options6 "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/options" + options5 "github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy/options" "github.com/bilalcaliskan/s3-manager/cmd/versioning/set/utils" @@ -108,6 +110,27 @@ func DeleteAllBucketTags(svc s3iface.S3API, opts *options3.TagOptions) (res *s3. }) } +func GetTransferAcceleration(svc s3iface.S3API, opts *options6.TransferAccelerationOptions) (res *s3.GetBucketAccelerateConfigurationOutput, err error) { + return svc.GetBucketAccelerateConfiguration(&s3.GetBucketAccelerateConfigurationInput{ + Bucket: aws.String(opts.BucketName), + }) +} + +func SetTransferAcceleration(svc s3iface.S3API, opts *options6.TransferAccelerationOptions) (res *s3.PutBucketAccelerateConfigurationOutput, err error) { + var status string + switch opts.DesiredState { + case "enabled": + status = "Enabled" + case "disabled": + status = "Suspended" + } + + return svc.PutBucketAccelerateConfiguration(&s3.PutBucketAccelerateConfigurationInput{ + Bucket: aws.String(opts.BucketName), + AccelerateConfiguration: &s3.AccelerateConfiguration{Status: aws.String(status)}, + }) +} + func GetBucketPolicy(svc s3iface.S3API, opts *options5.BucketPolicyOptions) (res *s3.GetBucketPolicyOutput, err error) { return svc.GetBucketPolicy(&s3.GetBucketPolicyInput{ Bucket: aws.String(opts.BucketName), diff --git a/internal/aws/aws_test.go b/internal/aws/aws_test.go index a57479e..f8ff80b 100644 --- a/internal/aws/aws_test.go +++ b/internal/aws/aws_test.go @@ -6,6 +6,9 @@ import ( "testing" "time" + options6 "github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy/options" + options5 "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/options" + options4 "github.com/bilalcaliskan/s3-manager/cmd/tags/options" options3 "github.com/bilalcaliskan/s3-manager/cmd/versioning/options" @@ -42,15 +45,27 @@ var ( defaultGetBucketVersioningOutput = &s3.GetBucketVersioningOutput{ Status: aws.String("Enabled"), } - defaultGetBucketVersioningErr error - defaultPutBucketVersioningOutput = &s3.PutBucketVersioningOutput{} - defaultPutBucketVersioningErr error - defaultGetBucketTaggingErr error - defaultGetBucketTaggingOutput = &s3.GetBucketTaggingOutput{} - defaultPutBucketTaggingErr error - defaultPutBucketTaggingOutput = &s3.PutBucketTaggingOutput{} - defaultDeleteBucketTaggingErr error - defaultDeleteBucketTaggingOutput = &s3.DeleteBucketTaggingOutput{} + defaultGetBucketVersioningErr error + defaultPutBucketVersioningOutput = &s3.PutBucketVersioningOutput{} + defaultPutBucketVersioningErr error + defaultGetBucketTaggingErr error + defaultGetBucketTaggingOutput = &s3.GetBucketTaggingOutput{} + defaultPutBucketTaggingErr error + defaultPutBucketTaggingOutput = &s3.PutBucketTaggingOutput{} + defaultDeleteBucketTaggingErr error + defaultDeleteBucketTaggingOutput = &s3.DeleteBucketTaggingOutput{} + defaultGetBucketAccelerationOutput = &s3.GetBucketAccelerateConfigurationOutput{ + Status: aws.String("Enabled"), + } + defaultGetBucketAccelerationErr error + defaultPutBucketAccelerationOutput = &s3.PutBucketAccelerateConfigurationOutput{} + defaultPutBucketAccelerationErr error + defaultGetBucketPolicyErr error + defaultGetBucketPolicyOutput = &s3.GetBucketPolicyOutput{} + defaultPutBucketPolicyErr error + defaultPutBucketPolicyOutput = &s3.PutBucketPolicyOutput{} + defaultDeleteBucketPolicyErr error + defaultDeleteBucketPolicyOutput = &s3.DeleteBucketPolicyOutput{} ) type mockS3Client struct { @@ -82,6 +97,14 @@ func (m *mockS3Client) DeleteObject(input *s3.DeleteObjectInput) (*s3.DeleteObje return defaultDeleteObjectOutput, defaultDeleteObjectErr } +func (m *mockS3Client) GetBucketAccelerateConfiguration(input *s3.GetBucketAccelerateConfigurationInput) (*s3.GetBucketAccelerateConfigurationOutput, error) { + return defaultGetBucketAccelerationOutput, defaultGetBucketAccelerationErr +} + +func (m *mockS3Client) PutBucketAccelerateConfiguration(input *s3.PutBucketAccelerateConfigurationInput) (*s3.PutBucketAccelerateConfigurationOutput, error) { + return defaultPutBucketAccelerationOutput, defaultPutBucketAccelerationErr +} + func (m *mockS3Client) GetBucketVersioning(input *s3.GetBucketVersioningInput) (*s3.GetBucketVersioningOutput, error) { return defaultGetBucketVersioningOutput, defaultGetBucketVersioningErr } @@ -102,6 +125,18 @@ func (m *mockS3Client) DeleteBucketTagging(input *s3.DeleteBucketTaggingInput) ( return defaultDeleteBucketTaggingOutput, defaultDeleteBucketTaggingErr } +func (m *mockS3Client) GetBucketPolicy(input *s3.GetBucketPolicyInput) (*s3.GetBucketPolicyOutput, error) { + return defaultGetBucketPolicyOutput, defaultGetBucketPolicyErr +} + +func (m *mockS3Client) PutBucketPolicy(input *s3.PutBucketPolicyInput) (*s3.PutBucketPolicyOutput, error) { + return defaultPutBucketPolicyOutput, defaultPutBucketPolicyErr +} + +func (m *mockS3Client) DeleteBucketPolicy(input *s3.DeleteBucketPolicyInput) (*s3.DeleteBucketPolicyOutput, error) { + return defaultDeleteBucketPolicyOutput, defaultDeleteBucketPolicyErr +} + func TestGetAllFilesHappyPath(t *testing.T) { m := &mockS3Client{} defaultListObjectsOutput.Contents = []*s3.Object{ @@ -511,3 +546,93 @@ func TestDeleteBucketTaggingSuccess(t *testing.T) { _, err := DeleteAllBucketTags(&mockS3Client{}, tagOpts) assert.Nil(t, err) } + +func TestGetTransferAcceleration(t *testing.T) { + taOpts := options5.GetTransferAccelerationOptions() + defer func() { + taOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + taOpts.RootOptions = rootOpts + + defaultGetBucketAccelerationErr = nil + + res, err := GetTransferAcceleration(&mockS3Client{}, taOpts) + assert.NotNil(t, res) + assert.Nil(t, err) +} + +func TestSetTransferAcceleration(t *testing.T) { + taOpts := options5.GetTransferAccelerationOptions() + defer func() { + taOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + taOpts.RootOptions = rootOpts + + taOpts.DesiredState = "enabled" + defaultPutBucketAccelerationErr = nil + res, err := SetTransferAcceleration(&mockS3Client{}, taOpts) + assert.NotNil(t, res) + assert.Nil(t, err) +} + +func TestSetTransferAcceleration2(t *testing.T) { + taOpts := options5.GetTransferAccelerationOptions() + defer func() { + taOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + taOpts.RootOptions = rootOpts + + taOpts.DesiredState = "disabled" + defaultPutBucketAccelerationErr = nil + res, err := SetTransferAcceleration(&mockS3Client{}, taOpts) + assert.NotNil(t, res) + assert.Nil(t, err) +} + +func TestGetBucketPolicy(t *testing.T) { + bpOpts := options6.GetBucketPolicyOptions() + defer func() { + bpOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + bpOpts.RootOptions = rootOpts + + res, err := GetBucketPolicy(&mockS3Client{}, bpOpts) + assert.NotNil(t, res) + assert.Nil(t, err) +} + +func TestSetBucketPolicy(t *testing.T) { + bpOpts := options6.GetBucketPolicyOptions() + defer func() { + bpOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + bpOpts.RootOptions = rootOpts + + res, err := SetBucketPolicy(&mockS3Client{}, bpOpts) + assert.NotNil(t, res) + assert.Nil(t, err) +} + +func TestDeleteBucketPolicy(t *testing.T) { + bpOpts := options6.GetBucketPolicyOptions() + defer func() { + bpOpts.SetZeroValues() + }() + rootOpts := options.GetRootOptions() + rootOpts.Region = "us-east-1" + bpOpts.RootOptions = rootOpts + + res, err := DeleteBucketPolicy(&mockS3Client{}, bpOpts) + assert.NotNil(t, res) + assert.Nil(t, err) +}