Skip to content

Commit

Permalink
Enable environment variable in action name
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutoyasugi committed Oct 12, 2021
1 parent ff6238d commit 55d2286
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
10 changes: 7 additions & 3 deletions parsers/manifest_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions parsers/manifest_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions tests/dat/manifest_validate_action_name_env_var.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 55d2286

Please sign in to comment.