|
@ -51,35 +51,38 @@ func getInstanceName(instance *ec2.Instance) string { |
|
|
|
|
|
|
|
|
// Helper function to filter the displayed instances by filtered state.
|
|
|
// Helper function to filter the displayed instances by filtered state.
|
|
|
func fetchAndDisplayInstances(state string, svc *ec2.EC2, list *tview.List, detailsTable *tview.Table) { |
|
|
func fetchAndDisplayInstances(state string, svc *ec2.EC2, list *tview.List, detailsTable *tview.Table) { |
|
|
var filterName string |
|
|
// Init params struct to pass to AWS SDK.
|
|
|
switch state { |
|
|
params := &ec2.DescribeInstancesInput{} |
|
|
case "running": |
|
|
|
|
|
filterName = "running" |
|
|
// Validate provided state.
|
|
|
case "stopped": |
|
|
validStates := map[string]bool{ |
|
|
filterName = "stopped" |
|
|
"running": true, |
|
|
case "terminated": |
|
|
"stopped": true, |
|
|
filterName = "terminated" |
|
|
"terminated": true, |
|
|
default: |
|
|
|
|
|
filterName = "any" |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
params := &ec2.DescribeInstancesInput{} |
|
|
// If a valid state is provided, then set the filter, else fetch all instances.
|
|
|
if filterName != "any" { |
|
|
// If not, no filter is set and all instances will be fetched.
|
|
|
|
|
|
if validStates[state] { |
|
|
params.Filters = []*ec2.Filter{ |
|
|
params.Filters = []*ec2.Filter{ |
|
|
{ |
|
|
{ |
|
|
Name: aws.String("instance-state-name"), |
|
|
Name: aws.String("instance-state-name"), |
|
|
Values: []*string{aws.String(filterName)}, |
|
|
Values: []*string{aws.String(state)}, |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Log error if API call to fetch instances fails.
|
|
|
resp, err := svc.DescribeInstances(params) |
|
|
resp, err := svc.DescribeInstances(params) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Fatalf("Failed to retrieve EC2 instances: %s", err) |
|
|
log.Fatalf("Failed to retrieve EC2 instances: %s", err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Clear the current list and set currentInstances slice to zero.
|
|
|
list.Clear() |
|
|
list.Clear() |
|
|
currentInstances = nil |
|
|
currentInstances = nil |
|
|
|
|
|
|
|
|
|
|
|
// Use filter state to fetch instance list and append to currentInstances slice.
|
|
|
for _, res := range resp.Reservations { |
|
|
for _, res := range resp.Reservations { |
|
|
for _, instance := range res.Instances { |
|
|
for _, instance := range res.Instances { |
|
|
currentInstances = append(currentInstances, instance) |
|
|
currentInstances = append(currentInstances, instance) |
|
@ -87,14 +90,18 @@ func fetchAndDisplayInstances(state string, svc *ec2.EC2, list *tview.List, deta |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if len(currentInstances) > 0 { |
|
|
// If no instances exist for filter, clear the instanceDetails table. Otherwise display details for the first instance found.
|
|
|
|
|
|
if len(currentInstances) == 0 { |
|
|
|
|
|
detailsTable.Clear() |
|
|
|
|
|
detailsTable.SetCell(0, 0, tview.NewTableCell("No instance details to display").SetAlign(tview.AlignCenter)) |
|
|
|
|
|
} else { |
|
|
displayInstanceDetails(detailsTable, currentInstances[0]) |
|
|
displayInstanceDetails(detailsTable, currentInstances[0]) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Helper function to display the instance details of the first instance in the list. Function executes when application first loads.
|
|
|
// Helper function to display the instance details of the first instance in the list. Function executes when application first loads.
|
|
|
func initialDisplay(svc *ec2.EC2, list *tview.List, detailsTable *tview.Table) { |
|
|
func initialDisplay(svc *ec2.EC2, list *tview.List, detailsTable *tview.Table) { |
|
|
fetchAndDisplayInstances("running", svc, list, detailsTable) |
|
|
fetchAndDisplayInstances("", svc, list, detailsTable) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Helper function to display the instance details.
|
|
|
// Helper function to display the instance details.
|
|
|