polemos/mtdaws/utils.go

102 lines
3.2 KiB
Go
Raw Normal View History

2023-03-21 13:34:52 +01:00
package mtdaws
2023-03-14 15:12:28 +01:00
import (
"context"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
2023-03-21 13:34:52 +01:00
"github.com/thefeli73/polemos/state"
2023-03-14 15:12:28 +01:00
)
2023-03-21 13:34:52 +01:00
2023-03-22 10:47:29 +01:00
// AwsInstance is basic info about a single aws instance (instance id, redion, pubIP and privIP)
type AwsInstance struct {
InstanceID string
Region string
PublicIP string
PrivateIP string
}
2023-03-21 13:34:52 +01:00
// NewConfig creates a AWS config for a specific region
func NewConfig(region string, credentials string) aws.Config {
2023-03-20 15:43:14 +01:00
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigFiles([]string{credentials}), config.WithRegion(region))
if err != nil {
fmt.Println("Error creating config:", err)
fmt.Println("Configure Credentials in line with the documentation found here: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials")
os.Exit(1)
}
return cfg
}
2023-03-21 13:34:52 +01:00
// GetCloudID returns a string to find the instance based on information from aws
func GetCloudID(instance AwsInstance) string {
return "aws_" + instance.Region + "_" + instance.InstanceID
}
// GetInstances scans all configured regions for instances and add them to services
func GetInstances(config state.Config) []AwsInstance {
2023-03-22 10:47:29 +01:00
awsInstances := []AwsInstance{}
2023-03-21 13:34:52 +01:00
for _, region := range config.AWS.Regions {
awsConfig := NewConfig(region, config.AWS.CredentialsPath)
instances, err := Instances(awsConfig)
if err != nil {
fmt.Println("Error listing instances:", err)
continue
}
2023-03-22 10:47:29 +01:00
//fmt.Println("Listing instances in region:", region)
2023-03-21 13:34:52 +01:00
for _, instance := range instances {
2023-03-22 10:47:29 +01:00
var publicAddr string
if instance.PublicIpAddress != nil {
publicAddr = aws.ToString(instance.PublicIpAddress)
}
awsInstances = append(awsInstances, AwsInstance{
InstanceID: aws.ToString(instance.InstanceId),
Region: region,
PublicIP: publicAddr,
PrivateIP: aws.ToString(instance.PrivateIpAddress)})
2023-03-21 13:34:52 +01:00
}
}
2023-03-22 10:47:29 +01:00
return awsInstances
2023-03-21 13:34:52 +01:00
}
2023-03-22 10:47:29 +01:00
// PrintInstanceInfo prints info about a specific instance in a region
func PrintInstanceInfo(instance *types.Instance) {
fmt.Println("\tInstance ID:", aws.ToString(instance.InstanceId))
fmt.Println("\t\tInstance Type:", string(instance.InstanceType))
fmt.Println("\t\tAMI ID:", aws.ToString(instance.ImageId))
fmt.Println("\t\tState:", string(instance.State.Name))
fmt.Println("\t\tAvailability Zone:", aws.ToString(instance.Placement.AvailabilityZone))
if instance.PublicIpAddress != nil {
2023-03-22 10:47:29 +01:00
fmt.Println("\t\tPublic IP Address:", aws.ToString(instance.PublicIpAddress))
}
2023-03-22 10:47:29 +01:00
fmt.Println("\t\tPrivate IP Address:", aws.ToString(instance.PrivateIpAddress))
}
2023-03-21 13:34:52 +01:00
// Instances returns all instances for a config i.e. a region
2023-03-22 15:46:49 +01:00
func Instances(config aws.Config) ([]types.Instance, error) {
svc := ec2.NewFromConfig(config)
input := &ec2.DescribeInstancesInput{}
2023-03-22 15:46:49 +01:00
var instances []types.Instance
paginator := ec2.NewDescribeInstancesPaginator(svc, input)
for paginator.HasMorePages() {
page, err := paginator.NextPage(context.TODO())
if err != nil {
return nil, err
}
for _, reservation := range page.Reservations {
for _, instance := range reservation.Instances {
2023-03-22 15:46:49 +01:00
instances = append(instances, instance)
}
}
}
return instances, nil
2023-03-22 14:24:56 +01:00
}