Skip to content

Commit

Permalink
service: add Getters and Validate, Require methods for task, event an…
Browse files Browse the repository at this point in the history
…d output.

resolves #349, #348. related with #337
  • Loading branch information
ilgooz committed Aug 29, 2018
1 parent 5386414 commit d0ca943
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 110 deletions.
22 changes: 19 additions & 3 deletions api/deploy_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (d *serviceDeployer) FromGzippedTar(r io.Reader) (*service.Service, *import
func (d *serviceDeployer) deploy(path string) (*service.Service, *importer.ValidationError, error) {
defer os.RemoveAll(path)

service, err := importer.From(path)
s, err := importer.From(path)
validationErr, err := d.assertValidationError(err)
if err != nil {
return nil, nil, err
Expand All @@ -105,10 +105,26 @@ func (d *serviceDeployer) deploy(path string) (*service.Service, *importer.Valid
d.sendStatus(fmt.Sprintf("%s [DEPRECATED] Please use .dockerignore instead of .mesgignore", aurora.Red("⨯")), DONE)
}
d.sendStatus(fmt.Sprintf("%s Image built with success.", aurora.Green("✔")), DONE)
d.injectConfigurationInDependencies(service, imageHash)
d.adjustFields(s)
d.injectConfigurationInDependencies(s, imageHash)

d.sendStatus(fmt.Sprintf("%s Completed.", aurora.Green("✔")), DONE)
return service, nil, services.Save(service)
return s, nil, services.Save(s)
}

func (d *serviceDeployer) adjustFields(s *service.Service) {
for eventKey, event := range s.Events {
event.Key = eventKey
}
for taskKey, task := range s.Tasks {
task.Key = taskKey
task.ServiceName = s.Name
for outputKey, output := range task.Outputs {
output.Key = outputKey
output.TaskKey = taskKey
output.ServiceName = s.Name
}
}
}

func (d *serviceDeployer) injectConfigurationInDependencies(s *service.Service, imageHash string) {
Expand Down
18 changes: 5 additions & 13 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,12 @@ type Event struct {

// Create creates an event.
func Create(s *service.Service, eventKey string, eventData map[string]interface{}) (*Event, error) {
event, ok := s.Events[eventKey]
if !ok {
return nil, &service.EventNotFoundError{
EventKey: eventKey,
ServiceName: s.Name,
}
event, err := s.GetEvent(eventKey)
if err != nil {
return nil, err
}
warnings := s.ValidateParametersSchema(event.Data, eventData)
if len(warnings) > 0 {
return nil, &service.InvalidEventDataError{
EventKey: eventKey,
ServiceName: s.Name,
Warnings: warnings,
}
if err := event.RequireData(eventData); err != nil {
return nil, err
}
return &Event{
Service: s,
Expand Down
7 changes: 6 additions & 1 deletion event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func TestCreateNotPresentEvent(t *testing.T) {
s := service.Service{
Name: serviceName,
Events: map[string]*service.Event{
eventName: {},
eventName: {
Key: eventName,
ServiceName: serviceName,
},
},
}
var data map[string]interface{}
Expand All @@ -53,6 +56,8 @@ func TestCreateInvalidData(t *testing.T) {
Name: serviceName,
Events: map[string]*service.Event{
eventName: {
Key: eventName,
ServiceName: serviceName,
Data: map[string]*service.Parameter{
"xxx": {},
},
Expand Down
29 changes: 13 additions & 16 deletions execution/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@ import (
"time"

"github.com/mesg-foundation/core/pubsub"
"github.com/mesg-foundation/core/service"
)

// Complete marks an execution as complete and puts into the list of processed tasks.
func (execution *Execution) Complete(outputKey string, outputData map[string]interface{}) error {
output, ok := execution.Service.Tasks[execution.Task].Outputs[outputKey]
if !ok {
return &service.TaskOutputNotFoundError{
TaskKey: execution.Task,
TaskOutputKey: outputKey,
ServiceName: execution.Service.Name,
}
var (
s = execution.Service
taskKey = execution.Task
)
task, err := s.GetTask(taskKey)
if err != nil {
return err
}
output, err := task.GetOutput(outputKey)
if err != nil {
return err
}
warnings := execution.Service.ValidateParametersSchema(output.Data, outputData)
if len(warnings) > 0 {
return &service.InvalidTaskOutputError{
TaskKey: execution.Task,
TaskOutputKey: outputKey,
ServiceName: execution.Service.Name,
Warnings: warnings,
}
if err := output.RequireData(outputData); err != nil {
return err
}

if err := execution.moveFromInProgressToProcessed(); err != nil {
Expand Down
10 changes: 9 additions & 1 deletion execution/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ func TestCompleteNotFound(t *testing.T) {
s := service.Service{
Name: serviceName,
Tasks: map[string]*service.Task{
taskKey: {},
taskKey: {
Key: taskKey,
ServiceName: serviceName,
},
},
}
var inputs map[string]interface{}
Expand All @@ -85,8 +88,13 @@ func TestCompleteInvalidOutputs(t *testing.T) {
Name: serviceName,
Tasks: map[string]*service.Task{
taskKey: {
Key: taskKey,
ServiceName: serviceName,
Outputs: map[string]*service.Output{
outputKey: {
Key: outputKey,
TaskKey: taskKey,
ServiceName: serviceName,
Data: map[string]*service.Parameter{
"foo": {},
},
Expand Down
19 changes: 5 additions & 14 deletions execution/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,12 @@ import (

// Create creates an execution with a unique ID and puts it in the pending list.
func Create(s *service.Service, taskKey string, taskInputs map[string]interface{}, tags []string) (*Execution, error) {
task, ok := s.Tasks[taskKey]
if !ok {
return nil, &service.TaskNotFoundError{
TaskKey: taskKey,
ServiceName: s.Name,
}
task, err := s.GetTask(taskKey)
if err != nil {
return nil, err
}
warnings := s.ValidateParametersSchema(task.Inputs, taskInputs)
if len(warnings) > 0 {
return nil, &service.InvalidTaskInputError{
TaskKey: taskKey,
ServiceName: s.Name,
Warnings: warnings,
}
if err := task.RequireInputs(taskInputs); err != nil {
return nil, err
}
execution := &Execution{
Service: s,
Expand All @@ -32,7 +24,6 @@ func Create(s *service.Service, taskKey string, taskInputs map[string]interface{
Tags: tags,
CreatedAt: time.Now(),
}
var err error
execution.ID, err = generateID(execution)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions execution/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func TestCreateInvalidInputs(t *testing.T) {
Name: serviceName,
Tasks: map[string]*service.Task{
taskKey: {
Key: taskKey,
ServiceName: serviceName,
Inputs: map[string]*service.Parameter{
"foo": {
Type: "String",
Expand Down
9 changes: 3 additions & 6 deletions service/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func TestEventNotFoundError(t *testing.T) {

// Test InvalidEventDataError
func TestInvalidEventDataError(t *testing.T) {
s := &Service{}
tests := parameterTests{
&parameterTest{Key: "keyString", Type: "String", Value: 2323, Error: "not a string"},
&parameterTest{Key: "keyNumber", Type: "Number", Value: "string", Error: "not a number"},
Expand All @@ -64,7 +63,7 @@ func TestInvalidEventDataError(t *testing.T) {
err := InvalidEventDataError{
EventKey: "TestInvalidEventDataErrorEventKey",
ServiceName: "TestInvalidEventDataError",
Warnings: s.ValidateParametersSchema(tests.parameterTestsToMapParameter(),
Warnings: validateParametersSchema(tests.parameterTestsToMapParameter(),
tests.parameterTestsToMapData()),
}
require.Contains(t, err.Error(), `Data of event "TestInvalidEventDataErrorEventKey" is invalid in service "TestInvalidEventDataError"`)
Expand All @@ -82,7 +81,6 @@ func TestTaskNotFoundError(t *testing.T) {

// Test InvalidTaskInputError
func TestInvalidTaskInputError(t *testing.T) {
s := &Service{}
tests := parameterTests{
&parameterTest{Key: "keyString", Type: "String", Value: 2323, Error: "not a string"},
&parameterTest{Key: "keyNumber", Type: "Number", Value: "string", Error: "not a number"},
Expand All @@ -94,7 +92,7 @@ func TestInvalidTaskInputError(t *testing.T) {
err := InvalidTaskInputError{
TaskKey: "TestInvalidTaskInputErrorKey",
ServiceName: "TestInvalidTaskInputError",
Warnings: s.ValidateParametersSchema(tests.parameterTestsToMapParameter(),
Warnings: validateParametersSchema(tests.parameterTestsToMapParameter(),
tests.parameterTestsToMapData()),
}
require.Contains(t, err.Error(), `Inputs of task "TestInvalidTaskInputErrorKey" are invalid in service "TestInvalidTaskInputError"`)
Expand All @@ -113,7 +111,6 @@ func TestOutputNotFoundError(t *testing.T) {

// Test InvalidOutputDataError
func TestInvalidOutputDataError(t *testing.T) {
s := &Service{}
tests := parameterTests{
&parameterTest{Key: "keyString", Type: "String", Value: 2323, Error: "not a string"},
&parameterTest{Key: "keyNumber", Type: "Number", Value: "string", Error: "not a number"},
Expand All @@ -126,7 +123,7 @@ func TestInvalidOutputDataError(t *testing.T) {
TaskKey: "TaskKey",
TaskOutputKey: "OutputKey",
ServiceName: "TestInvalidOutputDataError",
Warnings: s.ValidateParametersSchema(tests.parameterTestsToMapParameter(),
Warnings: validateParametersSchema(tests.parameterTestsToMapParameter(),
tests.parameterTestsToMapData()),
}
require.Contains(t, err.Error(), `Outputs "OutputKey" of task "TaskKey" are invalid in service "TestInvalidOutputDataError"`)
Expand Down
31 changes: 31 additions & 0 deletions service/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package service

// GetEvent returns event eventKey.
func (s *Service) GetEvent(eventKey string) (*Event, error) {
event, ok := s.Events[eventKey]
if !ok {
return nil, &EventNotFoundError{
EventKey: eventKey,
ServiceName: s.Name,
}
}
return event, nil
}

// ValidateData validates event data to match with paremeter config.
func (e *Event) ValidateData(eventData map[string]interface{}) []*ParameterWarning {
return validateParametersSchema(e.Data, eventData)
}

// RequireData requires event data to match with paremeter config.
func (e *Event) RequireData(eventData map[string]interface{}) error {
warnings := e.ValidateData(eventData)
if len(warnings) > 0 {
return &InvalidEventDataError{
EventKey: e.Key,
ServiceName: e.ServiceName,
Warnings: warnings,
}
}
return nil
}
Loading

0 comments on commit d0ca943

Please sign in to comment.