From 11bee22359ea881850026f57d31fa0a72311a276 Mon Sep 17 00:00:00 2001 From: Robert Willard Date: Tue, 16 Jun 2015 12:02:49 -0700 Subject: [PATCH 1/6] change to privateIP --- aws.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws.go b/aws.go index bc65cf6..3a48f05 100644 --- a/aws.go +++ b/aws.go @@ -34,7 +34,7 @@ func getEC2Instances(config map[string]string) (instances Instances) { for _, res := range resp.Reservations { for _, inst := range res.Instances { - if inst.DNSName != "" { + if inst.PrivateIPAddress != "" { var tags []Tag for _, tag := range inst.Tags { @@ -45,7 +45,7 @@ func getEC2Instances(config map[string]string) (instances Instances) { tags = append(tags, Tag{"Security group", sg.Name}) } - instances[inst.DNSName] = tags + instances[inst.PrivateIPAddress] = tags } } } From 498aec45e27c179ce9c719135dd655e68b3b7004 Mon Sep 17 00:00:00 2001 From: Robert Willard Date: Tue, 16 Jun 2015 13:06:37 -0700 Subject: [PATCH 2/6] Display instance name --- main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index aaccf82..9be1eae 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,7 @@ func getMatchedInstances(clouds CloudInstances, filter string) (matched []StrMap "addr": addr, "tag_name": tag.Name, "tag_value": tag.Value, + "instance_name": getInstanceName(tags), }) break @@ -92,7 +93,14 @@ func getMatchedInstances(clouds CloudInstances, filter string) (matched []StrMap } func formatMatchedInstance(inst StrMap) string { - return "Cloud: " + inst["cloud"] + "\tMatched by: " + inst["tag_name"] + "=" + inst["tag_value"] + "\tAddr: " + inst["addr"] + return "Name: " + inst["instance_name"] + "\tMatched by: " + inst["tag_name"] + "=" + inst["tag_value"] + "\tAddr: " + inst["addr"] +} + +func getInstanceName(tags []Tag) string { + for _, tag := range tags { + if tag.Name == "Name" { return tag.Value } + } + return "" } func main() { From 2428064b074d836af29064deee24051adbd97d28 Mon Sep 17 00:00:00 2001 From: Robert Willard Date: Tue, 16 Jun 2015 14:27:42 -0700 Subject: [PATCH 3/6] Add output formatting in config --- main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 9be1eae..542b0bd 100644 --- a/main.go +++ b/main.go @@ -92,8 +92,13 @@ func getMatchedInstances(clouds CloudInstances, filter string) (matched []StrMap return } -func formatMatchedInstance(inst StrMap) string { - return "Name: " + inst["instance_name"] + "\tMatched by: " + inst["tag_name"] + "=" + inst["tag_value"] + "\tAddr: " + inst["addr"] +func formatMatchedInstance(inst StrMap, output string) string { + c := strings.Fields(output) + s := make([]string, len(c)) + for i := 0; i < len(c); i++ { + s[i] = strings.Title(c[i]) + ": " + inst[c[i]] + } + return strings.Join(s, "\t") } func getInstanceName(tags []Tag) string { @@ -121,7 +126,7 @@ func main() { matched_instance = match[0] } else { for i, host := range match { - fmt.Println(strconv.Itoa(i+1)+") ", formatMatchedInstance(host)) + fmt.Println(strconv.Itoa(i+1)+") ", formatMatchedInstance(host, config[host["cloud"]]["output_format"])) } fmt.Print("Choose instance: ") @@ -144,7 +149,7 @@ func main() { } fmt.Println("Connecting to instance:") - fmt.Println(formatMatchedInstance(matched_instance)) + fmt.Println(formatMatchedInstance(matched_instance, config[matched_instance["cloud"]]["output_format"])) } if len(args) == 0 { From 7ff8df68baab7e99da576b0b020bae2fdbce100f Mon Sep 17 00:00:00 2001 From: Robert Willard Date: Tue, 16 Jun 2015 15:51:24 -0700 Subject: [PATCH 4/6] Output format specified by user --- main.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 542b0bd..28e5c0f 100644 --- a/main.go +++ b/main.go @@ -93,12 +93,20 @@ func getMatchedInstances(clouds CloudInstances, filter string) (matched []StrMap } func formatMatchedInstance(inst StrMap, output string) string { - c := strings.Fields(output) - s := make([]string, len(c)) - for i := 0; i < len(c); i++ { - s[i] = strings.Title(c[i]) + ": " + inst[c[i]] + c := strings.SplitAfter(output, "{") + for i := 1; i < len(c); i++ { + s := strings.SplitN(c[i], "}", 2) + c[i] = getStringValue(inst, s[0]) + output = strings.Replace(output, "{" + s[0] + "}", c[i], -1) } - return strings.Join(s, "\t") + return output +} + +func getStringValue(inst StrMap, s string) string{ + if len(inst[s]) > 0 { + return inst[s] + } + return "{" + s + "}" } func getInstanceName(tags []Tag) string { From 3d7e9587959cf66e180f5f2dec33efa63f7a64b5 Mon Sep 17 00:00:00 2001 From: Robert Willard Date: Tue, 16 Jun 2015 16:21:53 -0700 Subject: [PATCH 5/6] specify connection type --- aws.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/aws.go b/aws.go index 3a48f05..636807f 100644 --- a/aws.go +++ b/aws.go @@ -34,7 +34,7 @@ func getEC2Instances(config map[string]string) (instances Instances) { for _, res := range resp.Reservations { for _, inst := range res.Instances { - if inst.PrivateIPAddress != "" { + if inst.DNSName != "" { var tags []Tag for _, tag := range inst.Tags { @@ -44,8 +44,16 @@ func getEC2Instances(config map[string]string) (instances Instances) { for _, sg := range inst.SecurityGroups { tags = append(tags, Tag{"Security group", sg.Name}) } - - instances[inst.PrivateIPAddress] = tags + ci := config["connection_interface"] + if ci == "private_ip" { + instances[inst.PrivateIPAddress] = tags + } else if ci == "public_ip" { + instances[inst.IPAddress] = tags + } else if ci == "private_dns" { + instances[inst.PrivateDNSName] = tags + } else { + instances[inst.DNSName] = tags + } } } } From b6632d8940dce47c350a540d6cc1ddaaee683194 Mon Sep 17 00:00:00 2001 From: Robert Willard Date: Tue, 16 Jun 2015 16:31:39 -0700 Subject: [PATCH 6/6] added defaults --- aws.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aws.go b/aws.go index 636807f..9b1f641 100644 --- a/aws.go +++ b/aws.go @@ -21,6 +21,10 @@ func getEC2Instances(config map[string]string) (instances Instances) { config["region"] = "us-east-1" } + if _, ok := config["output_format"]; !ok { + config["output_format"] = "Cloud: {cloud} \tMatched by: {tag_name} = {tag_value} \tAddr: {addr}" + } + auth := aws.Auth{AccessKey: config["access_key"], SecretKey: config["secret_key"]} e := ec2.New(auth, aws.Regions[config["region"]])