From 6c3e2acabb377f0d8679419ec1226a7470cc92db Mon Sep 17 00:00:00 2001 From: schulze Date: Tue, 21 Mar 2023 13:34:52 +0100 Subject: [PATCH] Refactor, move function to sdk --- main.go | 31 +++++-------------------------- {mtd_aws => mtdaws}/sdk.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 29 deletions(-) rename {mtd_aws => mtdaws}/sdk.go (68%) diff --git a/main.go b/main.go index 3194e7c..afced4b 100644 --- a/main.go +++ b/main.go @@ -4,39 +4,18 @@ import ( "fmt" "os" - uuid "github.com/nu7hatch/gouuid" - "github.com/thefeli73/polemos/mtd_aws" + "github.com/thefeli73/polemos/mtdaws" "github.com/thefeli73/polemos/state" ) + func main() { fmt.Println("Starting Polemos") - config, err := state.Load_conf("config.yaml") + config, err := state.LoadConf("config.yaml") if err != nil { fmt.Println("Error loading config:", err) os.Exit(1) } - //aws_config := mtd_aws.New_config(config.AWS.Region) - //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) - } - } - -} \ No newline at end of file + mtdaws.IndexInstances(config) +} diff --git a/mtd_aws/sdk.go b/mtdaws/sdk.go similarity index 68% rename from mtd_aws/sdk.go rename to mtdaws/sdk.go index f52774a..3b055b3 100644 --- a/mtd_aws/sdk.go +++ b/mtdaws/sdk.go @@ -1,4 +1,4 @@ -package mtd_aws +package mtdaws import ( "context" @@ -9,8 +9,12 @@ import ( "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" + "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 cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigFiles([]string{credentials}), config.WithRegion(region)) if err != nil { @@ -20,7 +24,28 @@ func New_config(region string, credentials string) aws.Config { } 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 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)) } +// Instances returns all instances for a config i.e. a region func Instances(config aws.Config) ([]*types.Instance, error) { svc := ec2.NewFromConfig(config)