Skip to content

Commit

Permalink
Merge pull request #3 from goldeneggg/add_tests
Browse files Browse the repository at this point in the history
refactoring and add tests
  • Loading branch information
goldeneggg authored Sep 25, 2016
2 parents aa11912 + 7e4b121 commit e3628d7
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ before_install:
- GO15VENDOREXPERIMENT=1 godep restore -v

script:
- make test
- make test-all

sudo: false
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $ godep version

### Run tests

* Run `make test`
* Run `make test-all`

### Run vet

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build:
@echo "Building ${GOBIN}/$(BINNAME)"
@GO15VENDOREXPERIMENT=1 godep go build -o ${GOBIN}/$(BINNAME) $(PGM_PATH)

test:
test-all:
@echo "Testing"
@GO15VENDOREXPERIMENT=1 godep go test -race -v $(PGM_PATH)
@GO15VENDOREXPERIMENT=1 godep go test -race -v $(PGM_PATH)/awsec2...
Expand Down
53 changes: 12 additions & 41 deletions awsec2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
)

import (
"github.com/goldeneggg/lsec2/constants"
)

type Client struct {
PrintHeader bool
OnlyPrivateIP bool
Expand All @@ -31,7 +27,7 @@ func (client *Client) Print() error {
return nil
}

func (client *Client) buildInfos() ([]*instanceInfo, error) {
func (client *Client) buildInfos() ([]*InstanceInfo, error) {
sess, err := session.NewSession(&aws.Config{Region: aws.String(client.Region)})
if err != nil {
return nil, fmt.Errorf("aws new session error: %v", err)
Expand All @@ -44,11 +40,15 @@ func (client *Client) buildInfos() ([]*instanceInfo, error) {
return nil, fmt.Errorf("aws describe instances error: %v", err)
}

var infos []*instanceInfo
var infos []*InstanceInfo

for _, reservation := range output.Reservations {
for _, instance := range reservation.Instances {
infos = append(infos, newInstanceInfo(instance))
info, err := NewInstanceInfo(instance)
if err != nil {
return nil, err
}
infos = append(infos, info)
}
}

Expand All @@ -62,10 +62,10 @@ func (client *Client) filterParams() *ec2.DescribeInstancesInput {
// ex. "Name=Value"
// ex. "Name=Value1,Value2"
for _, tag := range client.Tags {
tagNameValue := strings.Split(tag, constants.TagPairSeparator)
name := aws.String(constants.TagFilterPrefix + tagNameValue[0])
tagNameValue := strings.Split(tag, TagPairSeparator)
name := aws.String(TagFilterPrefix + tagNameValue[0])
values := make([]*string, 0, 3)
for _, value := range strings.Split(tagNameValue[1], constants.TagValueSeparator) {
for _, value := range strings.Split(tagNameValue[1], TagValueSeparator) {
values = append(values, aws.String(value))
}

Expand All @@ -83,45 +83,16 @@ func (client *Client) filterParams() *ec2.DescribeInstancesInput {
return &ec2.DescribeInstancesInput{Filters: filters}
}

func (client *Client) printInfos(infos []*instanceInfo) {
func (client *Client) printInfos(infos []*InstanceInfo) {
if client.PrintHeader {
infos[0].printHeader()
}

for _, info := range infos {
if client.OnlyPrivateIP {
fmt.Printf("%s\n", info.privateIPAddress)
fmt.Printf("%s\n", info.PrivateIPAddress)
} else {
info.printRow()
}
}
}

func newInstanceInfo(instance *ec2.Instance) *instanceInfo {
i := &instanceInfo{
privateIPAddress: fetchItem(instance.PrivateIpAddress),
instanceID: fetchItem(instance.InstanceId),
instanceType: fetchItem(instance.InstanceType),
stateName: fetchItem(instance.State.Name),
publicIPAddress: fetchItem(instance.PublicIpAddress),
}

tags := make(map[string]string)
for _, tag := range instance.Tags {
tags[*tag.Key] = *tag.Value
}

if len(tags) > 0 {
i.tags = tags
}

return i
}

func fetchItem(instanceItem *string) string {
if len(aws.StringValue(instanceItem)) == 0 {
return constants.UndefinedItem
}

return *instanceItem
}
10 changes: 4 additions & 6 deletions constants/const.go → awsec2/const.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package constants

const (
ExitStsOk = iota
ExitStsNg
)
package awsec2

const (
TagFilterPrefix = "tag:"
TagPairSeparator = "="
TagValueSeparator = ","

ParsedTagSeparator = ","
FieldSeparater = "\t"

UndefinedItem = "UNDEFINED"
)
94 changes: 63 additions & 31 deletions awsec2/instance_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,94 @@ import (
"reflect"
"sort"
"strings"
)

const (
fieldSeparater = "\t"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)

type instanceInfo struct {
instanceID string `header:"INSTANCE_ID"`
privateIPAddress string `header:"PRIVATE_IP"`
publicIPAddress string `header:"PUBLIC_IP"`
instanceType string `header:"TYPE"`
stateName string `header:"STATE"`
tags map[string]string `header:"TAGS"`
type InstanceInfo struct {
InstanceID string `header:"INSTANCE_ID"`
PrivateIPAddress string `header:"PRIVATE_IP"`
PublicIPAddress string `header:"PUBLIC_IP"`
InstanceType string `header:"TYPE"`
StateName string `header:"STATE"`
Tags map[string]string `header:"TAGS"`
}

func (info *instanceInfo) printHeader() {
var headers []string
func NewInstanceInfo(instance *ec2.Instance) (*InstanceInfo, error) {
if instance == nil {
return nil, fmt.Errorf("ec2.Instance: %v is invalid", instance)
}

rt := reflect.TypeOf(*info)
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
headers = append(headers, field.Tag.Get("header"))
i := &InstanceInfo{
InstanceID: fetchItem(instance.InstanceId),
PrivateIPAddress: fetchItem(instance.PrivateIpAddress),
PublicIPAddress: fetchItem(instance.PublicIpAddress),
InstanceType: fetchItem(instance.InstanceType),
StateName: fetchItem(instance.State.Name),
}

fmt.Printf("%s\n", strings.Join(headers, fieldSeparater))
}
tags := make(map[string]string)
for _, tag := range instance.Tags {
tags[*tag.Key] = *tag.Value
}

if len(tags) > 0 {
i.Tags = tags
}

func (info *instanceInfo) printRow() {
fmt.Printf("%s\n", info.parseRow())
return i, nil
}

func (info *instanceInfo) parseRow() string {
func (info *InstanceInfo) ParseRow() string {
var values []string

values = append(values, info.instanceID)
values = append(values, info.privateIPAddress)
values = append(values, info.publicIPAddress)
values = append(values, info.instanceType)
values = append(values, info.stateName)
values = append(values, info.InstanceID)
values = append(values, info.PrivateIPAddress)
values = append(values, info.PublicIPAddress)
values = append(values, info.InstanceType)
values = append(values, info.StateName)

values = append(values, strings.Join(info.parseTags(), ","))
values = append(values, strings.Join(info.parseTags(), ParsedTagSeparator))

return fmt.Sprintf("%s", strings.Join(values, fieldSeparater))
return fmt.Sprintf("%s", strings.Join(values, FieldSeparater))
}

func (info *instanceInfo) parseTags() []string {
func (info *InstanceInfo) printHeader() {
var headers []string

rt := reflect.TypeOf(*info)
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
headers = append(headers, field.Tag.Get("header"))
}

fmt.Printf("%s\n", strings.Join(headers, FieldSeparater))
}

func (info *InstanceInfo) printRow() {
fmt.Printf("%s\n", info.ParseRow())
}

func (info *InstanceInfo) parseTags() []string {
var tagNames []string
for name := range info.tags {
for name := range info.Tags {
tagNames = append(tagNames, name)
}
sort.Strings(tagNames)

var tags []string
for _, name := range tagNames {
tags = append(tags, name+"="+info.tags[name])
tags = append(tags, name+"="+info.Tags[name])
}

return tags
}

func fetchItem(instanceItem *string) string {
if len(aws.StringValue(instanceItem)) == 0 {
return UndefinedItem
}

return *instanceItem
}
Loading

0 comments on commit e3628d7

Please sign in to comment.