Skip to content

Commit 98f5174

Browse files
authored
Exporter: fix generation of names for databricks_library resources (#2832)
Internally, exporter uses following resource ID for `databricks_library`: `<cluster_id>/<type>: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<first 12 digits of MD5 of the original name>`. 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<first 12 digits of MD5 of the original name>`, calculate MD5 of the original string, not the lower-cased form
1 parent 74e229d commit 98f5174

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

exporter/context.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,14 +764,15 @@ func (ic *importContext) ResourceName(r *resource) string {
764764
name = r.ID
765765
}
766766
name = ic.prefix + name
767+
origCaseName := name
767768
name = strings.ToLower(name)
768769
name = ic.regexFix(name, ic.nameFixes)
769770
// this is either numeric id or all-non-ascii
770771
if regexp.MustCompile(`^\d`).MatchString(name) || name == "" {
771772
if name == "" {
772-
name = r.ID
773+
origCaseName = r.ID
773774
}
774-
name = fmt.Sprintf("r%x", md5.Sum([]byte(name)))[0:12]
775+
name = fmt.Sprintf("r%x", md5.Sum([]byte(origCaseName)))[0:12]
775776
}
776777
return name
777778
}

exporter/exporter_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,12 @@ func TestResourceName(t *testing.T) {
13481348
norm = ic.ResourceName(&resource{
13491349
Name: "9721431b_bcd3_4526_b90f_f5de2befec8c|8737798193",
13501350
})
1351-
assert.Equal(t, "r7322b058678", norm)
1351+
assert.Equal(t, "r56cde0f5eda", norm)
1352+
1353+
assert.NotEqual(t, ic.ResourceName(&resource{
1354+
Name: "0A"}), ic.ResourceName(&resource{
1355+
Name: "0a",
1356+
}))
13521357

13531358
norm = ic.ResourceName(&resource{
13541359
Name: "General Policy - All Users",

exporter/importables.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ var resourcesMap map[string]importable = map[string]importable{
261261
{Path: "egg", Resource: "databricks_dbfs_file", Match: "dbfs_path"},
262262
},
263263
Name: func(ic *importContext, d *schema.ResourceData) string {
264-
return d.Id()
264+
id := d.Id()
265+
return "lib_" + id + fmt.Sprintf("_%x", md5.Sum([]byte(id)))[:9]
265266
},
266267
},
267268
"databricks_cluster": {

exporter/importables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func TestClusterLibrary(t *testing.T) {
237237
ic := importContextForTest()
238238
d := clusters.ResourceLibrary().TestResourceData()
239239
d.SetId("a-b-c")
240-
assert.Equal(t, "a-b-c", resourcesMap["databricks_library"].Name(ic, d))
240+
assert.Equal(t, "lib_a-b-c_7b193b3d", resourcesMap["databricks_library"].Name(ic, d))
241241
}
242242

243243
func TestImportClusterLibraries(t *testing.T) {

0 commit comments

Comments
 (0)