Skip to content

Commit

Permalink
Merge pull request #190 from ulucinar/template-funcs
Browse files Browse the repository at this point in the history
Add ToLower & ToUpper template functions for config.TemplatedStringAsIdentifier
  • Loading branch information
ulucinar authored Apr 18, 2023
2 parents 0ccac1f + 6e2b097 commit 1268a48
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
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

0 comments on commit 1268a48

Please sign in to comment.