Skip to content

Commit

Permalink
Merge pull request #2 from undeadops/convert-aws-sdk-v2
Browse files Browse the repository at this point in the history
Converting to AWS SDK v2
  • Loading branch information
undeadops committed Jul 23, 2018
2 parents 00fab52 + 740fab6 commit 83ab02c
Show file tree
Hide file tree
Showing 1,873 changed files with 1,505,315 additions and 77 deletions.
7 changes: 7 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ builds:
- windows
goarch:
- amd64
- 386
- arm
- arm64
ignore:
- goos: openbsd
goarch: arm
goarm: 6
checksum:
# You can change the name of the checksums file.
# This is parsed with the Go template engine and the following variables
Expand Down
16 changes: 0 additions & 16 deletions Makefile

This file was deleted.

1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2
50 changes: 32 additions & 18 deletions ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,49 @@ import (
"log"
"net/url"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/ec2"

"github.com/fatih/color"
"github.com/gosuri/uitable"
"github.com/urfave/cli"
)

//{{$ec2.Name}}\t\t{{$ec2.state}}\t{{$ec2.id}}\t{{$ec2.role}}\t{{$ec2.cost_center}}\t{{$ec2.env}}\t{{$ec2.instancetype}}\t{{$ec2.ipaddress}}\t{{$ec2.publicIpaddress}}

func printEc2Instances(instances []map[string]string) error {
table := uitable.New()
table.AddRow("NAME", "ID", "STATE", "ROLE", "COST_CENTER", "IP Address", "PUBLIC ADDRESS", "AMI")
for _, i := range instances {
fmt.Printf(
"\x1b[33m%-25s\x1b[0m\t%-20s\t%-7s\t%-26s\t%-15s\t%-15s\t%-15s\t%-10s\n",
i["name"], i["id"], i["state"], i["role"], i["cost_center"], i["ipAddress"], i["publicIpAddress"], i["ami"])
table.AddRow(color.YellowString(i["name"]), i["id"], i["state"], i["role"], i["cost_center"], i["ipAddress"], i["publicIpAddress"], i["ami"])
}
fmt.Println(table)
return nil
}

func ec2List(c *cli.Context) error {
fmt.Println("EC2 List Instances")
fmt.Println("AWS Config Profile: ", awsProfile)
cfg, err := external.LoadDefaultAWSConfig(
external.WithSharedConfigProfile(awsProfile),
)
if err != nil {
panic("unable to load SDK config, " + err.Error())
}

var ec2instances []map[string]string
// TODO: Revisit this... what happens when profile isnt defaulted to us-east-1
cfg.Region = awsRegion

sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: awsProfile,
}))
var ec2instances []map[string]string

ec2svc := ec2.New(sess)
params := &ec2.DescribeInstancesInput{
Filters: generateFilter(),
}
resp, err := ec2svc.DescribeInstances(params)

svc := ec2.New(cfg)

req := svc.DescribeInstancesRequest(params)
resp, err := req.Send()
if err != nil {
fmt.Println("there was an error listing instances in", err.Error())
fmt.Println("there was an error listing instances in", err)
log.Fatal(err.Error())
}

Expand All @@ -62,14 +71,19 @@ func ec2List(c *cli.Context) error {
role = url.QueryEscape(*keys.Value)
}
}

instanceType, _ := instances.InstanceType.MarshalValue()
stateName, _ := instances.InstanceType.MarshalValue()
arch, _ := instances.Architecture.MarshalValue()

i := map[string]string{
"id": *instances.InstanceId,
"name": name,
"ipAddress": *instances.PrivateIpAddress,
"instanceType": *instances.InstanceType,
"instanceType": instanceType,
"keyName": *instances.KeyName,
"state": *instances.State.Name,
"arch": *instances.Architecture,
"state": stateName,
"arch": arch,
"cost_center": costCenter,
"env": env,
"role": role,
Expand Down
63 changes: 46 additions & 17 deletions elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,75 @@ import (
"log"
"strconv"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go-v2/aws/awserr"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/elb"
"github.com/gosuri/uitable"

"github.com/fatih/color"
"github.com/urfave/cli"
)

func printElb(elb []map[string]string) error {
table := uitable.New()
table.AddRow("NAME", "Members", "IN:OUT", "Availability", "DNS")
for _, e := range elb {
fmt.Printf(
"%-45s\t%-3s\t%-2s:%-2s\t%-15s\t%-50s\n",
color.YellowString(e["name"]), e["numInstances"], color.GreenString(e["instances_in"]),
color.RedString(e["instances_out"]), e["scheme"], e["dns"])
table.AddRow(color.YellowString(e["name"]),
e["numInstances"],
color.GreenString(e["instances_in"])+":"+color.RedString(e["instances_out"]),
e["scheme"],
e["dns"],
)
}
fmt.Println(table)
return nil
}

func elbList(c *cli.Context) error {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: awsProfile,
}))
cfg, err := external.LoadDefaultAWSConfig(
external.WithSharedConfigProfile(awsProfile),
)
if err != nil {
panic("unable to load SDK config, " + err.Error())
}

// TODO: Revisit this... what happens when profile isnt defaulted to us-east-1
cfg.Region = awsRegion

