Skip to content

Commit

Permalink
Exporter: fix generation of names for databricks_library resources
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexott committed Oct 23, 2023
1 parent cb4a6f0 commit 6d77c9f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
5 changes: 3 additions & 2 deletions exporter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion exporter/importables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1123,3 +1123,4 @@ func TestIncrementalListDLT(t *testing.T) {
assert.Equal(t, 1, len(ic.testEmits))
})
}

0 comments on commit 6d77c9f

Please sign in to comment.