From 44da68c9aa1e178aa8e1bd2a5b19414c772cf64f Mon Sep 17 00:00:00 2001 From: Owen Niles Date: Thu, 20 Oct 2022 10:42:37 -0400 Subject: [PATCH] Add GetRegion method to HostHandler interface (#7451) * Add GetRegion method to HostHandler interface * Remove exported Region field from `AWSHost` type In package `github.com/whisthq/whist/backend/services/scaling-service/hosts/aws`. Replace with `region` field. --- .../services/scaling-service/hosts/aws/aws_host.go | 13 +++++++++---- .../services/scaling-service/hosts/host_handler.go | 4 ++++ .../scaling_algorithms/default/actions_test.go | 4 ++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/backend/services/scaling-service/hosts/aws/aws_host.go b/backend/services/scaling-service/hosts/aws/aws_host.go index d247e5e1747..ee80797648d 100644 --- a/backend/services/scaling-service/hosts/aws/aws_host.go +++ b/backend/services/scaling-service/hosts/aws/aws_host.go @@ -27,7 +27,7 @@ import ( // AWSHost implements the HostHandler interface using the AWS sdk. type AWSHost struct { // The AWS region the host is initialized in. - Region string + region string // The AWS specific configurations, including credentials. Config aws.Config // The EC2 client used for calling the AWS EC2 service API. @@ -52,7 +52,7 @@ func (host *AWSHost) Initialize(region string) error { } // Start AWS host and EC2 client - host.Region = region + host.region = region host.Config = cfg host.EC2 = ec2.NewFromConfig(cfg) @@ -81,6 +81,11 @@ func (host *AWSHost) Initialize(region string) error { return nil } +// GetRegion is part of the HostHandler interface. +func (h *AWSHost) GetRegion() string { + return h.region +} + // MakeInstances is a simple method that calls the RunInstances function from the ec2 client. func (host *AWSHost) MakeInstances(c context.Context, input *ec2.RunInstancesInput) (*ec2.RunInstancesOutput, error) { return host.EC2.RunInstances(c, input) @@ -162,7 +167,7 @@ func (host *AWSHost) SpinUpInstances(scalingCtx context.Context, numInstances in ImageID = aws.ToString(outputInstance.ImageId) Type = string(outputInstance.InstanceType) InstanceID = aws.ToString(outputInstance.InstanceId) - Region = host.Region + Region = host.region Status = "PRE_CONNECTION" ClientSHA = image.ClientSHA Name = host.GenerateName() @@ -372,7 +377,7 @@ func (host *AWSHost) getInstanceProfile() string { // GenerateName is a helper function for generating an instance // name using a random UUID. func (host *AWSHost) GenerateName() string { - return utils.Sprintf("ec2-%v-%v-%v-%v", host.Region, metadata.GetAppEnvironmentLowercase(), + return utils.Sprintf("ec2-%v-%v-%v-%v", host.region, metadata.GetAppEnvironmentLowercase(), metadata.GetGitCommit()[0:7], shortuuid.New()) } diff --git a/backend/services/scaling-service/hosts/host_handler.go b/backend/services/scaling-service/hosts/host_handler.go index 3ba38bf2da2..abd72feade6 100644 --- a/backend/services/scaling-service/hosts/host_handler.go +++ b/backend/services/scaling-service/hosts/host_handler.go @@ -19,6 +19,10 @@ import ( // implementation for each cloud service provider, which will interact directly with the provider's API. type HostHandler interface { Initialize(region string) error + + // GetRegion returns the name of the region with which an instance of the + // HostHandler type is associated. + GetRegion() string SpinUpInstances(scalingCtx context.Context, numInstances int32, maxWaitTimeReady time.Duration, image subscriptions.Image) (createdInstances []subscriptions.Instance, err error) SpinDownInstances(scalingCtx context.Context, instanceIDs []string) error WaitForInstanceTermination(context.Context, time.Duration, []string) error diff --git a/backend/services/scaling-service/scaling_algorithms/default/actions_test.go b/backend/services/scaling-service/scaling_algorithms/default/actions_test.go index 5becd4c7a0d..2864d18168e 100644 --- a/backend/services/scaling-service/scaling_algorithms/default/actions_test.go +++ b/backend/services/scaling-service/scaling_algorithms/default/actions_test.go @@ -195,6 +195,10 @@ func (db *mockDBClient) UpdateMandelbox(_ context.Context, _ subscriptions.Whist // mockHostHandler is used to test all interactions with cloud providers type mockHostHandler struct{} +func (h *mockHostHandler) GetRegion() string { + return "blah" +} + func (mh *mockHostHandler) Initialize(region string) error { return nil }