From ad7a36d986fd08e02a9df975f7e904bb36ab9398 Mon Sep 17 00:00:00 2001 From: piusranjan Date: Mon, 21 Aug 2017 20:41:26 +0300 Subject: [PATCH 1/4] This is an example using the AWS SDK for Go to list ec2 instances instance state By different region . By default it fetch all running and stopped instance --- .../service/ec2/instancesbyRegion/README.md | 28 +++++++ .../instancesbyRegion/filter_ec2_by_region.go | 83 +++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 example/service/ec2/instancesbyRegion/README.md create mode 100644 example/service/ec2/instancesbyRegion/filter_ec2_by_region.go diff --git a/example/service/ec2/instancesbyRegion/README.md b/example/service/ec2/instancesbyRegion/README.md new file mode 100644 index 00000000000..fe499b4281f --- /dev/null +++ b/example/service/ec2/instancesbyRegion/README.md @@ -0,0 +1,28 @@ + +# Example Fetch By region + +This is an example using the AWS SDK for Go to list ec2 instances instance state By different region . By default it fetch all running and stopped instance + + +# Usage + + +```sh +go run filter_ec2_by_region running +``` + +Output: +``` + +Fetching instace details for region: ap-south-1 +printing instance details..... +instance id i-00cf3fcsssdd373766 +current State stopped +done for region ap-south-1 **** + + + +Fetching instace details for region: eu-west-2 +There is no instance for the for region: eu-west-2 +done for region eu-west-2 **** + diff --git a/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go b/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go new file mode 100644 index 00000000000..78045ff8f89 --- /dev/null +++ b/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go @@ -0,0 +1,83 @@ +package main + +import ( + "fmt" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go/aws" + "os" +) + +func fetchRegion()[]string{ + + regions := []string{} + sess1 := session.Must(session.NewSession(&aws.Config{ + Region: aws.String("us-west-1"), + })) + + svc := ec2.New(sess1) + awsregions, err := svc.DescribeRegions(&ec2.DescribeRegionsInput{}) + if err != nil { + fmt.Println("Error", err) + } + for _, region := range awsregions.Regions { + regions=append(regions,*region.RegionName) + } + + return regions +} + +func main() { + + States:= []*string{} + if len(os.Args)>1 { + for i := 1; i < len(os.Args); i++ { + States=append(States,&os.Args[i]) + + } + } else { + States= []*string{ + aws.String("running"), + aws.String("pending"), + aws.String("stopped"), + } + } + + + regions:=fetchRegion() + for _, region := range regions { + sess := session.Must(session.NewSession(&aws.Config{ + Region: aws.String(region), + })) + + ec2Svc := ec2.New(sess) + params := &ec2.DescribeInstancesInput{ + Filters: []*ec2.Filter{ + &ec2.Filter{ + Name: aws.String("instance-state-name"), + Values: States, + }, + }, + } + + result, err := ec2Svc.DescribeInstances(params) + if err != nil { + fmt.Println("Error", err) + } else { + fmt.Println("\n\n\nFetching instace details for region: ", region ) + if len(result.Reservations)==0{ + fmt.Println("There is no instance for the for region: ", region ) + } + for _, reservation := range result.Reservations { + + fmt.Println("printing instance details.....") + for _, instance := range reservation.Instances { + fmt.Println("instance id "+*instance.InstanceId) + fmt.Println("current State "+*instance.State.Name) + } + } + fmt.Printf("done for region %s **** \n", region ) + } + } + } + From 2173e0d20544a94f1f8ac671e4a0f5a1d5972d43 Mon Sep 17 00:00:00 2001 From: piusranjan Date: Fri, 25 Aug 2017 09:44:06 +0300 Subject: [PATCH 2/4] Modified as per review comments --- .../service/ec2/instancesbyRegion/README.md | 18 +- .../instancesbyRegion/filter_ec2_by_region.go | 172 +++++++++++------- 2 files changed, 117 insertions(+), 73 deletions(-) diff --git a/example/service/ec2/instancesbyRegion/README.md b/example/service/ec2/instancesbyRegion/README.md index fe499b4281f..0a5f93ff018 100644 --- a/example/service/ec2/instancesbyRegion/README.md +++ b/example/service/ec2/instancesbyRegion/README.md @@ -8,21 +8,25 @@ This is an example using the AWS SDK for Go to list ec2 instances instance state ```sh -go run filter_ec2_by_region running +To fetch the stopped instance of all region use below: + ./filter_ec2_by_region --state running --state stopped +To fetch the stopped and running instance for region us-west-1 and eu-west-1 use below: + ./filter_ec2_by_region --state running --state stopped --region us-west-1 --region=eu-west-1 + ``` -Output: +Sample Output: ``` -Fetching instace details for region: ap-south-1 -printing instance details..... -instance id i-00cf3fcsssdd373766 +Fetching instace details for region: ap-south-1 with criteria: [running][stopped]** + printing instance details..... +instance id i-************ current State stopped done for region ap-south-1 **** -Fetching instace details for region: eu-west-2 -There is no instance for the for region: eu-west-2 +Fetching instace details for region: eu-west-2 with criteria: [running][stopped]** + There is no instance for the for region eu-west-2 with the matching Criteria: [running][stopped] done for region eu-west-2 **** diff --git a/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go b/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go index 78045ff8f89..e443a6f6d2d 100644 --- a/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go +++ b/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go @@ -1,83 +1,123 @@ package main import ( - "fmt" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/ec2" - "github.com/aws/aws-sdk-go/aws" - "os" + "flag" + "fmt" + "os" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ec2" ) -func fetchRegion()[]string{ +func fetchRegion() []string { + + var regions []string + awsSession := session.Must(session.NewSession(&aws.Config{})) + + svc := ec2.New(awsSession) + awsRegions, err := svc.DescribeRegions(&ec2.DescribeRegionsInput{}) + if err != nil { + fmt.Println("Error", err) + } + for _, region := range awsRegions.Regions { + regions = append(regions, *region.RegionName) + } + + return regions +} + +func (i *commandLineArumentType) String() string { + return "my string representation" +} + +func (i *commandLineArumentType) Set(value string) error { + *i = append(*i, value) + return nil +} + +type commandLineArumentType []string - regions := []string{} - sess1 := session.Must(session.NewSession(&aws.Config{ - Region: aws.String("us-west-1"), - })) +func parseArguments() ([]*string, []string) { + var StateArguments, RegionArguments commandLineArumentType + States := []*string{} + Regions := []string{} + flag.Var(&StateArguments, "state", "state list") + flag.Var(&RegionArguments, "region", "region list") + flag.Parse() + if flag.NFlag() != 0 { + for i := 0; i < len(StateArguments); i++ { + States = append(States, &StateArguments[i]) + } - svc := ec2.New(sess1) - awsregions, err := svc.DescribeRegions(&ec2.DescribeRegionsInput{}) - if err != nil { - fmt.Println("Error", err) - } - for _, region := range awsregions.Regions { - regions=append(regions,*region.RegionName) - } + for i := 0; i < len(RegionArguments); i++ { + Regions = append(Regions, RegionArguments[i]) + } + } - return regions + return States, Regions } +func usage() string { + + message := "\n\nMissing mandatory flag 'state'. Please use like below Example :\n\n" + message = message + "To fetch the stopped instance of all region use below:\n" + message = message + "\t./filter_ec2_by_region --state running --state stopped \n" + message = message + "To fetch the stopped and running instance for region us-west-1 and eu-west-1 use below:\n" + message = message + "\t./filter_ec2_by_region --state running --state stopped --region us-west-1 --region=eu-west-1\n" + return message + +} func main() { - States:= []*string{} - if len(os.Args)>1 { - for i := 1; i < len(os.Args); i++ { - States=append(States,&os.Args[i]) - - } - } else { - States= []*string{ - aws.String("running"), - aws.String("pending"), - aws.String("stopped"), - } - } - - - regions:=fetchRegion() - for _, region := range regions { + Regions := []string{} + States := []*string{} + States, Regions = parseArguments() + if len(States) == 0 { + fmt.Fprintf(os.Stderr, "error: %v\n", usage()) + os.Exit(1) + } + InstanceCriteria := " " + for _, State := range States { + InstanceCriteria = InstanceCriteria + "[" + *State + "]" + } + + if len(Regions) == 0 { + Regions = fetchRegion() + } + + for _, region := range Regions { sess := session.Must(session.NewSession(&aws.Config{ Region: aws.String(region), })) - - ec2Svc := ec2.New(sess) + + ec2Svc := ec2.New(sess) params := &ec2.DescribeInstancesInput{ - Filters: []*ec2.Filter{ - &ec2.Filter{ - Name: aws.String("instance-state-name"), - Values: States, - }, - }, - } - - result, err := ec2Svc.DescribeInstances(params) - if err != nil { - fmt.Println("Error", err) - } else { - fmt.Println("\n\n\nFetching instace details for region: ", region ) - if len(result.Reservations)==0{ - fmt.Println("There is no instance for the for region: ", region ) - } - for _, reservation := range result.Reservations { - - fmt.Println("printing instance details.....") - for _, instance := range reservation.Instances { - fmt.Println("instance id "+*instance.InstanceId) - fmt.Println("current State "+*instance.State.Name) - } - } - fmt.Printf("done for region %s **** \n", region ) - } - } - } + Filters: []*ec2.Filter{ + &ec2.Filter{ + Name: aws.String("instance-state-name"), + Values: States, + }, + }, + } + result, err := ec2Svc.DescribeInstances(params) + if err != nil { + fmt.Println("Error", err) + } else { + fmt.Printf("\n\n\nFetching instace details for region: %s with criteria: %s**\n ", region, InstanceCriteria) + if len(result.Reservations) == 0 { + fmt.Printf("There is no instance for the for region %s with the matching Criteria:%s \n", region, InstanceCriteria) + } + for _, reservation := range result.Reservations { + + fmt.Println("printing instance details.....") + for _, instance := range reservation.Instances { + fmt.Println("instance id " + *instance.InstanceId) + fmt.Println("current State " + *instance.State.Name) + } + } + fmt.Printf("done for region %s **** \n", region) + } + } +} From 5cb35f820dd9b5c5dfc3d4ede082b15de240a558 Mon Sep 17 00:00:00 2001 From: Jason Del Ponte Date: Fri, 25 Aug 2017 10:21:47 -0700 Subject: [PATCH 3/4] Minor refactor of style for examples --- .../service/ec2/instancesbyRegion/README.md | 14 +- .../instancesbyRegion/filter_ec2_by_region.go | 123 ---------------- .../instancesbyRegion/instancesByRegion.go | 137 ++++++++++++++++++ 3 files changed, 144 insertions(+), 130 deletions(-) delete mode 100644 example/service/ec2/instancesbyRegion/filter_ec2_by_region.go create mode 100644 example/service/ec2/instancesbyRegion/instancesByRegion.go diff --git a/example/service/ec2/instancesbyRegion/README.md b/example/service/ec2/instancesbyRegion/README.md index 0a5f93ff018..8125a4d5b85 100644 --- a/example/service/ec2/instancesbyRegion/README.md +++ b/example/service/ec2/instancesbyRegion/README.md @@ -8,16 +8,16 @@ This is an example using the AWS SDK for Go to list ec2 instances instance state ```sh -To fetch the stopped instance of all region use below: - ./filter_ec2_by_region --state running --state stopped -To fetch the stopped and running instance for region us-west-1 and eu-west-1 use below: - ./filter_ec2_by_region --state running --state stopped --region us-west-1 --region=eu-west-1 +# To fetch the stopped instance of all region use below: +./filter_ec2_by_region --state running --state stopped +# To fetch the stopped and running instance for region us-west-1 and eu-west-1 use below: +./filter_ec2_by_region --state running --state stopped --region us-west-1 --region=eu-west-1 ``` -Sample Output: -``` +## Sample Output +``` Fetching instace details for region: ap-south-1 with criteria: [running][stopped]** printing instance details..... instance id i-************ @@ -29,4 +29,4 @@ done for region ap-south-1 **** Fetching instace details for region: eu-west-2 with criteria: [running][stopped]** There is no instance for the for region eu-west-2 with the matching Criteria: [running][stopped] done for region eu-west-2 **** - +``` diff --git a/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go b/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go deleted file mode 100644 index e443a6f6d2d..00000000000 --- a/example/service/ec2/instancesbyRegion/filter_ec2_by_region.go +++ /dev/null @@ -1,123 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "os" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/ec2" -) - -func fetchRegion() []string { - - var regions []string - awsSession := session.Must(session.NewSession(&aws.Config{})) - - svc := ec2.New(awsSession) - awsRegions, err := svc.DescribeRegions(&ec2.DescribeRegionsInput{}) - if err != nil { - fmt.Println("Error", err) - } - for _, region := range awsRegions.Regions { - regions = append(regions, *region.RegionName) - } - - return regions -} - -func (i *commandLineArumentType) String() string { - return "my string representation" -} - -func (i *commandLineArumentType) Set(value string) error { - *i = append(*i, value) - return nil -} - -type commandLineArumentType []string - -func parseArguments() ([]*string, []string) { - var StateArguments, RegionArguments commandLineArumentType - States := []*string{} - Regions := []string{} - flag.Var(&StateArguments, "state", "state list") - flag.Var(&RegionArguments, "region", "region list") - flag.Parse() - if flag.NFlag() != 0 { - for i := 0; i < len(StateArguments); i++ { - States = append(States, &StateArguments[i]) - } - - for i := 0; i < len(RegionArguments); i++ { - Regions = append(Regions, RegionArguments[i]) - } - } - - return States, Regions -} - -func usage() string { - - message := "\n\nMissing mandatory flag 'state'. Please use like below Example :\n\n" - message = message + "To fetch the stopped instance of all region use below:\n" - message = message + "\t./filter_ec2_by_region --state running --state stopped \n" - message = message + "To fetch the stopped and running instance for region us-west-1 and eu-west-1 use below:\n" - message = message + "\t./filter_ec2_by_region --state running --state stopped --region us-west-1 --region=eu-west-1\n" - return message - -} -func main() { - - Regions := []string{} - States := []*string{} - States, Regions = parseArguments() - if len(States) == 0 { - fmt.Fprintf(os.Stderr, "error: %v\n", usage()) - os.Exit(1) - } - InstanceCriteria := " " - for _, State := range States { - InstanceCriteria = InstanceCriteria + "[" + *State + "]" - } - - if len(Regions) == 0 { - Regions = fetchRegion() - } - - for _, region := range Regions { - sess := session.Must(session.NewSession(&aws.Config{ - Region: aws.String(region), - })) - - ec2Svc := ec2.New(sess) - params := &ec2.DescribeInstancesInput{ - Filters: []*ec2.Filter{ - &ec2.Filter{ - Name: aws.String("instance-state-name"), - Values: States, - }, - }, - } - - result, err := ec2Svc.DescribeInstances(params) - if err != nil { - fmt.Println("Error", err) - } else { - fmt.Printf("\n\n\nFetching instace details for region: %s with criteria: %s**\n ", region, InstanceCriteria) - if len(result.Reservations) == 0 { - fmt.Printf("There is no instance for the for region %s with the matching Criteria:%s \n", region, InstanceCriteria) - } - for _, reservation := range result.Reservations { - - fmt.Println("printing instance details.....") - for _, instance := range reservation.Instances { - fmt.Println("instance id " + *instance.InstanceId) - fmt.Println("current State " + *instance.State.Name) - } - } - fmt.Printf("done for region %s **** \n", region) - } - } -} diff --git a/example/service/ec2/instancesbyRegion/instancesByRegion.go b/example/service/ec2/instancesbyRegion/instancesByRegion.go new file mode 100644 index 00000000000..5da4c7a9436 --- /dev/null +++ b/example/service/ec2/instancesbyRegion/instancesByRegion.go @@ -0,0 +1,137 @@ +// build example + +package main + +import ( + "flag" + "fmt" + "os" + "strings" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ec2" +) + +// Prints a list of instances for each region. If no regions are provided +// all regions will be searched. The state is required. +// +// Will use the AWS SDK for Go's default credential chain and region. You can +// specify the region with the AWS_REGION environment variable. +// +// Usage: instancesByRegion -state [-state val...] [-region region...] +func main() { + states, regions := parseArguments() + + if len(states) == 0 { + fmt.Fprintf(os.Stderr, "error: %v\n", usage()) + os.Exit(1) + } + instanceCriteria := " " + for _, state := range states { + instanceCriteria += "[" + state + "]" + } + + if len(regions) == 0 { + var err error + regions, err = fetchRegion() + if err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + os.Exit(1) + } + } + + for _, region := range regions { + sess := session.Must(session.NewSession(&aws.Config{ + Region: aws.String(region), + })) + + ec2Svc := ec2.New(sess) + params := &ec2.DescribeInstancesInput{ + Filters: []*ec2.Filter{ + &ec2.Filter{ + Name: aws.String("instance-state-name"), + Values: aws.StringSlice(states), + }, + }, + } + + result, err := ec2Svc.DescribeInstances(params) + if err != nil { + fmt.Println("Error", err) + } else { + fmt.Printf("\n\n\nFetching instace details for region: %s with criteria: %s**\n ", region, instanceCriteria) + if len(result.Reservations) == 0 { + fmt.Printf("There is no instance for the for region %s with the matching Criteria:%s \n", region, instanceCriteria) + } + for _, reservation := range result.Reservations { + + fmt.Println("printing instance details.....") + for _, instance := range reservation.Instances { + fmt.Println("instance id " + *instance.InstanceId) + fmt.Println("current State " + *instance.State.Name) + } + } + fmt.Printf("done for region %s **** \n", region) + } + } +} + +func fetchRegion() ([]string, error) { + awsSession := session.Must(session.NewSession(&aws.Config{})) + + svc := ec2.New(awsSession) + awsRegions, err := svc.DescribeRegions(&ec2.DescribeRegionsInput{}) + if err != nil { + return nil, err + } + + regions := make([]string, 0, len(awsRegions.Regions)) + for _, region := range awsRegions.Regions { + regions = append(regions, *region.RegionName) + } + + return regions, nil +} + +type flagArgs []string + +func (a flagArgs) String() string { + return strings.Join(a.Args(), ",") +} + +func (a *flagArgs) Set(value string) error { + *a = append(*a, value) + return nil +} +func (a flagArgs) Args() []string { + return []string(a) +} + +func parseArguments() (states []string, regions []string) { + var stateArgs, regionArgs flagArgs + + flag.Var(&stateArgs, "state", "state list") + flag.Var(®ionArgs, "region", "region list") + flag.Parse() + + if flag.NFlag() != 0 { + states = append([]string{}, stateArgs.Args()...) + regions = append([]string{}, regionArgs.Args()...) + } + + return states, regions +} + +func usage() string { + return ` + +Missing mandatory flag 'state'. Please use like below Example: + +To fetch the stopped instance of all region use below: + ./filter_ec2_by_region -state running -state stopped + +To fetch the stopped and running instance for region us-west-1 and eu-west-1 use below: + ./filter_ec2_by_region -state running -state stopped -region us-west-1 -region=eu-west-1 +` +} From 8ef1b3c4bd65455628895f4cf492845680724cad Mon Sep 17 00:00:00 2001 From: Jason Del Ponte Date: Fri, 25 Aug 2017 10:25:38 -0700 Subject: [PATCH 4/4] add example build tag --- example/service/ec2/instancesbyRegion/instancesByRegion.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/service/ec2/instancesbyRegion/instancesByRegion.go b/example/service/ec2/instancesbyRegion/instancesByRegion.go index 5da4c7a9436..869b28ab64c 100644 --- a/example/service/ec2/instancesbyRegion/instancesByRegion.go +++ b/example/service/ec2/instancesbyRegion/instancesByRegion.go @@ -1,4 +1,4 @@ -// build example +// +build example package main