Skip to content

Commit 0507721

Browse files
lionelvillardpaulcastro
authored andcommitted
Treat inline parameter value as JSON and add support for expanded par… (#221)
* Treat inline parameter value as JSON and add support for expanded parameters (#219) * Must resolve parameters for actions and triggers as well (#219) * Remove unused import. (#219)
1 parent 4fc540f commit 0507721

File tree

8 files changed

+234
-36
lines changed

8 files changed

+234
-36
lines changed

deployers/servicedeployer.go

+3-17
Original file line numberDiff line numberDiff line change
@@ -650,23 +650,14 @@ func (deployer *ServiceDeployer) printDeploymentAssets(assets *DeploymentApplica
650650
fmt.Println("Name: " + pack.Package.Name)
651651
fmt.Println(" bindings: ")
652652
for _, p := range pack.Package.Parameters {
653-
value := "?"
654-
if str, ok := p.Value.(string); ok {
655-
value = str
656-
}
657-
fmt.Println(" - name: " + p.Key + " value: " + value)
653+
fmt.Printf(" - %s : %v\n", p.Key, utils.PrettyJSON(p.Value))
658654
}
659655

660656
for _, action := range pack.Actions {
661657
fmt.Println(" * action: " + action.Action.Name)
662658
fmt.Println(" bindings: ")
663659
for _, p := range action.Action.Parameters {
664-
665-
value := "?"
666-
if str, ok := p.Value.(string); ok {
667-
value = str
668-
}
669-
fmt.Println(" - name: " + p.Key + " value: " + value)
660+
fmt.Printf(" - %s : %v\n", p.Key, utils.PrettyJSON(p.Value))
670661
}
671662
fmt.Println(" annotations: ")
672663
for _, p := range action.Action.Annotations {
@@ -689,12 +680,7 @@ func (deployer *ServiceDeployer) printDeploymentAssets(assets *DeploymentApplica
689680
fmt.Println(" bindings: ")
690681

691682
for _, p := range trigger.Parameters {
692-
693-
value := "?"
694-
if str, ok := p.Value.(string); ok {
695-
value = str
696-
}
697-
fmt.Println(" - name: " + p.Key + " value: " + value)
683+
fmt.Printf(" - %s : %v\n", p.Key, utils.PrettyJSON(p.Value))
698684
}
699685

700686
fmt.Println(" annotations: ")

parsers/manifest_parser.go

+70-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"path"
2525
"strings"
2626

27+
"encoding/json"
2728
"github.com/openwhisk/openwhisk-client-go/whisk"
2829
"github.com/openwhisk/openwhisk-wskdeploy/utils"
2930
"gopkg.in/yaml.v2"
@@ -95,12 +96,15 @@ func (dm *YAMLParser) ComposePackage(mani *ManifestYAML) (*whisk.Package, error)
9596
pag.Publish = &pub
9697

9798
keyValArr := make(whisk.KeyValueArr, 0)
98-
for name, value := range mani.Package.Inputs {
99+
for name, param := range mani.Package.Inputs {
99100
var keyVal whisk.KeyValue
100101
keyVal.Key = name
101-
keyVal.Value = utils.GetEnvVar(value)
102102

103-
keyValArr = append(keyValArr, keyVal)
103+
keyVal.Value = ResolveParameter(&param)
104+
105+
if keyVal.Value != nil {
106+
keyValArr = append(keyValArr, keyVal)
107+
}
104108
}
105109

106110
if len(keyValArr) > 0 {
@@ -178,12 +182,15 @@ func (dm *YAMLParser) ComposeActions(mani *ManifestYAML, manipath string) ([]uti
178182
}
179183

180184
keyValArr := make(whisk.KeyValueArr, 0)
181-
for name, value := range action.Inputs {
185+
for name, param := range action.Inputs {
182186
var keyVal whisk.KeyValue
183187
keyVal.Key = name
184-
keyVal.Value = utils.GetEnvVar(value)
185188

186-
keyValArr = append(keyValArr, keyVal)
189+
keyVal.Value = ResolveParameter(&param)
190+
191+
if keyVal.Value != nil {
192+
keyValArr = append(keyValArr, keyVal)
193+
}
187194
}
188195

189196
if len(keyValArr) > 0 {
@@ -239,12 +246,15 @@ func (dm *YAMLParser) ComposeTriggers(manifest *ManifestYAML) ([]*whisk.Trigger,
239246
}
240247

241248
keyValArr = make(whisk.KeyValueArr, 0)
242-
for name, value := range trigger.Inputs {
249+
for name, param := range trigger.Inputs {
243250
var keyVal whisk.KeyValue
244251
keyVal.Key = name
245-
keyVal.Value = utils.GetEnvVar(value)
246252

247-
keyValArr = append(keyValArr, keyVal)
253+
keyVal.Value = ResolveParameter(&param)
254+
255+
if keyVal.Value != nil {
256+
keyValArr = append(keyValArr, keyVal)
257+
}
248258
}
249259

250260
if len(keyValArr) > 0 {
@@ -276,3 +286,54 @@ func (action *Action) ComposeWskAction(manipath string) (*whisk.Action, error) {
276286
wskaction.Namespace = action.Namespace
277287
return wskaction, err
278288
}
289+
290+
// Resolve parameter input
291+
func ResolveParameter(param *Parameter) interface{} {
292+
value := utils.GetEnvVar(param.Value)
293+
294+
typ := param.Type
295+
if str, ok := value.(string); ok && (len(typ) == 0 || typ != "string") {
296+
var parsed interface{}
297+
err := json.Unmarshal([]byte(str), &parsed)
298+
if err == nil {
299+
return parsed
300+
}
301+
}
302+
return value
303+
}
304+
305+
// Provide custom Parameter marshalling and unmarshalling
306+
307+
type ParsedParameter Parameter
308+
309+
func (n *Parameter) UnmarshalYAML(unmarshal func(interface{}) error) error {
310+
var aux ParsedParameter
311+
if err := unmarshal(&aux); err == nil {
312+
n.Type = aux.Type
313+
n.Description = aux.Description
314+
n.Value = aux.Value
315+
n.Required = aux.Required
316+
n.Default = aux.Default
317+
n.Status = aux.Status
318+
n.Schema = aux.Schema
319+
return nil
320+
}
321+
322+
var inline interface{}
323+
if err := unmarshal(&inline); err != nil {
324+
return err
325+
}
326+
327+
n.Value = inline
328+
return nil
329+
}
330+
331+
func (n *Parameter) MarshalYAML() (interface{}, error) {
332+
if _, ok := n.Value.(string); len(n.Type) == 0 && len(n.Description) == 0 && ok {
333+
if !n.Required && len(n.Status) == 0 && n.Schema == nil {
334+
return n.Value.(string), nil
335+
}
336+
}
337+
338+
return n, nil
339+
}

parsers/yamlparser.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type Action struct {
5656
//mapping to wsk.Action.Namespace
5757
Namespace string `yaml:"namespace"` //used in deployment.yaml
5858
Credential string `yaml:"credential"` //used in deployment.yaml
59-
Inputs map[string]interface{} `yaml:"inputs"` //used in both manifest.yaml and deployment.yaml
59+
Inputs map[string]Parameter `yaml:"inputs"` //used in both manifest.yaml and deployment.yaml
6060
Outputs map[string]interface{} `yaml:"outputs"` //used in manifest.yaml
6161
//mapping to wsk.Action.Name
6262
Name string
@@ -75,13 +75,23 @@ type Dependency struct {
7575
Version string
7676
}
7777

78+
type Parameter struct {
79+
Type string `yaml:"type,omitempty"`
80+
Description string `yaml:"description,omitempty"`
81+
Value interface{} `yaml:"value,omitempty"` // JSON Value
82+
Required bool `yaml:"required,omitempty"`
83+
Default interface{} `yaml:"default,omitempty"`
84+
Status string `yaml:"status,omitempty"`
85+
Schema interface{} `yaml:"schema,omitempty"`
86+
}
87+
7888
type Trigger struct {
7989
//mapping to ????
8090
Feed string `yaml:"feed"` //used in manifest.yaml
8191
//mapping to wsk.Trigger.Namespace
82-
Namespace string `yaml:"namespace"` //used in deployment.yaml
83-
Credential string `yaml:"credential"` //used in deployment.yaml
84-
Inputs map[string]interface{} `yaml:"inputs"` //used in deployment.yaml
92+
Namespace string `yaml:"namespace"` //used in deployment.yaml
93+
Credential string `yaml:"credential"` //used in deployment.yaml
94+
Inputs map[string]Parameter `yaml:"inputs"` //used in deployment.yaml
8595
//mapping to wsk.Trigger.Name
8696
Name string
8797
Annotations map[string]interface{} `yaml:"annotations,omitempty"`
@@ -133,7 +143,7 @@ type Package struct {
133143
Triggers map[string]Trigger `yaml:"triggers"` //used in both manifest.yaml and deployment.yaml
134144
Feeds map[string]Feed `yaml:"feeds"` //used in both manifest.yaml and deployment.yaml
135145
Rules map[string]Rule `yaml:"rules"` //used in both manifest.yaml and deployment.yaml
136-
Inputs map[string]interface{} `yaml:"inputs"` //used in deployment.yaml
146+
Inputs map[string]Parameter `yaml:"inputs"` //used in deployment.yaml
137147
Sequences map[string]Sequence `yaml:"sequences"`
138148
Annotations map[string]interface{} `yaml:"annotations,omitempty"`
139149
//Parameters map[string]interface{} `yaml: parameters` // used in manifest.yaml

specification/openwhisk_v0.8.3.pdf

222 Bytes
Binary file not shown.

tests/dat/manifest6.yaml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package:
2+
name: manifest6
3+
actions:
4+
action1:
5+
inputs:
6+
inline1: '{ "key": true }'
7+
inline2: Just a string
8+
inline3: null
9+
inline4: true
10+
inline5: 42
11+
inline6: -531
12+
inline7: 432.432E-43
13+
inline8: '[ true, null, "boo", { "key": 0 }]'
14+
inline9: !!bool false
15+
inline0: !!float 456.423
16+
inlin10: # JSON null
17+
inlin11: True # JSON true
18+
19+
expand1:
20+
value: null
21+
type: string
22+
23+
expand2:
24+
value: true
25+
type: string
26+
27+
expand3:
28+
value: false
29+
type: string
30+
31+
expand4:
32+
value: 15646
33+
type: string
34+
35+
expand5:
36+
value: '{ "key": true }'
37+
type: string
38+
39+
expand6:
40+
value: '[ true, null, "boo", { "key": 0 }]'
41+
type: string
42+
43+
expand7:
44+
value: !!null null
45+
type: string

tests/src/deployers/manifestreader_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var ps *parsers.YAMLParser
1212
var ms *parsers.ManifestYAML
1313

1414
func init() {
15+
1516
sd = deployers.NewServiceDeployer()
1617
sd.ManifestPath = manifest_file
1718
mr = deployers.NewManfiestReader(sd)
@@ -30,3 +31,12 @@ func TestManifestReader_InitRootPackage(t *testing.T) {
3031
err := mr.InitRootPackage(ps, ms)
3132
assert.Equal(t, err, nil, "Init Root Package failed")
3233
}
34+
35+
// Test Parameters
36+
func TestManifestReader_param(t *testing.T) {
37+
ms := ps.ParseManifest("../../dat/manifest6.yaml")
38+
err := mr.InitRootPackage(ps, ms)
39+
assert.Equal(t, err, nil, "Init Root Package failed")
40+
41+
// TODO.
42+
}

tests/src/parser/yamlparser_test.go

+74-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var manifestfile1 = "../../dat/manifest1.yaml"
1313
var manifestfile3 = "../../dat/manifest3.yaml"
1414
var manifestfile4 = "../../dat/manifest4.yaml"
1515
var manifestfile5 = "../../dat/manifest5.yaml"
16+
var manifestfile6 = "../../dat/manifest6.yaml"
1617
var testfile1 = "../../dat/deploy1.yaml"
1718
var testfile2 = "../../dat/deploy2.yaml"
1819
var testfile3 = "../../dat/deploy3.yaml"
@@ -131,6 +132,74 @@ func TestParseManifestYAML_feed(t *testing.T) {
131132
}
132133
}
133134

135+
func TestParseManifestYAML_param(t *testing.T) {
136+
data, err := ioutil.ReadFile(manifestfile6)
137+
if err != nil {
138+
panic(err)
139+
}
140+
141+
var manifest parsers.ManifestYAML
142+
err = parsers.NewYAMLParser().Unmarshal(data, &manifest)
143+
if err != nil {
144+
panic(err)
145+
}
146+
147+
assert.Equal(t, 1, len(manifest.Package.Actions), "Get action list failed.")
148+
for action_name := range manifest.Package.Actions {
149+
var action = manifest.Package.Actions[action_name]
150+
switch action_name {
151+
case "action1":
152+
for param_name := range action.Inputs {
153+
var param = action.Inputs[param_name]
154+
switch param_name {
155+
case "inline1":
156+
assert.Equal(t, "{ \"key\": true }", param.Value, "Get param value failed.")
157+
case "inline2":
158+
assert.Equal(t, "Just a string", param.Value, "Get param value failed.")
159+
case "inline3":
160+
assert.Equal(t, nil, param.Value, "Get param value failed.")
161+
case "inline4":
162+
assert.Equal(t, true, param.Value, "Get param value failed.")
163+
case "inline5":
164+
assert.Equal(t, 42, param.Value, "Get param value failed.")
165+
case "inline6":
166+
assert.Equal(t, -531, param.Value, "Get param value failed.")
167+
case "inline7":
168+
assert.Equal(t, 432.432E-43, param.Value, "Get param value failed.")
169+
case "inline8":
170+
assert.Equal(t, "[ true, null, \"boo\", { \"key\": 0 }]", param.Value, "Get param value failed.")
171+
case "inline9":
172+
assert.Equal(t, false, param.Value, "Get param value failed.")
173+
case "inline0":
174+
assert.Equal(t, 456.423, param.Value, "Get param value failed.")
175+
case "inlin10":
176+
assert.Equal(t, nil, param.Value, "Get param value failed.")
177+
case "inlin11":
178+
assert.Equal(t, true, param.Value, "Get param value failed.")
179+
case "expand1":
180+
assert.Equal(t, nil, param.Value, "Get param value failed.")
181+
case "expand2":
182+
assert.Equal(t, true, param.Value, "Get param value failed.")
183+
case "expand3":
184+
assert.Equal(t, false, param.Value, "Get param value failed.")
185+
case "expand4":
186+
assert.Equal(t, 15646, param.Value, "Get param value failed.")
187+
case "expand5":
188+
assert.Equal(t, "{ \"key\": true }", param.Value, "Get param value failed.")
189+
case "expand6":
190+
assert.Equal(t, "[ true, null, \"boo\", { \"key\": 0 }]", param.Value, "Get param value failed.")
191+
case "expand7":
192+
assert.Equal(t, nil, param.Value, "Get param value failed.")
193+
default:
194+
t.Error("Get param name failed")
195+
}
196+
}
197+
default:
198+
t.Error("Get action name failed")
199+
}
200+
}
201+
}
202+
134203
func TestParseDeploymentYAML_Application(t *testing.T) {
135204
//var deployment utils.DeploymentYAML
136205
mm := parsers.NewYAMLParser()
@@ -158,8 +227,8 @@ func TestParseDeploymentYAML_Package(t *testing.T) {
158227
assert.Equal(t, "12345678ABCDEF", pkg.Credential, "Get package credential failed.")
159228
assert.Equal(t, 1, len(pkg.Inputs), "Get package input list failed.")
160229
//get and verify inputs
161-
for param_name, value := range pkg.Inputs {
162-
assert.Equal(t, "value", value, "Get input value failed.")
230+
for param_name, param := range pkg.Inputs {
231+
assert.Equal(t, "value", param.Value, "Get input value failed.")
163232
assert.Equal(t, "param", param_name, "Get input param name failed.")
164233
}
165234
}
@@ -179,11 +248,11 @@ func TestParseDeploymentYAML_Action(t *testing.T) {
179248
assert.Equal(t, "12345678ABCDEF", action.Credential, "Get action credential failed.")
180249
assert.Equal(t, 1, len(action.Inputs), "Get package input list failed.")
181250
//get and verify inputs
182-
for param_name, value := range action.Inputs {
183-
switch value.(type) {
251+
for param_name, param := range action.Inputs {
252+
switch param.Value.(type) {
184253
case string:
185254
assert.Equal(t, "name", param_name, "Get input param name failed.")
186-
assert.Equal(t, "Bernie", value, "Get input value failed.")
255+
assert.Equal(t, "Bernie", param.Value, "Get input value failed.")
187256
default:
188257
t.Error("Get input value type failed.")
189258
}

0 commit comments

Comments
 (0)