Refactor static AWS functions
from a magic number in the code to a configurable list in configuration.yaml
This commit is contained in:
@ -48,6 +48,8 @@ aws:
|
|||||||
enabled: false
|
enabled: false
|
||||||
# The enpoint of the WebSocket API
|
# The enpoint of the WebSocket API
|
||||||
endpoint: wss://abcdef1234.execute-api.eu-central-1.amazonaws.com/production
|
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
|
# Configuration of the logger
|
||||||
logger:
|
logger:
|
||||||
|
@ -45,8 +45,9 @@ var (
|
|||||||
Namespace: "default",
|
Namespace: "default",
|
||||||
},
|
},
|
||||||
AWS: AWS{
|
AWS: AWS{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
Endpoint: "",
|
Endpoint: "",
|
||||||
|
Functions: "",
|
||||||
},
|
},
|
||||||
Logger: logger{
|
Logger: logger{
|
||||||
Level: "INFO",
|
Level: "INFO",
|
||||||
@ -96,8 +97,9 @@ func (n *Nomad) URL() *url.URL {
|
|||||||
|
|
||||||
// AWS configures the AWS Lambda usage.
|
// AWS configures the AWS Lambda usage.
|
||||||
type AWS struct {
|
type AWS struct {
|
||||||
Enabled bool
|
Enabled bool
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
Functions string
|
||||||
}
|
}
|
||||||
|
|
||||||
// TLS configures TLS on a connection.
|
// TLS configures TLS on a connection.
|
||||||
|
@ -2,8 +2,10 @@ package environment
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/openHPI/poseidon/internal/config"
|
||||||
"github.com/openHPI/poseidon/internal/runner"
|
"github.com/openHPI/poseidon/internal/runner"
|
||||||
"github.com/openHPI/poseidon/pkg/dto"
|
"github.com/openHPI/poseidon/pkg/dto"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AWSEnvironmentManager contains no functionality at the moment.
|
// AWSEnvironmentManager contains no functionality at the moment.
|
||||||
@ -16,7 +18,6 @@ type AWSEnvironmentManager struct {
|
|||||||
func NewAWSEnvironmentManager(runnerManager runner.Manager) *AWSEnvironmentManager {
|
func NewAWSEnvironmentManager(runnerManager runner.Manager) *AWSEnvironmentManager {
|
||||||
m := &AWSEnvironmentManager{&AbstractManager{nil}, runnerManager}
|
m := &AWSEnvironmentManager{&AbstractManager{nil}, runnerManager}
|
||||||
runnerManager.Load()
|
runnerManager.Load()
|
||||||
m.Load()
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ func (a *AWSEnvironmentManager) Get(id dto.EnvironmentID, fetch bool) (runner.Ex
|
|||||||
|
|
||||||
func (a *AWSEnvironmentManager) CreateOrUpdate(
|
func (a *AWSEnvironmentManager) CreateOrUpdate(
|
||||||
id dto.EnvironmentID, request dto.ExecutionEnvironmentRequest) (bool, error) {
|
id dto.EnvironmentID, request dto.ExecutionEnvironmentRequest) (bool, error) {
|
||||||
if id != runner.AwsJavaEnvironmentID {
|
if !isAWSEnvironment(request) {
|
||||||
isCreated, err := a.NextHandler().CreateOrUpdate(id, request)
|
isCreated, err := a.NextHandler().CreateOrUpdate(id, request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("aws wrapped: %w", err)
|
return false, fmt.Errorf("aws wrapped: %w", err)
|
||||||
@ -59,6 +60,15 @@ func (a *AWSEnvironmentManager) CreateOrUpdate(
|
|||||||
return !ok, nil
|
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) {
|
func (a *AWSEnvironmentManager) Delete(id dto.EnvironmentID) (bool, error) {
|
||||||
e, ok := a.runnerManager.GetEnvironment(id)
|
e, ok := a.runnerManager.GetEnvironment(id)
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -79,11 +89,3 @@ func (a *AWSEnvironmentManager) Delete(id dto.EnvironmentID) (bool, error) {
|
|||||||
func (a *AWSEnvironmentManager) Statistics() map[dto.EnvironmentID]*dto.StatisticalExecutionEnvironmentData {
|
func (a *AWSEnvironmentManager) Statistics() map[dto.EnvironmentID]*dto.StatisticalExecutionEnvironmentData {
|
||||||
return a.NextHandler().Statistics()
|
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.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package environment
|
package environment
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/openHPI/poseidon/internal/config"
|
||||||
"github.com/openHPI/poseidon/internal/runner"
|
"github.com/openHPI/poseidon/internal/runner"
|
||||||
"github.com/openHPI/poseidon/pkg/dto"
|
"github.com/openHPI/poseidon/pkg/dto"
|
||||||
"github.com/openHPI/poseidon/tests"
|
"github.com/openHPI/poseidon/tests"
|
||||||
@ -13,15 +14,16 @@ import (
|
|||||||
func TestAWSEnvironmentManager_CreateOrUpdate(t *testing.T) {
|
func TestAWSEnvironmentManager_CreateOrUpdate(t *testing.T) {
|
||||||
runnerManager := runner.NewAWSRunnerManager()
|
runnerManager := runner.NewAWSRunnerManager()
|
||||||
m := NewAWSEnvironmentManager(runnerManager)
|
m := NewAWSEnvironmentManager(runnerManager)
|
||||||
uniqueImage := "random image string"
|
uniqueImage := "java11Exec"
|
||||||
|
|
||||||
t.Run("can create default Java environment", func(t *testing.T) {
|
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)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("can retrieve added environment", func(t *testing.T) {
|
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.NoError(t, err)
|
||||||
assert.Equal(t, environment.Image(), uniqueImage)
|
assert.Equal(t, environment.Image(), uniqueImage)
|
||||||
})
|
})
|
||||||
@ -78,13 +80,6 @@ func TestAWSEnvironmentManager_List(t *testing.T) {
|
|||||||
runnerManager := runner.NewAWSRunnerManager()
|
runnerManager := runner.NewAWSRunnerManager()
|
||||||
m := NewAWSEnvironmentManager(runnerManager)
|
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) {
|
t.Run("returs also environments of the rest of the manager chain", func(t *testing.T) {
|
||||||
nextHandler := &ManagerHandlerMock{}
|
nextHandler := &ManagerHandlerMock{}
|
||||||
existingEnvironment := NewAWSEnvironment()
|
existingEnvironment := NewAWSEnvironment()
|
||||||
@ -94,7 +89,7 @@ func TestAWSEnvironmentManager_List(t *testing.T) {
|
|||||||
|
|
||||||
environments, err := m.List(false)
|
environments, err := m.List(false)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
require.Len(t, environments, 2)
|
require.Len(t, environments, 1)
|
||||||
assert.Contains(t, environments, existingEnvironment)
|
assert.Contains(t, environments, existingEnvironment)
|
||||||
})
|
})
|
||||||
m.SetNextHandler(nil)
|
m.SetNextHandler(nil)
|
||||||
@ -106,7 +101,7 @@ func TestAWSEnvironmentManager_List(t *testing.T) {
|
|||||||
|
|
||||||
environments, err := m.List(false)
|
environments, err := m.List(false)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, environments, 2)
|
assert.Len(t, environments, 1)
|
||||||
assert.Contains(t, environments, localEnvironment)
|
assert.Contains(t, environments, localEnvironment)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,6 @@ type AWSRunnerManager struct {
|
|||||||
*AbstractManager
|
*AbstractManager
|
||||||
}
|
}
|
||||||
|
|
||||||
const AwsJavaEnvironmentID = 2142
|
|
||||||
|
|
||||||
// NewAWSRunnerManager creates a new runner manager that keeps track of all runners at AWS.
|
// NewAWSRunnerManager creates a new runner manager that keeps track of all runners at AWS.
|
||||||
func NewAWSRunnerManager() *AWSRunnerManager {
|
func NewAWSRunnerManager() *AWSRunnerManager {
|
||||||
return &AWSRunnerManager{NewAbstractManager()}
|
return &AWSRunnerManager{NewAbstractManager()}
|
||||||
|
Reference in New Issue
Block a user