Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ToLower & ToUpper template functions for config.TemplatedStringAsIdentifier #190

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions pkg/config/externalname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
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