Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Action Annotations #631

Merged
merged 6 commits into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 73 additions & 45 deletions deployers/deploymentreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/apache/incubator-openwhisk-client-go/whisk"
"github.com/apache/incubator-openwhisk-wskdeploy/parsers"
"github.com/apache/incubator-openwhisk-wskdeploy/utils"
"github.com/apache/incubator-openwhisk-wskdeploy/wski18n"
)

type DeploymentReader struct {
Expand Down Expand Up @@ -51,15 +52,20 @@ func (reader *DeploymentReader) HandleYaml() error {
// Update entities with deployment settings
func (reader *DeploymentReader) BindAssets() error {

reader.bindPackageInputsAndAnnotations()
reader.bindActionInputsAndAnnotations()
reader.bindTriggerInputsAndAnnotations()
if err := reader.bindPackageInputsAndAnnotations(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for surfacing these errors from the "bind" functions.

return err
}
if err := reader.bindActionInputsAndAnnotations(); err != nil {
return err
}
if err := reader.bindTriggerInputsAndAnnotations(); err != nil {
return err
}

return nil

}

func (reader *DeploymentReader) bindPackageInputsAndAnnotations() {
func (reader *DeploymentReader) bindPackageInputsAndAnnotations() error {

packMap := make(map[string]parsers.Package)

Expand Down Expand Up @@ -121,25 +127,33 @@ func (reader *DeploymentReader) bindPackageInputsAndAnnotations() {
serviceDeployPack.Package.Parameters = keyValArr
}

keyValArr = make(whisk.KeyValueArr, 0)

if len(pack.Annotations) > 0 {
// iterate over each annotation from deployment file
for name, input := range pack.Annotations {
var keyVal whisk.KeyValue

keyVal.Key = name
keyVal.Value = utils.GetEnvVar(input)

keyValArr = append(keyValArr, keyVal)
// check if annotation key in deployment file exists in manifest file
// setting a bool flag to false assuming key does not exist in manifest
keyExistsInManifest := false
// iterate over each annotation from manifest file
for i, a := range serviceDeployPack.Package.Annotations {
if name == a.Key {
// annotation key is found in manifest
keyExistsInManifest = true
// overwrite annotation in manifest file with deployment file
serviceDeployPack.Package.Annotations[i].Value = input
break
}
}
if !keyExistsInManifest {
err := wski18n.T("Annotation key \"" + name + "\" does not exist in manifest file but specified in deployment file.")
return utils.NewYAMLFormatError(err)
}
}

serviceDeployPack.Package.Annotations = keyValArr
}

}
return nil
}

func (reader *DeploymentReader) bindActionInputsAndAnnotations() {
func (reader *DeploymentReader) bindActionInputsAndAnnotations() error {

packMap := make(map[string]parsers.Package)

Expand Down Expand Up @@ -202,28 +216,34 @@ func (reader *DeploymentReader) bindActionInputsAndAnnotations() {
}
}

keyValArr = make(whisk.KeyValueArr, 0)

if len(action.Annotations) > 0 {
if wskAction, exists := serviceDeployPack.Actions[actionName]; exists {
// iterate over each annotation from deployment file
for name, input := range action.Annotations {
var keyVal whisk.KeyValue

keyVal.Key = name
keyVal.Value = input

keyValArr = append(keyValArr, keyVal)
}

if wskAction, exists := serviceDeployPack.Actions[actionName]; exists {
wskAction.Action.Annotations = keyValArr
// check if annotation key in deployment file exists in manifest file
// setting a bool flag to false assuming key does not exist in manifest
keyExistsInManifest := false
// iterate over each annotation from manifest file
for i, a := range wskAction.Action.Annotations {
if name == a.Key {
// annotation key is found in manifest
keyExistsInManifest = true
// overwrite annotation in manifest file with deployment file
wskAction.Action.Annotations[i].Value = input
break
}
}
if !keyExistsInManifest {
err := wski18n.T("Annotation key \"" + name + "\" does not exist in manifest file but specified in deployment file.")
return utils.NewYAMLFormatError(err)
}
}
}
}

}
return nil
}

func (reader *DeploymentReader) bindTriggerInputsAndAnnotations() {
func (reader *DeploymentReader) bindTriggerInputsAndAnnotations() error {

packMap := make(map[string]parsers.Package)

Expand Down Expand Up @@ -282,23 +302,31 @@ func (reader *DeploymentReader) bindTriggerInputsAndAnnotations() {
}
}

keyValArr = make(whisk.KeyValueArr, 0)

if len(trigger.Annotations) > 0 {
if wskTrigger, exists := serviceDeployment.Triggers[triggerName]; exists {
// iterate over each annotation from deployment file
for name, input := range trigger.Annotations {
var keyVal whisk.KeyValue

keyVal.Key = name
keyVal.Value = input

keyValArr = append(keyValArr, keyVal)
}

if wskTrigger, exists := serviceDeployment.Triggers[triggerName]; exists {
wskTrigger.Annotations = keyValArr
// check if annotation key in deployment file exists in manifest file
// setting a bool flag to false assuming key does not exist in manifest
keyExistsInManifest := false
// iterate over each annotation from manifest file
for i, a := range wskTrigger.Annotations {
if name == a.Key {
// annotation key is found in manifest
keyExistsInManifest = true
// overwrite annotation in manifest file with deployment file
wskTrigger.Annotations[i].Value = input
break
}
}
if !keyExistsInManifest {
err := wski18n.T("Annotation key \"" + name + "\" does not exist in manifest file but specified in deployment file.")
return utils.NewYAMLFormatError(err)
}
}
}

}

}
return nil
}
183 changes: 119 additions & 64 deletions deployers/deploymentreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
package deployers

