Skip to content

Commit

Permalink
Exporter: adjust generation & normalization of job names
Browse files Browse the repository at this point in the history
In `context.go` we had very aggressive name normalization rule `@.*$` that not necessary
stripped parts of the job name. Also added handling of the empty job name.
  • Loading branch information
alexott committed Oct 25, 2023
1 parent cb4a6f0 commit 73561d9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion exporter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ var nameFixes = []regexFix{
{regexp.MustCompile(`[0-9a-f]{8}[_-][0-9a-f]{4}[_-][0-9a-f]{4}` +
`[_-][0-9a-f]{4}[_-][0-9a-f]{12}[_-]`), ""},
// {regexp.MustCompile(`[_-][0-9]+[\._-][0-9]+[\._-].*\.([a-z0-9]{1,4})`), "_$1"},
{regexp.MustCompile(`@.*$`), ""},
// {regexp.MustCompile(`@.*$`), ""},
{regexp.MustCompile(`[-\s\.\|]`), "_"},
{regexp.MustCompile(`\W+`), "_"},
{regexp.MustCompile(`[_]{2,}`), "_"},
Expand Down
7 changes: 6 additions & 1 deletion exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,12 @@ var resourcesMap map[string]importable = map[string]importable{
ApiVersion: common.API_2_1,
Service: "jobs",
Name: func(ic *importContext, d *schema.ResourceData) string {
return fmt.Sprintf("%s_%s", d.Get("name").(string), d.Id())
name := d.Get("name").(string)
if name == "" {
name = "job"
}
return nameNormalizationRegex.ReplaceAllString(
fmt.Sprintf("%s_%s", name, d.Id()), "_")
},
Depends: []reference{
{Path: "email_notifications.on_failure", Resource: "databricks_user", Match: "user_name"},
Expand Down
11 changes: 11 additions & 0 deletions exporter/importables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ func TestClusterNameFromID(t *testing.T) {
assert.Equal(t, "c", resourcesMap["databricks_cluster"].Name(ic, d))
}

func TestJobName(t *testing.T) {
ic := importContextForTest()
d := jobs.ResourceJob().TestResourceData()
d.SetId("12345")
// job without name
assert.Equal(t, "job_12345", resourcesMap["databricks_job"].Name(ic, d))
// job with name
d.Set("name", "test@1pm")
assert.Equal(t, "test_1pm_12345", resourcesMap["databricks_job"].Name(ic, d))
}

func TestClusterLibrary(t *testing.T) {
ic := importContextForTest()
d := clusters.ResourceLibrary().TestResourceData()
Expand Down

0 comments on commit 73561d9

Please sign in to comment.