diff --git a/pkg/config/externalname.go b/pkg/config/externalname.go index 9eb69f0f..ae9859df 100644 --- a/pkg/config/externalname.go +++ b/pkg/config/externalname.go @@ -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")) } diff --git a/pkg/config/externalname_test.go b/pkg/config/externalname_test.go index 54d86a56..52f7ae0d 100644 --- a/pkg/config/externalname_test.go +++ b/pkg/config/externalname_test.go @@ -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) {