From 61175acc67f8118125d53348c858d1488f0937fb Mon Sep 17 00:00:00 2001 From: schulze Date: Mon, 17 Apr 2023 15:20:40 +0200 Subject: [PATCH] update services config after MTD --- mtdaws/mtd.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mtdaws/mtd.go b/mtdaws/mtd.go index e45e4ca..3f81795 100644 --- a/mtdaws/mtd.go +++ b/mtdaws/mtd.go @@ -2,6 +2,7 @@ package mtdaws import ( "fmt" + "net/netip" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -9,6 +10,32 @@ import ( "github.com/thefeli73/polemos/state" ) +// AWSUpdateService updates a specified service config to match a newly moved instance +func AWSUpdateService(config state.Config, region string, service state.CustomUUID, newInstanceID string) (state.Config) { + awsConfig := NewConfig(region, config.AWS.CredentialsPath) + svc := ec2.NewFromConfig(awsConfig) + instance, err := getInstanceDetailsFromString(svc, newInstanceID) + if err != nil { + fmt.Println("Error getting instance details:\t", err) + return config + } + + var publicAddr string + if instance.PublicIpAddress != nil { + publicAddr = aws.ToString(instance.PublicIpAddress) + } + formattedinstance := AwsInstance{ + InstanceID: aws.ToString(instance.InstanceId), + Region: region, + PublicIP: publicAddr, + PrivateIP: aws.ToString(instance.PrivateIpAddress), + } + cloudid := GetCloudID(formattedinstance) + serviceip := netip.MustParseAddr(publicAddr) + config.MTD.Services[service] = state.Service{CloudID: cloudid, ServiceIP: serviceip} + return config +} + // AWSMoveInstance moves a specified instance to a new availability region func AWSMoveInstance(config state.Config) (state.Config) { // pseudorandom instance from all services for testing @@ -83,5 +110,7 @@ func AWSMoveInstance(config state.Config) (state.Config) { fmt.Println("Deleted snapshot:\t", snapshotID) } + AWSUpdateService(config, region, serviceUUID, newInstanceID) + return config }