mrutkows closed pull request #766: adding support for using env. variables in 
inputs JSON
URL: https://github.com/apache/incubator-openwhisk-wskdeploy/pull/766
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/parsers/manifest_parser_test.go b/parsers/manifest_parser_test.go
index c66e5bbc..3bc0a5e5 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 3bbb11bd..4c03588d 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 fb771769..57030d05 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 492b5180..5877dc6b 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 cf9026e0..643d8155 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 == "" {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to