Skip to content

Commit

Permalink
Changed TOSCA activity to comply with TOSCA 1.3 new definition
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentganne committed Jan 29, 2020
1 parent 853eb13 commit 7bcc821
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 47 deletions.
12 changes: 6 additions & 6 deletions commands/deployments/workflows/dep_wf_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ func init() {
}
fmt.Println(" Activities:")
for _, activity := range step.Activities {
if activity.CallOperation != "" {
fmt.Println(" - Call Operation:", activity.CallOperation)
if activity.CallOperation != nil {
fmt.Println(" - Call Operation:", activity.CallOperation.Operation)
}
if activity.Delegate != "" {
fmt.Println(" - Delegate:", activity.Delegate)
if activity.Delegate != nil {
fmt.Println(" - Delegate:", activity.Delegate.Workflow)
}
if activity.SetState != "" {
fmt.Println(" - Set State:", activity.SetState)
}
if activity.Inline != "" {
fmt.Println(" - Inline:", activity.Inline)
if activity.Inline != nil {
fmt.Println(" - Inline:", activity.Inline.Workflow)
}
}
if len(step.OnSuccess) > 0 {
Expand Down
6 changes: 4 additions & 2 deletions deployments/definition_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,12 +1033,14 @@ func testInlineWorkflow(t *testing.T) {
step := wfInstall.Steps["Some_other_inline"]
require.Equal(t, step.Target, "")
require.Equal(t, len(step.Activities), 1)
require.Equal(t, step.Activities[0].Inline, "my_custom_wf")
require.NotNil(t, step.Activities[0].Inline)
require.Equal(t, step.Activities[0].Inline.Workflow, "my_custom_wf")

step = wfInstall.Steps["inception_inline"]
require.Equal(t, step.Target, "")
require.Equal(t, len(step.Activities), 1)
require.Equal(t, step.Activities[0].Inline, "inception")
require.NotNil(t, step.Activities[0].Inline)
require.Equal(t, step.Activities[0].Inline.Workflow, "inception")

wfInception, err := GetWorkflow(ctx, deploymentID, "inception")
require.Nil(t, err)
Expand Down
14 changes: 7 additions & 7 deletions deployments/internal/store_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func StoreWorkflow(ctx context.Context, deploymentID, workflowName string, workf
func transformedWorkflow(workflow *tosca.Workflow) *tosca.Workflow {
for _, step := range workflow.Steps {
for _, activity := range step.Activities {
if activity.CallOperation != "" {
if activity.CallOperation != nil {
// Preserve case for requirement and target node name in case of relationship operation
opSlice := strings.SplitN(activity.CallOperation, "/", 2)
opSlice := strings.SplitN(activity.CallOperation.Operation, "/", 2)
opSlice[0] = strings.ToLower(opSlice[0])
activity.CallOperation = strings.Join(opSlice, "/")
activity.CallOperation.Operation = strings.Join(opSlice, "/")
}
}
}
Expand Down Expand Up @@ -82,11 +82,11 @@ func checkNestedWorkflow(topology tosca.Topology, workflow tosca.Workflow, neste
nestedWfs = append(nestedWfs, wfName)
for _, step := range workflow.Steps {
for _, activity := range step.Activities {
if activity.Inline != "" {
if collections.ContainsString(nestedWfs, activity.Inline) {
return errors.Errorf("A cycle has been detected in inline workflows [initial: %q, repeated: %q]", nestedWfs[0], activity.Inline)
if activity.Inline != nil {
if collections.ContainsString(nestedWfs, activity.Inline.Workflow) {
return errors.Errorf("A cycle has been detected in inline workflows [initial: %q, repeated: %q]", nestedWfs[0], activity.Inline.Workflow)
}
if err := checkNestedWorkflow(topology, topology.TopologyTemplate.Workflows[activity.Inline], nestedWfs, activity.Inline); err != nil {
if err := checkNestedWorkflow(topology, topology.TopologyTemplate.Workflows[activity.Inline.Workflow], nestedWfs, activity.Inline.Workflow); err != nil {
return err
}
}
Expand Down
7 changes: 5 additions & 2 deletions deployments/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func enhanceWorkflows(ctx context.Context, deploymentID string) error {
for sn, s := range wf.Steps {
var isCancellable bool
for _, a := range s.Activities {
switch strings.ToLower(a.CallOperation) {
if a.CallOperation == nil {
continue
}
switch strings.ToLower(a.CallOperation.Operation) {
case tosca.RunnableSubmitOperationName, tosca.RunnableRunOperationName:
isCancellable = true
}
Expand All @@ -100,7 +103,7 @@ func enhanceWorkflows(ctx context.Context, deploymentID string) error {
OperationHost: s.OperationHost,
Activities: []tosca.Activity{
tosca.Activity{
CallOperation: tosca.RunnableCancelOperationName,
CallOperation: &tosca.OperationActivity{Operation: tosca.RunnableCancelOperationName},
},
},
}
Expand Down
4 changes: 4 additions & 0 deletions prov/monitoring/monitoring_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (m *mockActivity) Value() string {
return m.v
}

func (m *mockActivity) Inputs() map[string]tosca.ParameterDefinition {
return nil
}

func testComputeMonitoringHook(t *testing.T, client *api.Client, cfg config.Configuration) {
log.SetDebug(true)

Expand Down
4 changes: 4 additions & 0 deletions prov/validation/compute_capability_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ystia/yorc/v4/helper/consulutil"
"github.com/ystia/yorc/v4/tasks"
"github.com/ystia/yorc/v4/tasks/workflow/builder"
"github.com/ystia/yorc/v4/tosca"
)

type mockActivity struct {
Expand All @@ -47,6 +48,9 @@ func (m *mockActivity) Value() string {
return m.v
}

func (m *mockActivity) Inputs() map[string]tosca.ParameterDefinition {
return nil
}
func testPostComputeCreationHook(t *testing.T, cfg config.Configuration) {
ctx := context.Background()

Expand Down
21 changes: 21 additions & 0 deletions tasks/workflow/builder/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package builder

import "github.com/ystia/yorc/v4/tosca"

//go:generate go-enum -f=activities.go --lower

// ActivityType x ENUM(
Expand All @@ -36,10 +38,15 @@ type Activity interface {
// For call-operation activities it's the operation name.
// For inline activities it's the inlined workflow name.
Value() string

// Inputs returns the inputs parameters defined in the activity
// It is not relevant for set-state activities
Inputs() map[string]tosca.ParameterDefinition
}

type delegateActivity struct {
delegate string
inputs map[string]tosca.ParameterDefinition
}

func (da delegateActivity) Type() ActivityType {
Expand All @@ -48,6 +55,9 @@ func (da delegateActivity) Type() ActivityType {
func (da delegateActivity) Value() string {
return da.delegate
}
func (da delegateActivity) Inputs() map[string]tosca.ParameterDefinition {
return da.inputs
}

type setStateActivity struct {
state string
Expand All @@ -59,9 +69,13 @@ func (s setStateActivity) Type() ActivityType {
func (s setStateActivity) Value() string {
return s.state
}
func (s setStateActivity) Inputs() map[string]tosca.ParameterDefinition {
return nil
}

type callOperationActivity struct {
operation string
inputs map[string]tosca.ParameterDefinition
}

func (c callOperationActivity) Type() ActivityType {
Expand All @@ -70,9 +84,13 @@ func (c callOperationActivity) Type() ActivityType {
func (c callOperationActivity) Value() string {
return c.operation
}
func (c callOperationActivity) Inputs() map[string]tosca.ParameterDefinition {
return c.inputs
}

type inlineActivity struct {
inline string
inputs map[string]tosca.ParameterDefinition
}

func (i inlineActivity) Type() ActivityType {
Expand All @@ -81,3 +99,6 @@ func (i inlineActivity) Type() ActivityType {
func (i inlineActivity) Value() string {
return i.inline
}
func (i inlineActivity) Inputs() map[string]tosca.ParameterDefinition {
return i.inputs
}
14 changes: 7 additions & 7 deletions tasks/workflow/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,20 @@ func buildStepFromWFStep(deploymentID, wfName, stepName string, wfSteps map[stri
func buildStepActivities(s *Step, wfStep *tosca.Step) (bool, error) {
var targetIsMandatory bool
for i, wfActivity := range wfStep.Activities {
if wfActivity.Delegate != "" {
if wfActivity.Delegate != nil {
targetIsMandatory = true
s.Activities = append(s.Activities, delegateActivity{delegate: wfActivity.Delegate})
} else if wfActivity.CallOperation != "" {
s.Activities = append(s.Activities, delegateActivity{wfActivity.Delegate.Workflow, wfActivity.Delegate.Inputs})
} else if wfActivity.CallOperation != nil {
targetIsMandatory = true
s.Activities = append(s.Activities, callOperationActivity{operation: wfActivity.CallOperation})
if isAsyncOperation(wfActivity.CallOperation) {
s.Activities = append(s.Activities, callOperationActivity{wfActivity.CallOperation.Operation, wfActivity.CallOperation.Inputs})
if isAsyncOperation(wfActivity.CallOperation.Operation) {
s.Async = true
}
} else if wfActivity.SetState != "" {
targetIsMandatory = true
s.Activities = append(s.Activities, setStateActivity{state: wfActivity.SetState})
} else if wfActivity.Inline != "" {
s.Activities = append(s.Activities, inlineActivity{inline: wfActivity.Inline})
} else if wfActivity.Inline != nil {
s.Activities = append(s.Activities, inlineActivity{wfActivity.Inline.Workflow, wfActivity.Inline.Inputs})
} else {
return false, errors.Errorf("Unsupported activity type for step: %q, activity nb: %d, activity: %+v", s.Name, i, wfActivity)
}
Expand Down
24 changes: 12 additions & 12 deletions tasks/workflow/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func testBuildStep(t *testing.T, srv1 *testutil.TestServer) {
Target: "nodeName",
TargetRelationShip: "",
Activities: []tosca.Activity{
{Delegate: "install"},
{Delegate: &tosca.WorkflowActivity{Workflow: "install"}},
{SetState: "installed"},
{CallOperation: "script.sh"},
{CallOperation: &tosca.OperationActivity{Operation: "script.sh"}},
},
},
"Some_other_inline": {
//Target: "nodeName",
Activities: []tosca.Activity{
{Inline: "my_custom_wf"},
{Inline: &tosca.WorkflowActivity{Workflow: "my_custom_wf"}},
},
},
}}
Expand Down Expand Up @@ -88,13 +88,13 @@ func testBuildStepWithNext(t *testing.T, srv1 *testutil.TestServer) {
Target: "nodeName",
OnSuccess: []string{"downstream"},
Activities: []tosca.Activity{
{Delegate: "install"},
{Delegate: &tosca.WorkflowActivity{Workflow: "install"}},
},
},
"downstream": {
Target: "downstream",
Activities: []tosca.Activity{
{CallOperation: "script.sh"},
{CallOperation: &tosca.OperationActivity{Operation: "script.sh"}},
},
},
}}
Expand Down Expand Up @@ -126,7 +126,7 @@ func testBuildStepWithNonExistentNextStep(t *testing.T, srv1 *testutil.TestServe
Target: "nodeName",
OnSuccess: []string{"nonexistent"},
Activities: []tosca.Activity{
{Delegate: "install"},
{Delegate: &tosca.WorkflowActivity{Workflow: "install"}},
},
},
}}
Expand Down Expand Up @@ -154,39 +154,39 @@ func testBuildWorkFlow(t *testing.T, srv1 *testutil.TestServer) {
Target: "nodeName",
OnSuccess: []string{"step10", "step12"},
Activities: []tosca.Activity{
{Delegate: "install"},
{Delegate: &tosca.WorkflowActivity{Workflow: "install"}},
},
},
"step10": {
Target: "nodeName",
OnSuccess: []string{"step13"},
Activities: []tosca.Activity{
{Delegate: "install"},
{Delegate: &tosca.WorkflowActivity{Workflow: "install"}},
},
},
"step12": {
Target: "nodeName",
OnSuccess: []string{"step13"},
Activities: []tosca.Activity{
{Delegate: "install"},
{Delegate: &tosca.WorkflowActivity{Workflow: "install"}},
},
},
"step13": {
Target: "nodeName",
Activities: []tosca.Activity{
{Delegate: "install"},
{Delegate: &tosca.WorkflowActivity{Workflow: "install"}},
},
},
"step15": {
Target: "nodeName",
Activities: []tosca.Activity{
{Inline: "inception"},
{Inline: &tosca.WorkflowActivity{Workflow: "inception"}},
},
},
"step20": {
Target: "nodeName",
Activities: []tosca.Activity{
{Delegate: "install"},
{Delegate: &tosca.WorkflowActivity{Workflow: "install"}},
},
},
}}
Expand Down
8 changes: 6 additions & 2 deletions tasks/workflow/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package workflow
import (
"context"
"path"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -169,7 +170,7 @@ func testRunStep(t *testing.T, srv1 *testutil.TestServer, cc *api.Client) {
if mah.target != s.Target {
t.Errorf("step.run() pre_activity_hook.target = %v, want %v", mah.target, s.Target)
}
if mah.activity != s.Activities[0] {
if !reflect.DeepEqual(mah.activity, s.Activities[0]) {
t.Errorf("step.run() pre_activity_hook.activity = %v, want %v", mah.activity, s.Activities[0])
}
}
Expand All @@ -184,7 +185,7 @@ func testRunStep(t *testing.T, srv1 *testutil.TestServer, cc *api.Client) {
if mah.target != s.Target {
t.Errorf("step.run() post_activity_hook.target = %v, want %v", mah.target, s.Target)
}
if mah.activity != s.Activities[0] {
if !reflect.DeepEqual(mah.activity, s.Activities[0]) {
t.Errorf("step.run() post_activity_hook.activity = %v, want %v", mah.activity, s.Activities[0])
}
}
Expand Down Expand Up @@ -233,8 +234,11 @@ func testWorkflowInputs(t *testing.T, srv1 *testutil.TestServer, cc *api.Client)
true,
nil},
{"TestWorkflowInput",
// user in a workflow input whose value will be added in task inputs
args{"greet", map[string]string{"user": "YorcUser"}, "GreetingsComponent_say_hello"},
false,
// greetings_user is defined as {get_input user}
// hello_msg is defined in the topology inputs
map[string]string{"greetings_user": "YorcUser", "hello_msg": "Hello"}},
}

Expand Down
10 changes: 6 additions & 4 deletions tosca/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,21 @@ func parsing(t *testing.T) {
computeInstallStep := installWF.Steps["Compute_install"]
assert.Equal(t, "Compute", computeInstallStep.Target)
assert.Nil(t, computeInstallStep.OnSuccess)
assert.Equal(t, "", computeInstallStep.Activities[0].CallOperation)
assert.Nil(t, computeInstallStep.Activities[0].CallOperation)
assert.Equal(t, "", computeInstallStep.Activities[0].SetState)
assert.Equal(t, "install", computeInstallStep.Activities[0].Delegate)
require.NotNil(t, computeInstallStep.Activities[0].Delegate)
assert.Equal(t, "install", computeInstallStep.Activities[0].Delegate.Workflow)

uninstallWF := topologyTemplate.Workflows["uninstall"]
assert.Len(t, installWF.Steps, 1)
assert.Contains(t, uninstallWF.Steps, "Compute_uninstall")
computeUninstallStep := uninstallWF.Steps["Compute_uninstall"]
assert.Equal(t, "Compute", computeUninstallStep.Target)
assert.Nil(t, computeUninstallStep.OnSuccess)
assert.Equal(t, "", computeUninstallStep.Activities[0].CallOperation)
assert.Nil(t, computeUninstallStep.Activities[0].CallOperation)
assert.Equal(t, "", computeUninstallStep.Activities[0].SetState)
assert.Equal(t, "uninstall", computeUninstallStep.Activities[0].Delegate)
require.NotNil(t, computeUninstallStep.Activities[0].Delegate)
assert.Equal(t, "uninstall", computeUninstallStep.Activities[0].Delegate.Workflow)
}

func parsingSubstitutionMappings(t *testing.T) {
Expand Down
Loading

0 comments on commit 7bcc821

Please sign in to comment.