-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/omerh/awsctl/pkg/helpers" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
getRiExample = ` | ||
# Get amazon resources | ||
get ri --status active --region us-east-1 | ||
` | ||
getRihort = ("Get AWS EC2 Reservation Details") | ||
) | ||
|
||
var getRiCmd = &cobra.Command{ | ||
Use: "ri", | ||
Short: getRihort, | ||
Example: getRiExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
region, _ := cmd.Flags().GetString("region") | ||
|
||
if region == "all" { | ||
awsRegions, _ := helpers.GetAllAwsRegions() | ||
for _, r := range awsRegions { | ||
riSummary, ec2Summary := generateRIreport(r) | ||
utilizationReport(riSummary, ec2Summary, r) | ||
} | ||
return | ||
} | ||
|
||
if region == "" { | ||
region = helpers.GetDefaultAwsRegion() | ||
} | ||
|
||
riSummary, ec2Summary := generateRIreport(region) | ||
utilizationReport(riSummary, ec2Summary, region) | ||
}, | ||
} | ||
|
||
func generateRIreport(region string) (map[string]int64, map[string]int64) { | ||
riSummary := helpers.GetAllReservations(region, "active") | ||
allEC2 := helpers.GetAllEC2Instances(region, "ondemand", "running") | ||
ec2Summary := helpers.SummeriesEC2Instances(allEC2) | ||
return riSummary, ec2Summary | ||
} | ||
|
||
func utilizationReport(ri map[string]int64, ec2 map[string]int64, region string) { | ||
fmt.Printf("Reservation status in %v (Only for running instances)\n", region) | ||
fmt.Println("=================================================================") | ||
for instanceType, instanceCount := range ec2 { | ||
fmt.Printf("- You have %v %v with a resevation of %v\n", instanceCount, instanceType, ri[instanceType]) | ||
} | ||
fmt.Println() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package helpers | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
) | ||
|
||
type riSummary struct { | ||
instanceType string | ||
instanceCount int | ||
} | ||
|
||
// GetAllReservations retrive all reservations | ||
func GetAllReservations(region string, state string) map[string]int64 { | ||
awsSession, _ := InitAwsSession(region) | ||
svc := ec2.New(awsSession) | ||
filter := []*ec2.Filter{ | ||
{ | ||
Name: aws.String("state"), | ||
Values: []*string{ | ||
aws.String(state), | ||
}, | ||
}, | ||
} | ||
|
||
input := &ec2.DescribeReservedInstancesInput{ | ||
Filters: filter, | ||
} | ||
|
||
result, _ := svc.DescribeReservedInstances(input) | ||
summaryResult := summeriesRI(result) | ||
return summaryResult | ||
} | ||
|
||
func summeriesRI(ri *ec2.DescribeReservedInstancesOutput) map[string]int64 { | ||
summary := make(map[string]int64, len(ri.ReservedInstances)) | ||
for _, r := range ri.ReservedInstances { | ||
i := summary[*r.InstanceType] | ||
summary[*r.InstanceType] = i + *r.InstanceCount | ||
} | ||
return summary | ||
} |