From 73561d9e0d8a96ce8829cb3c6ad69c395abce306 Mon Sep 17 00:00:00 2001 From: Alex Ott Date: Wed, 25 Oct 2023 09:08:10 +0200 Subject: [PATCH] Exporter: adjust generation & normalization of job names 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. --- exporter/context.go | 2 +- exporter/importables.go | 7 ++++++- exporter/importables_test.go | 11 +++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/exporter/context.go b/exporter/context.go index 6c1b3369a0..9731b09a0a 100644 --- a/exporter/context.go +++ b/exporter/context.go @@ -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,}`), "_"}, diff --git a/exporter/importables.go b/exporter/importables.go index bd9c22482d..5245110de4 100644 --- a/exporter/importables.go +++ b/exporter/importables.go @@ -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"}, diff --git a/exporter/importables_test.go b/exporter/importables_test.go index 9284d8ac4a..48af5489a7 100644 --- a/exporter/importables_test.go +++ b/exporter/importables_test.go @@ -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()