From 6e2b097cae7c39d75329de0f3a87f7fdd220ee45 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Fri, 14 Apr 2023 01:37:32 +0300 Subject: [PATCH] Add ToLower & ToUpper template functions for config.TemplatedStringAsIdentifier Signed-off-by: Alper Rifat Ulucinar --- pkg/config/externalname.go | 23 ++++++++++++++++----- pkg/config/externalname_test.go | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/pkg/config/externalname.go b/pkg/config/externalname.go index 9eb69f0f..5532d181 100644 --- a/pkg/config/externalname.go +++ b/pkg/config/externalname.go @@ -75,22 +75,35 @@ func ParameterAsIdentifier(param string) ExternalName { // file. You can use TF registry documentation of given resource to // see what's available. // -// terraformProviderConfig: The Terraform configuration object of the provider. You can +// setup.configuration: The Terraform configuration object of the provider. You can // // take a look at the TF registry provider configuration object // to see what's available. Not to be confused with ProviderConfig // custom resource of the Crossplane provider. // +// setup.client_metadata: The Terraform client metadata available for the provider, +// +// such as the AWS account ID for the AWS provider. +// // external_name: The value of external name annotation of the custom resource. // // 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 on the .external_name template 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) {