Skip to content

Commit

Permalink
Do not rename if actual name and naming hint is equal (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmennes authored Oct 23, 2024
1 parent fc3856f commit a12b4e2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions base/access_provider/sync_to_target/naming_hint/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func (g *uniqueNameGenerator) Generate(ap *sync_to_target.AccessProvider) (strin
name = name[:maxLength]
}

if ap.ActualName != nil && *ap.ActualName == ap.NamingHint {
// This case is when external access provider is imported. We try to avoid renaming if possible.
if _, found := g.existingNames[*ap.ActualName]; !found {
// Actual name was not used before so we can use it directly
g.existingNames[*ap.ActualName] = 0

return *ap.ActualName, nil
}
// Else we will generate a new name
} //nolint:wsl

if ap.ActualName != nil {
// Search for post fix ID
originalNameSplit := strings.Split(*ap.ActualName, fmt.Sprintf("%[1]c%[1]c", g.splitCharacter))
Expand Down
55 changes: 55 additions & 0 deletions base/access_provider/sync_to_target/naming_hint/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/raito-io/cli/base/access_provider/sync_to_target"
)
Expand Down Expand Up @@ -438,8 +439,62 @@ func TestUniqueGeneratorIT_Generate_DuplicatedNames_uppercase(t *testing.T) {
assert.True(t, upperCaseRegex.MatchString(name))

existingNames[name] = struct{}{}
}
}

func TestUniqueGeneratorIT_Generate_DuplicatedNames_uppercase_ImportedAccessProvider(t *testing.T) {
//Given
constraints := NamingConstraints{
UpperCaseLetters: true,
LowerCaseLetters: false,
Numbers: true,
MaxLength: 32,
SpecialCharacters: "_-@#$",
}

generator, err := NewUniqueNameGenerator(logger, "", &constraints)

assert.NoError(t, err)

accessProviderGenerator := func(id int, namingHint string) *sync_to_target.AccessProvider {
return &sync_to_target.AccessProvider{
Id: fmt.Sprintf("SomeID%d", id),
NamingHint: namingHint,
ActualName: nil,
}
}

importedAccessProviderGenerator := func(id int, actualName string) *sync_to_target.AccessProvider {
return &sync_to_target.AccessProvider{
Id: fmt.Sprintf("ExternalId%d", id),
NamingHint: actualName,
ActualName: &actualName,
}
}
ap := accessProviderGenerator(0, "the_same_name")
output, err := generator.Generate(ap)
require.NoError(t, err)
require.Equal(t, "THE_SAME_NAME", output)

// IMPORTED ACCESS PROVIDER
importedActualName := "Some Actual Name w^th Non-generated char"
ap = importedAccessProviderGenerator(0, importedActualName)
output, err = generator.Generate(ap)
require.NoError(t, err)
assert.Equal(t, importedActualName, output)

// Not able to import the same name
ap = importedAccessProviderGenerator(1, "THE_SAME_NAME")
output, err = generator.Generate(ap)
require.NoError(t, err)
assert.Equal(t, "THE_SAME_NAME__0", output)

// generate new name for non imported access provider
ap = accessProviderGenerator(1, "Some Actual Name w^th Non-generated char")
output, err = generator.Generate(ap)
require.NoError(t, err)
assert.Equal(t, "SOME_ACTUAL_NAME_W_TH_NON-", output)

}

func TestUniqueGeneratorIT_Generate_WithPrefix(t *testing.T) {
Expand Down

0 comments on commit a12b4e2

Please sign in to comment.