Skip to content

Commit

Permalink
fix: unable to export roles having same name but different applicatio…
Browse files Browse the repository at this point in the history
…n identifier (#183)
  • Loading branch information
ANUGRAHG authored Nov 14, 2024
1 parent c4271b5 commit b93982c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
13 changes: 6 additions & 7 deletions pkg/tfimportprovider/directoryRoleImportProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func createDirectoryRoleImportBlock(data map[string]interface{}, directoryId str
if len(filterValues) != 0 {
var directoryAllRoles []string

for _, value := range roles {
for x, value := range roles {
role := value.(map[string]interface{})
resourceName := output.FormatResourceNameGeneric(fmt.Sprintf("%v", role["name"]))
directoryAllRoles = append(directoryAllRoles, resourceName)
if slices.Contains(filterValues, resourceName) {
importBlock += templateDirectoryRoleImport(role, directoryId, resourceDoc)
importBlock += templateDirectoryRoleImport(x, role, directoryId, resourceDoc)
count++
}
}
Expand All @@ -65,20 +65,19 @@ func createDirectoryRoleImportBlock(data map[string]interface{}, directoryId str
}

} else {
for _, value := range roles {
for x, value := range roles {
role := value.(map[string]interface{})
importBlock += templateDirectoryRoleImport(role, directoryId, resourceDoc)
importBlock += templateDirectoryRoleImport(x, role, directoryId, resourceDoc)
count++
}
}
return importBlock, count, nil
}

func templateDirectoryRoleImport(role map[string]interface{}, directoryId string, resourceDoc tfutils.EntityDocs) string {
func templateDirectoryRoleImport(x int, role map[string]interface{}, directoryId string, resourceDoc tfutils.EntityDocs) string {

resourceDoc.Import = strings.Replace(resourceDoc.Import, "'", "", -1)
resourceName := output.FormatResourceNameGeneric(fmt.Sprintf("%v", role["name"]))
template := strings.Replace(resourceDoc.Import, "<resource_name>", resourceName, -1)
template := strings.Replace(resourceDoc.Import, "<resource_name>", "directory_role_"+fmt.Sprint(x), -1)
template = strings.Replace(template, "<directory_id>", directoryId, -1)
template = strings.Replace(template, "<name>", fmt.Sprintf("%v", role["name"]), -1)
template = strings.Replace(template, "<role_template_name>", fmt.Sprintf("%v", role["role_template_name"]), -1)
Expand Down
4 changes: 2 additions & 2 deletions pkg/tfimportprovider/directoryRoleImportProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCreateDirectoryRoleImportBlock(t *testing.T) {
name: "No filter values",
data: dataRoles,
directoryId: "12345",
expectedBlock: "import {\n\t\t\t\tto = btp_directory_role.directory_usage_reporting_viewer\n\t\t\t\tid = \"12345,Directory Usage Reporting Viewer,Directory_Usage_Reporting_Viewer,uas!b36585\"\n\t\t\t }\n\n",
expectedBlock: "import {\n\t\t\t\tto = btp_directory_role.directory_role_0\n\t\t\t\tid = \"12345,Directory Usage Reporting Viewer,Directory_Usage_Reporting_Viewer,uas!b36585\"\n\t\t\t }\n\n",
expectedCount: 1,
expectError: false,
},
Expand All @@ -40,7 +40,7 @@ func TestCreateDirectoryRoleImportBlock(t *testing.T) {
data: dataMultipleRoles,
directoryId: "12345",
filterValues: []string{"directory_usage_reporting_viewer"},
expectedBlock: "import {\n\t\t\t\tto = btp_directory_role.directory_usage_reporting_viewer\n\t\t\t\tid = \"12345,Directory Usage Reporting Viewer,Directory_Usage_Reporting_Viewer,uas!b36585\"\n\t\t\t }\n\n",
expectedBlock: "import {\n\t\t\t\tto = btp_directory_role.directory_role_0\n\t\t\t\tid = \"12345,Directory Usage Reporting Viewer,Directory_Usage_Reporting_Viewer,uas!b36585\"\n\t\t\t }\n\n",
expectedCount: 1,
expectError: false,
},
Expand Down
13 changes: 6 additions & 7 deletions pkg/tfimportprovider/subaccountRoleImportProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func createRoleImportBlock(data map[string]interface{}, subaccountId string, fil
if len(filterValues) != 0 {
var subaccountAllRoles []string

for _, value := range roles {
for x, value := range roles {
role := value.(map[string]interface{})
resourceName := output.FormatResourceNameGeneric(fmt.Sprintf("%v", role["name"]))
subaccountAllRoles = append(subaccountAllRoles, resourceName)
if slices.Contains(filterValues, resourceName) {
importBlock += templateRoleImport(role, subaccountId, resourceDoc)
importBlock += templateRoleImport(x, role, subaccountId, resourceDoc)
count++
}
}
Expand All @@ -65,20 +65,19 @@ func createRoleImportBlock(data map[string]interface{}, subaccountId string, fil
}

} else {
for _, value := range roles {
for x, value := range roles {
role := value.(map[string]interface{})
importBlock += templateRoleImport(role, subaccountId, resourceDoc)
importBlock += templateRoleImport(x, role, subaccountId, resourceDoc)
count++
}
}
return importBlock, count, nil
}

func templateRoleImport(role map[string]interface{}, subaccountId string, resourceDoc tfutils.EntityDocs) string {
func templateRoleImport(x int, role map[string]interface{}, subaccountId string, resourceDoc tfutils.EntityDocs) string {

resourceDoc.Import = strings.Replace(resourceDoc.Import, "'", "", -1)
resourceName := output.FormatResourceNameGeneric(fmt.Sprintf("%v", role["name"]))
template := strings.Replace(resourceDoc.Import, "<resource_name>", resourceName, -1)
template := strings.Replace(resourceDoc.Import, "<resource_name>", "subaccount_role_"+fmt.Sprint(x), -1)
template = strings.Replace(template, "<subaccount_id>", subaccountId, -1)
template = strings.Replace(template, "<name>", fmt.Sprintf("%v", role["name"]), -1)
template = strings.Replace(template, "<role_template_name>", fmt.Sprintf("%v", role["role_template_name"]), -1)
Expand Down
4 changes: 2 additions & 2 deletions pkg/tfimportprovider/subaccountRoleImportProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCreateRoleImportBlock(t *testing.T) {
name: "No filter values",
data: dataRoles,
subaccountId: "12345",
expectedBlock: "import {\n\t\t\t\tto = btp_subaccount_role.administrator\n\t\t\t\tid = \"12345,Administrator,Administrator,retention-manager-service!b1824\"\n\t\t\t }\n\n",
expectedBlock: "import {\n\t\t\t\tto = btp_subaccount_role.subaccount_role_0\n\t\t\t\tid = \"12345,Administrator,Administrator,retention-manager-service!b1824\"\n\t\t\t }\n\n",
expectedCount: 1,
expectError: false,
},
Expand All @@ -40,7 +40,7 @@ func TestCreateRoleImportBlock(t *testing.T) {
data: dataMultipleRoles,
subaccountId: "12345",
filterValues: []string{"businesscontextmanageradministrator"},
expectedBlock: "import {\n\t\t\t\tto = btp_subaccount_role.businesscontextmanageradministrator\n\t\t\t\tid = \"12345,BusinessContextManagerAdministrator,BusinessContextManagerAdministrator,business-context-manager-service!b28142\"\n\t\t\t }\n\n",
expectedBlock: "import {\n\t\t\t\tto = btp_subaccount_role.subaccount_role_1\n\t\t\t\tid = \"12345,BusinessContextManagerAdministrator,BusinessContextManagerAdministrator,business-context-manager-service!b28142\"\n\t\t\t }\n\n",
expectedCount: 1,
expectError: false,
},
Expand Down

0 comments on commit b93982c

Please sign in to comment.