Skip to content

Commit

Permalink
Fix REQ_TARGET on missing optional requirement
Browse files Browse the repository at this point in the history
When resolving a REQ_TARGET prop or attr fail only if the requirement is
not optional.

Signed-off-by: Albertin Loic <[email protected]>
  • Loading branch information
loicalbertin committed Nov 18, 2022
1 parent 0c1811f commit b5bb3d8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
32 changes: 28 additions & 4 deletions deployments/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ package deployments

import (
"context"
"github.com/pkg/errors"
"github.com/ystia/yorc/v4/helper/consulutil"
"github.com/ystia/yorc/v4/tosca"
"path"
"strconv"
"strings"

"github.com/pkg/errors"

"github.com/ystia/yorc/v4/helper/consulutil"
"github.com/ystia/yorc/v4/log"
"github.com/ystia/yorc/v4/tosca"
)

// Requirement describes a requirement assignment with its name and index
Expand Down Expand Up @@ -171,7 +174,6 @@ func getRequirementByIndex(ctx context.Context, deploymentID, nodeName, requirem

// GetTargetInstanceForRequirement returns the target node and instances
// associated with a given requirementIndex of the given nodeName/instanceName.
//
func GetTargetInstanceForRequirement(ctx context.Context, deploymentID, nodeName, requirementIndex, instanceName string) (string, []string, error) {
targetPrefix := path.Join(
consulutil.DeploymentKVPrefix, deploymentID,
Expand Down Expand Up @@ -260,3 +262,25 @@ func HasAnyRequirementFromNodeType(ctx context.Context, deploymentID, nodeName,
}
return false, "", nil
}

func GetRequirementDefinitionOnTypeByName(ctx context.Context, deploymentID, nodeType, requirementName string) (*tosca.RequirementDefinition, error) {
log.Debugf("-> searching for requirement %q on node type %q\n", requirementName, nodeType)
node := new(tosca.NodeType)
err := getExpectedTypeFromName(ctx, deploymentID, nodeType, node)
if err != nil {
return nil, err
}
for _, reqMap := range node.Requirements {
req, ok := reqMap[requirementName]
if ok {
log.Debugf("-> found requirement %q on node type %q\n", requirementName, nodeType)
return &req, nil
}
}

if node.DerivedFrom == "" {
log.Debugf("-> requirement %q not found on node type %q and no parent type\n", requirementName, nodeType)
return nil, nil
}
return GetRequirementDefinitionOnTypeByName(ctx, deploymentID, node.DerivedFrom, requirementName)
}
19 changes: 19 additions & 0 deletions deployments/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,25 @@ func (fr *functionResolver) resolveGetPropertyOrAttribute(ctx context.Context, r
if err != nil {
return nil, err
}
if actualNode == "" {
nodeType, err := GetNodeType(ctx, fr.deploymentID, fr.nodeName)
if err != nil {
return nil, err
}
req, err := GetRequirementDefinitionOnTypeByName(ctx, fr.deploymentID, nodeType, operands[1])
if err != nil {
return nil, err
}
if req == nil {
return nil, errors.Errorf("missing requirement %q on node %q", operands[1], fr.nodeName)
}
if req.Occurrences.LowerBound != 0 {
return nil, errors.Errorf("requirement %q on node %q not found while not optional (occurrences lower bound set to %d)", operands[1], fr.nodeName, req.Occurrences.LowerBound)
}
// this means that the relationship between those 2 nodes does not exist
// and the requirement definition occurrence lower bound is set to 0 meaning it is optional.
return nil, nil
}
default:
actualNode = entity
}
Expand Down

0 comments on commit b5bb3d8

Please sign in to comment.