Refactor, move function to sdk

This commit is contained in:
schulze 2023-03-21 13:34:52 +01:00
parent af47f13dcf
commit 6c3e2acabb
2 changed files with 34 additions and 29 deletions

29
main.go
View File

@ -4,39 +4,18 @@ import (
"fmt" "fmt"
"os" "os"
uuid "github.com/nu7hatch/gouuid" "github.com/thefeli73/polemos/mtdaws"
"github.com/thefeli73/polemos/mtd_aws"
"github.com/thefeli73/polemos/state" "github.com/thefeli73/polemos/state"
) )
func main() { func main() {
fmt.Println("Starting Polemos") fmt.Println("Starting Polemos")
config, err := state.Load_conf("config.yaml") config, err := state.LoadConf("config.yaml")
if err != nil { if err != nil {
fmt.Println("Error loading config:", err) fmt.Println("Error loading config:", err)
os.Exit(1) os.Exit(1)
} }
//aws_config := mtd_aws.New_config(config.AWS.Region) mtdaws.IndexInstances(config)
//mtd_aws.Instance_info(aws_config, config.AWS.InstanceID)
//mtd_aws.Instances(config.AWS.Region)
u, _ := uuid.NewV4() //blank is to send errors to the void
_=u
//fmt.Println(u)
for _, region := range config.AWS.Regions {
fmt.Println("Listing instances in region:", region)
aws_config := mtd_aws.New_config(region, config.AWS.Credentials_path)
instances, err := mtd_aws.Instances(aws_config)
if err != nil {
fmt.Println("Error listing instances:", err)
continue
}
for _, instance := range instances {
mtd_aws.Instance_info(aws_config, *instance.InstanceId)
}
}
} }

View File

@ -1,4 +1,4 @@
package mtd_aws package mtdaws
import ( import (
"context" "context"
@ -9,8 +9,12 @@ import (
"github.com/aws/aws-sdk-go-v2/config" "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"
"github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/google/uuid"
"github.com/thefeli73/polemos/state"
) )
func New_config(region string, credentials string) aws.Config {
// NewConfig creates a AWS config for a specific region
func NewConfig(region string, credentials string) aws.Config {
// Create a new AWS config // Create a new AWS config
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigFiles([]string{credentials}), config.WithRegion(region)) cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigFiles([]string{credentials}), config.WithRegion(region))
if err != nil { if err != nil {
@ -20,7 +24,28 @@ func New_config(region string, credentials string) aws.Config {
} }
return cfg return cfg
} }
func Instance_info(config aws.Config, instanceID string) {
// IndexInstances scans all configured regions for instances and add them to services
func IndexInstances(config state.Config) {
for _, region := range config.AWS.Regions {
//fmt.Println("Listing instances in region:", region)
awsConfig := NewConfig(region, config.AWS.CredentialsPath)
instances, err := Instances(awsConfig)
if err != nil {
fmt.Println("Error listing instances:", err)
continue
}
for _, instance := range instances {
InstanceInfo(awsConfig, *instance.InstanceId)
u := uuid.New()
_ = u
}
}
}
// InstanceInfo collects info about a specific instance in a region
func InstanceInfo(config aws.Config, instanceID string) {
// Create a new EC2 service client // Create a new EC2 service client
svc := ec2.NewFromConfig(config) svc := ec2.NewFromConfig(config)
@ -45,6 +70,7 @@ func Instance_info(config aws.Config, instanceID string) {
fmt.Println("Private IP Address:", aws.ToString(instance.PrivateIpAddress)) fmt.Println("Private IP Address:", aws.ToString(instance.PrivateIpAddress))
} }
// Instances returns all instances for a config i.e. a region
func Instances(config aws.Config) ([]*types.Instance, error) { func Instances(config aws.Config) ([]*types.Instance, error) {
svc := ec2.NewFromConfig(config) svc := ec2.NewFromConfig(config)