update services config after MTD

This commit is contained in:
schulze 2023-04-17 15:20:40 +02:00
parent 03eff229c6
commit 61175acc67

View File

@ -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
}