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

Don't error on already mapped tokens #1420

Merged
merged 1 commit into from
Oct 9, 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
28 changes: 18 additions & 10 deletions pkg/tfbridge/tokens/known_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import (

func knownModules[T b.ResourceInfo | b.DataSourceInfo](
prefix, defaultModule string, modules []string,
apply func(string, string, *T) error,
apply func(string, string, *T, error) error,
moduleTransform func(string) string,
) b.ElementStrategy[T] {
return func(tfToken string, elem *T) error {
tk := strings.TrimPrefix(tfToken, prefix)
if len(tk) == len(tfToken) {
return fmt.Errorf("token '%s' missing package prefix '%s'", tfToken, prefix)
return apply("", upperCamelCase(tk), elem,
fmt.Errorf("token '%s' missing package prefix '%s'", tfToken, prefix))
}
mod := defaultModule
for _, m := range modules {
Expand All @@ -41,10 +42,11 @@ func knownModules[T b.ResourceInfo | b.DataSourceInfo](
break
}
}
var err error
if mod == "" {
return fmt.Errorf("could not find a module that prefixes '%s' in '%#v'", tk, modules)
err = fmt.Errorf("could not find a module that prefixes '%s' in '%#v'", tk, modules)
}
return apply(moduleTransform(mod), upperCamelCase(strings.TrimPrefix(tk, mod)), elem)
return apply(moduleTransform(mod), upperCamelCase(strings.TrimPrefix(tk, mod)), elem, err)
}
}

Expand All @@ -67,12 +69,15 @@ func KnownModules(
}
}

func knownResource(finalize Make) func(mod, tk string, r *b.ResourceInfo) error {
return func(mod, tk string, r *b.ResourceInfo) error {
func knownResource(finalize Make) func(mod, tk string, r *b.ResourceInfo, err error) error {
return func(mod, tk string, r *b.ResourceInfo, err error) error {
if r.Tok != "" {
return nil
}
tk, err := finalize(mod, tk)
if err != nil {
return err
}
tk, err = finalize(mod, tk)
if err != nil {
return err
}
Expand All @@ -81,12 +86,15 @@ func knownResource(finalize Make) func(mod, tk string, r *b.ResourceInfo) error
}
}

func knownDataSource(finalize Make) func(mod, tk string, d *b.DataSourceInfo) error {
return func(mod, tk string, d *b.DataSourceInfo) error {
func knownDataSource(finalize Make) func(mod, tk string, d *b.DataSourceInfo, err error) error {
return func(mod, tk string, d *b.DataSourceInfo, err error) error {
if d.Tok != "" {
return nil
}
tk, err := finalize(mod, "get"+tk)
if err != nil {
return err
}
tk, err = finalize(mod, "get"+tk)
if err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/tfbridge/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,31 @@ func TestTokensKnownModules(t *testing.T) {
}, info.Resources)
}

func TestTokensKnownModulesAlreadyMapped(t *testing.T) {
info := tfbridge.ProviderInfo{
P: (&schema.Provider{
ResourcesMap: schema.ResourceMap{
"pkg_m1_fizz": nil,
"pkg_m2_buzz": nil,
"not_pkg_res": nil,
},
}).Shim(),
Resources: map[string]*tfbridge.ResourceInfo{
"pkg_m2_buzz": {Tok: "pkg:index:Buzz"},
"not_pkg_res": {Tok: "not:pkg:Res"},
},
}

err := info.ComputeTokens(tokens.KnownModules(
"pkg_", "", []string{"m1"}, tokens.MakeStandard("pkg")))
require.NoError(t, err)
assert.Equal(t, map[string]*tfbridge.ResourceInfo{
"pkg_m1_fizz": {Tok: "pkg:m1/fizz:Fizz"},
"pkg_m2_buzz": {Tok: "pkg:index:Buzz"},
"not_pkg_res": {Tok: "not:pkg:Res"},
}, info.Resources)
}

func TestTokensMappedModules(t *testing.T) {
info := tfbridge.ProviderInfo{
P: (&schema.Provider{
Expand Down
Loading