import (
"github.com/apache/incubator-openwhisk-client-go/whisk"
"github.com/stretchr/testify/assert"
"testing"
"github.com/apache/incubator-openwhisk-client-go/whisk"
"reflect"
)

var sd *ServiceDeployer
Expand Down Expand Up @@ -87,70 +88,124 @@ func TestDeploymentReader_bindTrigger(t *testing.T) {
}

func TestDeploymentReader_bindTrigger_packages(t *testing.T) {
//init variables
sDeployer := NewServiceDeployer()
sDeployer.DeploymentPath = "../tests/dat/deployment-deploymentreader-test-packages.yml"
sDeployer.Deployment.Triggers["locationUpdate"] = new(whisk.Trigger)

//parse deployment and bind triggers input and annotation
dReader := NewDeploymentReader(sDeployer)
dReader.HandleYaml()
dReader.bindTriggerInputsAndAnnotations()

trigger := sDeployer.Deployment.Triggers["locationUpdate"]
for _, param := range trigger.Parameters {
switch param.Key {
case "name":
assert.Equal(t, "Bernie", param.Value, "Failed to set inputs")
case "place":
assert.Equal(t, "DC", param.Value, "Failed to set inputs")
default:
assert.Fail(t, "Failed to get inputs key")

}
}
for _, annos := range trigger.Annotations {
switch annos.Key {
case "bbb":
assert.Equal(t, "this is an annotation", annos.Value, "Failed to set annotations")
default:
assert.Fail(t, "Failed to get annotation key")

}
}
//init variables
sDeployer := NewServiceDeployer()
sDeployer.DeploymentPath = "../tests/dat/deployment-deploymentreader-test-packages.yml"
sDeployer.Deployment.Triggers["locationUpdate"] = new(whisk.Trigger)

//parse deployment and bind triggers input and annotation
dReader := NewDeploymentReader(sDeployer)
dReader.HandleYaml()
dReader.bindTriggerInputsAndAnnotations()

trigger := sDeployer.Deployment.Triggers["locationUpdate"]
for _, param := range trigger.Parameters {
switch param.Key {
case "name":
assert.Equal(t, "Bernie", param.Value, "Failed to set inputs")
case "place":
assert.Equal(t, "DC", param.Value, "Failed to set inputs")
default:
assert.Fail(t, "Failed to get inputs key")

}
}
for _, annos := range trigger.Annotations {
switch annos.Key {
case "bbb":
assert.Equal(t, "this is an annotation", annos.Value, "Failed to set annotations")
default:
assert.Fail(t, "Failed to get annotation key")

}
}
}

func TestDeploymentReader_bindTrigger_package(t *testing.T) {
//init variables
sDeployer := NewServiceDeployer()
sDeployer.DeploymentPath = "../tests/dat/deployment-deploymentreader-test-package.yml"
sDeployer.Deployment.Triggers["locationUpdate"] = new(whisk.Trigger)

//parse deployment and bind triggers input and annotation
dReader := NewDeploymentReader(sDeployer)
dReader.HandleYaml()
dReader.bindTriggerInputsAndAnnotations()

assert.Equal(t, "triggerrule", dReader.DeploymentDescriptor.Package.Packagename)
trigger := sDeployer.Deployment.Triggers["locationUpdate"]
for _, param := range trigger.Parameters {
switch param.Key {
case "name":
assert.Equal(t, "Bernie", param.Value, "Failed to set inputs")
case "place":
assert.Equal(t, "DC", param.Value, "Failed to set inputs")
default:
assert.Fail(t, "Failed to get inputs key")

}
}
for _, annos := range trigger.Annotations {
switch annos.Key {
case "bbb":
assert.Equal(t, "this is an annotation", annos.Value, "Failed to set annotations")
default:
assert.Fail(t, "Failed to get annotation key")

}
}
//init variables
sDeployer := NewServiceDeployer()
sDeployer.DeploymentPath = "../tests/dat/deployment-deploymentreader-test-package.yml"
sDeployer.Deployment.Triggers["locationUpdate"] = new(whisk.Trigger)

//parse deployment and bind triggers input and annotation
dReader := NewDeploymentReader(sDeployer)
dReader.HandleYaml()
dReader.bindTriggerInputsAndAnnotations()

assert.Equal(t, "triggerrule", dReader.DeploymentDescriptor.Package.Packagename)
trigger := sDeployer.Deployment.Triggers["locationUpdate"]
for _, param := range trigger.Parameters {
switch param.Key {
case "name":
assert.Equal(t, "Bernie", param.Value, "Failed to set inputs")
case "place":
assert.Equal(t, "DC", param.Value, "Failed to set inputs")
default:
assert.Fail(t, "Failed to get inputs key")

}
}
for _, annos := range trigger.Annotations {
switch annos.Key {
case "bbb":
assert.Equal(t, "this is an annotation", annos.Value, "Failed to set annotations")
default:
assert.Fail(t, "Failed to get annotation key")

}
}
}