var elbList []map[string]string

elbsvc := elb.New(sess)
//elbsvc := elb.New(sess)
svc := elb.New(cfg)

// I don't want to filter results of ELB's
filter := &elb.DescribeLoadBalancersInput{}

loadbalancers, err := elbsvc.DescribeLoadBalancers(filter)
req := svc.DescribeLoadBalancersRequest(filter)
result, err := req.Send()
if err != nil {
fmt.Println("There was an error listing ELB Instances", err.Error())
log.Fatal(err.Error())
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case elb.ErrCodeAccessPointNotFoundException:
fmt.Println(elb.ErrCodeAccessPointNotFoundException, aerr.Error())
case elb.ErrCodeDependencyThrottleException:
fmt.Println(elb.ErrCodeDependencyThrottleException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return err
}

for _, lb := range loadbalancers.LoadBalancerDescriptions {
for _, lb := range result.LoadBalancerDescriptions {
elbfilter := &elb.DescribeInstanceHealthInput{
LoadBalancerName: lb.LoadBalancerName,
}
elbhealth, err := elbsvc.DescribeInstanceHealth(elbfilter)
req := svc.DescribeInstanceHealthRequest(elbfilter)
result, err := req.Send()
if err != nil {
fmt.Println("There was and error retreiving ELB Health", err.Error())
log.Fatal(err.Error())
Expand All @@ -53,7 +82,7 @@ func elbList(c *cli.Context) error {
noservice := 0
inservice := 0

for _, inst := range elbhealth.InstanceStates {
for _, inst := range result.InstanceStates {
switch state := *inst.State; state {
case "InService":
inservice++
Expand Down
24 changes: 12 additions & 12 deletions filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)

func generateFilter() []*ec2.Filter {
func generateFilter() []ec2.Filter {

filters := []*ec2.Filter{
&ec2.Filter{
filters := []ec2.Filter{
ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String("running"), aws.String("pending")},
Values: []string{"running", "pending"},
},
}
if awsFilterName != "" {
filters = append(filters, &ec2.Filter{
filters = append(filters, ec2.Filter{
Name: aws.String("tag:Name"),
Values: []*string{aws.String(awsFilterName)},
Values: []string{awsFilterName},
})
}

Expand All @@ -30,18 +30,18 @@ func generateFilter() []*ec2.Filter {
tags := result[i]
t := strings.Split(tags, "=")
if len(t) == 2 {
filters = append(filters, &ec2.Filter{
filters = append(filters, ec2.Filter{
Name: aws.String("tag:" + t[0]),
Values: []*string{aws.String(t[1])},
Values: []string{t[1]},
})
}
}
} else {
fmt.Println("Inside else")
t := strings.Split(awsFilterTags, "=")
filters = append(filters, &ec2.Filter{
filters = append(filters, ec2.Filter{
Name: aws.String("tag:" + t[0]),
Values: []*string{aws.String(t[1])},
Values: []string{t[1]},
})
}
}
Expand Down
41 changes: 27 additions & 14 deletions rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,52 @@ import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/rds"
"github.com/fatih/color"
"github.com/gosuri/uitable"
"github.com/urfave/cli"
)

func printRds(dbs []map[string]string) error {
table := uitable.New()
table.AddRow("NAME", "ENGINE", "VERSION", "IS_PUBLIC", "ENDPOINT")
for _, r := range dbs {
fmt.Printf(
"%-28s\t%s:%s\t%-18s\t%-50s\n",
color.YellowString(r["name"]), r["engine"], r["engine_version"],
r["isPublic"], r["endpoint"])
// fmt.Printf(
// "%-28s\t%s:%s\t%-18s\t%-50s\n",
// color.YellowString(r["name"]), r["engine"], r["engine_version"],
// r["isPublic"], r["endpoint"])
table.AddRow(color.YellowString(r["name"]), r["engine"], r["engine_version"], r["isPublic"], r["endpoint"])
}
fmt.Println(table)
return nil
}

func rdsList(c *cli.Context) error {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: awsProfile,
}))

var rdsList []map[string]string
cfg, err := external.LoadDefaultAWSConfig(
external.WithSharedConfigProfile(awsProfile),
)
if err != nil {
panic("unable to load SDK config, " + err.Error())
}

// TODO: Revisit this... what happens when profile isnt defaulted to us-east-1
cfg.Region = awsRegion

rdsFilter := &rds.DescribeDBInstancesInput{}

rdsSvc := rds.New(sess)
rdsInstances, err := rdsSvc.DescribeDBInstances(rdsFilter)
var rdsList []map[string]string

svc := rds.New(cfg)
req := svc.DescribeDBInstancesRequest(rdsFilter)
resp, err := req.Send()
if err != nil {
fmt.Println("There was an error listing RDS Instances", err.Error())
log.Fatal(err.Error())
}
for _, dbinst := range rdsInstances.DBInstances {

for _, dbinst := range resp.DBInstances {
r := map[string]string{
"name": *dbinst.DBInstanceIdentifier,
"id": *dbinst.DbiResourceId,
Expand Down
Loading

0 comments on commit 83ab02c

Please sign in to comment.