From ab10ab971a66b4af552b9931cb048e438deec300 Mon Sep 17 00:00:00 2001 From: ryutoyasugi Date: Tue, 12 Oct 2021 11:59:15 +0900 Subject: [PATCH] Enable environment variable in action name https://github.com/apache/openwhisk-wskdeploy/issues/1145 --- parsers/manifest_parser.go | 10 ++++++--- parsers/manifest_parser_test.go | 22 +++++++++++++++++++ ...manifest_validate_action_name_env_var.yaml | 22 +++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 tests/dat/manifest_validate_action_name_env_var.yaml diff --git a/parsers/manifest_parser.go b/parsers/manifest_parser.go index 725155906..6c831de01 100644 --- a/parsers/manifest_parser.go +++ b/parsers/manifest_parser.go @@ -874,7 +874,11 @@ func (dm *YAMLParser) ComposeActions(manifestFilePath string, actions map[string // update the action (of type Action) to set its name // here key name is the action name - action.Name = actionName + if i, ok := packageInputs.Inputs[wskenv.GetEnvVarName(actionName)]; ok { + action.Name = i.Value.(string) + } else { + action.Name = wskenv.ConvertSingleName(actionName) + } // Create action data object from client library wskaction := new(whisk.Action) @@ -946,7 +950,7 @@ func (dm *YAMLParser) ComposeActions(manifestFilePath string, actions map[string if wskaction.Annotations != nil { if webaction.HasAnnotation(&wskaction.Annotations, webaction.REQUIRE_WHISK_AUTH) { _, errorParser = webaction.ValidateRequireWhiskAuthAnnotationValue( - actionName, + action.Name, wskaction.Annotations.GetValue(webaction.REQUIRE_WHISK_AUTH)) } if errorParser != nil { @@ -967,7 +971,7 @@ func (dm *YAMLParser) ComposeActions(manifestFilePath string, actions map[string } // Set other top-level values for the action (e.g., name, version, publish, etc.) - wskaction.Name = actionName + wskaction.Name = action.Name pub := false wskaction.Publish = &pub wskaction.Version = wskenv.ConvertSingleName(action.Version) diff --git a/parsers/manifest_parser_test.go b/parsers/manifest_parser_test.go index 434cc56f2..284a20d3c 100644 --- a/parsers/manifest_parser_test.go +++ b/parsers/manifest_parser_test.go @@ -1969,6 +1969,28 @@ func TestPackageName_Env_Var(t *testing.T) { } } +func TestActionName_Env_Var(t *testing.T) { + testAction := "test_action" + os.Setenv("action_name", testAction) + file := "../tests/dat/manifest_validate_action_name_env_var.yaml" + p, m, _ := testLoadParseManifest(t, file) + actions, err := p.ComposeActionsFromAllPackages(m, m.Filepath, whisk.KeyValue{}, map[string]PackageInputs{}) + if err != nil { + assert.Fail(t, "Failed to compose actions") + } + packageName := "helloworld" + + assert.Equal(t, 1, len(m.Packages[packageName].Actions), "Get action list failed.") + action := actions[0] + wskprint.PrintlnOpenWhiskVerbose(false, fmt.Sprintf("actionName: %v", action)) + switch action.Action.Name { + case testAction: + assert.Equal(t, testAction, action.Action.Name, "Get action name failed.") + default: + t.Error("Get action name failed") + } +} + func TestRuleName_Env_Var(t *testing.T) { // read and parse manifest file with env var for rule name, and rule trigger and action testRule := "test_rule" diff --git a/tests/dat/manifest_validate_action_name_env_var.yaml b/tests/dat/manifest_validate_action_name_env_var.yaml new file mode 100644 index 000000000..314fa3f75 --- /dev/null +++ b/tests/dat/manifest_validate_action_name_env_var.yaml @@ -0,0 +1,22 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +packages: + helloworld: + actions: + $action_name: + function: actions/hello.js