Skip to content

Commit 74e229d

Browse files
authored
Exporter: adjust generation & normalization of job names (#2836)
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.
1 parent 205b67c commit 74e229d

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

exporter/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ var nameFixes = []regexFix{
139139
{regexp.MustCompile(`[0-9a-f]{8}[_-][0-9a-f]{4}[_-][0-9a-f]{4}` +
140140
`[_-][0-9a-f]{4}[_-][0-9a-f]{12}[_-]`), ""},
141141
// {regexp.MustCompile(`[_-][0-9]+[\._-][0-9]+[\._-].*\.([a-z0-9]{1,4})`), "_$1"},
142-
{regexp.MustCompile(`@.*$`), ""},
142+
// {regexp.MustCompile(`@.*$`), ""},
143143
{regexp.MustCompile(`[-\s\.\|]`), "_"},
144144
{regexp.MustCompile(`\W+`), "_"},
145145
{regexp.MustCompile(`[_]{2,}`), "_"},

exporter/importables.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,12 @@ var resourcesMap map[string]importable = map[string]importable{
337337
ApiVersion: common.API_2_1,
338338
Service: "jobs",
339339
Name: func(ic *importContext, d *schema.ResourceData) string {
340-
return fmt.Sprintf("%s_%s", d.Get("name").(string), d.Id())
340+
name := d.Get("name").(string)
341+
if name == "" {
342+
name = "job"
343+
}
344+
return nameNormalizationRegex.ReplaceAllString(
345+
fmt.Sprintf("%s_%s", name, d.Id()), "_")
341346
},
342347
Depends: []reference{
343348
{Path: "email_notifications.on_failure", Resource: "databricks_user", Match: "user_name"},

exporter/importables_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ func TestClusterNameFromID(t *testing.T) {
222222
assert.Equal(t, "c", resourcesMap["databricks_cluster"].Name(ic, d))
223223
}
224224

225+
func TestJobName(t *testing.T) {
226+
ic := importContextForTest()
227+
d := jobs.ResourceJob().TestResourceData()
228+
d.SetId("12345")
229+
// job without name
230+
assert.Equal(t, "job_12345", resourcesMap["databricks_job"].Name(ic, d))
231+
// job with name
232+
d.Set("name", "test@1pm")
233+
assert.Equal(t, "test_1pm_12345", resourcesMap["databricks_job"].Name(ic, d))
234+
}
235+
225236
func TestClusterLibrary(t *testing.T) {
226237
ic := importContextForTest()
227238
d := clusters.ResourceLibrary().TestResourceData()

0 commit comments

Comments
 (0)