Skip to content

Commit

Permalink
Migrated to latest sdk version
Browse files Browse the repository at this point in the history
  • Loading branch information
MeNsaaH committed Oct 30, 2020
1 parent 3d76a26 commit cac15ce
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
*.db
# tmp builds by air
tmp
logs
logs
config/config.yaml
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions config/aws.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 11 additions & 1 deletion config/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ refreshInterval: 1
# username: blank # Can
# password: blank
database:
type: sqlite
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
42 changes: 16 additions & 26 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -139,7 +130,6 @@ func LoadConfig() *Config {
}
}

fmt.Println(config.Auth)
return config
}

Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions provider/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
}
Expand Down
37 changes: 16 additions & 21 deletions provider/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
}

Expand All @@ -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)
Expand All @@ -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)
}
}

Expand All @@ -111,23 +108,22 @@ 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)
}
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)
}
}

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

0 comments on commit cac15ce

Please sign in to comment.