func TestDeploymentReader_BindAssets_ActionAnnotations(t *testing.T) {
sDeployer := NewServiceDeployer()
sDeployer.DeploymentPath = "../tests/dat/deployment_validate_action_annotations.yaml"
sDeployer.ManifestPath = "../tests/dat/manifest_validate_action_annotations.yaml"

//parse deployment and bind triggers input and annotation
dReader := NewDeploymentReader(sDeployer)
dReader.HandleYaml()
err := dReader.bindActionInputsAndAnnotations()

assert.Nil(t, err, "Failed to bind action annotations")

pkg_name := "packageActionAnnotations"
pkg := dReader.DeploymentDescriptor.Packages[pkg_name]
assert.NotNil(t, pkg, "Could not find package with name " + pkg_name)
action_name := "helloworld"
action := dReader.DeploymentDescriptor.GetProject().Packages[pkg_name].Actions[action_name]
assert.NotNil(t, action, "Could not find action with name " + action_name)
actual_annotations := action.Annotations
expected_annotations := map[string]interface{}{
"action_annotation_1": "this is annotation 1",
"action_annotation_2": "this is annotation 2",
}
assert.Equal(t, len(actual_annotations), len(expected_annotations), "Could not find expected number of annotations specified in manifest file")
eq := reflect.DeepEqual(actual_annotations, expected_annotations)
assert.True(t, eq, "Expected list of annotations does not match with actual list, expected annotations: %v actual annotations: %v", expected_annotations, actual_annotations)

pkg_name = "packageActionAnnotationsWithWebAction"
pkg = dReader.DeploymentDescriptor.Packages[pkg_name]
assert.NotNil(t, pkg, "Could not find package with name " + pkg_name)
action = dReader.DeploymentDescriptor.GetProject().Packages[pkg_name].Actions[action_name]
assert.NotNil(t, action, "Could not find action with name " + action_name)
actual_annotations = action.Annotations
expected_annotations["web-export"] = true
assert.Equal(t, len(actual_annotations), len(expected_annotations), "Could not find expected number of annotations specified in manifest file")
eq = reflect.DeepEqual(actual_annotations, expected_annotations)
assert.True(t, eq, "Expected list of annotations does not match with actual list, expected annotations: %v actual annotations: %v", expected_annotations, actual_annotations)

pkg_name = "packageActionAnnotationsFromDeployment"
pkg = dReader.DeploymentDescriptor.Packages[pkg_name]
assert.NotNil(t, pkg, "Could not find package with name " + pkg_name)
action = dReader.DeploymentDescriptor.GetProject().Packages[pkg_name].Actions[action_name]
assert.NotNil(t, action, "Could not find action with name " + action_name)
actual_annotations = action.Annotations
expected_annotations = map[string]interface{}{
"action_annotation_1": "this is annotation 1 from deployment",
"action_annotation_2": "this is annotation 2 from deployment",
}
assert.Equal(t, len(actual_annotations), len(expected_annotations), "Could not find expected number of annotations specified in manifest file")
eq = reflect.DeepEqual(actual_annotations, expected_annotations)
assert.True(t, eq, "Expected list of annotations does not match with actual list, expected annotations: %v actual annotations: %v", expected_annotations, actual_annotations)
}

8 changes: 6 additions & 2 deletions deployers/servicedeployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ func (deployer *ServiceDeployer) ConstructDeploymentPlan() error {
return utils.NewYAMLFormatError(errorString)
}
}
deploymentReader.BindAssets()
if err := deploymentReader.BindAssets(); err != nil {
return err
}
}

return err
Expand Down Expand Up @@ -250,7 +252,9 @@ func (deployer *ServiceDeployer) ConstructUnDeploymentPlan() (*DeploymentProject
}
}

deploymentReader.BindAssets()
if err := deploymentReader.BindAssets(); err != nil {
return deployer.Deployment, err
}
}

verifiedPlan := deployer.Deployment
Expand Down
Loading