Skip to content

Commit

Permalink
provisioner: add list template function (#776)
Browse files Browse the repository at this point in the history
This is useful for defining a fixed list instead of
splitting a comma-separated string.

See also previous #711

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Jun 3, 2024
1 parent 8b237a8 commit eb3b277
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions provisioner/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func renderTemplate(context *templateContext, file string) (string, error) {
"awsValidID": awsValidID,
"indent": sprig.GenericFuncMap()["indent"],
"dict": dict,
"list": list,
"scaleQuantity": scaleQuantity,
"instanceTypeCPUQuantity": func(instanceType string) (string, error) {
return instanceTypeCPUQuantity(context, instanceType)
Expand Down Expand Up @@ -275,6 +276,10 @@ func dict(args ...interface{}) (map[string]interface{}, error) {
return dict, nil
}

func list(args ...interface{}) []interface{} {
return args
}

// accountID returns just the ID part of an account
func accountID(account string) (string, error) {
items := strings.Split(account, ":")
Expand Down
23 changes: 23 additions & 0 deletions provisioner/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,29 @@ func TestDictInvalidArgs(t *testing.T) {
}
}

func TestList(t *testing.T) {
result, err := renderSingle(
t,
`
{{- $alist := list
"foo"
"bar"
1
}}
{{- range $i, $v := $alist }}
{{ $i }}={{ $v }}
{{- end }}
`,
nil)

require.NoError(t, err)
require.EqualValues(t, `
0=foo
1=bar
2=1
`, result)
}

func TestScaleQuantity(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down

0 comments on commit eb3b277

Please sign in to comment.