Skip to content

Commit

Permalink
Add GetRegion method to HostHandler interface (#7451)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
owenniles authored Oct 20, 2022
1 parent 468b5f0 commit 44da68c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
13 changes: 9 additions & 4 deletions backend/services/scaling-service/hosts/aws/aws_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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())
}

Expand Down
4 changes: 4 additions & 0 deletions backend/services/scaling-service/hosts/host_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 44da68c

Please sign in to comment.