Skip to content

Commit

Permalink
Add ToLower & ToUpper template functions for config.TemplatedStringAs…
Browse files Browse the repository at this point in the history
…Identifier

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Apr 13, 2023
1 parent 1c48474 commit cac9996
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/config/externalname.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,21 @@ func ParameterAsIdentifier(param string) ExternalName {
//
// It is required to use this as part of the template.
//
// The following template functions are available:
// ToLower: Converts the contents of the pipeline to lower-case
// ToUpper: Converts the contents of the pipeline to upper-case
// Please note that it's currently *not* possible to use
// the template functions in a pipeline involving the .external_name variable.
// Example usages:
// TemplatedStringAsIdentifier("index_name", "/subscriptions/{{ .terraformProviderConfig.subscription }}/{{ .external_name }}")
// TemplatedStringAsIdentifier("index.name", "/resource/{{ .external_name }}/static")
// TemplatedStringAsIdentifier("index.name", "{{ .parameters.cluster_id }}:{{ .parameters.node_id }}:{{ .external_name }}")
// TemplatedStringAsIdentifier("index_name", "/subscriptions/{{ .setup.configuration.subscription }}/{{ .external_name }}")
// TemplatedStringAsIdentifier("index_name", "/resource/{{ .external_name }}/static")
// TemplatedStringAsIdentifier("index_name", "{{ .parameters.cluster_id }}:{{ .parameters.node_id }}:{{ .external_name }}")
// TemplatedStringAsIdentifier("", "arn:aws:network-firewall:{{ .setup.configuration.region }}:{{ .setup.client_metadata.account_id }}:{{ .parameters.type | ToLower }}-rulegroup/{{ .external_name }}")
func TemplatedStringAsIdentifier(nameFieldPath, tmpl string) ExternalName {
t, err := template.New("getid").Parse(tmpl)
t, err := template.New("getid").Funcs(template.FuncMap{
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
}).Parse(tmpl)
if err != nil {
panic(errors.Wrap(err, "cannot parse template"))
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/config/externalname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,42 @@ func TestTemplatedGetIDFn(t *testing.T) {
id: "olala/paramval:myname/configval",
},
},
"TemplateFunctionToLower": {
reason: "Should work with a call of ToLower.",
args: args{
tmpl: "olala/{{ .parameters.ola | ToLower }}:{{ .external_name }}/{{ .setup.configuration.oma | ToLower }}",
externalName: "myname",
parameters: map[string]any{
"ola": "ALL_CAPITAL",
},
setup: map[string]any{
"configuration": map[string]any{
"oma": "CamelCase",
},
},
},
want: want{
id: "olala/all_capital:myname/camelcase",
},
},
"TemplateFunctionToUpper": {
reason: "Should work with a call of ToUpper.",
args: args{
tmpl: "olala/{{ .parameters.ola | ToUpper }}:{{ .external_name }}/{{ .setup.configuration.oma | ToUpper }}",
externalName: "myname",
parameters: map[string]any{
"ola": "all_small",
},
setup: map[string]any{
"configuration": map[string]any{
"oma": "CamelCase",
},
},
},
want: want{
id: "olala/ALL_SMALL:myname/CAMELCASE",
},
},
}
for n, tc := range cases {
t.Run(n, func(t *testing.T) {
Expand Down

0 comments on commit cac9996

Please sign in to comment.