From 46ec47ef22b1c57e5702b8af3353410db62757e4 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Mon, 19 Sep 2016 19:28:40 -0500 Subject: [PATCH 1/4] param: accept ALL THE THINGS --- load/resource_test.go | 2 +- render/render_test.go | 8 ++------ resource/param/preparer.go | 18 ++++++++++++++---- resource/param/preparer_test.go | 22 +++++++++++----------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/load/resource_test.go b/load/resource_test.go index d4d0df852..7e5c3f3c3 100644 --- a/load/resource_test.go +++ b/load/resource_test.go @@ -91,7 +91,7 @@ param x { preparer, ok := item.(*param.Preparer) require.True(t, ok, fmt.Sprintf("preparer was %T, not *param.Preparer", item)) - assert.Equal(t, "default", *preparer.Default) + assert.Equal(t, "default", preparer.Default) } func TestSetResourcesModules(t *testing.T) { diff --git a/render/render_test.go b/render/render_test.go index aacedb020..44d7831d1 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -56,7 +56,7 @@ func TestRenderParam(t *testing.T) { g := graph.New() g.Add("root", nil) g.Add("root/file.content.x", &content.Preparer{Destination: "{{param `destination`}}"}) - g.Add("root/param.destination", ¶m.Preparer{Default: newDefault("1")}) + g.Add("root/param.destination", ¶m.Preparer{Default: "1"}) g.ConnectParent("root", "root/file.content.x") g.ConnectParent("root", "root/param.destination") @@ -82,7 +82,7 @@ func TestRenderValues(t *testing.T) { g := graph.New() g.Add("root", nil) g.Add("root/file.content.x", &content.Preparer{Destination: "{{param `destination`}}"}) - g.Add("root/param.destination", ¶m.Preparer{Default: newDefault("1")}) + g.Add("root/param.destination", ¶m.Preparer{Default: "1"}) g.ConnectParent("root", "root/file.content.x") g.ConnectParent("root", "root/param.destination") @@ -101,7 +101,3 @@ func TestRenderValues(t *testing.T) { assert.Equal(t, "2", content.Destination) } - -func newDefault(x string) *string { - return &x -} diff --git a/resource/param/preparer.go b/resource/param/preparer.go index 2f5759584..97c78d42a 100644 --- a/resource/param/preparer.go +++ b/resource/param/preparer.go @@ -16,6 +16,7 @@ package param import ( "errors" + "fmt" "github.com/asteris-llc/converge/load/registry" "github.com/asteris-llc/converge/resource" @@ -30,7 +31,7 @@ type Preparer struct { // Default is an optional field that provides a default value if none is // provided to this parameter. If this field is not set, this param will be // treated as required. - Default *string `hcl:"default"` + Default interface{} `hcl:"default"` } // Prepare a new task @@ -43,9 +44,18 @@ func (p *Preparer) Prepare(render resource.Renderer) (resource.Task, error) { return nil, errors.New("param is required") } - def, err := render.Render("default", *p.Default) - if err != nil { - return nil, err + var def string + + switch v := p.Default.(type) { + case string: + var err error + def, err = render.Render("default", v) + if err != nil { + return nil, err + } + + default: + def = fmt.Sprintf("%v", v) } return &Param{Value: def}, nil diff --git a/resource/param/preparer_test.go b/resource/param/preparer_test.go index f9a1f077e..439f9d384 100644 --- a/resource/param/preparer_test.go +++ b/resource/param/preparer_test.go @@ -34,21 +34,25 @@ func TestPreparerInterface(t *testing.T) { func TestPreparerDefault(t *testing.T) { t.Parallel() - prep := ¶m.Preparer{Default: newDefault("x")} + vals := []interface{}{"x", true} - result, err := prep.Prepare(fakerenderer.New()) + for _, val := range vals { + prep := ¶m.Preparer{Default: val} - resultParam, ok := result.(*param.Param) - require.True(t, ok, fmt.Sprintf("expected %T, got %T", resultParam, result)) + result, err := prep.Prepare(fakerenderer.New()) - require.Nil(t, err) - assert.Equal(t, *prep.Default, resultParam.Value) + resultParam, ok := result.(*param.Param) + require.True(t, ok, fmt.Sprintf("expected %T, got %T", resultParam, result)) + + require.Nil(t, err) + assert.Equal(t, fmt.Sprintf("%v", prep.Default), resultParam.Value) + } } func TestPreparerProvided(t *testing.T) { t.Parallel() - prep := ¶m.Preparer{Default: newDefault("x")} + prep := ¶m.Preparer{Default: "x"} result, err := prep.Prepare(fakerenderer.NewWithValue("y")) @@ -69,7 +73,3 @@ func TestPreparerRequired(t *testing.T) { assert.EqualError(t, err, "param is required") } } - -func newDefault(x string) *string { - return &x -} From 931de652eedf28ce0211866eafe39103894f2762 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Mon, 19 Sep 2016 19:34:46 -0500 Subject: [PATCH 2/4] param: defer the stringification of value until asked This should make it easier to add support for composite data structures like lists and maps later. But that's a little beyond the scope of this PR! --- resource/param/param.go | 12 ++++++++---- resource/param/preparer.go | 5 ++--- resource/param/preparer_test.go | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/resource/param/param.go b/resource/param/param.go index 972587891..892dd297c 100644 --- a/resource/param/param.go +++ b/resource/param/param.go @@ -14,11 +14,15 @@ package param -import "github.com/asteris-llc/converge/resource" +import ( + "fmt" + + "github.com/asteris-llc/converge/resource" +) // Param controls parameter flow inside execution type Param struct { - Value string + Value interface{} } // Check just returns the current value of the parameter. It should never have to change. @@ -31,7 +35,7 @@ func (p *Param) Apply(r resource.Renderer) (resource.TaskStatus, error) { return p.Check(r) } -// String is the final value of thie Param +// String is the final value of this Param func (p *Param) String() string { - return p.Value + return fmt.Sprintf("%v", p.Value) } diff --git a/resource/param/preparer.go b/resource/param/preparer.go index 97c78d42a..a6812e937 100644 --- a/resource/param/preparer.go +++ b/resource/param/preparer.go @@ -16,7 +16,6 @@ package param import ( "errors" - "fmt" "github.com/asteris-llc/converge/load/registry" "github.com/asteris-llc/converge/resource" @@ -44,7 +43,7 @@ func (p *Preparer) Prepare(render resource.Renderer) (resource.Task, error) { return nil, errors.New("param is required") } - var def string + var def interface{} switch v := p.Default.(type) { case string: @@ -55,7 +54,7 @@ func (p *Preparer) Prepare(render resource.Renderer) (resource.Task, error) { } default: - def = fmt.Sprintf("%v", v) + def = p.Default } return &Param{Value: def}, nil diff --git a/resource/param/preparer_test.go b/resource/param/preparer_test.go index 439f9d384..01d635d05 100644 --- a/resource/param/preparer_test.go +++ b/resource/param/preparer_test.go @@ -45,7 +45,7 @@ func TestPreparerDefault(t *testing.T) { require.True(t, ok, fmt.Sprintf("expected %T, got %T", resultParam, result)) require.Nil(t, err) - assert.Equal(t, fmt.Sprintf("%v", prep.Default), resultParam.Value) + assert.Equal(t, prep.Default, resultParam.Value) } } From 7ce48058d99fad2975f8dbd22ccb973f436e8574 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Mon, 19 Sep 2016 19:44:21 -0500 Subject: [PATCH 3/4] param: disallow composite values --- resource/param/preparer.go | 6 +++++- resource/param/preparer_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/resource/param/preparer.go b/resource/param/preparer.go index a6812e937..7051a1cf7 100644 --- a/resource/param/preparer.go +++ b/resource/param/preparer.go @@ -16,6 +16,7 @@ package param import ( "errors" + "fmt" "github.com/asteris-llc/converge/load/registry" "github.com/asteris-llc/converge/resource" @@ -53,8 +54,11 @@ func (p *Preparer) Prepare(render resource.Renderer) (resource.Task, error) { return nil, err } - default: + case bool, int, float32, float64: def = p.Default + + default: + return nil, fmt.Errorf("composite values are not allowed in params, but got %T", v) } return &Param{Value: def}, nil diff --git a/resource/param/preparer_test.go b/resource/param/preparer_test.go index 01d635d05..6c8b8310a 100644 --- a/resource/param/preparer_test.go +++ b/resource/param/preparer_test.go @@ -34,12 +34,13 @@ func TestPreparerInterface(t *testing.T) { func TestPreparerDefault(t *testing.T) { t.Parallel() - vals := []interface{}{"x", true} + vals := []interface{}{"x", true, 1, 1.0} for _, val := range vals { prep := ¶m.Preparer{Default: val} result, err := prep.Prepare(fakerenderer.New()) + assert.NoError(t, err) resultParam, ok := result.(*param.Param) require.True(t, ok, fmt.Sprintf("expected %T, got %T", resultParam, result)) @@ -49,6 +50,29 @@ func TestPreparerDefault(t *testing.T) { } } +func TestPreparerCompositeValues(t *testing.T) { + t.Parallel() + + vals := []interface{}{ + []string{}, + map[string]string{}, + } + + for _, val := range vals { + prep := ¶m.Preparer{Default: val} + _, err := prep.Prepare(fakerenderer.New()) + + if assert.Error(t, err, fmt.Sprintf("No error from %T", val)) { + assert.EqualError( + t, + err, + fmt.Sprintf("composite values are not allowed in params, but got %T", val), + fmt.Sprintf("Wrong error for %T", val), + ) + } + } +} + func TestPreparerProvided(t *testing.T) { t.Parallel() From d54cf737721521fe8427bdc3f432df88942916b3 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Mon, 19 Sep 2016 19:46:34 -0500 Subject: [PATCH 4/4] docs: update field type --- docs/index.xml | 84 +++++++++++++------------- docs/resources/file-content/index.html | 4 +- docs/resources/index.xml | 84 +++++++++++++------------- docs/resources/module/index.html | 4 +- docs/resources/param/index.html | 22 +------ docs/resources/user-group/index.html | 16 +++++ docs/sitemap.xml | 12 ++-- docs_source/content/resources/param.md | 4 +- resource/param/preparer.go | 2 +- 9 files changed, 116 insertions(+), 116 deletions(-) diff --git a/docs/index.xml b/docs/index.xml index c029b7084..3613ce589 100644 --- a/docs/index.xml +++ b/docs/index.xml @@ -6,9 +6,50 @@ Recent content on Converge Hugo -- gohugo.io en-us - Fri, 16 Sep 2016 15:32:02 -0500 + Mon, 19 Sep 2016 19:45:28 -0500 + + param + http://converge.aster.is/resources/param/ + Mon, 19 Sep 2016 19:45:28 -0500 + + http://converge.aster.is/resources/param/ + + +<p>Param controls the flow of values through <code>module</code> calls. You can use the +<code>{{param &quot;name&quot;}}</code> template call anywhere you need the value of a param +inside the current module.</p> + +<h2 id="example">Example</h2> + +<pre><code class="language-hcl">param &quot;message&quot; { + default = &quot;Hello, World!&quot; +} + +param &quot;filename&quot; { + default = &quot;test.txt&quot; +} + +task &quot;render&quot; { + check = &quot;cat {{param `filename`}} | tee /dev/stderr | grep -q '{{param `message`}}'&quot; + apply = &quot;echo '{{param `message`}}' &gt; {{param `filename`}}&quot; +} + +</code></pre> + +<h2 id="parameters">Parameters</h2> + +<ul> +<li><code>default</code> (anything)</li> +</ul> + +<p>Default is an optional field that provides a default value if none is +provided to this parameter. If this field is not set, this param will be +treated as required.</p> + + + user.group http://converge.aster.is/resources/user-group/ @@ -574,47 +615,6 @@ the called module as the default values for the <code>param</code>s - - param - http://converge.aster.is/resources/param/ - Thu, 08 Sep 2016 23:18:03 -0700 - - http://converge.aster.is/resources/param/ - - -<p>Param controls the flow of values through <code>module</code> calls. You can use the -<code>{{param &quot;name&quot;}}</code> template call anywhere you need the value of a param -inside the current module.</p> - -<h2 id="example">Example</h2> - -<pre><code class="language-hcl">param &quot;message&quot; { - default = &quot;Hello, World!&quot; -} - -param &quot;filename&quot; { - default = &quot;test.txt&quot; -} - -task &quot;render&quot; { - check = &quot;cat {{param `filename`}} | tee /dev/stderr | grep -q '{{param `message`}}'&quot; - apply = &quot;echo '{{param `message`}}' &gt; {{param `filename`}}&quot; -} - -</code></pre> - -<h2 id="parameters">Parameters</h2> - -<ul> -<li><code>default</code> (optional string)</li> -</ul> - -<p>Default is an optional field that provides a default value if none is -provided to this parameter. If this field is not set, this param will be -treated as required.</p> - - - file.content http://converge.aster.is/resources/file-content/ diff --git a/docs/resources/file-content/index.html b/docs/resources/file-content/index.html index 87415fa04..07612378a 100644 --- a/docs/resources/file-content/index.html +++ b/docs/resources/file-content/index.html @@ -548,7 +548,7 @@

Parameters