From cac15ce316bd42109274e4e5e7be73ca0c429742 Mon Sep 17 00:00:00 2001 From: Mmadu Manasseh Date: Sat, 31 Oct 2020 00:10:08 +0100 Subject: [PATCH] Migrated to latest sdk version --- .air.toml | 2 +- .gitignore | 3 ++- README.md | 7 ++++-- config/aws.go | 40 +++++++++++++++++++++++++++++++++ config/config.example.yaml | 12 +++++++++- config/config.go | 42 ++++++++++++++--------------------- go.mod | 8 ++++++- go.sum | 31 ++++++++++++++++++++++++++ provider/aws/aws.go | 4 ++-- provider/aws/ec2.go | 37 ++++++++++++++----------------- provider/aws/s3.go | 44 ++++++++++++++----------------------- provider/aws/utils/state.go | 2 +- 12 files changed, 148 insertions(+), 84 deletions(-) create mode 100644 config/aws.go diff --git a/.air.toml b/.air.toml index dbb5447..f538cfe 100644 --- a/.air.toml +++ b/.air.toml @@ -11,7 +11,7 @@ cmd = "go build -o ./tmp/main ./web/main.go" # Binary file yields from `cmd`. bin = "tmp/main" # Customize binary. -full_bin = "APP_ENV=dev APP_USER=air ./tmp/main" +full_bin = "APP_ENV=dev APP_USER=air ./tmp/main --config config/config.yaml" # Watch these filename extensions. include_ext = ["go", "tpl", "tmpl", "html"] # Ignore these filename extensions or directories. diff --git a/.gitignore b/.gitignore index a7770cb..30d27d0 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ *.db # tmp builds by air tmp -logs \ No newline at end of file +logs +config/config.yaml \ No newline at end of file diff --git a/README.md b/README.md index 8dc68bf..017987c 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,13 @@ A Cloud Infrastructure Management Tool to stop, resume, clean and destroy resour ## Development +Copy `config/config.example.yaml` to `config/config.yaml` and make all necessary changes ```bash -# Web UI +cp config/config.example.yaml config/config.yaml + +# Start Web UI cd web -go run main.go +go run main.go --config ../config/config.yaml ``` using [air](https://github.com/cosmtrek/air) with autoreload UI features diff --git a/config/aws.go b/config/aws.go new file mode 100644 index 0000000..a1a430e --- /dev/null +++ b/config/aws.go @@ -0,0 +1,40 @@ +package config + +import ( + "github.com/aws/aws-sdk-go-v2/aws" + awsCfg "github.com/aws/aws-sdk-go-v2/config" + + "github.com/aws/aws-sdk-go-v2/credentials" + log "github.com/sirupsen/logrus" +) + +// AwsConfig Related Configurations +type AwsConfig struct { + // AWS Configs + Config aws.Config + AccessKeyID string + SecretAccessKey string + DefaultRegion string +} + +func loadAwsConfig(accessKeyID, secretAccessKey, defaultRegion string) aws.Config { + var ( + err error + cfg aws.Config + ) + if accessKeyID != "" && secretAccessKey != "" { + cfg, err = awsCfg.LoadDefaultConfig( + awsCfg.WithCredentialsProvider(credentials.StaticCredentialsProvider{ + Value: aws.Credentials{ + AccessKeyID: accessKeyID, SecretAccessKey: secretAccessKey, + Source: "Reka Variables", + }, + })) + } else { + cfg, err = awsCfg.LoadDefaultConfig(awsCfg.WithRegion(defaultRegion)) + } + if err != nil { + log.Fatal(err) + } + return cfg +} diff --git a/config/config.example.yaml b/config/config.example.yaml index ae84c5b..72f0f45 100644 --- a/config/config.example.yaml +++ b/config/config.example.yaml @@ -15,4 +15,14 @@ refreshInterval: 1 # username: blank # Can # password: blank database: - type: sqlite \ No newline at end of file + type: sqlite + +aws: + # Env: Access Key ID + # AWS_SECRET_ACCESS_KEY=SECRET + secretAccessKey: blank + # Secret Access Key + # AWS_ACCESS_KEY_ID=AKID + accessKeyID: blank + # AWS_REGION=blank + defaultRegion: us-east-2 \ No newline at end of file diff --git a/config/config.go b/config/config.go index a81b9f6..3d5ce8b 100644 --- a/config/config.go +++ b/config/config.go @@ -7,7 +7,6 @@ import ( "path/filepath" "strings" - "github.com/aws/aws-sdk-go-v2/aws" log "github.com/sirupsen/logrus" "github.com/spf13/viper" ) @@ -22,22 +21,13 @@ const ( appName = "REKA" ) -// AwsConfig Related Configurations -type AwsConfig struct { - // AWS Configs - Config aws.Config - AccessKey string `yaml:"accessKey"` - SecretAccessKey string `yaml:"secretAccessKey"` - DefaultRegion string `yaml:"defaultRegion"` -} - // DatabaseConfig Config for Dabatabase type DatabaseConfig struct { - Type string `yaml:"type"` - Name string `yaml:"name"` - Host string `yaml:"host"` - User string `yaml:"user"` - Password string `yaml:"password"` + Type string + Name string + Host string + User string + Password string } // GetConnectionString the connection string for database @@ -52,17 +42,18 @@ func (db *DatabaseConfig) SqliteDefaultPath() string { // Config : The Config values passed to application type Config struct { - Name string `yaml:"name"` - Providers []string `yaml:"providers"` - Database *DatabaseConfig `yaml:"database"` - Aws *AwsConfig `yaml:"aws"` - RefreshInterval int32 `yaml:"refreshInterval"` - LogPath string `yaml:"logPath"` - + Name string + Providers []string + Database *DatabaseConfig + Aws *AwsConfig + RefreshInterval int32 + LogPath string + + // Authentication Details to login to Reka Auth struct { - Username string `yaml:"username"` - Password string `yaml:"password"` - } `yaml:"auth"` + Username string + Password string + } staticPath string // Path to Static File } @@ -139,7 +130,6 @@ func LoadConfig() *Config { } } - fmt.Println(config.Auth) return config } diff --git a/go.mod b/go.mod index f1ff002..76bc9dc 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,16 @@ module github.com/mensaah/reka go 1.14 require ( - github.com/aws/aws-sdk-go-v2 v0.23.0 + github.com/aws/aws-sdk-go-v2 v0.28.0 + github.com/aws/aws-sdk-go-v2/config v0.2.1 + github.com/aws/aws-sdk-go-v2/credentials v0.1.3 + github.com/aws/aws-sdk-go-v2/feature/s3/manager v0.1.1 + github.com/aws/aws-sdk-go-v2/service/ec2 v0.28.0 + github.com/aws/aws-sdk-go-v2/service/s3 v0.28.0 github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/gin-gonic/gin v1.6.3 github.com/go-co-op/gocron v0.3.1 + github.com/go-sql-driver/mysql v1.5.0 // indirect github.com/jinzhu/now v1.1.1 github.com/labstack/gommon v0.3.0 github.com/pelletier/go-toml v1.8.1 // indirect diff --git a/go.sum b/go.sum index 48e15bd..437193a 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,30 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/aws/aws-sdk-go-v2 v0.23.0 h1:+E1q1LLSfHSDn/DzOtdJOX+pLZE2HiNV2yO5AjZINwM= github.com/aws/aws-sdk-go-v2 v0.23.0/go.mod h1:2LhT7UgHOXK3UXONKI5OMgIyoQL6zTAw/jwIeX6yqzw= +github.com/aws/aws-sdk-go-v2 v0.28.0 h1:+LRTQpWWXQBsjqd5nNkOGvKey6LWL01xZnyMQB/ZLwo= +github.com/aws/aws-sdk-go-v2 v0.28.0/go.mod h1:P9h1Cf+uOpElAT533QXKOzrpFaOlm8JMorThJNUfQ6Q= +github.com/aws/aws-sdk-go-v2/config v0.2.1 h1:VJHy+Pf3tTcj4QCF3y8sAvERHjMYKCpYGAMkbQkoJi4= +github.com/aws/aws-sdk-go-v2/config v0.2.1/go.mod h1:589BDFDfGgBgwpTUgiCBiZMzzwlyA6F5gVmm3mNHG50= +github.com/aws/aws-sdk-go-v2/credentials v0.1.3 h1:RO+Zd6vbsfRmr1M/hyzALKN2uuSzfR9x0DLVXSy6icA= +github.com/aws/aws-sdk-go-v2/credentials v0.1.3/go.mod h1:Bkc9+5pYXzMEv0lTLALes2mKqxHJuSGUQZpT8cm0RYc= +github.com/aws/aws-sdk-go-v2/ec2imds v0.1.3 h1:PwcbtgVwH5IhNIzpkK+g7/f5lJ5YmcetMGcFJ5Z+VM4= +github.com/aws/aws-sdk-go-v2/ec2imds v0.1.3/go.mod h1:7TwGIbQjJpNi4BB2fdAR4YrVcPe9025baOkFcuYIgLY= +github.com/aws/aws-sdk-go-v2/feature/s3/manager v0.1.1 h1:IaB5I9QPhMVmVkJaN9xYZjcnazcyt32O3vfnq1HsXnI= +github.com/aws/aws-sdk-go-v2/feature/s3/manager v0.1.1/go.mod h1:v8zMKLr4BzY8/44aFkHpkuGySGmoXzLBsQFNhPC3NG0= +github.com/aws/aws-sdk-go-v2/service/ec2 v0.28.0 h1:yPZURCMZsYOXN1yoGArWbKnw4700SrPgD0b0mKm/Jiw= +github.com/aws/aws-sdk-go-v2/service/ec2 v0.28.0/go.mod h1:jg4gBSpwnAHiaDRdXbmoToyD4wMzddziQt6JjTztZkQ= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v0.3.0 h1:OGNwNNeQvOZsa+zAK5nE7r6e0serfSAFznoXqbvbzFE= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v0.3.0/go.mod h1:bMiNrEKNefchodwRJnuwaiAZj2NJq8ZHAYASve6mbFs= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v0.1.0 h1:r6FAnK2190ahYhtzj0HRcxCWlb7SE9/zI8v73npLkYU= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v0.1.0/go.mod h1:aLwdZO0CArC1N9dbBxt7C2PBxkPktM0sDsr9iS7A9SY= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v0.3.0 h1:W9iV5Wv4ENG/tZJNj5cGAPqaIqTzYbfAuSLufE+nFHk= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v0.3.0/go.mod h1:+jAbMTkZ52kAOhaU0NUn5/gY6i4oRcIJxfPUVrpDdq4= +github.com/aws/aws-sdk-go-v2/service/s3 v0.28.0 h1:SwlO55TpntMeMvUCWwPae6RgsbplIx5ktxJ5CQnpq68= +github.com/aws/aws-sdk-go-v2/service/s3 v0.28.0/go.mod h1:0cSPvUIK053aHptJM5Z8KflwHky2xTTWZnjh2P/5bcA= +github.com/aws/aws-sdk-go-v2/service/sts v0.28.0 h1:vT3BsN262uYMR2J4JC7oPbUwNvbnYUJ2GTwOtbfK+tk= +github.com/aws/aws-sdk-go-v2/service/sts v0.28.0/go.mod h1:AoXZmcouo9fCC1Ut7dUeBf2JiCjNFb0WOmgSEGnpuKY= +github.com/awslabs/smithy-go v0.2.1 h1:guvR5teu1q6IoZnE9PFyPjxh/H7YEcZGysx9vu5ZwZ0= +github.com/awslabs/smithy-go v0.2.1/go.mod h1:hPOQwnmBLHsUphH13tVSjQhTAFma0/0XoZGbBcOuABI= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY= @@ -94,6 +118,8 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= @@ -186,6 +212,9 @@ github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E= github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= @@ -463,6 +492,8 @@ golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= diff --git a/provider/aws/aws.go b/provider/aws/aws.go index 01597f5..b307e8b 100644 --- a/provider/aws/aws.go +++ b/provider/aws/aws.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/aws/external" + awsCfg "github.com/aws/aws-sdk-go-v2/config" log "github.com/sirupsen/logrus" "github.com/mensaah/reka/config" @@ -22,7 +22,7 @@ func GetName() string { } func GetConfig() aws.Config { - cfg, err := external.LoadDefaultAWSConfig() + cfg, err := awsCfg.LoadDefaultConfig() if err != nil { panic("unable to load SDK config, " + err.Error()) } diff --git a/provider/aws/ec2.go b/provider/aws/ec2.go index 03c609a..63c55e0 100644 --- a/provider/aws/ec2.go +++ b/provider/aws/ec2.go @@ -14,7 +14,7 @@ import ( ) // returns only instance IDs of unprotected ec2 instances -func getInstanceDetails(svc *ec2.Client, output *ec2.DescribeInstancesResponse, region string, logger *log.Entry) ([]*types.Resource, error) { +func getInstanceDetails(svc *ec2.Client, output *ec2.DescribeInstancesOutput, region string, logger *log.Entry) ([]*types.Resource, error) { var ec2Instances []*types.Resource logger.Debug("Fetching EC2 Details") for _, reservation := range output.Reservations { @@ -42,14 +42,12 @@ func getInstanceDetails(svc *ec2.Client, output *ec2.DescribeInstancesResponse, // GetAllEC2Instances Get all instances func GetAllEC2Instances(cfg aws.Config, region string, logger *log.Entry) ([]*types.Resource, error) { logger.Debug("Fetching EC2 Instances") - svc := ec2.New(cfg) + svc := ec2.NewFromConfig(cfg) params := &ec2.DescribeInstancesInput{} // Build the request with its input parameters - req := svc.DescribeInstancesRequest(params) + resp, err := svc.DescribeInstances(context.Background(), params) - // Send the request, and get the response or error back - resp, err := req.Send(context.Background()) if err != nil { return nil, err } @@ -63,12 +61,12 @@ func GetAllEC2Instances(cfg aws.Config, region string, logger *log.Entry) ([]*ty // StopEC2Instances Stop Running Instances func StopEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log.Entry) error { - svc := ec2.New(cfg) - var instanceIds []string + svc := ec2.NewFromConfig(cfg) + var instanceIds []*string for _, instance := range instances { if instance.IsActive() { - instanceIds = append(instanceIds, instance.UUID) + instanceIds = append(instanceIds, &instance.UUID) } } @@ -82,8 +80,7 @@ func StopEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log.E InstanceIds: instanceIds, } - req := svc.StopInstancesRequest(params) - resp, err := req.Send(context.Background()) + resp, err := svc.StopInstances(context.Background(), params) // TODO Attach error to specific instance where the error occurred if possible if err != nil { fmt.Println(resp, err) @@ -93,12 +90,12 @@ func StopEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log.E // ResumeEC2Instances Resume Stopped instances func ResumeEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log.Entry) error { - svc := ec2.New(cfg) - var instanceIds []string + svc := ec2.NewFromConfig(cfg) + var instanceIds []*string for _, instance := range instances { if instance.IsStopped() { - instanceIds = append(instanceIds, instance.UUID) + instanceIds = append(instanceIds, &instance.UUID) } } @@ -111,8 +108,7 @@ func ResumeEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log } logger.Debug("Starting EC2 Instances ", instanceIds, " ...") - req := svc.StartInstancesRequest(params) - resp, err := req.Send(context.Background()) + resp, err := svc.StartInstances(context.Background(), params) // TODO Attach error to specific instance where the error occurred if possible if err != nil { fmt.Println(resp, err) @@ -120,14 +116,14 @@ func ResumeEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log return err } -// StartEC2Instances Start Stopped instances +// TerminateEC2Instances Shutdown instances func TerminateEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log.Entry) error { - svc := ec2.New(cfg) - var instanceIds []string + svc := ec2.NewFromConfig(cfg) + var instanceIds []*string for _, instance := range instances { if instance.IsStopped() || instance.IsActive() { - instanceIds = append(instanceIds, instance.UUID) + instanceIds = append(instanceIds, &instance.UUID) } } @@ -141,8 +137,7 @@ func TerminateEC2Instances(cfg aws.Config, instances []*types.Resource, logger * InstanceIds: instanceIds, } - req := svc.TerminateInstancesRequest(params) - resp, err := req.Send(context.Background()) + resp, err := svc.TerminateInstances(context.Background(), params) // TODO Attach error to specific instance where the error occurred if possible if err != nil { fmt.Println(resp, err) diff --git a/provider/aws/s3.go b/provider/aws/s3.go index f6b4615..a49e322 100644 --- a/provider/aws/s3.go +++ b/provider/aws/s3.go @@ -2,13 +2,14 @@ package aws import ( "context" + "errors" "fmt" "unsafe" "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/aws/awserr" + s3manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager" "github.com/aws/aws-sdk-go-v2/service/s3" - "github.com/aws/aws-sdk-go-v2/service/s3/s3manager" + s3Types "github.com/aws/aws-sdk-go-v2/service/s3/types" log "github.com/sirupsen/logrus" "github.com/mensaah/reka/provider/aws/utils" @@ -17,9 +18,12 @@ import ( func getS3BucketRegion(cfg aws.Config, bucketName string, logger *log.Entry) (string, error) { - region, err := s3manager.GetBucketRegion(context.Background(), cfg, bucketName, "us-west-2") + region, err := s3manager.GetBucketRegion(context.Background(), s3.NewFromConfig(cfg), bucketName) if err != nil { - if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "NotFound" { + var notFoundErr *s3Types.NoSuchBucket + if errors.As(err, ¬FoundErr) { + log.Printf("scan failed because the table was not found, %v", + notFoundErr.ErrorMessage()) return "", fmt.Errorf("unable to find bucket %s's region not found", bucketName) } return "", err @@ -34,15 +38,8 @@ func getS3BucketTags(svc *s3.Client, bucketName string, logger *log.Entry) (type Bucket: aws.String(bucketName), } - req := svc.GetBucketTaggingRequest(input) - result, err := req.Send(context.Background()) + result, err := svc.GetBucketTagging(context.Background(), input) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - default: - return types.ResourceTags{}, aerr - } - } return types.ResourceTags{}, err } // https://stackoverflow.com/a/48554123/7167357 @@ -51,7 +48,7 @@ func getS3BucketTags(svc *s3.Client, bucketName string, logger *log.Entry) (type } // returns only s3Bucket IDs of unprotected s3 instances -func getS3BucketsDetails(svc *s3.Client, cfg aws.Config, output *s3.ListBucketsResponse, logger *log.Entry) ([]*types.Resource, error) { +func getS3BucketsDetails(svc *s3.Client, cfg aws.Config, output *s3.ListBucketsOutput, logger *log.Entry) ([]*types.Resource, error) { var s3Buckets []*types.Resource for _, s3Bucket := range output.Buckets { // Get tags @@ -83,14 +80,11 @@ func getS3BucketsDetails(svc *s3.Client, cfg aws.Config, output *s3.ListBucketsR // GetAllS3Buckets Get all s3Buckets func getAllS3Buckets(cfg aws.Config, logger *log.Entry) ([]*types.Resource, error) { logger.Debug("Fetching S3 Buckets") - svc := s3.New(cfg) + svc := s3.NewFromConfig(cfg) params := &s3.ListBucketsInput{} // Build the request with its input parameters - req := svc.ListBucketsRequest(params) - - // Send the request, and get the response or error back - resp, err := req.Send(context.Background()) + resp, err := svc.ListBuckets(context.Background(), params) if err != nil { return nil, err } @@ -107,15 +101,8 @@ func destroyBucket(svc *s3.Client, bucket *types.Resource, logger *log.Entry) er Bucket: aws.String(bucket.UUID), } - req := svc.DeleteBucketRequest(input) - _, err := req.Send(context.Background()) + _, err := svc.DeleteBucket(context.Background(), input) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - default: - return aerr - } - } return err } @@ -135,8 +122,9 @@ func destroyS3Buckets(cfg aws.Config, s3Buckets []*types.Resource, logger *log.E // TODO Use Goroutines for region, buckets := range bucketsPerRegion { - svc := s3.New(cfg) - svc.Client.Config.Region = region + svc := s3.NewFromConfig(cfg, func(options *s3.Options) { + options.Region = region + }) for _, bucket := range buckets { err := destroyBucket(svc, bucket, logger) if err != nil { diff --git a/provider/aws/utils/state.go b/provider/aws/utils/state.go index 24dc06d..b0bb9b3 100644 --- a/provider/aws/utils/state.go +++ b/provider/aws/utils/state.go @@ -5,7 +5,7 @@ import ( ) // GetResourceState Get the current status of Resource: Pending, Running, ... Stopped -func GetResourceState(s int64) types.State { +func GetResourceState(s int32) types.State { switch s { case 0: return types.Pending