diff --git a/base/access_provider/sync_to_target/naming_hint/generator.go b/base/access_provider/sync_to_target/naming_hint/generator.go index ad6d56a..367bf99 100644 --- a/base/access_provider/sync_to_target/naming_hint/generator.go +++ b/base/access_provider/sync_to_target/naming_hint/generator.go @@ -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)) diff --git a/base/access_provider/sync_to_target/naming_hint/generator_test.go b/base/access_provider/sync_to_target/naming_hint/generator_test.go index e8cfaa1..d71c2e4 100644 --- a/base/access_provider/sync_to_target/naming_hint/generator_test.go +++ b/base/access_provider/sync_to_target/naming_hint/generator_test.go @@ -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" ) @@ -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) {