Refactor static AWS functions

from a magic number in the code to a configurable list in configuration.yaml
This commit is contained in:
Maximilian Paß
2022-02-02 10:22:16 +01:00
parent f6d9a6ddbb
commit d603a8ebb0
5 changed files with 27 additions and 28 deletions

View File

@ -48,6 +48,8 @@ aws:
enabled: false
# The enpoint of the WebSocket API
endpoint: wss://abcdef1234.execute-api.eu-central-1.amazonaws.com/production
# Currently, only static AWS environments are supported. You can list them here.
functions: "java11Exec go118Exec"
# Configuration of the logger
logger:

View File

@ -45,8 +45,9 @@ var (
Namespace: "default",
},
AWS: AWS{
Enabled: false,
Endpoint: "",
Enabled: false,
Endpoint: "",
Functions: "",
},
Logger: logger{
Level: "INFO",
@ -96,8 +97,9 @@ func (n *Nomad) URL() *url.URL {
// AWS configures the AWS Lambda usage.
type AWS struct {
Enabled bool
Endpoint string
Enabled bool
Endpoint string
Functions string
}
// TLS configures TLS on a connection.

View File

@ -2,8 +2,10 @@ package environment
import (
"fmt"
"github.com/openHPI/poseidon/internal/config"
"github.com/openHPI/poseidon/internal/runner"
"github.com/openHPI/poseidon/pkg/dto"
"strings"
)
// AWSEnvironmentManager contains no functionality at the moment.
@ -16,7 +18,6 @@ type AWSEnvironmentManager struct {
func NewAWSEnvironmentManager(runnerManager runner.Manager) *AWSEnvironmentManager {
m := &AWSEnvironmentManager{&AbstractManager{nil}, runnerManager}
runnerManager.Load()
m.Load()
return m
}
@ -43,7 +44,7 @@ func (a *AWSEnvironmentManager) Get(id dto.EnvironmentID, fetch bool) (runner.Ex
func (a *AWSEnvironmentManager) CreateOrUpdate(
id dto.EnvironmentID, request dto.ExecutionEnvironmentRequest) (bool, error) {
if id != runner.AwsJavaEnvironmentID {
if !isAWSEnvironment(request) {
isCreated, err := a.NextHandler().CreateOrUpdate(id, request)
if err != nil {
return false, fmt.Errorf("aws wrapped: %w", err)
@ -59,6 +60,15 @@ func (a *AWSEnvironmentManager) CreateOrUpdate(
return !ok, nil
}
func isAWSEnvironment(request dto.ExecutionEnvironmentRequest) bool {
for _, function := range strings.Fields(config.Config.AWS.Functions) {
if request.Image == function {
return true
}
}
return false
}
func (a *AWSEnvironmentManager) Delete(id dto.EnvironmentID) (bool, error) {
e, ok := a.runnerManager.GetEnvironment(id)
if !ok {
@ -79,11 +89,3 @@ func (a *AWSEnvironmentManager) Delete(id dto.EnvironmentID) (bool, error) {
func (a *AWSEnvironmentManager) Statistics() map[dto.EnvironmentID]*dto.StatisticalExecutionEnvironmentData {
return a.NextHandler().Statistics()
}
// Load fetches all remote environments in the local storage. ToDo: Fetch dynamically.
func (a *AWSEnvironmentManager) Load() {
_, err := a.CreateOrUpdate(runner.AwsJavaEnvironmentID, dto.ExecutionEnvironmentRequest{Image: "java11Exec"})
if err != nil {
log.WithError(err).Warn("Could not load aws environment.")
}
}

View File

@ -1,6 +1,7 @@
package environment
import (
"github.com/openHPI/poseidon/internal/config"
"github.com/openHPI/poseidon/internal/runner"
"github.com/openHPI/poseidon/pkg/dto"
"github.com/openHPI/poseidon/tests"
@ -13,15 +14,16 @@ import (
func TestAWSEnvironmentManager_CreateOrUpdate(t *testing.T) {
runnerManager := runner.NewAWSRunnerManager()
m := NewAWSEnvironmentManager(runnerManager)
uniqueImage := "random image string"
uniqueImage := "java11Exec"
t.Run("can create default Java environment", func(t *testing.T) {
_, err := m.CreateOrUpdate(runner.AwsJavaEnvironmentID, dto.ExecutionEnvironmentRequest{Image: uniqueImage})
config.Config.AWS.Functions = uniqueImage
_, err := m.CreateOrUpdate(tests.AnotherEnvironmentIDAsInteger, dto.ExecutionEnvironmentRequest{Image: uniqueImage})
assert.NoError(t, err)
})
t.Run("can retrieve added environment", func(t *testing.T) {
environment, err := m.Get(runner.AwsJavaEnvironmentID, false)
environment, err := m.Get(tests.AnotherEnvironmentIDAsInteger, false)
assert.NoError(t, err)
assert.Equal(t, environment.Image(), uniqueImage)
})
@ -78,13 +80,6 @@ func TestAWSEnvironmentManager_List(t *testing.T) {
runnerManager := runner.NewAWSRunnerManager()
m := NewAWSEnvironmentManager(runnerManager)
t.Run("contains the \"Load\"-ed environments", func(t *testing.T) {
environments, err := m.List(false)
assert.NoError(t, err)
require.Len(t, environments, 1)
assert.Equal(t, environments[0].ID(), dto.EnvironmentID(runner.AwsJavaEnvironmentID))
})
t.Run("returs also environments of the rest of the manager chain", func(t *testing.T) {
nextHandler := &ManagerHandlerMock{}
existingEnvironment := NewAWSEnvironment()
@ -94,7 +89,7 @@ func TestAWSEnvironmentManager_List(t *testing.T) {
environments, err := m.List(false)
assert.NoError(t, err)
require.Len(t, environments, 2)
require.Len(t, environments, 1)
assert.Contains(t, environments, existingEnvironment)
})
m.SetNextHandler(nil)
@ -106,7 +101,7 @@ func TestAWSEnvironmentManager_List(t *testing.T) {
environments, err := m.List(false)
assert.NoError(t, err)
assert.Len(t, environments, 2)
assert.Len(t, environments, 1)
assert.Contains(t, environments, localEnvironment)
})
}

View File

@ -10,8 +10,6 @@ type AWSRunnerManager struct {
*AbstractManager
}
const AwsJavaEnvironmentID = 2142
// NewAWSRunnerManager creates a new runner manager that keeps track of all runners at AWS.
func NewAWSRunnerManager() *AWSRunnerManager {
return &AWSRunnerManager{NewAbstractManager()}