From aefa94e3a6484decc945c8be430456b9085fe1fc Mon Sep 17 00:00:00 2001 From: Alex Ott Date: Mon, 23 Oct 2023 13:21:34 +0200 Subject: [PATCH] Exporter: fix generation of names for `databricks_library` resources Internally, exporter uses following resource ID for `databricks_library`: `/:coordinate` (i.e., `0426-122546-8xi8q5o3/pypi:chispa`). When generating final name of the resource, this name is normalized, and checked if it's starting with a number, as Terraform doesn't allow identifiers to start with a number. If the final name starts with number, then the artificial final name is generated consisting in form of `r`. This leads to generation of duplicate resources in case if cluster ID started with a number, and the same library was attached to the cluster multiple times, having only difference in the character case, like, `Chispa` and `chispa` (our clusters UI allows that). This PR fixes this issue with following changes: * add `lib_` prefix to the library name together with first 8 numbers of the MD5 of library ID * when generating resource name in form of `r`, calculate MD5 of the original string, not the lower-cased form --- exporter/context.go | 5 +++-- exporter/exporter_test.go | 7 ++++++- exporter/importables.go | 3 ++- exporter/importables_test.go | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/exporter/context.go b/exporter/context.go index 6c1b3369a0..332a23fb5a 100644 --- a/exporter/context.go +++ b/exporter/context.go @@ -764,14 +764,15 @@ func (ic *importContext) ResourceName(r *resource) string { name = r.ID } name = ic.prefix + name + origCaseName := name name = strings.ToLower(name) name = ic.regexFix(name, ic.nameFixes) // this is either numeric id or all-non-ascii if regexp.MustCompile(`^\d`).MatchString(name) || name == "" { if name == "" { - name = r.ID + origCaseName = r.ID } - name = fmt.Sprintf("r%x", md5.Sum([]byte(name)))[0:12] + name = fmt.Sprintf("r%x", md5.Sum([]byte(origCaseName)))[0:12] } return name } diff --git a/exporter/exporter_test.go b/exporter/exporter_test.go index 83c05c4d55..566a41356c 100644 --- a/exporter/exporter_test.go +++ b/exporter/exporter_test.go @@ -1348,7 +1348,12 @@ func TestResourceName(t *testing.T) { norm = ic.ResourceName(&resource{ Name: "9721431b_bcd3_4526_b90f_f5de2befec8c|8737798193", }) - assert.Equal(t, "r7322b058678", norm) + assert.Equal(t, "r56cde0f5eda", norm) + + assert.NotEqual(t, ic.ResourceName(&resource{ + Name: "0A"}), ic.ResourceName(&resource{ + Name: "0a", + })) norm = ic.ResourceName(&resource{ Name: "General Policy - All Users", diff --git a/exporter/importables.go b/exporter/importables.go index bd9c22482d..ea978aaec6 100644 --- a/exporter/importables.go +++ b/exporter/importables.go @@ -261,7 +261,8 @@ var resourcesMap map[string]importable = map[string]importable{ {Path: "egg", Resource: "databricks_dbfs_file", Match: "dbfs_path"}, }, Name: func(ic *importContext, d *schema.ResourceData) string { - return d.Id() + id := d.Id() + return "lib_" + id + fmt.Sprintf("_%x", md5.Sum([]byte(id)))[:9] }, }, "databricks_cluster": { diff --git a/exporter/importables_test.go b/exporter/importables_test.go index 9284d8ac4a..0902a87b69 100644 --- a/exporter/importables_test.go +++ b/exporter/importables_test.go @@ -226,7 +226,7 @@ func TestClusterLibrary(t *testing.T) { ic := importContextForTest() d := clusters.ResourceLibrary().TestResourceData() d.SetId("a-b-c") - assert.Equal(t, "a-b-c", resourcesMap["databricks_library"].Name(ic, d)) + assert.Equal(t, "lib_a-b-c_7b193b3d", resourcesMap["databricks_library"].Name(ic, d)) } func TestImportClusterLibraries(t *testing.T) {