You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by mr...@apache.org on 2018/03/07 17:58:52 UTC

[incubator-openwhisk-wskdeploy] branch master updated: adding support for using env. variables in inputs JSON (#766)

This is an automated email from the ASF dual-hosted git repository.

mrutkowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-wskdeploy.git


The following commit(s) were added to refs/heads/master by this push:
     new 76ca1e0  adding support for using env. variables in inputs JSON (#766)
76ca1e0 is described below

commit 76ca1e07f3f871f803d9b723e12c1c7255aa6c56
Author: Priti Desai <pd...@us.ibm.com>
AuthorDate: Wed Mar 7 09:58:50 2018 -0800

    adding support for using env. variables in inputs JSON (#766)
    
    * adding support for using env. variables in inputs JSON
    
    * fixing unit test failure
    
    * Update manifest_parser_test.go
    
    adding unset env. variable
---
 parsers/manifest_parser_test.go                   | 34 +++++++++++++++++++++--
 parsers/parameters.go                             |  5 +++-
 tests/dat/manifest_validate_json_params.yaml      |  6 ++++
 tests/dat/manifest_validate_multiline_params.yaml |  9 ++++++
 wskenv/environment.go                             | 10 +++++--
 5 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/parsers/manifest_parser_test.go b/parsers/manifest_parser_test.go
index c66e5bb..3bc0a5e 100644
--- a/parsers/manifest_parser_test.go
+++ b/parsers/manifest_parser_test.go
@@ -304,7 +304,7 @@ func TestParseManifestForMultiLineParams(t *testing.T) {
 		assert.Equal(t, expectedResult, actualResult, TEST_MSG_ACTION_FUNCTION_RUNTIME_MISMATCH)
 
 		// test # input params
-		expectedResult = strconv.FormatInt(10, 10)
+		expectedResult = strconv.FormatInt(13, 10)
 		actualResult = strconv.FormatInt(int64(len(action.Inputs)), 10)
 		assert.Equal(t, expectedResult, actualResult, TEST_MSG_PARAMETER_NUMBER_MISMATCH)
 
@@ -756,6 +756,10 @@ func TestComposeActionsForSingleLineParams(t *testing.T) {
 // Test 12: validate manifest_parser.ComposeActions() method for multi line parameters
 // manifest_parser should be able to parse input section with different types of values
 func TestComposeActionsForMultiLineParams(t *testing.T) {
+	os.Setenv("USERNAME", "MY_USERNAME")
+	os.Setenv("PASSWORD", "MY_PASSWORD")
+	defer os.Unsetenv("USERNAME")
+	defer os.Unsetenv("PASSWORD")
 
 	p, m, _ := testLoadParseManifest(t, "../tests/dat/manifest_validate_multiline_params.yaml")
 
@@ -826,6 +830,24 @@ func TestComposeActionsForMultiLineParams(t *testing.T) {
 		expectedResult = strconv.FormatFloat(2.9, 'f', -1, 64)
 		actualResult = strconv.FormatFloat(action.Action.Parameters.GetValue(paramName).(float64), 'f', -1, 64)
 		assert.Equal(t, expectedResult, actualResult, fmt.Sprintf(TEST_MSG_ACTION_PARAMETER_VALUE_MISMATCH, paramName))
+
+		// param_json_type_and_value_only_1 should be { "name": "Sam", "place": "Shire" }
+		paramName = "param_json_type_and_value_only_1"
+		expectedResult1 := map[string]interface{}{"name": "Sam", "place": "Shire"}
+		actualResult1 := action.Action.Parameters.GetValue(paramName)
+		assert.Equal(t, expectedResult1, actualResult1, fmt.Sprintf(TEST_MSG_ACTION_PARAMETER_VALUE_MISMATCH, paramName))
+
+		// param_json_type_and_value_only_2 should be { "name": "MY_USERNAME", "password": "MY_PASSWORD" }
+		paramName = "param_json_type_and_value_only_2"
+		expectedResult2 := map[string]interface{}{"name": "MY_USERNAME", "password": "MY_PASSWORD"}
+		actualResult2 := action.Action.Parameters.GetValue(paramName)
+		assert.Equal(t, expectedResult2, actualResult2, fmt.Sprintf(TEST_MSG_ACTION_PARAMETER_VALUE_MISMATCH, paramName))
+
+		// param_json_type_and_value_only_3 should be { "name": "${USERNAME}", "password": "${PASSWORD}" }
+		paramName = "param_json_type_and_value_only_3"
+		expectedResult3 := map[string]interface{}{"name": "${USERNAME}", "password": "${PASSWORD}"}
+		actualResult3 := action.Action.Parameters.GetValue(paramName)
+		assert.Equal(t, expectedResult3, actualResult3, fmt.Sprintf(TEST_MSG_ACTION_PARAMETER_VALUE_MISMATCH, paramName))
 	}
 }
 
@@ -1032,7 +1054,7 @@ func TestParseManifestForJSONParams(t *testing.T) {
 		assert.Equal(t, expectedResult, actualResult, TEST_MSG_ACTION_FUNCTION_RUNTIME_MISMATCH)
 
 		// validate the number of inputs to this action
-		expectedResult = strconv.FormatInt(6, 10)
+		expectedResult = strconv.FormatInt(8, 10)
 		actualResult = strconv.FormatInt(int64(len(action.Inputs)), 10)
 		assert.Equal(t, expectedResult, actualResult, TEST_MSG_PARAMETER_NUMBER_MISMATCH)
 
@@ -1065,6 +1087,14 @@ func TestParseManifestForJSONParams(t *testing.T) {
 				actualResult6 := param.Value.(map[interface{}]interface{})
 				expectedResult6 := map[interface{}]interface{}{"name": "Frodo", "place": "Undying Lands", "items": []interface{}{"Sting", "Mithril mail"}}
 				assert.Equal(t, expectedResult6, actualResult6, fmt.Sprintf(TEST_MSG_ACTION_PARAMETER_VALUE_MISMATCH, input))
+			case "member7":
+				actualResult7 := param.Value.(map[interface{}]interface{})
+				expectedResult7 := map[interface{}]interface{}{"name": "${USERNAME}", "password": "${PASSWORD}"}
+				assert.Equal(t, expectedResult7, actualResult7, fmt.Sprintf(TEST_MSG_ACTION_PARAMETER_VALUE_MISMATCH, input))
+			case "member8":
+				actualResult8 := param.Value.(map[interface{}]interface{})
+				expectedResult8 := map[interface{}]interface{}{"name": "$${USERNAME}", "password": "$${PASSWORD}"}
+				assert.Equal(t, expectedResult8, actualResult8, fmt.Sprintf(TEST_MSG_ACTION_PARAMETER_VALUE_MISMATCH, input))
 			}
 		}
 
diff --git a/parsers/parameters.go b/parsers/parameters.go
index 3bbb11b..4c03588 100644
--- a/parsers/parameters.go
+++ b/parsers/parameters.go
@@ -258,6 +258,9 @@ func resolveJSONParameter(filePath string, paramName string, param *Parameter, v
 		if param.Value != nil && reflect.TypeOf(param.Value).Kind() == reflect.Map {
 			if _, ok := param.Value.(map[interface{}]interface{}); ok {
 				var temp map[string]interface{} = utils.ConvertInterfaceMap(param.Value.(map[interface{}]interface{}))
+				for name, val := range temp {
+					temp[name] = wskenv.InterpolateStringWithEnvVar(val)
+				}
 				//fmt.Printf("EXIT: Parameter [%s] type=[%v] value=[%v]\n", paramName, param.Type, temp)
 				return temp, errorParser
 			}
@@ -322,7 +325,7 @@ func ResolveParameter(paramName string, param *Parameter, filePath string) (inte
 	}
 
 	// JSON - Handle both cases, where value 1) is a string containing JSON, 2) is a map of JSON
-	if param.Type == "json" {
+	if param.Value != nil && param.Type == "json" {
 		value, errorParser = resolveJSONParameter(filePath, paramName, param, value)
 	}
 
diff --git a/tests/dat/manifest_validate_json_params.yaml b/tests/dat/manifest_validate_json_params.yaml
index fb77176..57030d0 100644
--- a/tests/dat/manifest_validate_json_params.yaml
+++ b/tests/dat/manifest_validate_json_params.yaml
@@ -44,6 +44,12 @@ packages:
           member6:
             type: json
             value: { "name": "Frodo", "place": "Undying Lands", "items": [ "Sting", "Mithril mail" ] }
+          member7:
+            type: json
+            value: { "name": "${USERNAME}", "password": "${PASSWORD}" }
+          member8:
+            type: json
+            value: { "name": "$${USERNAME}", "password": "$${PASSWORD}" }
         outputs:
             fellowship:
               type: json
diff --git a/tests/dat/manifest_validate_multiline_params.yaml b/tests/dat/manifest_validate_multiline_params.yaml
index 492b518..5877dc6 100644
--- a/tests/dat/manifest_validate_multiline_params.yaml
+++ b/tests/dat/manifest_validate_multiline_params.yaml
@@ -32,6 +32,15 @@ packages:
                     param_string_type_and_value_only:
                         type: string
                         value: foo
+                    param_json_type_and_value_only_1:
+                        type: json
+                        value: '{ "name": "Sam", "place": "Shire" }'
+                    param_json_type_and_value_only_2:
+                        type: json
+                        value: { "name": "${USERNAME}", "password": "${PASSWORD}" }
+                    param_json_type_and_value_only_3:
+                        type: json
+                        value: { "name": "$${USERNAME}", "password": "$${PASSWORD}" }
                     # type only
                     param_string_type_only:
                         type: string
diff --git a/wskenv/environment.go b/wskenv/environment.go
index cf9026e..643d815 100644
--- a/wskenv/environment.go
+++ b/wskenv/environment.go
@@ -63,8 +63,13 @@ func InterpolateStringWithEnvVar(key interface{}) interface{} {
 				return c == '$' || c == '{' || c == '}'
 			}
 			for _, substr := range strings.FieldsFunc(keystr, f) {
-				//if the substr is a $ENV_VAR
-				if strings.Contains(keystr, "$"+substr) {
+				//if the substr is a $${ENV_VAR}
+				// return ${ENV_VAR}
+				if strings.Contains(keystr, "$${"+substr+"}") {
+					keystr = strings.Replace(keystr, "$${"+substr+"}", "${"+substr+"}", -1)
+					//if the substr is a $ENV_VAR
+					// return interpolated string using env. variable
+				} else if strings.Contains(keystr, "$"+substr) {
 					thisValue = os.Getenv(substr)
 					if thisValue == "" {
 						// TODO() i18n
@@ -72,6 +77,7 @@ func InterpolateStringWithEnvVar(key interface{}) interface{} {
 					}
 					keystr = strings.Replace(keystr, "$"+substr, thisValue, -1)
 					//if the substr is a ${ENV_VAR}
+					// return interpolated string using env. variable
 				} else if strings.Contains(keystr, "${"+substr+"}") {
 					thisValue = os.Getenv(substr)
 					if thisValue == "" {

-- 
To stop receiving notification emails like this one, please contact
mrutkowski@apache.org.