Skip to content

Commit

Permalink
Converting to use aws sdk v2
Browse files Browse the repository at this point in the history
  • Loading branch information
undeadops committed Jul 22, 2018
1 parent 00fab52 commit 1fb0b3a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 48 deletions.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2
48 changes: 35 additions & 13 deletions ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ 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/urfave/cli"
)

Expand All @@ -25,19 +26,27 @@ func ec2List(c *cli.Context) error {
fmt.Println("EC2 List Instances")
fmt.Println("AWS Config Profile: ", awsProfile)

var ec2instances []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

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)
if err != nil {

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())
log.Fatal(err.Error())
}
Expand All @@ -51,25 +60,38 @@ func ec2List(c *cli.Context) error {
for _, keys := range instances.Tags {
if *keys.Key == "Name" {
name = url.QueryEscape(*keys.Value)
} else {
name = "None"
}
if *keys.Key == "cost_center" {
costCenter = url.QueryEscape(*keys.Value)
} else {
costCenter = "Not Defined"
}
if *keys.Key == "env" {
env = url.QueryEscape(*keys.Value)
} else {
env = "unknown"
}
if *keys.Key == "role" {
role = url.QueryEscape(*keys.Value)
} else {
role = "Not Listed"
}
}

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
49 changes: 36 additions & 13 deletions elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ 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/fatih/color"
"github.com/urfave/cli"
)
Expand All @@ -22,29 +24,50 @@ func printElb(elb []map[string]string) error {
}

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 +76,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
28 changes: 18 additions & 10 deletions rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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/urfave/cli"
)
Expand All @@ -21,22 +21,30 @@ func printRds(dbs []map[string]string) error {
}

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

0 comments on commit 1fb0b3a

Please sign in to comment.